package com.zt.plat.module.qms.resource.material.dal.mapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierDO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialBatchPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialBatchRespVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialBatchDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialProductDO;
import com.zt.plat.module.qms.resource.material.enums.MaterialAcceptStatus;
import com.zt.plat.module.qms.resource.material.enums.MaterialBatchGongType;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 物料批次 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface MaterialBatchMapper extends BaseMapperX<MaterialBatchDO> {

    default PageResult<MaterialBatchDO> selectPage(MaterialBatchPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MaterialBatchDO>()
                .eqIfPresent(MaterialBatchDO::getProductId, reqVO.getProductId())
                .likeIfPresent(MaterialBatchDO::getBatchNo, reqVO.getBatchNo())
                .likeIfPresent(MaterialBatchDO::getLocation, reqVO.getLocation())
                .eqIfPresent(MaterialBatchDO::getSupplierId, reqVO.getSupplierId())
                .betweenIfPresent(MaterialBatchDO::getManufacturerDate, reqVO.getManufacturerDate())
                .betweenIfPresent(MaterialBatchDO::getDueDate, reqVO.getDueDate())
                .eqIfPresent(MaterialBatchDO::getAssignDepartmentId, reqVO.getAssignDepartmentId())
                .likeIfPresent(MaterialBatchDO::getAssignDepartmentName, reqVO.getAssignDepartmentName())
                .eqIfPresent(MaterialBatchDO::getAcceptanceStatus, reqVO.getAcceptanceStatus())
                .eqIfPresent(MaterialBatchDO::getAssayFlag, reqVO.getAssayFlag())
                .eqIfPresent(MaterialBatchDO::getAssayStatus, reqVO.getAssayStatus())
                .eqIfPresent(MaterialBatchDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialBatchDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialBatchDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialBatchDO::getId));
    }

//    @QmsPermission(deptIdColumn = "ASN_DEPT_ID", moduleDataRoleCodes = "ytjy_material_admin")
    @QmsPermission(deptIdColumn = "ASN_DEPT_ID", moduleDataRoleCodes = "ytjyAdmin,ytjy_material_admin")
    default PageResult<MaterialBatchRespVO> selectPageWithPdtInfo(MaterialBatchPageReqVO reqVO, List<Long> pdtIds) {

        MPJLambdaWrapper<MaterialBatchDO> wrapper = new MPJLambdaWrapperX<MaterialBatchDO>()
                .selectAll(MaterialBatchDO.class)
                .selectAs(MaterialProductDO::getName, MaterialBatchRespVO::getProductName)
                .selectAs(MaterialProductDO::getCode, MaterialBatchRespVO::getProductCode)
                .selectAs(MaterialProductDO::getStandardCapacity, MaterialBatchRespVO::getStandardCapacity)
                .selectAs(MaterialProductDO::getSpecification, MaterialBatchRespVO::getProductSpecification)
                .selectAs(MaterialProductDO::getModelNo, MaterialBatchRespVO::getProductModelNo)
                .selectAs(MaterialProductDO::getUnit, MaterialBatchRespVO::getUnit)
                // 标签模板，用于入库
                .selectAs(MaterialProductDO::getLabelTemplateKey, MaterialBatchRespVO::getLabelTemplateKey)
                // 关联的样品大类，用于检化验
                .selectAs(MaterialProductDO::getSampleIds, MaterialBatchRespVO::getProductSampleIds)
                // 生产日期和到期日期，用于验收和入库
                .selectAs("batch.MFR_DT", MaterialBatchDO::getManufacturerDate)
                .selectAs("batch.DUE_DT", MaterialBatchDO::getDueDate)
                .selectAs(SupplierDO::getName, MaterialBatchRespVO::getSupplierName)
                .leftJoin(MaterialProductDO.class, MaterialProductDO::getId, MaterialBatchDO::getProductId)
                .leftJoin(MaterialBatchDO.class, "batch", MaterialBatchDO::getId, MaterialBatchDO::getParentId)
                .leftJoin(SupplierDO.class, SupplierDO::getId, MaterialBatchDO::getSupplierId)
                // 只要批次
                .eq(MaterialBatchGongType.batch.name().equals(reqVO.getDataType()), MaterialBatchDO::getParentId, 0)
                // 只要工段
                .ne(MaterialBatchGongType.gong.name().equals(reqVO.getDataType()), MaterialBatchDO::getParentId, 0)
                // 用于批次分页
                .eq(StrUtil.isBlank(reqVO.getDataType()), MaterialBatchDO::getParentId, 0)
                // 用于退换货（已提交）
                .eqIfExists(MaterialBatchDO::getSubmitStatus, reqVO.getSubmitStatus())
                // 用于验收（提交且未验收）
                // 用于入库（提交且已验收）
                .eqIfExists(MaterialBatchDO::getAcceptanceStatus, reqVO.getAcceptanceStatus())
                // 用于验收
                .notExists(reqVO.getSubmitStatus() != null && reqVO.getSubmitStatus() == 1 && MaterialAcceptStatus.not_accepted.name().equals(reqVO.getAcceptanceStatus()),
                        "SELECT 1 FROM t_mtrl_lfc_dtl ld WHERE ld.BAT_GONG_ID = t.ID AND ld.TMT_STS = 0 AND ld.DELETED = 0")
                // 用于入库（分已处理和未处理）
                // 已处理
                .apply(MaterialAcceptStatus.accepted.name().equals(reqVO.getAcceptanceStatus())
                        && reqVO.getTreatment(), "t.INB_END_QTY >= t.INB_QTY")
                // 未处理
                .apply(MaterialAcceptStatus.accepted.name().equals(reqVO.getAcceptanceStatus())
                        && !reqVO.getTreatment(), "t.INB_END_QTY < t.INB_QTY")
                .in(CollUtil.isNotEmpty(pdtIds), MaterialBatchDO::getProductId, pdtIds)
                .eq(CollUtil.isEmpty(pdtIds) && reqVO.getProductId() != null, MaterialBatchDO::getProductId, reqVO.getProductId())
                .likeIfExists(MaterialBatchDO::getBatchNo, reqVO.getBatchNo())
                .likeIfExists(MaterialBatchDO::getLocation, reqVO.getLocation())
                .likeIfExists(MaterialBatchDO::getSupplierId, reqVO.getSupplierId())
                .eqIfExists(MaterialBatchDO::getAssignDepartmentId, reqVO.getAssignDepartmentId())
                .likeIfExists(MaterialBatchDO::getAssignDepartmentName, reqVO.getAssignDepartmentName())
                .eqIfExists(MaterialBatchDO::getAcceptanceStatus, reqVO.getAcceptanceStatus())
                .eqIfExists(MaterialBatchDO::getAssayFlag, reqVO.getAssayFlag())
                .eqIfExists(MaterialBatchDO::getAssayStatus, reqVO.getAssayStatus())
                .eqIfExists(MaterialBatchDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfExists(MaterialBatchDO::getRemark, reqVO.getRemark())
                .orderByDesc(MaterialBatchDO::getId);
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(MaterialBatchDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        return selectJoinPage(reqVO, MaterialBatchRespVO.class, wrapper);
    }

}