package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.DistributorCommission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface DistributorCommissionMapper extends BaseMapper<DistributorCommission> {

    @Select("SELECT COALESCE(SUM(amount), 0) FROM t_distributor_commission WHERE distributor_id = #{distributorId} AND status = 0")
    BigDecimal sumPendingCommission(@Param("distributorId") Long distributorId);

    @Select("SELECT COALESCE(SUM(amount), 0) FROM t_distributor_commission WHERE distributor_id = #{distributorId} AND status = 0 AND DATE_FORMAT(create_time, '%Y-%m') = #{yearMonth}")
    BigDecimal sumPendingCommissionByMonth(@Param("distributorId") Long distributorId, @Param("yearMonth") String yearMonth);

    @Update("UPDATE t_distributor_commission SET status = 1, settle_time = NOW() WHERE distributor_id = #{distributorId} AND status = 0 AND DATE_FORMAT(create_time, '%Y-%m') = #{yearMonth}")
    int settleByMonth(@Param("distributorId") Long distributorId, @Param("yearMonth") String yearMonth);
}
