package co.yixiang.yshop.module.distributor.dal.mysql.distributorwages;

import java.math.BigDecimal;
import java.util.*;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwages.DistributorWagesDO;
import org.apache.ibatis.annotations.Mapper;
import co.yixiang.yshop.module.distributor.controller.admin.distributorwages.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 分销商佣金 Mapper
 *
 * @author admin
 */
@Mapper
public interface DistributorWagesMapper extends BaseMapperX<DistributorWagesDO> {

    default PageResult<DistributorWagesDO> selectPage(DistributorWagesPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributorWagesDO>()
                .eqIfPresent(DistributorWagesDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(DistributorWagesDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorWagesDO::getAmount, reqVO.getAmount())
                .eqIfPresent(DistributorWagesDO::getRefuseAmount, reqVO.getRefuseAmount())
                .eqIfPresent(DistributorWagesDO::getDistributorId, reqVO.getDistributorId())
                .orderByDesc(DistributorWagesDO::getId));
    }

    default List<DistributorWagesDO> selectList(DistributorWagesExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DistributorWagesDO>()
                .eqIfPresent(DistributorWagesDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(DistributorWagesDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorWagesDO::getAmount, reqVO.getAmount())
                .eqIfPresent(DistributorWagesDO::getRefuseAmount, reqVO.getRefuseAmount())
                .eqIfPresent(DistributorWagesDO::getDistributorId, reqVO.getDistributorId())
                .orderByDesc(DistributorWagesDO::getId));
    }

    @Update("UPDATE yshop_distributor_wages SET amount = amount - #{amount} WHERE distributor_id = #{id} and (amount - #{amount}) > 0")
    int reduceAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);

    @Update("UPDATE yshop_distributor_wages SET refuse_amount = refuse_amount - #{amount} WHERE distributor_id = #{id}")
    int reduceRefuseAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);

    @Update("UPDATE yshop_distributor_wages SET refuse_amount = refuse_amount + #{amount} WHERE distributor_id = #{id}")
    int increaseRefuseAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);

    @Update("UPDATE yshop_distributor_wages SET amount = amount + #{amount}, refuse_amount = refuse_amount - #{amount} WHERE distributor_id = #{id}")
    int settlementAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);

    @Update("UPDATE yshop_distributor_wages SET amount = amount + #{amount} WHERE distributor_id = #{id}")
    int increaseAmount(@Param("id") Long id, @Param("amount") BigDecimal amount);

}
