package com.zt.plat.module.contractorder.dal.mysql.purchaseorder;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.datapermission.core.annotation.CompanyDataPermissionIgnore;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;

import com.zt.plat.module.contractorder.api.dto.order.OrderAndPlanDataReqDTO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PurchaseOrderPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.PurchaseOrderWithDetailsVO;
import com.zt.plat.module.contractorder.dal.dataobject.purchaseorder.PurchaseOrderDO;
import com.zt.plat.module.contractorder.dal.dataobject.salesorder.SalesOrderDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;


/**
 * 采购订单 Mapper
 *
 * @author 后台管理-1
 */
@Mapper
public interface PurchaseOrderMapper extends BaseMapperX<PurchaseOrderDO> {

    default PageResult<PurchaseOrderDO> selectPage(PurchaseOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PurchaseOrderDO>()
                .likeIfPresent(PurchaseOrderDO::getOrderSAPNumber, reqVO.getOrderSAPNumber())
                .likeIfPresent(PurchaseOrderDO::getSystemOrderNumber, reqVO.getSystemOrderNumber())
                .likeIfPresent(PurchaseOrderDO::getCpNum, reqVO.getCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getSupplierNumber, reqVO.getSupplierNumber())
                .likeIfPresent(PurchaseOrderDO::getSupplierName, reqVO.getSupplierName())
                .eqIfPresent(PurchaseOrderDO::getType, reqVO.getType())
                .betweenIfPresent(PurchaseOrderDO::getVoucherDate, reqVO.getVoucherDate())
                .eqIfPresent(PurchaseOrderDO::getPurchaseOrganizationCustomsDeclaration, reqVO.getPurchaseOrganizationCustomsDeclaration())
                .likeIfPresent(PurchaseOrderDO::getReceiveFactoryName, reqVO.getReceiveFactoryName())
                .eqIfPresent(PurchaseOrderDO::getReceiveFactoryNumber, reqVO.getReceiveFactoryNumber())
                .likeIfPresent(PurchaseOrderDO::getReceiveWarehouseName, reqVO.getReceiveWarehouseName())
                .eqIfPresent(PurchaseOrderDO::getReceiveWarehouseNumber, reqVO.getReceiveWarehouseNumber())
                .eqIfPresent(PurchaseOrderDO::getPurchaseGroup, reqVO.getPurchaseGroup())
                .eqIfPresent(PurchaseOrderDO::getCurrencyNumber, reqVO.getCurrencyNumber())
                .eqIfPresent(PurchaseOrderDO::getExchangeRate, reqVO.getExchangeRate())
                .likeIfPresent(PurchaseOrderDO::getPaperContractNumber, reqVO.getPaperContractNumber())
                .likeIfPresent(PurchaseOrderDO::getAgreementNumber, reqVO.getAgreementNumber())
                .eqIfPresent(PurchaseOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(PurchaseOrderDO::getAgentNumber, reqVO.getAgentNumber())
                .likeIfPresent(PurchaseOrderDO::getAgentName, reqVO.getAgentName())
                .eqIfPresent(PurchaseOrderDO::getMtrlTp, reqVO.getMtrlTp())
//                .eqIfPresent(PurchaseOrderDO::getOrderNumber, reqVO.getOrderNumber())
                .eqIfPresent(PurchaseOrderDO::getContractNumber, reqVO.getContractNumber())
                .eqIfPresent(PurchaseOrderDO::getMaterialNumber, reqVO.getMaterialNumber())
                .likeIfPresent(PurchaseOrderDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(PurchaseOrderDO::getContractName, reqVO.getContractName())
                .likeIfPresent(PurchaseOrderDO::getTenantNumber, reqVO.getTenantNumber())
                .betweenIfPresent(PurchaseOrderDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(PurchaseOrderDO::getErpPurchaseCompanyNumber, reqVO.getErpPurchaseCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getErpPurchaseCompanyName, reqVO.getErpPurchaseCompanyName())
                .eqIfPresent(PurchaseOrderDO::getErpSalesCompanyNumber, reqVO.getErpSalesCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getErpSalesCompanyName, reqVO.getErpSalesCompanyName())
                .likeIfPresent(PurchaseOrderDO::getPurchaseOrganizationName, reqVO.getPurchaseOrganizationName())
                .eqIfPresent(PurchaseOrderDO::getErpStatus, reqVO.getErpStatus())
                .eqIfPresent(PurchaseOrderDO::getSplyBsnTp, reqVO.getSplyBsnTp() != null ? reqVO.getSplyBsnTp() : "PUR")
                .eqIfPresent(PurchaseOrderDO::getCause, reqVO.getCause())
                .eqIfPresent(PurchaseOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(PurchaseOrderDO::getMeteringType, reqVO.getMeteringType())
                .likeIfPresent(PurchaseOrderDO::getPurchaseGroupName, reqVO.getPurchaseGroupName())
                .orderByDesc(PurchaseOrderDO::getId));
    }

    List<PurchaseOrderWithDetailsVO> selectOrderByOrderNos(@Param("orderNos") List<String> orderNos);

    List<PurchaseOrderWithDetailsVO> selectOrderByIds(@Param("ids") List<Long> id);

    PurchaseOrderDO selectByOrderId(@Param("orderId") Long orderId);

    List<PurchaseOrderDO> queryOrderAndPlanData(OrderAndPlanDataReqDTO reqDTO);

    List<PurchaseOrderWithDetailsVO>  selectOrderByAndAllIds(@Param("ids") List<Long> ids);

    PurchaseOrderDO findOrderById(@Param("id") Long id);

    @CompanyDataPermissionIgnore
    @DeptDataPermissionIgnore
    default PageResult<PurchaseOrderDO> selectLinkPage(PurchaseOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PurchaseOrderDO>()
                .likeIfPresent(PurchaseOrderDO::getOrderSAPNumber, reqVO.getOrderSAPNumber())
                .likeIfPresent(PurchaseOrderDO::getSystemOrderNumber, reqVO.getSystemOrderNumber())
                .likeIfPresent(PurchaseOrderDO::getCpNum, reqVO.getCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getSupplierNumber, reqVO.getSupplierNumber())
                .likeIfPresent(PurchaseOrderDO::getSupplierName, reqVO.getSupplierName())
                .eqIfPresent(PurchaseOrderDO::getType, reqVO.getType())
                .betweenIfPresent(PurchaseOrderDO::getVoucherDate, reqVO.getVoucherDate())
                .eqIfPresent(PurchaseOrderDO::getPurchaseOrganizationCustomsDeclaration, reqVO.getPurchaseOrganizationCustomsDeclaration())
                .likeIfPresent(PurchaseOrderDO::getReceiveFactoryName, reqVO.getReceiveFactoryName())
                .eqIfPresent(PurchaseOrderDO::getReceiveFactoryNumber, reqVO.getReceiveFactoryNumber())
                .likeIfPresent(PurchaseOrderDO::getReceiveWarehouseName, reqVO.getReceiveWarehouseName())
                .eqIfPresent(PurchaseOrderDO::getReceiveWarehouseNumber, reqVO.getReceiveWarehouseNumber())
                .eqIfPresent(PurchaseOrderDO::getPurchaseGroup, reqVO.getPurchaseGroup())
                .eqIfPresent(PurchaseOrderDO::getCurrencyNumber, reqVO.getCurrencyNumber())
                .eqIfPresent(PurchaseOrderDO::getExchangeRate, reqVO.getExchangeRate())
                .likeIfPresent(PurchaseOrderDO::getPaperContractNumber, reqVO.getPaperContractNumber())
                .likeIfPresent(PurchaseOrderDO::getAgreementNumber, reqVO.getAgreementNumber())
                .eqIfPresent(PurchaseOrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(PurchaseOrderDO::getAgentNumber, reqVO.getAgentNumber())
                .likeIfPresent(PurchaseOrderDO::getAgentName, reqVO.getAgentName())
                .eqIfPresent(PurchaseOrderDO::getMtrlTp, reqVO.getMtrlTp())
//                .eqIfPresent(PurchaseOrderDO::getOrderNumber, reqVO.getOrderNumber())
                .eqIfPresent(PurchaseOrderDO::getContractNumber, reqVO.getContractNumber())
                .eqIfPresent(PurchaseOrderDO::getMaterialNumber, reqVO.getMaterialNumber())
                .likeIfPresent(PurchaseOrderDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(PurchaseOrderDO::getContractName, reqVO.getContractName())
                .likeIfPresent(PurchaseOrderDO::getTenantNumber, reqVO.getTenantNumber())
                .betweenIfPresent(PurchaseOrderDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(PurchaseOrderDO::getErpPurchaseCompanyNumber, reqVO.getErpPurchaseCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getErpPurchaseCompanyName, reqVO.getErpPurchaseCompanyName())
                .eqIfPresent(PurchaseOrderDO::getErpSalesCompanyNumber, reqVO.getErpSalesCompanyNumber())
                .likeIfPresent(PurchaseOrderDO::getErpSalesCompanyName, reqVO.getErpSalesCompanyName())
                .likeIfPresent(PurchaseOrderDO::getPurchaseOrganizationName, reqVO.getPurchaseOrganizationName())
                .eqIfPresent(PurchaseOrderDO::getErpStatus, reqVO.getErpStatus())
                .eqIfPresent(PurchaseOrderDO::getSplyBsnTp, reqVO.getSplyBsnTp() != null ? reqVO.getSplyBsnTp() : "PUR")
                .eqIfPresent(PurchaseOrderDO::getCause, reqVO.getCause())
                .eqIfPresent(PurchaseOrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(PurchaseOrderDO::getMeteringType, reqVO.getMeteringType())
                .likeIfPresent(PurchaseOrderDO::getPurchaseGroupName, reqVO.getPurchaseGroupName())
                .orderByDesc(PurchaseOrderDO::getId));
    }
}
