package com.sonic.lion.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sonic.lion.domain.entity.PayTrade;
import com.sonic.lion.domain.input.RefundVipInput;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

public interface PayTradeDao extends BaseMapper<PayTrade> {

    /**
     * 统计充值实付金额
     *
     * @param uid
     * @return
     */
    long sumChargeOccurAmount(@Param("uid") long uid);

    /**
     * 批量关闭交易(交易关闭时间小于当前时间的交易)
     *
     * @return
     */
    int closeWaitPayTrade();

    /**
     * 根据资源key查询已付款的交易数并对加锁(除待付款和已关闭)
     *
     * @param resourceKey
     * @return
     */
    int countPaidByResourceKeyLock(String resourceKey);

    /**
     * 根据资源key查询已付款的交易数(除待付款和已关闭)
     *
     * @param resourceKey
     * @return
     */
    int countPaidByResourceKey(String resourceKey);

    /**
     * 根据用户uid和支付交易号查询最后一笔充值交易
     *
     * @param srcUid
     * @param paymentTradeNo
     * @return
     */
    PayTrade selectBySrcUidAndPaymentTradeNoLast(@Param("srcUid") Long srcUid, @Param("paymentTradeNo") String paymentTradeNo);

    PayTrade selectBySrcUidAndPaymentTradeNoAndTypeLast(@Param("srcUid") Long srcUid, @Param("paymentTradeNo") String paymentTradeNo, @Param("bizType") String bizType);


    @Update("update t_pay_trade set error_message = #{errorMessage} where trade_no=#{tradeNo}")
    int updateErrorMessage(@Param("errorMessage") String errorMessage, @Param("tradeNo") String tradeNo);

    @Update("update t_pay_trade set status=#{status}  , error_message = #{errorMessage} where trade_no=#{tradeNo} AND status not in (2,4)")
    int updateStatusErrorMessage(@Param("status") int status, @Param("errorMessage") String errorMessage, @Param("tradeNo") String tradeNo);

    @Select("select count(1) from t_pay_trade where srcAccountId = #{srcAccountId} AND gift_amount>0 ")
    int isThanksGivingDayExits(Long srcAccountId);


    @Update("update t_pay_trade set standbox  = #{standbox} where id=#{id}")
    int setStanbox(@Param("standbox") Integer standbox, @Param("id") Long id);

    @Select("SELECT count(1)  from t_pay_trade where des_account_no = #{userId} AND biz_type=200 ")
    int countWelcomeGift(@Param("userId") Long userId);

    @Select("SELECT count(1)  from t_pay_trade where des_account_no = #{userId} AND biz_type=207 ")
    int countBuffGift(@Param("userId") Long userId);

    @Select("SELECT count(1)  from t_pay_trade where ip = #{ip} AND biz_type=207 ")
    int countBuffGiftByIp(@Param("ip") String ip);

    @Select(" SELECT COUNT(1)  FROM t_pay_trade t where  t.des_account_no =  #{userId} AND t.biz_type=207 ")
    Integer countNewUserGiftBuff(@Param("userId") Long id);


    /**
     * 用户总充值
     *
     * @param id
     * @return
     */
    @Select(" SELECT IFNULL(SUM(amount),0)  FROM t_pay_trade t WHERE  t.src_account_id = #{userId} AND t.biz_type=600 AND channel_id=6")
    Integer countTotalBuy(@Param("userId") Long id);


    /**
     * 用户总 退款
     *
     * @param id
     * @return
     */
    @Select("SELECT  IFNULL(SUM(buff),0) FROM apple_refund_record WHERE user_id =  #{userId}")
    Integer countTotalRefund(@Param("userId") Long id);

    /**
     * 查询用户 的 下单数量
     *
     * @param id
     * @return
     */
    @Select("SELECT t.price_real FROM t_order t JOIN t_order_item t1 ON t1.order_id = t.id   WHERE  t.is_delete=0 AND  t.buyer_id = #{userId} AND t1.source_type != 'HANDSEL' ")
    List<Long> countOrder(@Param("userId") Long id);



    @Select("SELECT t.create_time  FROM t_order t JOIN t_order_item t1 ON t1.order_id = t.id   WHERE  t.is_delete=0 AND  t.buyer_id = #{userId} AND t1.source_type != 'HANDSEL' order by t.create_time asc  limit 1 ")
    Date getFirstOrderTime(@Param("userId") Long id);

    /**
     * 查询有赠送 VIP 时间 的人
     *
     * @return
     */
    @Select(" SELECT user_id,member_type,reward_member_type ,reward_end_time ,end_time FROM t_user_member WHERE reward_end_time > NOW()")
    List<RefundVipInput> rewardEndTimeUsers();


    /**
     * 查询没有 赠送 VIP 时间的人
     *
     * @return
     */
    @Select(" SELECT user_id,member_type,reward_member_type ,reward_end_time ,end_time  FROM t_user_member WHERE (reward_end_time is null or reward_end_time<NOW()) AND  end_time > NOW()")
    List<RefundVipInput> noRewardEndTimeUsers();

    @Update("update t_pay_trade set channel_id = #{channel} where trade_no =  #{tradeNo}")
    int updateChannelByTradeNo(@Param("channel") String channel, @Param("tradeNo") String tradeNo);


    /**
     * 查询是否已经充值过
     *
     * @param uid
     * @return
     */
    @Select("SELECT COUNT(1) FROM t_pay_trade WHERE  src_account_id = #{uid} AND biz_type = 600 AND status IN (2,4,6,7) AND  payment_trade_no is  null")
    int countAnyChargeOrders(@Param("uid") Long uid);

    @Update("update t_pay_trade set gift_amount = #{giftAmount} where id=#{id}")
    int updateGiftAmountByTradeNo(@Param("id") Long id,@Param("giftAmount") Long giftAmount);
}