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

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.distributor.DistributorDO;
import org.apache.ibatis.annotations.Mapper;
import co.yixiang.yshop.module.distributor.controller.admin.distributor.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 分销商 Mapper
 *
 * @author admin
 */
@Mapper
public interface DistributorMapper extends BaseMapperX<DistributorDO> {

    default PageResult<DistributorDO> selectPage(DistributorPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributorDO>()
                .eqIfPresent(DistributorDO::getUserId, reqVO.getUserId())
                .likeIfPresent(DistributorDO::getRealName, reqVO.getRealName())
                .eqIfPresent(DistributorDO::getLevelId, reqVO.getLevelId())
                .eqIfPresent(DistributorDO::getAddress, reqVO.getAddress())
                .eqIfPresent(DistributorDO::getReason, reqVO.getReason())
                .eqIfPresent(DistributorDO::getRefuse, reqVO.getRefuse())
                .eqIfPresent(DistributorDO::getAddUpAmount, reqVO.getAddUpAmount())
                .eqIfPresent(DistributorDO::getAddUpWages, reqVO.getAddUpWages())
                .eqIfPresent(DistributorDO::getAddUpDistributor, reqVO.getAddUpDistributor())
                .betweenIfPresent(DistributorDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorDO::getSuperiorIdP1, reqVO.getSuperiorIdP1())
                .eqIfPresent(DistributorDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
                .eqIfPresent(DistributorDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .eqIfPresent(DistributorDO::getStatus, reqVO.getStatus())
                .orderByDesc(DistributorDO::getId));
    }

    default List<DistributorDO> selectList(DistributorExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DistributorDO>()
                .eqIfPresent(DistributorDO::getUserId, reqVO.getUserId())
                .likeIfPresent(DistributorDO::getRealName, reqVO.getRealName())
                .eqIfPresent(DistributorDO::getLevelId, reqVO.getLevelId())
                .eqIfPresent(DistributorDO::getAddress, reqVO.getAddress())
                .eqIfPresent(DistributorDO::getReason, reqVO.getReason())
                .eqIfPresent(DistributorDO::getRefuse, reqVO.getRefuse())
                .eqIfPresent(DistributorDO::getAddUpAmount, reqVO.getAddUpAmount())
                .eqIfPresent(DistributorDO::getAddUpWages, reqVO.getAddUpWages())
                .eqIfPresent(DistributorDO::getAddUpDistributor, reqVO.getAddUpDistributor())
                .betweenIfPresent(DistributorDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorDO::getSuperiorIdP1, reqVO.getSuperiorIdP1())
                .eqIfPresent(DistributorDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
                .eqIfPresent(DistributorDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .eqIfPresent(DistributorDO::getStatus, reqVO.getStatus())
                .orderByDesc(DistributorDO::getId));
    }

    @Update("update yshop_distributor set superior_id_p1 = null where superior_id_p1 = #{id}")
    void updateP1(@Param("id") Long id);

    @Update("update yshop_distributor set superior_id_p2 = null where superior_id_p2 = #{id}")
    void updateP2(@Param("id") Long id);

    @Update("update yshop_distributor set add_up_distributor = add_up_distributor + 1 where id = #{id}")
    void addUpDistributor(@Param("id") Long id);

    @Update("update yshop_distributor set add_up_wages = add_up_wages + #{addUpWages},add_up_amount = add_up_amount + #{productPrice} where id = #{id}")
    void addAmount(@Param("id") Long id, @Param("productPrice") BigDecimal productPrice, @Param("addUpWages") BigDecimal addUpWages);
}
