package com.sonic.lion.dao;

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

import java.util.List;

public interface AccountBuffDao extends BaseMapper<AccountBuff> {

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

    /**
     * 增加可提现收入
     *
     * @param id
     * @param buff
     * @return
     */
    int addWithdrawableIncome(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 减少指定账户余额
     *
     * @param id
     * @param buff
     * @return
     */
    int decWithdrawableIncome(@Param("id") Long id, @Param("buff") Long buff);


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

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


    /**
     * 增加用户待入账金额
     *
     * @param id
     * @param buff
     * @return
     */
    int addAwaitingIncome(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 待入账资金入账
     *
     * @param id
     * @param buff
     * @return
     */
    int awaitingIncomeToWithdrawableIncome(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 扣减待入账金额
     *
     * @param id
     * @param buff
     * @return
     */
    int decAwaitingIncome(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 增加指定账户充值金额
     *
     * @param id
     * @param buff
     * @return
     */
    int addBalance(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 减少指定账户充值金额
     *
     * @param id
     * @param buff
     * @return
     */
    int decBalance(@Param("id") Long id, @Param("buff") Long buff);


    /**
     * 冻结余额
     *
     * @param id
     * @param buff
     * @return
     */
    @Update(" update account_buff set balance = balance - #{buff} , frozen_balance = frozen_balance+ #{buff} where uid = #{id}   and balance >= #{buff}")
    int frozenBalance(@Param("id") Long id, @Param("buff") Long buff);

    /**
     * 解冻
     *
     * @param id
     * @param buff
     * @return
     */
    @Update("update account_buff set balance = balance + #{buff} , frozen_balance = frozen_balance - #{buff} where uid = #{id}   and frozen_balance >= #{buff}")
    int unFrozenBalance(@Param("id") Long id, @Param("buff") Long buff);


    /**
     * 增加总充值金额
     *
     * @param id
     * @param buff
     * @return
     */
    @Update("update account_buff set recharge_total  = recharge_total  + #{buff} where uid = #{id}")
    int addRechargeTotal(@Param("id") Long id, @Param("buff") Long buff);


    /**
     * 查询总充值表
     *
     * @return
     */
    @Select("SELECT uid AS uId, SUM(buff) AS rechargeTotal FROM account_buff_bill WHERE biz_type = 600 AND in_or_out =1  GROUP BY uid")
    List<AccountBuff> getHistroyRechargeTotal();


    /**
     * 增加提现 在途金额
     *
     * @param id
     * @param buff
     * @return 条数
     */
    @Update("update account_buff set withdraw_on_going  = withdraw_on_going  + #{buff} where id = #{id}")
    int addWithdrawOnGoing(@Param("id") Long id, @Param("buff") Long buff);


    /**
     * 减少 提现在途金额
     *
     * @param id
     * @param buff
     * @return
     */
    @Update("update account_buff set withdraw_on_going  = withdraw_on_going  - #{buff} where id = #{id} AND withdraw_on_going >= #{buff}")
    int decWithdrawOnGoing(@Param("id") Long id, @Param("buff") Long buff);


    /**
     * 根据uid 查询账户
     *
     * @param uid
     * @return
     */
    @Select("select * from account_buff where uid=#{uid} limit 1")
    AccountBuff selectByUid(@Param("uid") Long uid);
}