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

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

/**
 * 分销商订单 Mapper
 *
 * @author admin
 */
@Mapper
public interface DistributorOrderMapper extends BaseMapperX<DistributorOrderDO> {

    default PageResult<DistributorOrderDO> selectPage(DistributorOrderPageReqVO reqVO) {
        LambdaQueryWrapperX<DistributorOrderDO> wrapperX = new LambdaQueryWrapperX<>();
        if (reqVO.getSuperiorIdP1() != null && reqVO.getSuperiorIdP2() != null) {
            wrapperX.or(w -> w
                    .eq(DistributorOrderDO::getSuperiorIdP1, reqVO.getSuperiorIdP1()).or()
                    .eq(DistributorOrderDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
            );
        } else {
            wrapperX.eqIfPresent(DistributorOrderDO::getSuperiorIdP1, reqVO.getSuperiorIdP1());
            wrapperX.eqIfPresent(DistributorOrderDO::getSuperiorIdP2, reqVO.getSuperiorIdP2());
        }
        return selectPage(reqVO, wrapperX
                .betweenIfPresent(DistributorOrderDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DistributorOrderDO::getSettlementTime, reqVO.getSettlementTime())
                .eqIfPresent(DistributorOrderDO::getOid, reqVO.getOid())
                .eqIfPresent(DistributorOrderDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(DistributorOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DistributorOrderDO::getDistributionRule, reqVO.getDistributionRule())
                .eqIfPresent(DistributorOrderDO::getDistributionPurchase, reqVO.getDistributionPurchase())
                .eqIfPresent(DistributorOrderDO::getDistributionProductSettlement, reqVO.getDistributionProductSettlement())
                .eqIfPresent(DistributorOrderDO::getDistributionWagesSettlement, reqVO.getDistributionWagesSettlement())
                .eqIfPresent(DistributorOrderDO::getUserId, reqVO.getUserId())
                .likeIfPresent(DistributorOrderDO::getUserName, reqVO.getUserName())
                .eqIfPresent(DistributorOrderDO::getProductId, reqVO.getProductId())
                .eqIfPresent(DistributorOrderDO::getProductAttrValueId, reqVO.getProductAttrValueId())
                .likeIfPresent(DistributorOrderDO::getProductName, reqVO.getProductName())
                .eqIfPresent(DistributorOrderDO::getProductPrice, reqVO.getProductPrice())
                .eqIfPresent(DistributorOrderDO::getDetailId, reqVO.getDetailId())
                .eqIfPresent(DistributorOrderDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorOrderDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .eqIfPresent(DistributorOrderDO::getFirstWages, reqVO.getFirstWages())
                .eqIfPresent(DistributorOrderDO::getSecondWages, reqVO.getSecondWages())
                .eqIfPresent(DistributorOrderDO::getFirstAmount, reqVO.getFirstAmount())
                .eqIfPresent(DistributorOrderDO::getSecondAmount, reqVO.getSecondAmount())
                .orderByDesc(DistributorOrderDO::getId));
    }

    default List<DistributorOrderDO> selectList(DistributorOrderExportReqVO reqVO) {
        LambdaQueryWrapperX<DistributorOrderDO> wrapperX = new LambdaQueryWrapperX<>();
        if (reqVO.getSuperiorIdP1() != null && reqVO.getSuperiorIdP2() != null) {
            wrapperX.or(w -> w
                    .eq(DistributorOrderDO::getSuperiorIdP1, reqVO.getSuperiorIdP1()).or()
                    .eq(DistributorOrderDO::getSuperiorIdP2, reqVO.getSuperiorIdP2())
            );
        } else {
            wrapperX.eqIfPresent(DistributorOrderDO::getSuperiorIdP1, reqVO.getSuperiorIdP1());
            wrapperX.eqIfPresent(DistributorOrderDO::getSuperiorIdP2, reqVO.getSuperiorIdP2());
        }
        return selectList(wrapperX
                .betweenIfPresent(DistributorOrderDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DistributorOrderDO::getSettlementTime, reqVO.getSettlementTime())
                .eqIfPresent(DistributorOrderDO::getOid, reqVO.getOid())
                .eqIfPresent(DistributorOrderDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(DistributorOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DistributorOrderDO::getDistributionRule, reqVO.getDistributionRule())
                .eqIfPresent(DistributorOrderDO::getDistributionPurchase, reqVO.getDistributionPurchase())
                .eqIfPresent(DistributorOrderDO::getDistributionProductSettlement, reqVO.getDistributionProductSettlement())
                .eqIfPresent(DistributorOrderDO::getDistributionWagesSettlement, reqVO.getDistributionWagesSettlement())
                .eqIfPresent(DistributorOrderDO::getUserId, reqVO.getUserId())
                .likeIfPresent(DistributorOrderDO::getUserName, reqVO.getUserName())
                .eqIfPresent(DistributorOrderDO::getProductId, reqVO.getProductId())
                .eqIfPresent(DistributorOrderDO::getProductAttrValueId, reqVO.getProductAttrValueId())
                .likeIfPresent(DistributorOrderDO::getProductName, reqVO.getProductName())
                .eqIfPresent(DistributorOrderDO::getProductPrice, reqVO.getProductPrice())
                .eqIfPresent(DistributorOrderDO::getDetailId, reqVO.getDetailId())
                .eqIfPresent(DistributorOrderDO::getSuperiorNameP1, reqVO.getSuperiorNameP1())
                .eqIfPresent(DistributorOrderDO::getSuperiorNameP2, reqVO.getSuperiorNameP2())
                .eqIfPresent(DistributorOrderDO::getFirstWages, reqVO.getFirstWages())
                .eqIfPresent(DistributorOrderDO::getSecondWages, reqVO.getSecondWages())
                .eqIfPresent(DistributorOrderDO::getFirstAmount, reqVO.getFirstAmount())
                .eqIfPresent(DistributorOrderDO::getSecondAmount, reqVO.getSecondAmount())
                .inIfPresent(DistributorOrderDO::getDetailId, reqVO.getDetailIds())
                .orderByDesc(DistributorOrderDO::getId));
    }

}
