package com.izouma.nineth.repo;

import com.izouma.nineth.domain.TransferMoneyInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;


public interface TransferMoneyInfoRepo extends JpaRepository<TransferMoneyInfo, Long>, JpaSpecificationExecutor<TransferMoneyInfo> {

    /**
     * 查找处理中的转账记录
     * @return
     */
    @Query(nativeQuery = true, value="select tmi.* from transfer_money_info tmi " +
            "   where tmi.status = 'PROCESSING' and tmi.del = 0  limit 500 ")
    List<TransferMoneyInfo> findProcessingList();

    /**
     * 查找转账交易记录
     * @param tradeNo
     * @return
     */
    Optional<TransferMoneyInfo> findByTradeNo(String tradeNo);

    /**
     * 查找转账记录
     * @param divMoneyId
     * @return
     */
    List<TransferMoneyInfo> findByDivMoneyId(Long divMoneyId);


    @Query(value="select tmi from TransferMoneyInfo tmi where tmi.status <> com.izouma.nineth.enums.TransferStatus.FAILED " +
            "   and tmi.toUserId = ?1 and tmi.reason = ?2  ")
    List<TransferMoneyInfo> findBySameReason(Long userId, String reason);

}
