package com.zt.plat.module.qms.business.bus.dal.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.framework.common.pojo.PageResult;
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.bus.controller.vo.BusinessSampleEntrustRegistrationExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationPageReqVO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigEntrustSourceDO;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 委检登记业务 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessSampleEntrustRegistrationMapper extends BaseMapperX<BusinessSampleEntrustRegistrationDO> {

    default PageResult<BusinessSampleEntrustRegistrationExtendRespVO> selectPage(BusinessSampleEntrustRegistrationPageReqVO reqVO) {
        return selectJoinPage(reqVO, BusinessSampleEntrustRegistrationExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustRegistrationDO>()
        		.selectSub(BusinessSampleEntrustDetailDO.class, s -> s.selectCount(BusinessSampleEntrustDetailDO::getId).eq(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, BusinessSampleEntrustRegistrationDO::getId).eq(BusinessSampleEntrustDetailDO::getIsWeighing, 1), BusinessSampleEntrustRegistrationExtendRespVO::getSampleWeighingCount)
        		.leftJoin(ConfigEntrustSourceDO.class, ConfigEntrustSourceDO::getId, BusinessSampleEntrustRegistrationDO::getConfigEntrustSourceId)
        		.selectAll(BusinessSampleEntrustRegistrationDO.class)
        		.selectAs(ConfigEntrustSourceDO::getName, BusinessSampleEntrustRegistrationExtendRespVO::getConfigEntrustSourceName)
        		.selectAs(ConfigEntrustSourceDO::getKey, BusinessSampleEntrustRegistrationExtendRespVO::getConfigEntrustSourceKey)
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustId, reqVO.getEntrustId())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustNumber, reqVO.getEntrustNumber())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustUnit, reqVO.getEntrustUnit())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getConfigEntrustSourceId, reqVO.getConfigEntrustSourceId())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleQuantity, reqVO.getSampleQuantity())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getTaskQuantity, reqVO.getTaskQuantity())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getTaskFinishedQuantity, reqVO.getTaskFinishedQuantity())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getRemaineSampleRequirement, reqVO.getRemaineSampleRequirement())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getCreateWay, reqVO.getCreateWay())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustType, reqVO.getEntrustType())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustTime, reqVO.getEntrustTime())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleSender, reqVO.getSampleSender())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getSampleSendDate, reqVO.getSampleSendDate())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleReceiver, reqVO.getSampleReceiver())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getSampleReceiveDate, reqVO.getSampleReceiveDate())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleStatus, reqVO.getSampleStatus())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getExternalInfomationDataCollectionId, reqVO.getExternalInfomationDataCollectionId())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getExternalInfomation, reqVO.getExternalInfomation())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getConfigReportTemplateId, reqVO.getConfigReportTemplateId())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getConfigReportTemplateKey, reqVO.getConfigReportTemplateKey())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getTel, reqVO.getTel())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getFax, reqVO.getFax())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getAddress, reqVO.getAddress())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getPostal, reqVO.getPostal())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEmail, reqVO.getEmail())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getEntrustCategory, reqVO.getEntrustCategory())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleCategory, reqVO.getSampleCategory())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSampleOrigin, reqVO.getSampleOrigin())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSecrecyRequire, reqVO.getSecrecyRequire())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getAssayMethod, reqVO.getAssayMethod())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getIsMaterialAssayStandard, reqVO.getIsMaterialAssayStandard())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getRegistrationStatus, reqVO.getRegistrationStatus())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDataCheckStatus, reqVO.getDataCheckStatus())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getAssayStatus, reqVO.getAssayStatus())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDataStatus, reqVO.getDataStatus())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentMainId, reqVO.getDocumentMainId())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentSendWay, reqVO.getDocumentSendWay())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentPublisher, reqVO.getDocumentPublisher())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentPublisherDate, reqVO.getDocumentPublisherDate())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentReceiver, reqVO.getDocumentReceiver())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getDocumentReceiveDate, reqVO.getDocumentReceiveDate())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleEntrustRegistrationDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getRemark, reqVO.getRemark())
                .eqIfPresent(BusinessSampleEntrustRegistrationDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(ConfigEntrustSourceDO::getName, reqVO.getConfigEntrustSourceName())
                .orderByDesc(BusinessSampleEntrustRegistrationDO::getId));
    }
    
    default BusinessSampleEntrustRegistrationExtendRespVO selectExtendById(Long id) {
    	return selectJoinOne(BusinessSampleEntrustRegistrationExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustRegistrationDO>()
    			.leftJoin(ConfigEntrustSourceDO.class, ConfigEntrustSourceDO::getId, BusinessSampleEntrustRegistrationDO::getConfigEntrustSourceId)
        		.selectAll(BusinessSampleEntrustRegistrationDO.class)
        		.selectAs(ConfigEntrustSourceDO::getName, BusinessSampleEntrustRegistrationExtendRespVO::getConfigEntrustSourceName)
        		.selectAs(ConfigEntrustSourceDO::getKey, BusinessSampleEntrustRegistrationExtendRespVO::getConfigEntrustSourceKey)
        		.eq(BusinessSampleEntrustRegistrationDO::getId, id)
    			);
    }


	//查询报告待编制数据
	Page<BusinessSampleEntrustRegistrationDO> queryWaitingDataForReport(IPage<?> page, @Param("param") BusinessSampleEntrustRegistrationPageReqVO param);
}