package com.sonic.lion.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sonic.lion.enums.BizType;
import com.sonic.lion.domain.entity.PayCallChannelRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

public interface PayCallChannelRecordDao extends BaseMapper<PayCallChannelRecord> {


    @Select("select  *  from  t_pay_call_channel_record where transaction_id =#{transactionId} ")
    PayCallChannelRecord getByTransactionId(@Param("transactionId") String transactionId);

    @Select("select  *  from  t_pay_call_channel_record where channel=#{channel} AND  trade_no =#{tradeNo} limit 1 ")
    PayCallChannelRecord getByTradeNoAndChannel(@Param("tradeNo") String tradeNo, @Param("channel") int channel);

    /**
     * 更新 通道调用状态
     *
     * @param id
     * @param status
     * @return
     */
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);


    @Update("update t_pay_call_channel_record set transaction_id =#{transactionId},status=2  where trade_no=#{trade_no} AND channel=9")
    int updateTransactionId(@Param("trade_no") String trade_no, @Param("transactionId") String transactionId);

    /**
     * 根据tradeNo查询最后一条记录
     *
     * @param tradeNo
     * @param bizType
     * @return
     */
    PayCallChannelRecord selectByTradeNoAndBizTypeLast(@Param("tradeNo") String tradeNo, @Param("bizType") BizType bizType);

    /**
     * 查询应该检查的记录:
     * 状态为 INIT，PROCESSING, UNKNOW
     * 下一次查询时间小于当前时间
     * 查询次数小于10次
     *
     * @return
     */
    List<PayCallChannelRecord> selectWillCheckRecord(@Param("startCreateTime") LocalDateTime startCreateTime);

    /**
     * @param batchId
     * @return
     */
    PayCallChannelRecord selectByBatchId(@Param("batchId") String batchId);


    /**
     * 查询需要检查的 充值渠道 记录
     *
     * @param startCreateTime
     * @return
     */
    List<PayCallChannelRecord> selectWillCheckRecordCharge(@Param("startCreateTime") LocalDateTime startCreateTime);

    /**
     * 查询需要检查的 提现渠道记录
     *
     * @param startCreateTime
     * @return
     */
    List<PayCallChannelRecord> selectWillCheckRecordWithdraw(@Param("startCreateTime") LocalDateTime startCreateTime);

}