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

import java.util.*;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustUnStandardProjectDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAnalysisGroupDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;

import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;

/**
 * 委检登记样品检测项目表(无标准) Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessSampleEntrustUnStandardProjectMapper extends BaseMapperX<BusinessSampleEntrustUnStandardProjectDO> {

    default PageResult<BusinessSampleEntrustUnStandardProjectDO> selectPage(BusinessSampleEntrustUnStandardProjectPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BusinessSampleEntrustUnStandardProjectDO>()
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getBusinessSampleEntrustDetailId, reqVO.getBusinessSampleEntrustDetailId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getAssayDepartmentId, reqVO.getAssayDepartmentId())
                .likeIfPresent(BusinessSampleEntrustUnStandardProjectDO::getAssayDepartmentName, reqVO.getAssayDepartmentName())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getBusinessAssayTaskDataId, reqVO.getBusinessAssayTaskDataId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getConfigAssayMethodProjectId, reqVO.getConfigAssayMethodProjectId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getTaskCount, reqVO.getTaskCount())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getIsAssignTasked, reqVO.getIsAssignTasked())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getDictionaryProjectId, reqVO.getDictionaryProjectId())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getUsage, reqVO.getUsage())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getIsEnabled, reqVO.getIsEnabled())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessSampleEntrustUnStandardProjectDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessSampleEntrustUnStandardProjectDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessSampleEntrustUnStandardProjectDO::getId));
    }
    
    default List<BusinessSampleEntrustUnStandardProjectExtendRespVO> selectList(BusinessSampleEntrustUnStandardProjectPageReqVO reqVO) {
		MPJLambdaWrapper<BusinessSampleEntrustUnStandardProjectDO> mpjLambdaWrapper = new MPJLambdaWrapperX<BusinessSampleEntrustUnStandardProjectDO>()
    	.leftJoin(BusinessSampleEntrustDetailDO.class, BusinessSampleEntrustDetailDO::getId, BusinessSampleEntrustUnStandardProjectDO::getBusinessSampleEntrustDetailId)
		.leftJoin(BusinessSubSampleDO.class, BusinessSubSampleDO::getId, BusinessSampleEntrustUnStandardProjectDO::getBusinessSubSampleId)
		.leftJoin(BusinessSubSampleAnalysisGroupDO.class, on -> on
				.eq(BusinessSubSampleAnalysisGroupDO::getBusinessSubSampleId, BusinessSampleEntrustUnStandardProjectDO::getBusinessSubSampleId)
				.eq(BusinessSubSampleAnalysisGroupDO::getAssayDepartmentId, BusinessSampleEntrustUnStandardProjectDO::getAssayDepartmentId))
		.leftJoin(DictionaryProjectDO.class, DictionaryProjectDO::getId, BusinessSampleEntrustUnStandardProjectDO::getDictionaryProjectId)
		.selectAll(BusinessSampleEntrustUnStandardProjectDO.class)
		.selectAs(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessSampleEntrustRegistrationId)
		.selectAs(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessBaseSampleId)
		.selectAs(DictionaryProjectDO::getName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectName)
		.selectAs(DictionaryProjectDO::getSimpleName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectSimpleName)
		.selectAs(DictionaryProjectDO::getShowName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectShowName)
		.selectAs(BusinessSubSampleDO::getSampleName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getSampleName)
		.selectAs(BusinessSubSampleDO::getSampleCode, BusinessSampleEntrustUnStandardProjectExtendRespVO::getSampleCode)
		.selectAs(BusinessSubSampleDO::getSampleAssayCode, BusinessSampleEntrustUnStandardProjectExtendRespVO::getSampleAssayCode)
		.selectAs(BusinessSubSampleDO::getConfigSubSampleId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getConfigSubSampleId)
		.selectAs(BusinessSubSampleDO::getSampleFlowNodeKey, BusinessSampleEntrustUnStandardProjectExtendRespVO::getSampleFlowNodeKey)
		.selectAs(BusinessSubSampleDO::getSampleFlowNodeTime, BusinessSampleEntrustUnStandardProjectExtendRespVO::getSampleFlowNodeTime);

	    if (ObjectUtil.isNotEmpty(reqVO.getAssayDepartmentId())) {
    		mpjLambdaWrapper.eq(BusinessSampleEntrustUnStandardProjectDO::getAssayDepartmentId, reqVO.getAssayDepartmentId());
		}
	    
	    if (ObjectUtil.isNotEmpty(reqVO.getIsAssignTasked())) {
    		mpjLambdaWrapper.eq(BusinessSampleEntrustUnStandardProjectDO::getIsAssignTasked, reqVO.getIsAssignTasked());
		}
	    
	    if (ObjectUtil.isNotEmpty(reqVO.getSampleName())) {
	    	mpjLambdaWrapper.like(BusinessSubSampleDO::getSampleName, reqVO.getSampleName());
		}
	    
	    if (ObjectUtil.isNotEmpty(reqVO.getSampleAssayCode())) {
	    	mpjLambdaWrapper.like(BusinessSubSampleDO::getSampleAssayCode, reqVO.getSampleAssayCode());
		}
    	if (ObjectUtil.isNotEmpty(reqVO.getSampleFlowNodeTime()) && ObjectUtil.isNotEmpty(reqVO.getSampleFlowNodeTime()[0]) && ObjectUtil.isNotEmpty(reqVO.getSampleFlowNodeTime()[1])) {
    		mpjLambdaWrapper.between(BusinessSubSampleDO::getSampleFlowNodeTime, reqVO.getSampleFlowNodeTime()[0], reqVO.getSampleFlowNodeTime()[1]);
		}
	    
	    if (ObjectUtil.isAllNotEmpty(reqVO.getDictionaryProjectIdList()) && !ArrayUtil.isEmpty(reqVO.getDictionaryProjectIdList())) {
	    	mpjLambdaWrapper.in(BusinessSampleEntrustUnStandardProjectDO::getDictionaryProjectId, reqVO.getDictionaryProjectIdList());
        }
	    
	    if (ObjectUtil.isNotEmpty(reqVO.getSampleStatus())) {
    		mpjLambdaWrapper.eq(BusinessSubSampleAnalysisGroupDO::getSampleStatus, reqVO.getSampleStatus());
		}
		mpjLambdaWrapper.orderByAsc(BusinessSubSampleDO::getSampleFlowNodeTime);
    	return selectJoinList(BusinessSampleEntrustUnStandardProjectExtendRespVO.class, mpjLambdaWrapper);
    }
    
    default List<BusinessSampleEntrustUnStandardProjectDO> selectByBusinessSampleEntrustDetailIds(List<Long> businessSampleEntrustDetailIdList) {
    	return selectList(new LambdaQueryWrapperX<BusinessSampleEntrustUnStandardProjectDO>()
    			.in(BusinessSampleEntrustUnStandardProjectDO::getBusinessSampleEntrustDetailId, businessSampleEntrustDetailIdList));
    }

    default List<BusinessSampleEntrustUnStandardProjectExtendRespVO> selectExtendByIds(List<Long> idList) {
    	return selectJoinList(BusinessSampleEntrustUnStandardProjectExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustUnStandardProjectDO>()
    			.leftJoin(BusinessSampleEntrustDetailDO.class, BusinessSampleEntrustDetailDO::getId, BusinessSampleEntrustUnStandardProjectDO::getBusinessSampleEntrustDetailId)
    			.leftJoin(DictionaryProjectDO.class, DictionaryProjectDO::getId, BusinessSampleEntrustUnStandardProjectDO::getDictionaryProjectId)
    			.selectAll(BusinessSampleEntrustUnStandardProjectDO.class)
    			.selectAs(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessSampleEntrustRegistrationId)
    			.selectAs(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessBaseSampleId)
    			.selectAs(DictionaryProjectDO::getName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectName)
    			.selectAs(DictionaryProjectDO::getSimpleName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectSimpleName)
    			.selectAs(DictionaryProjectDO::getShowName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectShowName)
    			.in(BusinessSampleEntrustUnStandardProjectDO::getId, idList)
    			.orderByAsc(BusinessSampleEntrustUnStandardProjectDO::getId)
    			);
    } 
    
    default List<BusinessSampleEntrustUnStandardProjectExtendRespVO> selectByBusinessBaseSampleId(Long businessBaseSampleId) {
    	return selectJoinList(BusinessSampleEntrustUnStandardProjectExtendRespVO.class, new MPJLambdaWrapperX<BusinessSampleEntrustUnStandardProjectDO>()
    			.leftJoin(BusinessSampleEntrustDetailDO.class, BusinessSampleEntrustDetailDO::getId, BusinessSampleEntrustUnStandardProjectDO::getBusinessSampleEntrustDetailId)
    			.leftJoin(DictionaryProjectDO.class, DictionaryProjectDO::getId, BusinessSampleEntrustUnStandardProjectDO::getDictionaryProjectId)
    			.selectAll(BusinessSampleEntrustUnStandardProjectDO.class)
    			.selectAs(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessSampleEntrustRegistrationId)
    			.selectAs(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, BusinessSampleEntrustUnStandardProjectExtendRespVO::getBusinessBaseSampleId)
    			.selectAs(DictionaryProjectDO::getName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectName)
    			.selectAs(DictionaryProjectDO::getSimpleName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectSimpleName)
    			.selectAs(DictionaryProjectDO::getShowName, BusinessSampleEntrustUnStandardProjectExtendRespVO::getDictionaryProjectShowName)
    			.eq(BusinessSampleEntrustDetailDO::getBusinessBaseSampleId, businessBaseSampleId)
    			.orderByAsc(BusinessSampleEntrustUnStandardProjectDO::getId)
    			);
    } 

}