package com.jeequan.jeepay.service.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.jeequan.jeepay.db.entity.PayUser;
import com.jeequan.jeepay.db.entity.RedPacketAccount;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-01-17
 */
public interface RedPacketAccountMapper extends BaseMapper<RedPacketAccount> {

    /**
     * 数据统计
     * @param wrapper
     * @return
     */
    @Select("SELECT SUM(a.total) as total, SUM(a.totalBalance) as totalBalance FROM (SELECT COUNT( 1 ) AS total, IFNULL( SUM( balance ), 0 ) AS totalBalance FROM t_red_packet_account ${ ew.customSqlSegment } GROUP BY pay_user_id) a")
    Map<String, Object> stsCount(@Param(Constants.WRAPPER) Wrapper<RedPacketAccount> wrapper);

    /**
     * 根据付款用户id以及门店id获取用户红包账户
     * @param payUserId
     * @param storeId
     * @return
     */
    @Select("SELECT * FROM t_red_packet_account WHERE pay_user_id = #{payUserId} and store_id = #{storeId}")
    RedPacketAccount getByStorePayUserId(@Param("payUserId") String payUserId, @Param("storeId")String storeId);

    /**
     * 修改账户余额
     * @param accountId
     * @param changeAmt
     */
    @Update("UPDATE t_red_packet_account set balance = balance - #{changeAmt},last_trans_time = now() WHERE account_id = #{accountId}")
    void upAccountBanlace(@Param("accountId") String accountId, @Param("changeAmt") Long changeAmt);
}
