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

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.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.*;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 入库明细,验收入库、盘盈入库等 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface MaterialInventoryInboundDetailMapper extends BaseMapperX<MaterialInventoryInboundDetailDO> {

    default PageResult<MaterialInventoryInboundDetailDO> selectPage(MaterialInventoryInboundDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MaterialInventoryInboundDetailDO>()
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundId, reqVO.getInboundId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getBatchId, reqVO.getBatchId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getBatchGongduanId, reqVO.getBatchGongduanId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getMaterialInfomationId, reqVO.getMaterialInfomationId())
                .likeIfPresent(MaterialInventoryInboundDetailDO::getInboundUserName, reqVO.getInboundUserName())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundUserId, reqVO.getInboundUserId())
                .likeIfPresent(MaterialInventoryInboundDetailDO::getInboundDepartmentName, reqVO.getInboundDepartmentName())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundDepartmentId, reqVO.getInboundDepartmentId())
                .betweenIfPresent(MaterialInventoryInboundDetailDO::getInboundTime, reqVO.getInboundTime())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialInventoryInboundDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInventoryInboundDetailDO::getId));
    }

    @QmsPermission(deptIdColumn = "INB_DEPT_ID",
            moduleDataRoleCodes = QmsPermissionConstant.ADMIN_ROLE + "," + QmsPermissionConstant.MATERIAL_ADMIN_ROLE)
    default PageResult<MaterialInventoryInboundDetailRespVO> selectPageWithMaterialInfo(MaterialInventoryInboundDetailPageReqVO reqVO){
        MPJLambdaWrapperX<MaterialInventoryInboundDetailDO> wrapperX = new MPJLambdaWrapperX<MaterialInventoryInboundDetailDO>()
                .selectAll(MaterialInventoryInboundDetailDO.class)
                .selectAs(MaterialInfomationDO::getCode, MaterialInventoryInboundDetailRespVO::getInfomationCode)
                .selectAs(MaterialInfomationDO::getExpirationDate, MaterialInventoryInboundDetailRespVO::getExpirationDate)
                .selectAs(MaterialProductDO::getName, MaterialInventoryInboundDetailRespVO::getInfomationName)
                .selectAs(MaterialProductDO::getManufacturer, MaterialInventoryInboundDetailRespVO::getManufacturer)
                .selectAs(MaterialProductDO::getLabelTemplateKey, MaterialInventoryInboundDetailRespVO::getLabelTemplateKey)
                .selectAs(MaterialProductDO::getSpecification, MaterialInventoryInboundDetailRespVO::getSpecification)
                .selectAs(MaterialInventoryInboundDO::getBusinessType, MaterialInventoryInboundDetailRespVO::getBusinessType)
                .selectAs(ConfigWarehouseLocationDO::getName, MaterialInventoryInboundDetailRespVO::getLocationName)
                .selectAs(ConfigWarehouseLocationDO::getCode, MaterialInventoryInboundDetailRespVO::getLocationCode)
                .selectAs(MaterialBatchDO::getBatchNo, MaterialInventoryInboundDetailRespVO::getBatchNo)
                .leftJoin(MaterialInfomationDO.class, MaterialInfomationDO::getId, MaterialInventoryInboundDetailDO::getMaterialInfomationId)
                .leftJoin(MaterialProductDO.class, MaterialProductDO::getId, MaterialInfomationDO::getProductId)
                .leftJoin(MaterialInventoryInboundDO.class, MaterialInventoryInboundDO::getId, MaterialInventoryInboundDetailDO::getInboundId)
                .leftJoin(ConfigWarehouseLocationDO.class, ConfigWarehouseLocationDO::getId, MaterialInfomationDO::getLocationId)
                .leftJoin(MaterialBatchDO.class, MaterialBatchDO::getId, MaterialInfomationDO::getBatchId)
                .eqIfPresent(MaterialInfomationDO::getProductId, reqVO.getProductId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundId, reqVO.getInboundId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getBatchId, reqVO.getBatchId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getBatchGongduanId, reqVO.getBatchGongduanId())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getMaterialInfomationId, reqVO.getMaterialInfomationId())
                .likeIfPresent(MaterialInventoryInboundDetailDO::getInboundUserName, reqVO.getInboundUserName())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundUserId, reqVO.getInboundUserId())
                .likeIfPresent(MaterialInventoryInboundDetailDO::getInboundDepartmentName, reqVO.getInboundDepartmentName())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundDepartmentId, reqVO.getInboundDepartmentId())
                .betweenIfPresent(MaterialInventoryInboundDetailDO::getInboundTime, reqVO.getInboundTime())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialInventoryInboundDetailDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialInventoryInboundDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialInventoryInboundDetailDO::getId);
        return selectJoinPage(reqVO, MaterialInventoryInboundDetailRespVO.class, wrapperX);
    }

    default List<MaterialInventoryInboundDetailRespVO> selectMaterialInfoListByInboundId(Long inboundId) {
        MPJLambdaWrapperX<MaterialInventoryInboundDetailDO> wrapperX = new MPJLambdaWrapperX<MaterialInventoryInboundDetailDO>()
                .selectAll(MaterialInventoryInboundDetailDO.class)
                .selectAs(MaterialInfomationDO::getCode, MaterialInventoryInboundDetailRespVO::getInfomationCode)
                .selectAs(MaterialInfomationDO::getExpirationDate, MaterialInventoryInboundDetailRespVO::getExpirationDate)
                .selectAs(MaterialProductDO::getName, MaterialInventoryInboundDetailRespVO::getInfomationName)
                .selectAs(MaterialProductDO::getManufacturer, MaterialInventoryInboundDetailRespVO::getManufacturer)
                .selectAs(MaterialProductDO::getLabelTemplateKey, MaterialInventoryInboundDetailRespVO::getLabelTemplateKey)
                .selectAs(MaterialProductDO::getSpecification, MaterialInventoryInboundDetailRespVO::getSpecification)
                .selectAs(MaterialInventoryInboundDO::getBusinessType, MaterialInventoryInboundDetailRespVO::getBusinessType)
                .selectAs(ConfigWarehouseLocationDO::getName, MaterialInventoryInboundDetailRespVO::getLocationName)
                .selectAs(ConfigWarehouseLocationDO::getCode, MaterialInventoryInboundDetailRespVO::getLocationCode)
                .selectAs(MaterialBatchDO::getBatchNo, MaterialInventoryInboundDetailRespVO::getBatchNo)
                .leftJoin(MaterialInfomationDO.class, MaterialInfomationDO::getId, MaterialInventoryInboundDetailDO::getMaterialInfomationId)
                .leftJoin(MaterialProductDO.class, MaterialProductDO::getId, MaterialInfomationDO::getProductId)
                .leftJoin(MaterialBatchDO.class, MaterialBatchDO::getId, MaterialInfomationDO::getBatchId)
                .leftJoin(MaterialInventoryInboundDO.class, MaterialInventoryInboundDO::getId, MaterialInventoryInboundDetailDO::getInboundId)
                .leftJoin(ConfigWarehouseLocationDO.class, ConfigWarehouseLocationDO::getId, MaterialInfomationDO::getLocationId)
                .eqIfPresent(MaterialInventoryInboundDetailDO::getInboundId, inboundId);
        return selectJoinList(MaterialInventoryInboundDetailRespVO.class, wrapperX);
    }
}