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

import java.util.*;

import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl.On;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.controller.vo.BusinessAssayTaskDataExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataGroupRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataReqVO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
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.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import cn.hutool.core.collection.CollUtil;

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 BusinessAssayTaskDataMapper extends BaseMapperX<BusinessAssayTaskDataDO> {
	
	default List<BusinessAssayTaskDataExtendRespVO> selectList(BusinessAssayTaskDataReqVO reqVO) {
		return selectJoinList(BusinessAssayTaskDataExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDataDO>()
        		.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDataDO::getConfigAssayMethodId)
        		.leftJoin(BusinessSubSampleDO.class, BusinessSubSampleDO::getId, BusinessAssayTaskDataDO::getBusinessSubSampleId)
        		.selectAll(BusinessAssayTaskDataDO.class)
        		.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskDataExtendRespVO::getConfigAssayMethodName)
        		.selectAs(BusinessSubSampleDO::getSampleName, BusinessAssayTaskDataExtendRespVO::getSampleName)
        		.selectAs(BusinessSubSampleDO::getSampleCode, BusinessAssayTaskDataExtendRespVO::getSampleCode)
        		.selectAs(BusinessSubSampleDO::getSampleAssayCode, BusinessAssayTaskDataExtendRespVO::getSampleAssayCode)
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessBaseSampleId, reqVO.getBusinessBaseSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubParentSampleId, reqVO.getBusinessSubParentSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessAssayTaskId, reqVO.getBusinessAssayTaskId())
                .eqIfPresent(BusinessAssayTaskDataDO::getTaskType, reqVO.getTaskType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayType, reqVO.getAssayType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayProject, reqVO.getAssayProject())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentId, reqVO.getAssayDepartmentId())
                .likeIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentName, reqVO.getAssayDepartmentName())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayOperator, reqVO.getAssayOperator())
                .betweenIfPresent(BusinessAssayTaskDataDO::getAssignTaskTime, reqVO.getAssignTaskTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsAssignTasked, reqVO.getIsAssignTasked())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsReported, reqVO.getIsReported())
                .eqIfPresent(BusinessAssayTaskDataDO::getReporter, reqVO.getReporter())
                .betweenIfPresent(BusinessAssayTaskDataDO::getReportTime, reqVO.getReportTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigSampleFlowId, reqVO.getConfigSampleFlowId())
                .eqIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeKey, reqVO.getSampleFlowNodeKey())
                .betweenIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeTime, reqVO.getSampleFlowNodeTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getAnalysisCount, reqVO.getAnalysisCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessAssayTaskDataDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessAssayTaskDataDO::getId));
    }

    default PageResult<BusinessAssayTaskDataExtendRespVO> selectPage(BusinessAssayTaskDataPageReqVO reqVO) {
        return selectJoinPage(reqVO, BusinessAssayTaskDataExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDataDO>()
        		.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDataDO::getConfigAssayMethodId)
        		.leftJoin(BusinessSubSampleDO.class, BusinessSubSampleDO::getId, BusinessAssayTaskDataDO::getBusinessSubSampleId)
        		.selectAll(BusinessAssayTaskDataDO.class)
        		.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskDataExtendRespVO::getConfigAssayMethodName)
        		.selectAs(BusinessSubSampleDO::getSampleName, BusinessAssayTaskDataExtendRespVO::getSampleName)
        		.selectAs(BusinessSubSampleDO::getSampleCode, BusinessAssayTaskDataExtendRespVO::getSampleCode)
        		.selectAs(BusinessSubSampleDO::getSampleAssayCode, BusinessAssayTaskDataExtendRespVO::getSampleAssayCode)
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessBaseSampleId, reqVO.getBusinessBaseSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubParentSampleId, reqVO.getBusinessSubParentSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessAssayTaskId, reqVO.getBusinessAssayTaskId())
                .eqIfPresent(BusinessAssayTaskDataDO::getTaskType, reqVO.getTaskType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayType, reqVO.getAssayType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayProject, reqVO.getAssayProject())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentId, reqVO.getAssayDepartmentId())
                .likeIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentName, reqVO.getAssayDepartmentName())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayOperator, reqVO.getAssayOperator())
                .betweenIfPresent(BusinessAssayTaskDataDO::getAssignTaskTime, reqVO.getAssignTaskTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsAssignTasked, reqVO.getIsAssignTasked())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsReported, reqVO.getIsReported())
                .eqIfPresent(BusinessAssayTaskDataDO::getReporter, reqVO.getReporter())
                .betweenIfPresent(BusinessAssayTaskDataDO::getReportTime, reqVO.getReportTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigSampleFlowId, reqVO.getConfigSampleFlowId())
                .eqIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeKey, reqVO.getSampleFlowNodeKey())
                .betweenIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeTime, reqVO.getSampleFlowNodeTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getAnalysisCount, reqVO.getAnalysisCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessAssayTaskDataDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getRemark, reqVO.getRemark())
                .orderByDesc(BusinessAssayTaskDataDO::getId));
    }
	
	default List<BusinessAssayTaskDataExtendRespVO> selectResultAssessmentList(BusinessAssayTaskDataReqVO reqVO) {
		return selectJoinList(BusinessAssayTaskDataExtendRespVO.class, new MPJLambdaWrapperX<BusinessAssayTaskDataDO>()
        		.leftJoin(ConfigAssayMethodDO.class, ConfigAssayMethodDO::getId, BusinessAssayTaskDataDO::getConfigAssayMethodId)
        		.leftJoin(BusinessSubSampleDO.class, BusinessSubSampleDO::getId, BusinessAssayTaskDataDO::getBusinessSubSampleId)
//        		.leftJoin(BusinessSubSampleAnalysisGroupDO.class, on -> on
//        				.eq(BusinessSubSampleAnalysisGroupDO::getBusinessSubSampleId, BusinessAssayTaskDataDO::getBusinessSubSampleId).eq(BusinessSubSampleAnalysisGroupDO::getAssayDepartmentId, 103))
        		.selectAll(BusinessAssayTaskDataDO.class)
        		.selectAs(ConfigAssayMethodDO::getName, BusinessAssayTaskDataExtendRespVO::getConfigAssayMethodName)
        		.selectAs(BusinessSubSampleDO::getSampleName, BusinessAssayTaskDataExtendRespVO::getSampleName)
        		.selectAs(BusinessSubSampleDO::getSampleCode, BusinessAssayTaskDataExtendRespVO::getSampleCode)
        		.selectAs(BusinessSubSampleDO::getSampleAssayCode, BusinessAssayTaskDataExtendRespVO::getSampleAssayCode)
//        		.selectAs(BusinessSubSampleAnalysisGroupDO::getSampleReceiver, BusinessAssayTaskDataExtendRespVO::getSampleReceiver)
//        		.selectAs(BusinessSubSampleAnalysisGroupDO::getSampleReceiveTime, BusinessAssayTaskDataExtendRespVO::getSampleReceiveTime)
        		.eq(BusinessAssayTaskDataDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessBaseSampleId, reqVO.getBusinessBaseSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubParentSampleId, reqVO.getBusinessSubParentSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessSubSampleId, reqVO.getBusinessSubSampleId())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigAssayMethodId, reqVO.getConfigAssayMethodId())
                .eqIfPresent(BusinessAssayTaskDataDO::getBusinessAssayTaskId, reqVO.getBusinessAssayTaskId())
                .eqIfPresent(BusinessAssayTaskDataDO::getTaskType, reqVO.getTaskType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayType, reqVO.getAssayType())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayProject, reqVO.getAssayProject())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentId, reqVO.getAssayDepartmentId())
                .likeIfPresent(BusinessAssayTaskDataDO::getAssayDepartmentName, reqVO.getAssayDepartmentName())
                .eqIfPresent(BusinessAssayTaskDataDO::getAssayOperator, reqVO.getAssayOperator())
                .betweenIfPresent(BusinessAssayTaskDataDO::getAssignTaskTime, reqVO.getAssignTaskTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsAssignTasked, reqVO.getIsAssignTasked())
                .eqIfPresent(BusinessAssayTaskDataDO::getIsReported, reqVO.getIsReported())
                .eqIfPresent(BusinessAssayTaskDataDO::getReporter, reqVO.getReporter())
                .betweenIfPresent(BusinessAssayTaskDataDO::getReportTime, reqVO.getReportTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getConfigSampleFlowId, reqVO.getConfigSampleFlowId())
                .eqIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeKey, reqVO.getSampleFlowNodeKey())
                .betweenIfPresent(BusinessAssayTaskDataDO::getSampleFlowNodeTime, reqVO.getSampleFlowNodeTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getAnalysisCount, reqVO.getAnalysisCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getUpdateCount, reqVO.getUpdateCount())
                .eqIfPresent(BusinessAssayTaskDataDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BusinessAssayTaskDataDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BusinessAssayTaskDataDO::getRemark, reqVO.getRemark())
                .orderByAsc(BusinessAssayTaskDataDO::getBusinessSubSampleId)
                .orderByAsc(BusinessAssayTaskDataDO::getReportTime));
    }
	
	/**
	 * 查询未指派的分析任务分组
	 * @param reqVO
	 * @return
	 */
	List<BusinessAssayTaskDataGroupRespVO> selectUnAssayTaskGroupList(@Param("reqVO") BusinessAssayTaskDataReqVO reqVO);

	/**
	 * 查询未审核的分析任务分组
	 * @param reqVO
	 * @return
	 */
	List<BusinessAssayTaskDataGroupRespVO> selectUnAuditTaskGroupList(@Param("reqVO") BusinessAssayTaskDataReqVO reqVO);

    /**
     * 根据任务分配单id查询分析任务
     * @param businessAssayTaskId 任务分配单id
     * @return
     */
	default List<BusinessAssayTaskDataDO> selectByBusinessAssayTaskId(Long businessAssayTaskId) {
		return selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>()
				.eq(BusinessAssayTaskDataDO::getBusinessAssayTaskId, businessAssayTaskId));
	}

	/**
	 * 查询是否上报的分析任务
	 * @param excludeIds 排除的id列表
	 * @param businessSubSampleId 子样id
	 * @param configAssayMethodId 分析方法id
	 * @param isReported 是否上报
	 * @return
	 */
	default List<BusinessAssayTaskDataDO> selectIsReportedList(List<Long> excludeIds, Long businessSubSampleId, Long configAssayMethodId, Integer isReported) {
		LambdaQueryWrapper<BusinessAssayTaskDataDO> queryWrapper = new LambdaQueryWrapperX<BusinessAssayTaskDataDO>();
		if (CollUtil.isNotEmpty(excludeIds)) {
			queryWrapper.notIn(BusinessAssayTaskDataDO::getId, excludeIds);
		}
		queryWrapper.eq(BusinessAssayTaskDataDO::getBusinessSubSampleId, businessSubSampleId)
		.eq(BusinessAssayTaskDataDO::getConfigAssayMethodId, configAssayMethodId)
		.eq(BusinessAssayTaskDataDO::getIsReported, isReported);
		return selectList(queryWrapper);
	}

	/**
	 * 查询最大分析次数的检测任务数据
	 * @param businessSubSampleId 子样id
	 * @param configAssayMethodId 分析方法
	 * @return
	 */
	default List<BusinessAssayTaskDataDO> selectMaxAnalysisCountByBusinessSubSampleIdAndConfigAssayMethodId(Long businessSubSampleId, Long configAssayMethodId) {
		BusinessAssayTaskDataDO maxAnalysisCount = selectOne(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>()
				.eq(BusinessAssayTaskDataDO::getBusinessSubSampleId, businessSubSampleId)
				.eq(BusinessAssayTaskDataDO::getConfigAssayMethodId, configAssayMethodId)
				.orderByDesc(BusinessAssayTaskDataDO::getAnalysisCount)
				.last("LIMIT 1"));
		
		return selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>()
				.eq(BusinessAssayTaskDataDO::getBusinessSubSampleId, businessSubSampleId)
				.eq(BusinessAssayTaskDataDO::getConfigAssayMethodId, configAssayMethodId)
				.eq(BusinessAssayTaskDataDO::getAnalysisCount, maxAnalysisCount.getAnalysisCount()));
		
		
	}

}