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

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.BusinessAssayTaskDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDetailDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.common.dic.dal.dataobject.DictionaryBusinessDO;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

/**
 * 检测任务分配业务 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BusinessAssayTaskMapper extends BaseMapperX<BusinessAssayTaskDO> {

    default PageResult<BusinessAssayTaskExtendRespVO> selectPage(BusinessAssayTaskPageReqVO reqVO) {
        return selectJoinPage(reqVO, BusinessAssayTaskExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDO>()
        		.selectSub(BusinessAssayTaskDetailDO.class, s -> s.selectCount(BusinessAssayTaskDetailDO::getId).eq(BusinessAssayTaskDetailDO::getBusinessAssayTaskId, BusinessAssayTaskDO::getId), BusinessAssayTaskExtendRespVO::getAssayTaskCount)
//        		.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDO::getConfigAssayMethodId)
        		.leftJoin(DictionaryBusinessDO.class, DictionaryBusinessDO::getId, BusinessAssayTaskDO::getDictionaryBusinessId)
        		.selectAll(BusinessAssayTaskDO.class)
//        		.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskExtendRespVO::getConfigAssayMethodName)
//        		.selectAs(ConfigAssayMethodDO::getMethodNameCategory, BusinessAssayTaskExtendRespVO::getConfigAssayMethodNameAndCategory)
        		.selectAs(DictionaryBusinessDO::getName, BusinessAssayTaskExtendRespVO::getDictionaryBusinessName)
                .eqIfPresent(BusinessAssayTaskDO::getTaskNo, reqVO.getTaskNo())
                .likeIfPresent(BusinessAssayTaskDO::getTaskName, reqVO.getTaskName())
                .eqIfPresent(BusinessAssayTaskDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
                .eqIfPresent(BusinessAssayTaskDO::getDictionaryBusinessKey, reqVO.getDictionaryBusinessKey())
//                .eqIfPresent(BusinessAssayTaskDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDO::getConfigReportTemplateId, reqVO.getConfigReportTemplateId())
                .eqIfPresent(BusinessAssayTaskDO::getConfigReportTemplateKey, reqVO.getConfigReportTemplateKey())
                .eqIfPresent(BusinessAssayTaskDO::getTaskSourceType, reqVO.getTaskSourceType())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignOperator, reqVO.getTaskAssignOperator())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignOperatorId, reqVO.getTaskAssignOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskAssignTime, reqVO.getTaskAssignTime())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignStatus, reqVO.getTaskAssignStatus())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskAssignSubmitTime, reqVO.getTaskAssignSubmitTime())
                .eqIfPresent(BusinessAssayTaskDO::getIsIngredients, reqVO.getIsIngredients())
                .eqIfPresent(BusinessAssayTaskDO::getIngredientsWay, reqVO.getIngredientsWay())
                .eqIfPresent(BusinessAssayTaskDO::getIngredientsStatus, reqVO.getIngredientsStatus())
                .eqIfPresent(BusinessAssayTaskDO::getAssayOperator, reqVO.getAssayOperator())
                .eqIfPresent(BusinessAssayTaskDO::getAssayOperatorId, reqVO.getAssayOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getAssayTime, reqVO.getAssayTime())
                .betweenIfPresent(BusinessAssayTaskDO::getAssaySubmitTime, reqVO.getAssaySubmitTime())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssayStatus, reqVO.getTaskAssayStatus())
                .inIfPresent(BusinessAssayTaskDO::getTaskAssayStatus, reqVO.getTaskAssayStatusList())
                .betweenIfPresent(BusinessAssayTaskDO::getReportTime, reqVO.getReportTime())
                .eqIfPresent(BusinessAssayTaskDO::getReportOperator, reqVO.getReportOperator())
                .eqIfPresent(BusinessAssayTaskDO::getReportOperatorId, reqVO.getReportOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskFinishTime, reqVO.getTaskFinishTime())
                .eqIfPresent(BusinessAssayTaskDO::getFlowInstanceId, reqVO.getFlowInstanceId())
                .eqIfPresent(BusinessAssayTaskDO::getFormDataCollectionId, reqVO.getFormDataCollectionId())
                .eqIfPresent(BusinessAssayTaskDO::getFormValue, reqVO.getFormValue())
                .eqIfPresent(BusinessAssayTaskDO::getDocumentSignature, reqVO.getDocumentSignature())
                .eqIfPresent(BusinessAssayTaskDO::getFinishStatus, reqVO.getFinishStatus())
                .eqIfPresent(BusinessAssayTaskDO::getFlowStatus, reqVO.getFlowStatus())
                .eqIfPresent(BusinessAssayTaskDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessAssayTaskDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessAssayTaskDO::getRemark, reqVO.getRemark())
//                .likeIfPresent(ConfigAssayMethodDO::getName, reqVO.getConfigAssayMethodName())
//                .likeIfPresent(ConfigAssayMethodDO::getMethodNameCategory, reqVO.getConfigAssayMethodNameAndCategory())
                .likeIfPresent(DictionaryBusinessDO::getName, reqVO.getDictionaryBusinessName())
                .neIfPresent(BusinessAssayTaskDO::getAssayOperator, reqVO.getNeAssayOperator())
                .eqIfPresent(BusinessAssayTaskDO::getDeptId, reqVO.getDeptId())
                .orderByDesc(BusinessAssayTaskDO::getId));
    }


	default List<BusinessAssayTaskExtendRespVO> selectList(BusinessAssayTaskPageReqVO reqVO) {
		return selectJoinList(BusinessAssayTaskExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDO>()
        		.selectSub(BusinessAssayTaskDetailDO.class, s -> s.selectCount(BusinessAssayTaskDetailDO::getId).eq(BusinessAssayTaskDetailDO::getBusinessAssayTaskId, BusinessAssayTaskDO::getId), BusinessAssayTaskExtendRespVO::getAssayTaskCount)
//        		.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDO::getConfigAssayMethodId)
        		.leftJoin(DictionaryBusinessDO.class, DictionaryBusinessDO::getId, BusinessAssayTaskDO::getDictionaryBusinessId)
        		.selectAll(BusinessAssayTaskDO.class)
//        		.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskExtendRespVO::getConfigAssayMethodName)
//        		.selectAs(ConfigAssayMethodDO::getMethodNameCategory, BusinessAssayTaskExtendRespVO::getConfigAssayMethodNameAndCategory)
        		.selectAs(DictionaryBusinessDO::getName, BusinessAssayTaskExtendRespVO::getDictionaryBusinessName)
                .eqIfPresent(BusinessAssayTaskDO::getTaskNo, reqVO.getTaskNo())
                .likeIfPresent(BusinessAssayTaskDO::getTaskName, reqVO.getTaskName())
                .eqIfPresent(BusinessAssayTaskDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
                .eqIfPresent(BusinessAssayTaskDO::getDictionaryBusinessKey, reqVO.getDictionaryBusinessKey())
//                .eqIfPresent(BusinessAssayTaskDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDO::getConfigReportTemplateId, reqVO.getConfigReportTemplateId())
                .eqIfPresent(BusinessAssayTaskDO::getConfigReportTemplateKey, reqVO.getConfigReportTemplateKey())
                .eqIfPresent(BusinessAssayTaskDO::getTaskSourceType, reqVO.getTaskSourceType())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignOperator, reqVO.getTaskAssignOperator())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignOperatorId, reqVO.getTaskAssignOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskAssignTime, reqVO.getTaskAssignTime())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssignStatus, reqVO.getTaskAssignStatus())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskAssignSubmitTime, reqVO.getTaskAssignSubmitTime())
                .eqIfPresent(BusinessAssayTaskDO::getIsIngredients, reqVO.getIsIngredients())
                .eqIfPresent(BusinessAssayTaskDO::getIngredientsWay, reqVO.getIngredientsWay())
                .eqIfPresent(BusinessAssayTaskDO::getIngredientsStatus, reqVO.getIngredientsStatus())
                .eqIfPresent(BusinessAssayTaskDO::getAssayOperator, reqVO.getAssayOperator())
                .eqIfPresent(BusinessAssayTaskDO::getAssayOperatorId, reqVO.getAssayOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getAssayTime, reqVO.getAssayTime())
                .betweenIfPresent(BusinessAssayTaskDO::getAssaySubmitTime, reqVO.getAssaySubmitTime())
                .eqIfPresent(BusinessAssayTaskDO::getTaskAssayStatus, reqVO.getTaskAssayStatus())
                .inIfPresent(BusinessAssayTaskDO::getTaskAssayStatus, reqVO.getTaskAssayStatusList())
                .betweenIfPresent(BusinessAssayTaskDO::getReportTime, reqVO.getReportTime())
                .eqIfPresent(BusinessAssayTaskDO::getReportOperator, reqVO.getReportOperator())
                .eqIfPresent(BusinessAssayTaskDO::getReportOperatorId, reqVO.getReportOperatorId())
                .betweenIfPresent(BusinessAssayTaskDO::getTaskFinishTime, reqVO.getTaskFinishTime())
                .eqIfPresent(BusinessAssayTaskDO::getFlowInstanceId, reqVO.getFlowInstanceId())
                .eqIfPresent(BusinessAssayTaskDO::getFormDataCollectionId, reqVO.getFormDataCollectionId())
                .eqIfPresent(BusinessAssayTaskDO::getFormValue, reqVO.getFormValue())
                .eqIfPresent(BusinessAssayTaskDO::getDocumentSignature, reqVO.getDocumentSignature())
                .eqIfPresent(BusinessAssayTaskDO::getFinishStatus, reqVO.getFinishStatus())
                .eqIfPresent(BusinessAssayTaskDO::getFlowStatus, reqVO.getFlowStatus())
                .eqIfPresent(BusinessAssayTaskDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessAssayTaskDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessAssayTaskDO::getRemark, reqVO.getRemark())
//                .likeIfPresent(ConfigAssayMethodDO::getName, reqVO.getConfigAssayMethodName())
//                .likeIfPresent(ConfigAssayMethodDO::getMethodNameCategory, reqVO.getConfigAssayMethodNameAndCategory())
                .likeIfPresent(DictionaryBusinessDO::getName, reqVO.getDictionaryBusinessName())
                .eqIfPresent(BusinessAssayTaskDO::getDeptId, reqVO.getDeptId())
                .neIfPresent(BusinessAssayTaskDO::getAssayOperator, reqVO.getNeAssayOperator())
                .orderByDesc(BusinessAssayTaskDO::getId));
	}
    
    
//    /**
//     * 根据分析方法、 分析人和分配人 查询进行中的任务分配单。
//     * @param configAssayMethodId 分析方法
//     * @param assayUser 分析人
//     * @param assignUser 分配人
//     * @return
//     */
//    default BusinessAssayTaskDO selectAssignInProgressByAssayUserAndAssignUser(Long configAssayMethodId, String assayUser, String assignUser) {
//    	return selectOne(new LambdaQueryWrapperX<BusinessAssayTaskDO>()
//                .eq(BusinessAssayTaskDO::getConfigAssayMethodId, configAssayMethodId)
//    			.eq(BusinessAssayTaskDO::getAssayOperator, assayUser)
//    			.eq(BusinessAssayTaskDO::getTaskAssignOperator, assignUser)
//    			.eq(BusinessAssayTaskDO::getTaskAssignStatus, QmsCommonConstant.IN_PROGRESS));
//    }
	
    /**
     * 根据分析方法类型id、 分析人和分配人 查询进行中的任务分配单。
     * @param dictionaryBusinessId 分析方法
     * @param assayUser 分析人
     * @param assignUser 分配人
     * @return
     */
    default BusinessAssayTaskDO selectAssignInProgressByAssayUserAndAssignUser(Long dictionaryBusinessId, String assayUser, String assignUser) {
    	return selectOne(new LambdaQueryWrapperX<BusinessAssayTaskDO>()
                .eq(BusinessAssayTaskDO::getDictionaryBusinessId, dictionaryBusinessId)
    			.eq(BusinessAssayTaskDO::getAssayOperator, assayUser)
    			.eq(BusinessAssayTaskDO::getTaskAssignOperator, assignUser)
    			.eq(BusinessAssayTaskDO::getTaskAssignStatus, QmsCommonConstant.IN_PROGRESS));
    }

    /**
     * 根据id查询分配任务单
     * @param id
     * @return
     */
	default BusinessAssayTaskExtendRespVO selectBusinessAssayTaskById(Long id) {
		return selectJoinOne(BusinessAssayTaskExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDO>()
				.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDO::getConfigAssayMethodId)
        		.leftJoin(DictionaryBusinessDO.class, DictionaryBusinessDO::getId, BusinessAssayTaskDO::getDictionaryBusinessId)
				.selectAll(BusinessAssayTaskDO.class)
//				.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskExtendRespVO::getConfigAssayMethodName)
//        		.selectAs(ConfigAssayMethodDO::getMethodNameCategory, BusinessAssayTaskExtendRespVO::getConfigAssayMethodNameAndCategory)
        		.selectAs(DictionaryBusinessDO::getName, BusinessAssayTaskExtendRespVO::getDictionaryBusinessName)
        		.selectAs(ConfigAssayMethodDO::getReportDataType, BusinessAssayTaskExtendRespVO::getReportDataType)
				.eq(BusinessAssayTaskDO::getId, id));
	}

}