package com.sonic.lion.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sonic.lion.domain.entity.AccountBuffBill;
import com.sonic.lion.domain.entity.PayTrade;
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 AccountBuffBillDao extends BaseMapper<AccountBuffBill> {

    /**
     * 更新bill 状态
     *
     * @param tradeNo
     * @param status
     * @param message
     * @return
     */
    @Update("update account_buff_bill set bill_status =#{status} ,withdraw_status='WITHDRAW_FAIL' , reason=#{message} where trade_no=#{tradeNo} AND biz_type =500 ")
    int updateStatus(@Param("tradeNo") String tradeNo, @Param("status") int status, @Param("message") String message);


    Long sumTotal(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("uid") Long uid, @Param("inOrOut") Integer inOrOut, @Param("type") String type, @Param("bizType") String bizType);


    /**
     * 消费 汇总
     *
     * @param startTime
     * @param endTime
     * @param uid
     * @return
     */
    Long sumConsumption(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("uid") Long uid);


    /**
     * 更新提现流水 的状态 和 原因。
     *
     * @param id
     * @param status
     * @param message
     * @return
     */
    @Update("update account_buff_bill set withdraw_status =#{status} , reason=#{message} where id=#{id} ")
    int updateWithdrawStatus(@Param("id") Long id, @Param("status") String status, @Param("message") String message);


    @Update("update account_buff_bill set balance= balance - #{amount} where id=#{id} ")
    int updateWithdrawBillAmount(@Param("id") Long id, @Param("amount") Long amount);

    /**
     * 列出 某时间 之前的 审核中的 bill
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select("SELECT t1.* FROM t_pay_trade t1 JOIN account_buff_bill  t2 ON t1.trade_no = t2.`trade_no`\n" +
            "WHERE t2.biz_type= 500 AND t2.withdraw_status= 'IN_REVIEW' AND t2.create_time < #{endTime} AND t2.create_time > #{beginTime}")
    List<PayTrade> listTimeOutInReviewWithdrawBill(@Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 根据 tradeNo 更新 提现状态
     *
     * @param tradeNo
     * @param withdrawSuccess
     * @return
     */
    @Update("update account_buff_bill set withdraw_status =#{status}   where trade_no=#{tradeNo} AND biz_type = 500")
    int updateWithdrawStatusByTradeNo(@Param("tradeNo") String tradeNo, @Param("status") AccountBuffBill.WithdrawStatus withdrawSuccess);


    /**
     * 修改 bill 金额。
     *
     * @param tradeNo
     * @param amount
     */
    @Update("update account_buff_bill set buff=#{amount} where trade_no=#{tradeNo} AND uid = #{uid}  AND in_or_out = 1")
    void updateAmountByTradeNo(@Param("tradeNo") String tradeNo, @Param("uid") Long uid, @Param("amount") Long amount);

    /**
     * 根据 tradeNo 更新 提现状态
     *
     * @param buffType
     * @param billId
     * @return
     */
    @Update("update account_buff_bill set buff_type =#{buffType}   where id=#{billId}")
    int updateWithdrawableIncome(@Param("buffType") Integer buffType, @Param("billId") Long billId);

    /**
     * 统计用户在PST当天充值的Buff总金额
     * @param userId
     * @param startTime
     * @return
     */
    Long statPstCurrentDayChargeBuff(@Param("userId") Long userId, @Param("startTime") LocalDateTime startTime);

}