package com.sonic.lion.dao;

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

import java.util.List;

public interface PayAccountFundDao extends BaseMapper<PayAccountFund> {

    /**
     * 通过uid和支付渠道查询账户id
     *
     * @param uid
     * @param payChannel
     * @return
     */
    Long selectAccountIdByUid(@Param("uid") Long uid, @Param("payChannel") PayChannel payChannel);

    /**
     * 增加指定账户余额
     *
     * @param accountId
     * @param amount
     * @return
     */
    int addBalanceAmount(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 减少指定账户余额
     *
     * @param accountId
     * @param amount
     * @return
     */
    int decBalance(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 获取账户信息并加锁
     *
     * @param payChannel
     * @param accountId
     * @return
     */
    PayAccountFund selectByAccountIdAndPayChannelLock(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel);

    /**
     * 冻结资金
     *
     * @param id
     * @param amount
     * @return
     */
    int frozen(@Param("id") Long id, @Param("amount") Long amount);

    /**
     * 扣除冻结资金
     *
     * @param id
     * @param amount
     * @return
     */
    int decFreezeAmount(@Param("id") Long id, @Param("amount") Long amount);

    /**
     * 根据id获取账户信息并加锁
     *
     * @param id
     * @return
     */
    PayAccountFund selectByIdLock(@Param("id") Long id);

    /**
     * 增加用户待入账金额
     *
     * @param accountId
     * @param payChannel
     * @param amount
     * @return
     */
    int addAwaitingAmount(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 待入账资金入账
     *
     * @param accountId
     * @param payChannel
     * @param amount
     * @return
     */
    int awaitingFundToBalance(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 扣减待入账金额
     *
     * @param accountId
     * @param payChannel
     * @param amount
     * @return
     */
    int decAwaitingAmount(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 增加指定账户充值金额
     *
     * @param accountId
     * @param amount
     * @return
     */
    int addChargeAmount(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);

    /**
     * 减少指定账户充值金额
     *
     * @param accountId
     * @param amount
     * @return
     */
    int decChargeAmount(@Param("accountId") Long accountId, @Param("payChannel") PayChannel payChannel, @Param("amount") Long amount);


    @Select("select a.balance,a.account_id As accountId,a.charge  from t_pay_account_fund a, t_user b  where (a.balance > 0 or a.charge >0 )  AND a.account_id = b.id AND   a.status =1 AND  b.account_status=1 AND a.account_id!=1")
    List<PayAccountFund> listAmountEnable();

    @Update("update  t_pay_account_fund set  balance=0 , charge=0 where  account_id =#{accountId}")
    int removeOldMoney(@Param("accountId") Long accountId);


    @Update("update  account_buff set  balance = balance+ #{charge} , withdrawable_income = withdrawable_income+#{balance}  where  uid =#{accountId}")
    int updateBuff(@Param("accountId")Long accountId, @Param("balance")Long balance, @Param("charge")Long charge);
}