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

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

/**
 * 分销商所属用户 Mapper
 *
 * @author admin
 */
@Mapper
public interface DistributorUserMapper extends BaseMapperX<DistributorUserDO> {

    default PageResult<DistributorUserDO> selectPage(DistributorUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DistributorUserDO>()
                .eqIfPresent(DistributorUserDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(DistributorUserDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorUserDO::getSuperiorIdP1, reqVO.getSuperiorIdP1())
                .eqIfPresent(DistributorUserDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorUserDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
                .eqIfPresent(DistributorUserDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .betweenIfPresent(DistributorUserDO::getExpireTime, reqVO.getExpireTime())
                .likeIfPresent(DistributorUserDO::getNickname, reqVO.getNickname())
                .eqIfPresent(DistributorUserDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(DistributorUserDO::getProductCount, reqVO.getProductCount())
                .eqIfPresent(DistributorUserDO::getOrderCount, reqVO.getOrderCount())
                .eqIfPresent(DistributorUserDO::getWages, reqVO.getWages())
                .orderByDesc(DistributorUserDO::getId));
    }

    default List<DistributorUserDO> selectList(DistributorUserExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<DistributorUserDO>()
                .eqIfPresent(DistributorUserDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(DistributorUserDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DistributorUserDO::getSuperiorIdP1, reqVO.getSuperiorIdP1())
                .eqIfPresent(DistributorUserDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorUserDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
                .eqIfPresent(DistributorUserDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .betweenIfPresent(DistributorUserDO::getExpireTime, reqVO.getExpireTime())
                .likeIfPresent(DistributorUserDO::getNickname, reqVO.getNickname())
                .eqIfPresent(DistributorUserDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(DistributorUserDO::getProductCount, reqVO.getProductCount())
                .eqIfPresent(DistributorUserDO::getOrderCount, reqVO.getOrderCount())
                .eqIfPresent(DistributorUserDO::getWages, reqVO.getWages())
                .inIfPresent(DistributorUserDO::getSuperiorIdP1, reqVO.getSuperiorIdP1s())
                .inIfPresent(DistributorUserDO::getSuperiorIdP2, reqVO.getSuperiorIdP2s())
                .orderByDesc(DistributorUserDO::getId));
    }

    @Update("delete from yshop_distributor_user where superior_id_p1 = #{id}")
    void deleteP1(@Param("id") Long id);

    @Update("delete from yshop_distributor_user where superior_id_p2 = #{id}")
    void deleteP2(@Param("id") Long id);

}

