package com.zt.plat.module.qms.business.bus.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayProjectDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCCoefficientDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCCoefficientParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCManagementDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCManagementParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCManagementProjectDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessQCManagementSampleParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubParentSampleAssessmentDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAssessmentDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayProjectDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayReportDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCCoefficientDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCCoefficientParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCManagementDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCManagementParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCManagementProjectDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessQCManagementSampleParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleAssessmentMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleAssessmentMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleMapper;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignManualParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignManualTypeParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignProjectMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignProjectNoMethodParam;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleTaskAssignSampleParam;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleTaskAssignContext;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigAssayMethodProjectExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigQCSampleMethodExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigQCSampleMethodParameterExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodConfInfo;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodConfItem;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.MaterialAssayStandardMethodExtendRespVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodParameterDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodProjectParameterDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodParameterMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodProjectMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodProjectParameterMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigQCSampleMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigQCSampleMethodParameterMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardMethodMapper;
import com.zt.plat.module.qms.business.dic.dal.mapper.DictionaryProjectMapper;
import com.zt.plat.module.qms.core.code.SequenceUtil;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import com.zt.plat.module.system.api.dept.DeptApi;
import com.zt.plat.module.system.api.dept.dto.DeptRespDTO;
import com.zt.plat.module.system.api.user.AdminUserApi;
import com.zt.plat.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class SampleTaskAssignServiceImpl implements SampleTaskAssignService {

    @Resource
    private FlowExecutor flowExecutor;
    
    @Resource
    private AdminUserApi adminUserApi;
    
    @Resource
    private DeptApi deptApi; 

	@Resource
	private SequenceUtil sequenceUtil;
	
    @Resource
    private BusinessAssayTaskMapper businessAssayTaskMapper;
    
    @Resource
    private BusinessAssayTaskParameterDataMapper businessAssayTaskParameterDataMapper;
    
    @Resource
    private BusinessAssayTaskDetailMapper businessAssayTaskDetailMapper;
    
    @Resource
    private BusinessSubSampleMapper businessSubSampleMapper;
    
    @Resource
    private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
    
    @Resource
    private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;
    
    @Resource
    private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;
    
    @Resource
    private BusinessSubSampleAssessmentMapper businessSubSampleAssessmentMapper;
    
    @Resource
    private BusinessSubParentSampleAssessmentMapper businessSubParentSampleAssessmentMapper;
    
    @Resource
    private BusinessAssayReportDataMapper businessAssayReportDataMapper;
    
    @Resource
    private ConfigSubSampleMethodMapper configSubSampleMethodMapper;
    
    @Resource
    private ConfigAssayMethodMapper configAssayMethodMapper;
    
    @Resource
    private ConfigAssayMethodParameterMapper configAssayMethodParameterMapper;
    
    @Resource
    private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;
    
    @Resource
    private ConfigAssayMethodProjectParameterMapper configAssayMethodProjectParameterMapper;
    
    @Resource
    private DictionaryProjectMapper dictionaryProjectMapper;
    
    @Resource
    private ConfigQCSampleMethodMapper configQCSampleMethodMapper;
    
    @Resource
    private ConfigQCSampleMethodParameterMapper configQCSampleMethodParameterMapper;
    
    @Resource
    private BusinessQCCoefficientDataMapper businessQCCoefficientDataMapper;
    
    @Resource
    private BusinessQCCoefficientParameterDataMapper businessQCCoefficientParameterDataMapper;
    
    @Resource
    private BusinessQCManagementDataMapper businessQCManagementDataMapper;
    
    @Resource
    private BusinessQCManagementSampleParameterDataMapper businessQCManagementSampleParameterDataMapper;
    
    @Resource
    private BusinessQCManagementProjectDataMapper businessQCManagementProjectDataMapper;
    
    @Resource
    private BusinessQCManagementParameterDataMapper businessQCManagementParameterDataMapper;
    
    @Resource
    private MaterialAssayStandardMethodMapper materialAssayStandardMethodMapper;

	@Override
	public List<AdminUserRespDTO> getAssignUserList() {
		//当前登录用户
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		Long visitDeptId = loginUser.getVisitDeptId();
		//当前部门及子部门id
		List<Long> deptIdList = new ArrayList<>();
		deptIdList.add(visitDeptId);
		//获取子部门
		CommonResult<List<DeptRespDTO>> childDeptListResult = deptApi.getChildDeptList(visitDeptId);
		if (childDeptListResult.isSuccess()) {
			List<DeptRespDTO> childDeptList = childDeptListResult.getData();
			if (CollUtil.isNotEmpty(childDeptList)) {
				List<Long> childDeptIdList = childDeptList.stream().map(m -> m.getId()).collect(Collectors.toList());
				deptIdList.addAll(childDeptIdList);
			}
		}		
		CommonResult<List<AdminUserRespDTO>> userListResult = adminUserApi.getUserListByDeptIds(deptIdList);
		return userListResult.getCheckedData();
	}

	@Override
	public AdminUserRespDTO getUserInfoById(Long userId) {
		CommonResult<AdminUserRespDTO> useResult = adminUserApi.getUser(userId);
		return useResult.getCheckedData();
	}

	@Override
	public List<UnAssignTaskedSubSampleRespVO> getAssaySampleList(BusinessAssayTaskDataReqVO reqVO) {
		List<UnAssignTaskedSubSampleRespVO> list = businessAssayTaskDataMapper.selectUnAssignTaskedSubSample(reqVO);
		return list;
	}
	
	/** 
	 * 通过子样方法检测项目
	 */
	@Override
	public List<AssayMethodProjectRespVO> getAssayMethodProjectList(List<Long> businessSubSampleIdList, Long configAssayMethodId) {
		//当前登录用户
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		Long visitDeptId = loginUser.getVisitDeptId();
		
		List<BusinessSubSampleDO> businessSubSampleList = businessSubSampleMapper.selectByIds(businessSubSampleIdList);
		List<Long> configSubSampleIdList = businessSubSampleList.stream().map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
		//查询子样分析方法及检测项目
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigSubSampleIdsAndAssayDepartmentId(configSubSampleIdList, visitDeptId);
		//查询分析方法检测项目
		List<AssayMethodProjectRespVO> list = businessAssayProjectDataMapper.selectAssayMethodProjectByBusinessSubSampleIdListAndConfigAssayMethodId(businessSubSampleIdList, configAssayMethodId);
		for (AssayMethodProjectRespVO assayMethodProject : list) {
			List<ConfigAssayMethodProjectExtendRespVO> methodList = configAssayMethodProjectList.stream().filter(f -> assayMethodProject.getDictionaryProjectId().equals(f.getDictionaryProjectId())).collect(Collectors.toList());
			assayMethodProject.setMethodList(methodList);
		}
		return list;
	}

	@Override
	public List<AssayMethodProjectRespVO> getMaterialAssayStandardMethodProjectList(List<Long> businessSubSampleIdList, Long baseSampleId, Long configAssayMethodId) {
		//当前登录用户
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		Long visitDeptId = loginUser.getVisitDeptId();
		//查询子样
//		List<BusinessSubSampleDO> businessSubSampleList = businessSubSampleMapper.selectByIds(businessSubSampleIdList);
		//子样配置id
//		List<Long> configSubSampleIdList = businessSubSampleList.stream().map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
		//查询子样分析方法及检测项目
//		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigSubSampleIdsAndAssayDepartmentId(configSubSampleIdList, visitDeptId);
		
		//根据样品大类，查询所有子样方法
		List<ConfigSubSampleMethodExtendRespVO> configSubSampleMethodList = configSubSampleMethodMapper.selectByBaseSampleId(baseSampleId);
		
		//查询分析方法检测项目
		List<AssayMethodProjectRespVO> list = businessAssayProjectDataMapper.selectAssayMethodProjectByBusinessSubSampleIdListAndConfigAssayMethodId(businessSubSampleIdList, configAssayMethodId);
		
		List<Long> dictionaryProjectIdList = list.stream().map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
		
		List<MaterialAssayStandardMethodExtendRespVO> materialAssayStandardMethodList = materialAssayStandardMethodMapper.selectByBaseSampleIdAndDictionaryProjectIdsAndAssayDepartmentId(baseSampleId, dictionaryProjectIdList, visitDeptId);
		
		for (AssayMethodProjectRespVO assayMethodProject : list) {
			List<ConfigAssayMethodProjectExtendRespVO> methodList = materialAssayStandardMethodList.stream().filter(f -> assayMethodProject.getDictionaryProjectId().equals(f.getDictionaryProjectId())).map(m -> {
				ConfigSubSampleMethodExtendRespVO configSubSampleMethodExtend = configSubSampleMethodList.stream().filter(f -> m.getConfigAssayMethodId().equals(f.getConfigAssayMethodId())).findFirst().orElse(null);
				String assayType = configSubSampleMethodExtend.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
				if (m.getIsDualCup().equals(1)) {
					assayType = QmsCommonConstant.ASSAY_TYPE_DOUBLE_CUP;
				}
				ConfigAssayMethodProjectExtendRespVO configAssayMethod = new ConfigAssayMethodProjectExtendRespVO();
				configAssayMethod.setConfigAssayMethodId(m.getConfigAssayMethodId());
				configAssayMethod.setConfigAssayMethodName(m.getConfigAssayMethodName());
				configAssayMethod.setAssayType(assayType);
				configAssayMethod.setConfigAssayMethodNameAndCategory(m.getConfigAssayMethodNameAndCategory());
				return configAssayMethod;
			}).collect(Collectors.toList());
			assayMethodProject.setMethodList(methodList);
		}
		return list;
	}
	

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void changeMethod(ChangeAssayMethodReqVO req) {
		List<Long> businessSubSampleIdList = req.getBusinessSubSampleIdList();
		Long configAssayMethodId = req.getConfigAssayMethodId();		
		List<AssayMethodProjectRespVO> changeConfigAssayMethodProjectList = req.getChangeConfigAssayMethodProjectList();
		
		// 先按 configAssayMethodId 分组
	    Map<Long, List<AssayMethodProjectRespVO>> groupedByConfigId = changeConfigAssayMethodProjectList.stream()
	        .collect(Collectors.groupingBy(AssayMethodProjectRespVO::getConfigAssayMethodId));

	    // 遍历每个分组，检查 assayType 是否唯一
	    for (Map.Entry<Long, List<AssayMethodProjectRespVO>> entry : groupedByConfigId.entrySet()) {
	        List<AssayMethodProjectRespVO> group = entry.getValue();
	        
	        // 获取该组所有非空的 assayType（去重）
	        Set<String> assayTypes = group.stream()
	            .map(AssayMethodProjectRespVO::getAssayType)
	            .filter(Objects::nonNull)
	            .collect(Collectors.toSet());

	        if (assayTypes.size() > 1) {
	            // 取第一个元素的 configAssayMethodNameAndCategory 作为代表（假设同 configId 下名称一致）
	            String methodName = group.get(0).getConfigAssayMethodNameAndCategory();
	            if (methodName == null || methodName.trim().isEmpty()) {
	                methodName = "（未命名）";
	            }
	            
	            throw new ServiceException(1_032_050_000,
	                String.format("检测方法“%s”存在多种分析类型：%s，请确保同一检测方法仅对应一种分析类型。",
	                    methodName,
	                    assayTypes.stream().sorted().collect(Collectors.joining(", "))
	                )
	            );
	        }
	    }
		
		List<Long> changeConfigAssayMethodIdList = changeConfigAssayMethodProjectList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
		
		LocalDateTime currentDateTime = LocalDateTime.now();
		
		//需要更新的报表数据
		List<BusinessAssayReportDataDO> updateBusinessAssayReportDataDOList = new ArrayList<>();
		
		//需要更新的分析任务
		List<BusinessAssayTaskDataDO> updateBusinessAssayTaskDataDOList = new ArrayList<>();
		//需要更新的分析任务参数
		List<BusinessAssayTaskParameterDataDO> updateBusinessAssayTaskParameterDataDOList = new ArrayList<>();		
		//需要更新的分析任务检测项目
		List<BusinessAssayProjectDataDO> updateBusinessAssayProjectDataDOList = new ArrayList<>();
		//需要更新的分析任务检测项目参数
		List<BusinessAssayParameterDataDO> updateBusinessAssayParameterDataDOList = new ArrayList<>();
		
		//需要更新的子样判定
		List<BusinessSubSampleAssessmentDO> updateBusinessSubSampleAssessmentDOList = new ArrayList<>();
		
		//需要更新的分样判定
		List<BusinessSubParentSampleAssessmentDO> updateBusinessSubParentSampleAssessmentDOList = new ArrayList<>();
		
		//需要新建的分析任务
		List<BusinessAssayTaskDataDO> saveBusinessAssayTaskDataDOList = new ArrayList<>();
		//需要新建的分析任务参数
		List<BusinessAssayTaskParameterDataDO> saveBusinessAssayTaskParameterDataDOList = new ArrayList<>();	
		//需要新建的分析任务检测项目
		List<BusinessAssayProjectDataDO> saveBusinessAssayProjectDataDOList = new ArrayList<>();
		//需要新建的分析任务检测项目参数
		List<BusinessAssayParameterDataDO> saveBusinessAssayParameterDataDOList = new ArrayList<>();
		
		//需要新建的子样判定
		List<BusinessSubSampleAssessmentDO> saveBusinessSubSampleAssessmentDOList = new ArrayList<>();
		
		//需要新建的分样判定
		List<BusinessSubParentSampleAssessmentDO> saveBusinessSubParentSampleAssessmentDOList = new ArrayList<>();

		//需要删除的分析任务
		List<Long> removeAssayTaskDataIdList = new ArrayList<>(); 
		//需要删除的分析任务参数
		List<Long> removeAssayTaskParameterDataIdList = new ArrayList<>(); 
		//需要删除的分析任务检测项目
		List<Long> removeAssayProjectIdList = new ArrayList<>(); 
		//需要删除的分析任务检测项目参数
		List<Long> removeAssayParameterIdList = new ArrayList<>();

		//需要删除的子样判定
		List<Long> removeSubSampleAssessmentIdList = new ArrayList<>();

		//需要删除的分样判定
		List<Long> removeSubParentSampleAssessmentIdList = new ArrayList<>();
		
		//查询子样
		List<BusinessSubSampleDO> businessSubSampleDOList = businessSubSampleMapper.selectByIds(businessSubSampleIdList);
		List<Long> businessBaseSampleIdList = businessSubSampleDOList.stream().map(m -> m.getBusinessBaseSampleId()).distinct().collect(Collectors.toList());
		List<Long> businessSubParentSampleIdList = businessSubSampleDOList.stream().map(m -> m.getBusinessSubParentSampleId()).distinct().collect(Collectors.toList());
		List<Long> configSubSampleIdList = businessSubSampleDOList.stream().map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
	    //查询检测任务 (通过分样id，可以查询双杯样的)
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);

		boolean disabled = businessAssayTaskDataDOList.stream().anyMatch(m -> m.getIsAssignTasked().equals(QmsCommonConstant.YES));
		if (disabled) {
			throw new ServiceException(1_032_001_000, "当前样品存在已分配任务，不允许修改分析方法");
		}
		
		List<Long> businessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//查询检测任务的参数
		List<BusinessAssayTaskParameterDataExtendRespVO> businessAssayTaskParameterDataList = businessAssayTaskParameterDataMapper.selectExtendByBusinessAssayTaskDataIds(businessAssayTaskDataIdList);
		
		//查询检测任务的检测项目
		List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectByBusinessAssayTaskDataIds(businessAssayTaskDataIdList);
		List<Long> businessAssayProjectDataIdList = businessAssayProjectDataList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//查询检测任务的检测项目的参数
		List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList = businessAssayParameterDataMapper.selectByBusinessAssayProjectDataIds(businessAssayProjectDataIdList);
		
		//查询子样判定 (通过分样id，可以查询双杯样的)
		List<BusinessSubSampleAssessmentDO> businessSubSampleAssessmentDOList = businessSubSampleAssessmentMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);
		
		//查询分样判定
		List<BusinessSubParentSampleAssessmentExtendRespVO> businessSubParentSampleAssessmentList = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);
		
		//查询要变更的分析方法配置
		List<ConfigAssayMethodDO> configAssayMethodDOList = configAssayMethodMapper.selectByIds(changeConfigAssayMethodIdList);
		//查询要变更的分析方法参数配置
		List<ConfigAssayMethodParameterDO> configAssayMethodParameterDOList = configAssayMethodParameterMapper.selectByConfigAssayMethodIds(changeConfigAssayMethodIdList);
		//分析方法检测项目配置
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigAssayMethodIds(changeConfigAssayMethodIdList);
		//分析方法检测项目参数配置
		List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList = configAssayMethodProjectParameterMapper.selectByConfigAssayMethodIds(changeConfigAssayMethodIdList);
		//查询要变更的子样及分析方法配置
		List<ConfigSubSampleMethodExtendRespVO> configSubSampleMethodList = configSubSampleMethodMapper.selectByConfigSubSampleIdsAndConfigAssayMethodIds(configSubSampleIdList, changeConfigAssayMethodIdList);
		
		//循环分样判定
		for (BusinessSubParentSampleAssessmentExtendRespVO businessSubParentSampleAssessment : businessSubParentSampleAssessmentList) {
			
			//循环变更的分析方法
			for (Long changeConfigAssayMethodId : changeConfigAssayMethodIdList) {
				//如果当前分析方法不等于要变更的方法
				if (!businessSubParentSampleAssessment.getConfigAssayMethodId().equals(changeConfigAssayMethodId)) {
					//查询是否已有要变更的方法的分样判定
					BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodIdAndRetestCount(businessSubParentSampleAssessment.getBusinessSubParentSampleId(), changeConfigAssayMethodId, businessSubParentSampleAssessment.getRetestCount());
					if (businessSubParentSampleAssessmentDO == null) {//不存在
						//根据变更方法过滤出检测项目
						List<Long> projectIdList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
						//分析类型
						String changeAssayType = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getAssayType()).distinct().findFirst().orElse(null);
						
						
						//查询分析方法
						ConfigAssayMethodDO configAssayMethodDO = configAssayMethodDOList.stream().filter(f -> f.getId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);
						//查询子样分析方法
						ConfigSubSampleMethodExtendRespVO configSubSampleMethod = configSubSampleMethodList.stream().filter(f -> f.getConfigSubSampleParentId().equals(businessSubParentSampleAssessment.getConfigSubSampleParentId()) && f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);
						switch (changeAssayType) {//根据前端传入的分析类型，修改子样分析方法中的分析任务数
						case QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL: {
							configSubSampleMethod.setTaskCount(2);//平行
							break;
						}
						default:
							configSubSampleMethod.setTaskCount(1);//单杯
							break;
						}
						
						//扩展配置
				    	String configInfomation = configSubSampleMethod.getConfigInfomation();
						List<ConfigSubSampleMethodConfItem> getParamList = null;
				    	if (StringUtils.isNotBlank(configInfomation)) {
				    		//解析配置
							ConfigSubSampleMethodConfInfo configSubSampleMethodConfInfo = JSON.parseObject(configInfomation, ConfigSubSampleMethodConfInfo.class);
							getParamList = configSubSampleMethodConfInfo.getGetParam();
						}
						
						//根据任务数判断是平行还是
						String assayType = configSubSampleMethod.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
						
						businessSubParentSampleAssessmentDO = new BusinessSubParentSampleAssessmentDO();
						businessSubParentSampleAssessmentDO.setId(IdWorker.getId());
						businessSubParentSampleAssessmentDO.setBusinessSubParentSampleId(businessSubParentSampleAssessment.getBusinessSubParentSampleId());
						businessSubParentSampleAssessmentDO.setConfigAssayMethodId(changeConfigAssayMethodId);
						businessSubParentSampleAssessmentDO.setAssayType(assayType);
						businessSubParentSampleAssessmentDO.setTaskType("常规");
						saveBusinessSubParentSampleAssessmentDOList.add(businessSubParentSampleAssessmentDO);
						
						BusinessSubSampleDO businessSubSampleDO = null;
						if (!QmsCommonConstant.ASSAY_TYPE_DOUBLE_CUP.equals(businessSubParentSampleAssessment.getAssayType())) {//如果不为双杯样
							businessSubSampleDO = businessSubSampleDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).findFirst().orElse(null);
						}

						//子样判定数据
						BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();
						businessSubSampleAssessmentDO.setId(IdWorker.getId());	
						businessSubSampleAssessmentDO.setBusinessBaseSampleId(businessSubParentSampleAssessment.getBusinessBaseSampleId());
						businessSubSampleAssessmentDO.setBusinessSubParentSampleId(businessSubParentSampleAssessment.getBusinessSubParentSampleId());
						businessSubSampleAssessmentDO.setBusinessSubParentSampleAssessmentId(businessSubParentSampleAssessmentDO.getId());
						if (businessSubSampleDO != null) {
							businessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleDO.getId());
						}
						businessSubSampleAssessmentDO.setConfigAssayMethodId(changeConfigAssayMethodId);
						businessSubSampleAssessmentDO.setAssayType(assayType);
						businessSubSampleAssessmentDO.setTaskType("常规");
						
						saveBusinessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
						
						//查询旧的检测项目
						List<Long> oldBusinessAssayProjectDataIdList = businessAssayProjectDataList.stream().filter(f -> projectIdList.contains(f.getDictionaryProjectId()) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
						//添加到删除列表
						removeAssayProjectIdList.addAll(oldBusinessAssayProjectDataIdList);
						List<Long> oldBusinessAssayParameterDataIdList = businessAssayParameterDataDOList.stream().filter(f -> oldBusinessAssayProjectDataIdList.contains(f.getBusinessAssayProjectDataId())).map(m -> m.getId()).collect(Collectors.toList());
						removeAssayParameterIdList.addAll(oldBusinessAssayParameterDataIdList);

						List<String> projectShowNameList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).map(m -> m.getDictionaryProjectShowName()).distinct().collect(Collectors.toList());
						if (projectShowNameList.size() <= 0) {
							//查询旧的检测任务
							List<Long> oldBusinessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeAssayTaskDataIdList.addAll(oldBusinessAssayTaskDataIdList);
							if (oldBusinessAssayTaskDataIdList.size() > 0) {
								List<Long> oldBusinessAssayTaskParameterDataIdList = businessAssayTaskParameterDataList.stream().filter(f -> oldBusinessAssayTaskDataIdList.contains(f.getBusinessAssayTaskDataId())).map(m -> m.getId()).collect(Collectors.toList());
								//添加分析任务属性删除列表
								removeAssayTaskParameterDataIdList.addAll(oldBusinessAssayTaskParameterDataIdList);
							}
							//查询旧的子样判定
							List<Long> oldBusinessSubSampleAssesmentIdList = businessSubSampleAssessmentDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeSubSampleAssessmentIdList.addAll(oldBusinessSubSampleAssesmentIdList);
							
							//添加到删除列表
							removeSubParentSampleAssessmentIdList.add(businessSubParentSampleAssessment.getId());
						} else {
							//查询旧的检测任务
							List<BusinessAssayTaskDataDO> oldBusinessAssayTaskDataList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).collect(Collectors.toList());
							for (BusinessAssayTaskDataDO oldBusinessAssayTaskData : oldBusinessAssayTaskDataList) {
								boolean isAdd = updateBusinessAssayTaskDataDOList.stream().anyMatch(m -> m.getId().equals(oldBusinessAssayTaskData.getId()));
								if (!isAdd) {//如果未添加过更新，则修改检测项目并添加更新
									oldBusinessAssayTaskData.setAssayProject(CollUtil.join(projectShowNameList, ","));
									updateBusinessAssayTaskDataDOList.add(oldBusinessAssayTaskData);
								}
							}
						}
						
						BusinessAssayTaskDataDO oldBusinessAssayTaskDataDO = businessAssayTaskDataDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).findFirst().orElse(null);
						
						//根据检测方法循环
						BusinessAssayTaskDataDO businessAssayTaskDataDO = null;
						//根据任务数循环
						for (int i = 0; i < configSubSampleMethod.getTaskCount(); i++) {
							//子样检测任务
							businessAssayTaskDataDO = new BusinessAssayTaskDataDO();
							businessAssayTaskDataDO.setId(IdWorker.getId());
							businessAssayTaskDataDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
							businessAssayTaskDataDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
							businessAssayTaskDataDO.setBusinessSubSampleId(businessSubSampleDO.getId());
							businessAssayTaskDataDO.setBusinessSubSampleAssessmentId(businessSubSampleAssessmentDO.getId());//子样判定id
							businessAssayTaskDataDO.setConfigAssayMethodId(changeConfigAssayMethodId);
							businessAssayTaskDataDO.setAssayType(assayType);
							businessAssayTaskDataDO.setTaskType("常规");
							businessAssayTaskDataDO.setConfigSampleFlowId(oldBusinessAssayTaskDataDO.getConfigSampleFlowId());
							businessAssayTaskDataDO.setSampleFlowNodeKey(oldBusinessAssayTaskDataDO.getSampleFlowNodeKey());
							businessAssayTaskDataDO.setSampleFlowNodeTime(currentDateTime);
							businessAssayTaskDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
							businessAssayTaskDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
							
							//子样检测任务参数
							List<ConfigAssayMethodParameterDO> currConfigAssayMethodParameterDOList = configAssayMethodParameterDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).collect(Collectors.toList());
							BusinessAssayTaskParameterDataDO businessAssayTaskParameterDataDO = null;
							for (ConfigAssayMethodParameterDO configAssayMethodParameterDO : currConfigAssayMethodParameterDOList) {
								businessAssayTaskParameterDataDO = new BusinessAssayTaskParameterDataDO();
								businessAssayTaskParameterDataDO.setId(IdWorker.getId());
								businessAssayTaskParameterDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
								businessAssayTaskParameterDataDO.setConfigAssayMethodParameterId(configAssayMethodParameterDO.getId());
								businessAssayTaskParameterDataDO.setDataType(configAssayMethodParameterDO.getDataType());
								businessAssayTaskParameterDataDO.setDecimalPosition(configAssayMethodParameterDO.getDecimalPosition());	
								
								if (StringUtils.isNotBlank(configAssayMethodParameterDO.getDefaultValue())) {
									businessAssayTaskParameterDataDO.setValue(configAssayMethodParameterDO.getDefaultValue());
								}
								if (getParamList != null) {
									BusinessAssayTaskParameterDataExtendRespVO oldBusinessAssayTaskParameterData = businessAssayTaskParameterDataList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessAssayTaskDataId().equals(oldBusinessAssayTaskDataDO.getId()) && f.getKey().equals(configAssayMethodParameterDO.getKey())).findFirst().orElse(null);
									ConfigSubSampleMethodConfItem targetAttribute = getParamList.stream().filter(f -> StringUtils.isNotBlank(f.getTarget().getAttribute()) && f.getTarget().getAttribute().equals(oldBusinessAssayTaskParameterData.getKey())).findFirst().orElse(null);
									if (targetAttribute != null) {
										businessAssayTaskParameterDataDO.setValue(oldBusinessAssayTaskParameterData.getValue());
									}
								}

								saveBusinessAssayTaskParameterDataDOList.add(businessAssayTaskParameterDataDO);
							}
							
							
							List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).collect(Collectors.toList());
							
							StringBuilder assayProjectBuilder = new StringBuilder();
							
							for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
								
								//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
								if (!projectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
									continue;
								}
								
								assayProjectBuilder.append(configAssayMethodProjectDO.getShowName()).append(",");
								

								//检测项目
								BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
								businessAssayProjectDataDO.setId(IdWorker.getId());
								businessAssayProjectDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
								businessAssayProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
								businessAssayProjectDataDO.setDictionaryProjectId(configAssayMethodProjectDO.getDictionaryProjectId());
								businessAssayProjectDataDO.setDataType(configAssayMethodProjectDO.getDataType());
								businessAssayProjectDataDO.setDecimalPosition(configAssayMethodProjectDO.getDecimalPosition());
								businessAssayProjectDataDO.setEffectiveDigit(configAssayMethodProjectDO.getEffectiveDigit());
								businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);
								businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
								businessAssayProjectDataDO.setIsEnabled(QmsCommonConstant.YES);
								businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
								
								saveBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
								
								List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDOList = configAssayMethodProjectParameterList.stream().filter(f -> f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectDO.getId())).collect(Collectors.toList());
								for (ConfigAssayMethodProjectParameterDO configAssayMethodProjectParameterDO : configAssayMethodProjectParameterDOList) {
									BusinessAssayParameterDataDO businessAssayParameterDataDO = new BusinessAssayParameterDataDO();
									businessAssayParameterDataDO.setId(IdWorker.getId());
									businessAssayParameterDataDO.setConfigAssayMethodProjectParameterId(configAssayMethodProjectParameterDO.getId());
									businessAssayParameterDataDO.setBusinessAssayProjectDataId(businessAssayProjectDataDO.getId());
									businessAssayParameterDataDO.setDictionaryParameterId(configAssayMethodProjectParameterDO.getDictionaryParameterId());
									businessAssayParameterDataDO.setDataType(configAssayMethodProjectParameterDO.getDataType());
									businessAssayParameterDataDO.setDecimalPosition(configAssayMethodProjectParameterDO.getDecimalPosition());
									
									if (StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getDefaultValue())) {
										businessAssayParameterDataDO.setValue(configAssayMethodProjectParameterDO.getDefaultValue());
									}
									
									saveBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
								}
							}
							if (assayProjectBuilder.length() > 1) {
								assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
							}
							businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
							saveBusinessAssayTaskDataDOList.add(businessAssayTaskDataDO);
						}
						
					} else {//已存在
						//分析类型
						String changeAssayType = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getAssayType()).distinct().findFirst().orElse(null);
						if (!businessSubParentSampleAssessmentDO.getAssayType().equals(changeAssayType)) {
							String configAssayMethodNameAndCategory = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getConfigAssayMethodNameAndCategory()).distinct().findFirst().orElse(null);
							throw new ServiceException(1_032_001_000, "检测方法“"+configAssayMethodNameAndCategory+"”，存在与当前分析类型不同的分析任务！");
						}
						
						//根据变更方法过滤出检测项目
						List<Long> projectIdList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
						
						//查询分析方法
						//ConfigAssayMethodDO configAssayMethodDO = configAssayMethodDOList.stream().filter(f -> f.getId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);
						//查询子样分析方法
						//ConfigSubSampleMethodExtendRespVO configSubSampleMethod = configSubSampleMethodList.stream().filter(f -> f.getConfigSubSampleParentId().equals(businessSubParentSampleAssessment.getConfigSubSampleParentId()) && f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);

						//根据任务数判断是平行还是
						//String assayType = configSubSampleMethod.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
						
						//查询子样判定
						//BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(businessSubParentSampleAssessmentDO.getBusinessSubParentSampleId(), changeConfigAssayMethodId);

						//查询旧的检测项目
						List<Long> oldBusinessAssayProjectDataIdList = businessAssayProjectDataList.stream().filter(f -> projectIdList.contains(f.getDictionaryProjectId()) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
						//添加到删除列表
						removeAssayProjectIdList.addAll(oldBusinessAssayProjectDataIdList);
						List<Long> oldBusinessAssayParameterDataIdList = businessAssayParameterDataDOList.stream().filter(f -> oldBusinessAssayProjectDataIdList.contains(f.getBusinessAssayProjectDataId())).map(m -> m.getId()).collect(Collectors.toList());
						removeAssayParameterIdList.addAll(oldBusinessAssayParameterDataIdList);

						List<String> projectShowNameList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).map(m -> m.getDictionaryProjectShowName()).distinct().collect(Collectors.toList());
						if (projectShowNameList.size() <= 0) {//当前方法无检测项目，则需要删除
							//查询旧的检测任务
							List<Long> oldBusinessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeAssayTaskDataIdList.addAll(oldBusinessAssayTaskDataIdList);
							//查询旧的子样判定
							List<Long> oldBusinessSubSampleAssesmentIdList = businessSubSampleAssessmentDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeSubSampleAssessmentIdList.addAll(oldBusinessSubSampleAssesmentIdList);
							
							//添加到删除列表
							removeSubParentSampleAssessmentIdList.add(businessSubParentSampleAssessment.getId());
						} else {
							//查询旧的检测任务
							List<BusinessAssayTaskDataDO> oldBusinessAssayTaskDataList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).collect(Collectors.toList());
							for (BusinessAssayTaskDataDO oldBusinessAssayTaskData : oldBusinessAssayTaskDataList) {
								boolean isAdd = updateBusinessAssayTaskDataDOList.stream().anyMatch(m -> m.getId().equals(oldBusinessAssayTaskData.getId()));
								if (!isAdd) {//如果未添加过更新，则修改检测项目并添加更新
									oldBusinessAssayTaskData.setAssayProject(CollUtil.join(projectShowNameList, ","));
									updateBusinessAssayTaskDataDOList.add(oldBusinessAssayTaskData);
								}
							}
						}

						//查询当前存在的分析任务
						List<BusinessAssayTaskDataDO> currBusinessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(businessSubParentSampleAssessmentDO.getBusinessSubParentSampleId(), changeConfigAssayMethodId);
						
						//当前存在的分析任务循环
						for (BusinessAssayTaskDataDO businessAssayTaskDataDO : currBusinessAssayTaskDataDOList) {
							
							List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).collect(Collectors.toList());
							
							StringBuilder assayProjectBuilder = new StringBuilder(businessAssayTaskDataDO.getAssayProject());
							assayProjectBuilder.append(",");
							
							for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
								
								//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
								if (!projectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
									continue;
								}
								
								assayProjectBuilder.append(configAssayMethodProjectDO.getShowName()).append(",");
								

								//检测项目
								BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
								businessAssayProjectDataDO.setId(IdWorker.getId());
								businessAssayProjectDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
								businessAssayProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
								businessAssayProjectDataDO.setDictionaryProjectId(configAssayMethodProjectDO.getDictionaryProjectId());
								businessAssayProjectDataDO.setDataType(configAssayMethodProjectDO.getDataType());
								businessAssayProjectDataDO.setDecimalPosition(configAssayMethodProjectDO.getDecimalPosition());
								businessAssayProjectDataDO.setEffectiveDigit(configAssayMethodProjectDO.getEffectiveDigit());
								businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);
								businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
								businessAssayProjectDataDO.setIsEnabled(QmsCommonConstant.YES);
								businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
								
								saveBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
								
								List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDOList = configAssayMethodProjectParameterList.stream().filter(f -> f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectDO.getId())).collect(Collectors.toList());
								for (ConfigAssayMethodProjectParameterDO configAssayMethodProjectParameterDO : configAssayMethodProjectParameterDOList) {
									BusinessAssayParameterDataDO businessAssayParameterDataDO = new BusinessAssayParameterDataDO();
									businessAssayParameterDataDO.setId(IdWorker.getId());
									businessAssayParameterDataDO.setConfigAssayMethodProjectParameterId(configAssayMethodProjectParameterDO.getId());
									businessAssayParameterDataDO.setBusinessAssayProjectDataId(businessAssayProjectDataDO.getId());
									businessAssayParameterDataDO.setDictionaryParameterId(configAssayMethodProjectParameterDO.getDictionaryParameterId());
									businessAssayParameterDataDO.setDataType(configAssayMethodProjectParameterDO.getDataType());
									businessAssayParameterDataDO.setDecimalPosition(configAssayMethodProjectParameterDO.getDecimalPosition());
									
									saveBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
								}
							}
							if (assayProjectBuilder.length() > 1) {
								assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
							}
							businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
							updateBusinessAssayTaskDataDOList.add(businessAssayTaskDataDO);
						}
						
						
					}
					
				}
				
			}
			
		}
		
		//查询报表数据
		List<BusinessAssayReportDataDO> businessAssayReportDataDOList = businessAssayReportDataMapper.selectBytBusinessBaseSampleIds(businessBaseSampleIdList);
		for (BusinessAssayReportDataDO businessAssayReportDataDO : businessAssayReportDataDOList) {
			String dataSource = businessAssayReportDataDO.getDataSource();
			if (dataSource.contains(req.getConfigAssayMethodId().toString())) {//判定是否存在
				List<Long> dataSourceList = new ArrayList<>();
				String[] dataSourceSplit = dataSource.split(",");
				for (int i = 0; i < dataSourceSplit.length; i++) {
					dataSourceList.add(Long.parseLong(dataSourceSplit[i]));
				}
				
				//移除当前的
				dataSourceList.remove(req.getConfigAssayMethodId());
				//添加新的
				dataSourceList.addAll(changeConfigAssayMethodIdList);
				
				//重新赋值
				businessAssayReportDataDO.setDataSource(CollUtil.join(dataSourceList, ","));
				
				//添加到更新列表
				updateBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
			}
		}
		
		

		//======================== 删除 =====================================================================

		if (removeAssayTaskDataIdList.size() > 0) {
			businessAssayTaskDataMapper.deleteByIds(removeAssayTaskDataIdList);
		}
		if (removeAssayTaskParameterDataIdList.size() > 0) {
			businessAssayTaskParameterDataMapper.deleteByIds(removeAssayTaskParameterDataIdList);
		}
		
		if (removeAssayProjectIdList.size() > 0) {
			businessAssayProjectDataMapper.deleteByIds(removeAssayProjectIdList);
		}
		if (removeAssayParameterIdList.size() > 0) {
			businessAssayParameterDataMapper.deleteByIds(removeAssayParameterIdList);
		}
		
		if (removeSubSampleAssessmentIdList.size() > 0) {
			businessSubSampleAssessmentMapper.deleteByIds(removeSubSampleAssessmentIdList);
		}
		
		if (removeSubParentSampleAssessmentIdList.size() > 0) {
			businessSubParentSampleAssessmentMapper.deleteByIds(removeSubParentSampleAssessmentIdList);
		}
		
		//======================== 修改 =====================================================================

		if (updateBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.updateBatch(updateBusinessAssayReportDataDOList);
		}
		if (updateBusinessAssayTaskParameterDataDOList.size() > 0) {
			businessAssayTaskParameterDataMapper.updateBatch(updateBusinessAssayTaskParameterDataDOList);
		}
		
		if (updateBusinessAssayTaskDataDOList.size() > 0) {
			businessAssayTaskDataMapper.updateBatch(updateBusinessAssayTaskDataDOList);
		}
		if (updateBusinessAssayProjectDataDOList.size() > 0) {
			businessAssayProjectDataMapper.updateBatch(updateBusinessAssayProjectDataDOList);
		}
		if (updateBusinessAssayParameterDataDOList.size() > 0) {
			businessAssayParameterDataMapper.updateBatch(updateBusinessAssayParameterDataDOList);
		}

		if (updateBusinessSubSampleAssessmentDOList.size() > 0) {
			businessSubSampleAssessmentMapper.updateBatch(updateBusinessSubSampleAssessmentDOList);
		}

		if (updateBusinessSubParentSampleAssessmentDOList.size() > 0) {
			businessSubParentSampleAssessmentMapper.updateBatch(updateBusinessSubParentSampleAssessmentDOList);
		}
		
		//======================== 新建 =====================================================================
		
		if (saveBusinessAssayTaskDataDOList.size() > 0) {
			businessAssayTaskDataMapper.insertBatch(saveBusinessAssayTaskDataDOList);
		}
		if (saveBusinessAssayTaskParameterDataDOList.size() > 0) {
			businessAssayTaskParameterDataMapper.insertBatch(saveBusinessAssayTaskParameterDataDOList);
		}
		
		if (saveBusinessAssayProjectDataDOList.size() > 0) {
			businessAssayProjectDataMapper.insertBatch(saveBusinessAssayProjectDataDOList);
		}
		if (saveBusinessAssayParameterDataDOList.size() > 0) {
			businessAssayParameterDataMapper.insertBatch(saveBusinessAssayParameterDataDOList);
		}

		if (saveBusinessSubSampleAssessmentDOList.size() > 0) {
			businessSubSampleAssessmentMapper.insertBatch(saveBusinessSubSampleAssessmentDOList);
		}

		if (saveBusinessSubParentSampleAssessmentDOList.size() > 0) {
			businessSubParentSampleAssessmentMapper.insertBatch(saveBusinessSubParentSampleAssessmentDOList);
		}
		
		
		
	}

	/**
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void changeMethod(ChangeAssayMethodReqVO req) {
		List<Long> businessSubSampleIdList = req.getBusinessSubSampleIdList();
		Long configAssayMethodId = req.getConfigAssayMethodId();		
		List<AssayMethodProjectRespVO> changeConfigAssayMethodProjectList = req.getChangeConfigAssayMethodProjectList();
		List<Long> changeConfigAssayMethodIdList = changeConfigAssayMethodProjectList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
		
		LocalDateTime currentDateTime = LocalDateTime.now();
		
		//需要更新的报表数据
		List<BusinessAssayReportDataDO> updateBusinessAssayReportDataDOList = new ArrayList<>();
		
		//需要更新的分析任务
		List<BusinessAssayTaskDataDO> updateBusinessAssayTaskDataDOList = new ArrayList<>();
		//需要更新的分析任务检测项目
		List<BusinessAssayProjectDataDO> updateBusinessAssayProjectDataDOList = new ArrayList<>();
		//需要更新的分析任务检测项目参数
		List<BusinessAssayParameterDataDO> updateBusinessAssayParameterDataDOList = new ArrayList<>();
		
		//需要更新的子样判定
		List<BusinessSubSampleAssessmentDO> updateBusinessSubSampleAssessmentDOList = new ArrayList<>();
		
		//需要更新的分样判定
		List<BusinessSubParentSampleAssessmentDO> updateBusinessSubParentSampleAssessmentDOList = new ArrayList<>();
		
		//需要新建的分析任务
		List<BusinessAssayTaskDataDO> saveBusinessAssayTaskDataDOList = new ArrayList<>();
		//需要新建的分析任务检测项目
		List<BusinessAssayProjectDataDO> saveBusinessAssayProjectDataDOList = new ArrayList<>();
		//需要新建的分析任务检测项目参数
		List<BusinessAssayParameterDataDO> saveBusinessAssayParameterDataDOList = new ArrayList<>();
		
		//需要新建的子样判定
		List<BusinessSubSampleAssessmentDO> saveBusinessSubSampleAssessmentDOList = new ArrayList<>();
		
		//需要新建的分样判定
		List<BusinessSubParentSampleAssessmentDO> saveBusinessSubParentSampleAssessmentDOList = new ArrayList<>();

		//需要删除的分析任务
		List<Long> removeAssayTaskDataIdList = new ArrayList<>(); 
		//需要删除的分析任务检测项目
		List<Long> removeAssayProjectIdList = new ArrayList<>(); 
		//需要删除的分析任务检测项目参数
		List<Long> removeAssayParameterIdList = new ArrayList<>();

		//需要删除的子样判定
		List<Long> removeSubSampleAssessmentIdList = new ArrayList<>();

		//需要删除的分样判定
		List<Long> removeSubParentSampleAssessmentIdList = new ArrayList<>();
		
		//查询子样
		List<BusinessSubSampleDO> businessSubSampleDOList = businessSubSampleMapper.selectByIds(businessSubSampleIdList);
		List<Long> businessBaseSampleIdList = businessSubSampleDOList.stream().map(m -> m.getBusinessBaseSampleId()).distinct().collect(Collectors.toList());
		List<Long> businessSubParentSampleIdList = businessSubSampleDOList.stream().map(m -> m.getBusinessSubParentSampleId()).distinct().collect(Collectors.toList());
		List<Long> configSubSampleIdList = businessSubSampleDOList.stream().map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
	    //查询检测任务 (通过分样id，可以查询双杯样的)
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);

		boolean disabled = businessAssayTaskDataDOList.stream().anyMatch(m -> m.getIsAssignTasked().equals(QmsCommonConstant.YES));
		if (disabled) {
			throw new ServiceException(1_032_001_000, "当前样品存在已分配任务，不允许修改分析方法");
		}
		
		List<Long> businessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//查询检测任务的检测项目
		List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectByBusinessAssayTaskDataIds(businessAssayTaskDataIdList);
		List<Long> businessAssayProjectDataIdList = businessAssayProjectDataList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//查询检测任务的检测项目的参数
		List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList = businessAssayParameterDataMapper.selectByBusinessAssayProjectDataIds(businessAssayProjectDataIdList);
		
		//查询子样判定 (通过分样id，可以查询双杯样的)
		List<BusinessSubSampleAssessmentDO> businessSubSampleAssessmentDOList = businessSubSampleAssessmentMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);
		
		//查询分样判定
		List<BusinessSubParentSampleAssessmentExtendRespVO> businessSubParentSampleAssessmentList = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);
		
		//查询要变更的分析方法配置
		List<ConfigAssayMethodDO> configAssayMethodDOList = configAssayMethodMapper.selectByIds(changeConfigAssayMethodIdList);
		//分析方法检测项目配置
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigAssayMethodIds(changeConfigAssayMethodIdList);
		//分析方法检测项目参数配置
		List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList = configAssayMethodProjectParameterMapper.selectByConfigAssayMethodIds(changeConfigAssayMethodIdList);
		//查询要变更的子样及分析方法配置
		List<ConfigSubSampleMethodExtendRespVO> configSubSampleMethodList = configSubSampleMethodMapper.selectByConfigSubSampleIdsAndConfigAssayMethodIds(configSubSampleIdList, changeConfigAssayMethodIdList);
		
		//循环分样判定
		for (BusinessSubParentSampleAssessmentExtendRespVO businessSubParentSampleAssessment : businessSubParentSampleAssessmentList) {
			
			//循环变更的分析方法
			for (Long changeConfigAssayMethodId : changeConfigAssayMethodIdList) {
				//如果当前分析方法不等于要变更的方法
				if (!businessSubParentSampleAssessment.getConfigAssayMethodId().equals(changeConfigAssayMethodId)) {
					//查询是否已有要变更的方法的分样判定
					BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodIdAndRetestCount(businessSubParentSampleAssessment.getBusinessSubParentSampleId(), changeConfigAssayMethodId, businessSubParentSampleAssessment.getRetestCount());
					if (businessSubParentSampleAssessmentDO == null) {//不存在
						//根据变更方法过滤出检测项目
						List<Long> projectIdList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
						
						//查询分析方法
						ConfigAssayMethodDO configAssayMethodDO = configAssayMethodDOList.stream().filter(f -> f.getId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);
						//查询子样分析方法
						ConfigSubSampleMethodExtendRespVO configSubSampleMethod = configSubSampleMethodList.stream().filter(f -> f.getConfigSubSampleParentId().equals(businessSubParentSampleAssessment.getConfigSubSampleParentId()) && f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);

						//根据任务数判断是平行还是
						String assayType = configSubSampleMethod.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
						businessSubParentSampleAssessmentDO = new BusinessSubParentSampleAssessmentDO();
						businessSubParentSampleAssessmentDO.setId(IdWorker.getId());
						businessSubParentSampleAssessmentDO.setBusinessSubParentSampleId(businessSubParentSampleAssessment.getBusinessSubParentSampleId());
						businessSubParentSampleAssessmentDO.setConfigAssayMethodId(changeConfigAssayMethodId);
						businessSubParentSampleAssessmentDO.setAssayType(assayType);
						businessSubParentSampleAssessmentDO.setTaskType("常规");
						saveBusinessSubParentSampleAssessmentDOList.add(businessSubParentSampleAssessmentDO);
						
						BusinessSubSampleDO businessSubSampleDO = null;
						if (!QmsCommonConstant.ASSAY_TYPE_DOUBLE_CUP.equals(businessSubParentSampleAssessment.getAssayType())) {//如果不为双杯样
							businessSubSampleDO = businessSubSampleDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).findFirst().orElse(null);
						}

						//子样判定数据
						BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();
						businessSubSampleAssessmentDO.setId(IdWorker.getId());	
						businessSubSampleAssessmentDO.setBusinessBaseSampleId(businessSubParentSampleAssessment.getBusinessBaseSampleId());
						businessSubSampleAssessmentDO.setBusinessSubParentSampleId(businessSubParentSampleAssessment.getBusinessSubParentSampleId());
						businessSubSampleAssessmentDO.setBusinessSubParentSampleAssessmentId(businessSubParentSampleAssessmentDO.getId());
						if (businessSubSampleDO != null) {
							businessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleDO.getId());
						}
						businessSubSampleAssessmentDO.setConfigAssayMethodId(changeConfigAssayMethodId);
						businessSubSampleAssessmentDO.setAssayType(assayType);
						businessSubSampleAssessmentDO.setTaskType("常规");
						
						saveBusinessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
						
						//查询旧的检测项目
						List<Long> oldBusinessAssayProjectDataIdList = businessAssayProjectDataList.stream().filter(f -> projectIdList.contains(f.getDictionaryProjectId()) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
						//添加到删除列表
						removeAssayProjectIdList.addAll(oldBusinessAssayProjectDataIdList);
						List<Long> oldBusinessAssayParameterDataIdList = businessAssayParameterDataDOList.stream().filter(f -> oldBusinessAssayProjectDataIdList.contains(f.getBusinessAssayProjectDataId())).map(m -> m.getId()).collect(Collectors.toList());
						removeAssayParameterIdList.addAll(oldBusinessAssayParameterDataIdList);
						if (projectIdList.size() == changeConfigAssayMethodProjectList.size()) {
							//查询旧的检测任务
							List<Long> oldBusinessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeAssayTaskDataIdList.addAll(oldBusinessAssayTaskDataIdList);
							//查询旧的子样判定
							List<Long> oldBusinessSubSampleAssesmentIdList = businessSubSampleAssessmentDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeSubSampleAssessmentIdList.addAll(oldBusinessSubSampleAssesmentIdList);
							
							//添加到删除列表
							removeSubParentSampleAssessmentIdList.add(businessSubParentSampleAssessment.getId());
						} else {
							List<String> projectShowNameList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).map(m -> m.getDictionaryProjectShowName()).distinct().collect(Collectors.toList());
							//查询旧的检测任务
							List<BusinessAssayTaskDataDO> oldBusinessAssayTaskDataList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).collect(Collectors.toList());
							for (BusinessAssayTaskDataDO oldBusinessAssayTaskData : oldBusinessAssayTaskDataList) {
								boolean isAdd = updateBusinessAssayTaskDataDOList.stream().anyMatch(m -> m.getId().equals(oldBusinessAssayTaskData.getId()));
								if (!isAdd) {//如果未添加过更新，则修改检测项目并添加更新
									oldBusinessAssayTaskData.setAssayProject(CollUtil.join(projectShowNameList, ","));
									updateBusinessAssayTaskDataDOList.add(oldBusinessAssayTaskData);
								}
							}
						}
						
						BusinessAssayTaskDataDO oldBusinessAssayTaskDataDO = businessAssayTaskDataDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).findFirst().orElse(null);
						
						//根据检测方法循环
						BusinessAssayTaskDataDO businessAssayTaskDataDO = null;
						//根据任务数循环
						for (int i = 0; i < configSubSampleMethod.getTaskCount(); i++) {
							//子样检测任务
							businessAssayTaskDataDO = new BusinessAssayTaskDataDO();
							businessAssayTaskDataDO.setId(IdWorker.getId());
							businessAssayTaskDataDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
							businessAssayTaskDataDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
							businessAssayTaskDataDO.setBusinessSubSampleId(businessSubSampleDO.getId());
							businessAssayTaskDataDO.setBusinessSubSampleAssessmentId(businessSubSampleAssessmentDO.getId());//子样判定id
							businessAssayTaskDataDO.setConfigAssayMethodId(changeConfigAssayMethodId);
							businessAssayTaskDataDO.setAssayType(assayType);
							businessAssayTaskDataDO.setTaskType("常规");
							businessAssayTaskDataDO.setConfigSampleFlowId(oldBusinessAssayTaskDataDO.getConfigSampleFlowId());
							businessAssayTaskDataDO.setSampleFlowNodeKey(oldBusinessAssayTaskDataDO.getSampleFlowNodeKey());
							businessAssayTaskDataDO.setSampleFlowNodeTime(currentDateTime);
							businessAssayTaskDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
							businessAssayTaskDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
							
							List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).collect(Collectors.toList());
							
							StringBuilder assayProjectBuilder = new StringBuilder();
							
							for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
								
								//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
								if (!projectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
									continue;
								}
								
								assayProjectBuilder.append(configAssayMethodProjectDO.getShowName()).append(",");
								

								//检测项目
								BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
								businessAssayProjectDataDO.setId(IdWorker.getId());
								businessAssayProjectDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
								businessAssayProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
								businessAssayProjectDataDO.setDictionaryProjectId(configAssayMethodProjectDO.getDictionaryProjectId());
								businessAssayProjectDataDO.setDataType(configAssayMethodProjectDO.getDataType());
								businessAssayProjectDataDO.setDecimalPosition(configAssayMethodProjectDO.getDecimalPosition());
								businessAssayProjectDataDO.setEffectiveDigit(configAssayMethodProjectDO.getEffectiveDigit());
								businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);
								businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
								businessAssayProjectDataDO.setIsEnabled(QmsCommonConstant.YES);
								businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
								
								saveBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
								
								List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDOList = configAssayMethodProjectParameterList.stream().filter(f -> f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectDO.getId())).collect(Collectors.toList());
								for (ConfigAssayMethodProjectParameterDO configAssayMethodProjectParameterDO : configAssayMethodProjectParameterDOList) {
									BusinessAssayParameterDataDO businessAssayParameterDataDO = new BusinessAssayParameterDataDO();
									businessAssayParameterDataDO.setId(IdWorker.getId());
									businessAssayParameterDataDO.setConfigAssayMethodProjectParameterId(configAssayMethodProjectParameterDO.getId());
									businessAssayParameterDataDO.setBusinessAssayProjectDataId(businessAssayProjectDataDO.getId());
									businessAssayParameterDataDO.setDictionaryParameterId(configAssayMethodProjectParameterDO.getDictionaryParameterId());
									businessAssayParameterDataDO.setDataType(configAssayMethodProjectParameterDO.getDataType());
									businessAssayParameterDataDO.setDecimalPosition(configAssayMethodProjectParameterDO.getDecimalPosition());
									
									saveBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
								}
							}
							if (assayProjectBuilder.length() > 1) {
								assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
							}
							businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
							saveBusinessAssayTaskDataDOList.add(businessAssayTaskDataDO);
						}
						
					} else {//已存在
						//根据变更方法过滤出检测项目
						List<Long> projectIdList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
						
						//查询分析方法
						//ConfigAssayMethodDO configAssayMethodDO = configAssayMethodDOList.stream().filter(f -> f.getId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);
						//查询子样分析方法
						//ConfigSubSampleMethodExtendRespVO configSubSampleMethod = configSubSampleMethodList.stream().filter(f -> f.getConfigSubSampleParentId().equals(businessSubParentSampleAssessment.getConfigSubSampleParentId()) && f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).findFirst().orElse(null);

						//根据任务数判断是平行还是
						//String assayType = configSubSampleMethod.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
						
						//查询子样判定
						//BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(businessSubParentSampleAssessmentDO.getBusinessSubParentSampleId(), changeConfigAssayMethodId);

						//查询旧的检测项目
						List<Long> oldBusinessAssayProjectDataIdList = businessAssayProjectDataList.stream().filter(f -> projectIdList.contains(f.getDictionaryProjectId()) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
						//添加到删除列表
						removeAssayProjectIdList.addAll(oldBusinessAssayProjectDataIdList);
						List<Long> oldBusinessAssayParameterDataIdList = businessAssayParameterDataDOList.stream().filter(f -> oldBusinessAssayProjectDataIdList.contains(f.getBusinessAssayProjectDataId())).map(m -> m.getId()).collect(Collectors.toList());
						removeAssayParameterIdList.addAll(oldBusinessAssayParameterDataIdList);
						if (projectIdList.size() == changeConfigAssayMethodProjectList.size()) {
							//查询旧的检测任务
							List<Long> oldBusinessAssayTaskDataIdList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeAssayTaskDataIdList.addAll(oldBusinessAssayTaskDataIdList);
							//查询旧的子样判定
							List<Long> oldBusinessSubSampleAssesmentIdList = businessSubSampleAssessmentDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).map(m -> m.getId()).collect(Collectors.toList());
							//添加到删除列表
							removeSubSampleAssessmentIdList.addAll(oldBusinessSubSampleAssesmentIdList);
							
							//添加到删除列表
							removeSubParentSampleAssessmentIdList.add(businessSubParentSampleAssessment.getId());
						} else {
							List<String> projectShowNameList = changeConfigAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).map(m -> m.getDictionaryProjectShowName()).distinct().collect(Collectors.toList());
							//查询旧的检测任务
							List<BusinessAssayTaskDataDO> oldBusinessAssayTaskDataList = businessAssayTaskDataDOList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getBusinessSubParentSampleId().equals(businessSubParentSampleAssessment.getBusinessSubParentSampleId())).collect(Collectors.toList());
							for (BusinessAssayTaskDataDO oldBusinessAssayTaskData : oldBusinessAssayTaskDataList) {
								boolean isAdd = updateBusinessAssayTaskDataDOList.stream().anyMatch(m -> m.getId().equals(oldBusinessAssayTaskData.getId()));
								if (!isAdd) {//如果未添加过更新，则修改检测项目并添加更新
									oldBusinessAssayTaskData.setAssayProject(CollUtil.join(projectShowNameList, ","));
									updateBusinessAssayTaskDataDOList.add(oldBusinessAssayTaskData);
								}
							}
						}

						//查询当前存在的分析任务
						List<BusinessAssayTaskDataDO> currBusinessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(businessSubParentSampleAssessmentDO.getBusinessSubParentSampleId(), changeConfigAssayMethodId);
						
						//当前存在的分析任务循环
						for (BusinessAssayTaskDataDO businessAssayTaskDataDO : currBusinessAssayTaskDataDOList) {
							
							List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(changeConfigAssayMethodId)).collect(Collectors.toList());
							
							StringBuilder assayProjectBuilder = new StringBuilder(businessAssayTaskDataDO.getAssayProject());
							assayProjectBuilder.append(",");
							
							for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
								
								//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
								if (!projectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
									continue;
								}
								
								assayProjectBuilder.append(configAssayMethodProjectDO.getShowName()).append(",");
								

								//检测项目
								BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
								businessAssayProjectDataDO.setId(IdWorker.getId());
								businessAssayProjectDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
								businessAssayProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
								businessAssayProjectDataDO.setDictionaryProjectId(configAssayMethodProjectDO.getDictionaryProjectId());
								businessAssayProjectDataDO.setDataType(configAssayMethodProjectDO.getDataType());
								businessAssayProjectDataDO.setDecimalPosition(configAssayMethodProjectDO.getDecimalPosition());
								businessAssayProjectDataDO.setEffectiveDigit(configAssayMethodProjectDO.getEffectiveDigit());
								businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);
								businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
								businessAssayProjectDataDO.setIsEnabled(QmsCommonConstant.YES);
								businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
								
								saveBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
								
								List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDOList = configAssayMethodProjectParameterList.stream().filter(f -> f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectDO.getId())).collect(Collectors.toList());
								for (ConfigAssayMethodProjectParameterDO configAssayMethodProjectParameterDO : configAssayMethodProjectParameterDOList) {
									BusinessAssayParameterDataDO businessAssayParameterDataDO = new BusinessAssayParameterDataDO();
									businessAssayParameterDataDO.setId(IdWorker.getId());
									businessAssayParameterDataDO.setConfigAssayMethodProjectParameterId(configAssayMethodProjectParameterDO.getId());
									businessAssayParameterDataDO.setBusinessAssayProjectDataId(businessAssayProjectDataDO.getId());
									businessAssayParameterDataDO.setDictionaryParameterId(configAssayMethodProjectParameterDO.getDictionaryParameterId());
									businessAssayParameterDataDO.setDataType(configAssayMethodProjectParameterDO.getDataType());
									businessAssayParameterDataDO.setDecimalPosition(configAssayMethodProjectParameterDO.getDecimalPosition());
									
									saveBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
								}
							}
							if (assayProjectBuilder.length() > 1) {
								assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
							}
							businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
							updateBusinessAssayTaskDataDOList.add(businessAssayTaskDataDO);
						}
						
						
					}
					
				}
				
			}
			
		}
		
		//查询报表数据
		List<BusinessAssayReportDataDO> businessAssayReportDataDOList = businessAssayReportDataMapper.selectBytBusinessBaseSampleIds(businessBaseSampleIdList);
		for (BusinessAssayReportDataDO businessAssayReportDataDO : businessAssayReportDataDOList) {
			String dataSource = businessAssayReportDataDO.getDataSource();
			if (dataSource.contains(req.getConfigAssayMethodId().toString())) {//判定是否存在
				List<Long> dataSourceList = new ArrayList<>();
				String[] dataSourceSplit = dataSource.split(",");
				for (int i = 0; i < dataSourceSplit.length; i++) {
					dataSourceList.add(Long.parseLong(dataSourceSplit[i]));
				}
				
				//移除当前的
				dataSourceList.remove(req.getConfigAssayMethodId());
				//添加新的
				dataSourceList.addAll(changeConfigAssayMethodIdList);
				
				//重新赋值
				businessAssayReportDataDO.setDataSource(CollUtil.join(dataSourceList, ","));
				
				//添加到更新列表
				updateBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
			}
		}
		
		

		//======================== 删除 =====================================================================

		if (removeAssayTaskDataIdList.size() > 0) {
			businessAssayTaskDataMapper.deleteByIds(removeAssayTaskDataIdList);
		}
		if (removeAssayProjectIdList.size() > 0) {
			businessAssayProjectDataMapper.deleteByIds(removeAssayProjectIdList);
		}
		if (removeAssayParameterIdList.size() > 0) {
			businessAssayParameterDataMapper.deleteByIds(removeAssayParameterIdList);
		}
		
		if (removeSubSampleAssessmentIdList.size() > 0) {
			businessSubSampleAssessmentMapper.deleteByIds(removeSubSampleAssessmentIdList);
		}
		
		if (removeSubParentSampleAssessmentIdList.size() > 0) {
			businessSubParentSampleAssessmentMapper.deleteByIds(removeSubParentSampleAssessmentIdList);
		}
		
		//======================== 修改 =====================================================================

		if (updateBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.updateBatch(updateBusinessAssayReportDataDOList);
		}
		
		if (updateBusinessAssayTaskDataDOList.size() > 0) {
			businessAssayTaskDataMapper.updateBatch(updateBusinessAssayTaskDataDOList);
		}
		if (updateBusinessAssayProjectDataDOList.size() > 0) {
			businessAssayProjectDataMapper.updateBatch(updateBusinessAssayProjectDataDOList);
		}
		if (updateBusinessAssayParameterDataDOList.size() > 0) {
			businessAssayParameterDataMapper.updateBatch(updateBusinessAssayParameterDataDOList);
		}

		if (updateBusinessSubSampleAssessmentDOList.size() > 0) {
			businessSubSampleAssessmentMapper.updateBatch(updateBusinessSubSampleAssessmentDOList);
		}

		if (updateBusinessSubParentSampleAssessmentDOList.size() > 0) {
			businessSubParentSampleAssessmentMapper.updateBatch(updateBusinessSubParentSampleAssessmentDOList);
		}
		
		//======================== 新建 =====================================================================
		
		if (saveBusinessAssayTaskDataDOList.size() > 0) {
			businessAssayTaskDataMapper.insertBatch(saveBusinessAssayTaskDataDOList);
		}
		if (saveBusinessAssayProjectDataDOList.size() > 0) {
			businessAssayProjectDataMapper.insertBatch(saveBusinessAssayProjectDataDOList);
		}
		if (saveBusinessAssayParameterDataDOList.size() > 0) {
			businessAssayParameterDataMapper.insertBatch(saveBusinessAssayParameterDataDOList);
		}

		if (saveBusinessSubSampleAssessmentDOList.size() > 0) {
			businessSubSampleAssessmentMapper.insertBatch(saveBusinessSubSampleAssessmentDOList);
		}

		if (saveBusinessSubParentSampleAssessmentDOList.size() > 0) {
			businessSubParentSampleAssessmentMapper.insertBatch(saveBusinessSubParentSampleAssessmentDOList);
		}
		
		
		
	}
**/
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse methodAssign(SampleTaskAssignMethodParam param) {
		if (param.getConfigAssayMethodId() == null) {
			throw new ServiceException(1_032_100_000, "无分析方法");
		}
		if (CollUtil.isEmpty(param.getAssignAssayUserList())) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("按方法分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;		
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse sampleAssign(SampleTaskAssignSampleParam param) {
		if (param.getAssignAssayUser() == null) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
		if (CollUtil.isEmpty(param.getAssignSampleIdList())) {
			throw new ServiceException(1_032_100_000, "无任务分配样品");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("按样品分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse manualAssign(SampleTaskAssignManualParam param) {
		if (param.getAssignAssayUser() == null) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
		if (CollUtil.isEmpty(param.getAssayTaskDataIdList())) {
			throw new ServiceException(1_032_100_000, "无分配的检测任务");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("手动分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse manualTypeAssign(SampleTaskAssignManualTypeParam param) {
		if (CollUtil.isEmpty(param.getAssignAssayUserList())) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
		if (CollUtil.isEmpty(param.getAssayTaskDataIdList())) {
			throw new ServiceException(1_032_100_000, "无分配的检测任务");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("手动分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse projectMethodAssign(SampleTaskAssignProjectMethodParam param) {
		if (CollUtil.isEmpty(param.getAssignAssayUserList())) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
		if (param.getConfigAssayMethodId() == null) {
			throw new ServiceException(1_032_100_000, "无分析方法");
		}
		if (CollUtil.isEmpty(param.getAssayProjectDataIdList())) {
			throw new ServiceException(1_032_100_000, "无分配的检测项目");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("手动分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public LiteflowResponse projectNoMethodAssign(SampleTaskAssignProjectNoMethodParam param) {
		if (CollUtil.isEmpty(param.getAssignAssayUserList())) {
			throw new ServiceException(1_032_100_000, "无任务分配人员");
		}
		if (param.getConfigAssayMethodId() == null) {
			throw new ServiceException(1_032_100_000, "无分析方法");
		}
		if (CollUtil.isEmpty(param.getSampleEntrustUnStandardProjectIdList())) {
			throw new ServiceException(1_032_100_000, "无分配的检测项目");
		}
    	Long tenantId = TenantContextHolder.getRequiredTenantId();
    	LiteflowResponse response = flowExecutor.execute2Resp("sampleTaskAssignChain" + tenantId, param, SampleTaskAssignContext.class);
	    if (!response.isSuccess()){
	    	log.error("手动分配任务失败", response.getCause());
            throw new ServiceException(1_032_100_000, response.getCause().getMessage());
	    }
	    return response;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void submitAssign(Long id) {
		BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectById(id);
		businessAssayTaskDO.setTaskAssignStatus(QmsCommonConstant.SUBMITTED);
		businessAssayTaskDO.setTaskAssignSubmitTime(LocalDateTime.now());
		businessAssayTaskMapper.updateById(businessAssayTaskDO);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void voidAssign(Long id) {
		BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectById(id);
		if (QmsCommonConstant.SUBMITTED.equals(businessAssayTaskDO.getTaskAssignStatus())) {
			throw new ServiceException(1_032_100_000, "任务已提交，不允许作废");
		}
		businessAssayTaskDO.setTaskAssignStatus(QmsCommonConstant.VOID);
		
	    List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>().eq(BusinessAssayTaskDataDO::getBusinessAssayTaskId, businessAssayTaskDO.getId()));
	    for (BusinessAssayTaskDataDO businessAssayTaskDataDO : businessAssayTaskDataDOList) {
	    	businessAssayTaskDataDO.setBusinessAssayTaskId(null);
	    	businessAssayTaskDataDO.setAssayOperatorId(null);
	    	businessAssayTaskDataDO.setAssayOperator(null);
	    	businessAssayTaskDataDO.setAssignTaskTime(null);
	    	businessAssayTaskDataDO.setIsAssignTasked(0);
		}
	    businessAssayTaskMapper.updateById(businessAssayTaskDO);
	    if (CollUtil.isNotEmpty(businessAssayTaskDataDOList)) {
		    businessAssayTaskDataMapper.updateBatch(businessAssayTaskDataDOList);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void removeAssignTaskDetail(BatchDeleteReqVO req) {
		List<BusinessAssayTaskDetailDO> businessAssayTaskDetailDOList = businessAssayTaskDetailMapper.selectByIds(req.getIds());
		List<Long> businessAssayTaskDataIdList = businessAssayTaskDetailDOList.stream().map(m -> m.getBusinessAssayTaskDataId()).collect(Collectors.toList());
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByIds(businessAssayTaskDataIdList);
		for (BusinessAssayTaskDataDO businessAssayTaskDataDO : businessAssayTaskDataDOList) {
			businessAssayTaskDataDO.setIsAssignTasked(QmsCommonConstant.NO);
			businessAssayTaskDataDO.setAssignTaskTime(null);
			businessAssayTaskDataDO.setAssayOperator(null);
			businessAssayTaskDataDO.setBusinessAssayTaskId(null);
		}
		//更新检测任务
		businessAssayTaskDataMapper.updateBatch(businessAssayTaskDataDOList);
		
		//删除任务单明细数据
		businessAssayTaskDetailMapper.physicalDeleteByIds(req.getIds());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createQcSample(CreateQcSampleReqVO req) {
		List<BusinessQCManagementDataDO> businessQCManagementDataDOList = new ArrayList<>();
		List<BusinessQCManagementSampleParameterDataDO> businessQCManagementSampleParameterDataDOList = new ArrayList<>();
		List<BusinessQCManagementProjectDataDO> businessQCManagementProjectDataDOList = new ArrayList<>();
		List<BusinessQCManagementParameterDataDO> businessQCManagementParameterDataDOList = new ArrayList<>();
		
		List<BusinessQCCoefficientDataDO> businessQCCoefficientDataDOList = new ArrayList<>();
		List<BusinessQCCoefficientParameterDataDO> businessQCCoefficientParameterDataDOList = new ArrayList<>();
		
		//分配任务
		BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectById(req.getBusinessAssayTaskId());
		
		//分析方法
		ConfigAssayMethodDO configAssayMethodDO = configAssayMethodMapper.selectById(businessAssayTaskDO.getConfigAssayMethodId());
		
		ConfigQCSampleMethodExtendRespVO configQCSampleMethod = configQCSampleMethodMapper.selectByConfigAssayMethodIdAndDictionaryBusinessKey(businessAssayTaskDO.getConfigAssayMethodId(), req.getDictionaryBusinessKey());
		
		if ("withoutProject".equals(req.getParentDictionaryBusinesskey())) {//不带检测项目的：如 空白样和标样
			
			BusinessQCCoefficientDataDO businessQCCoefficientDataDO = new BusinessQCCoefficientDataDO();
			businessQCCoefficientDataDO.setId(IdWorker.getId());
			businessQCCoefficientDataDO.setBusinessAssayTaskId(businessAssayTaskDO.getId());
			businessQCCoefficientDataDO.setConfigAssayMethodId(configQCSampleMethod.getConfigAssayMethodId());
			businessQCCoefficientDataDO.setSampleName(configQCSampleMethod.getDictionaryBusinessName());
			if (StringUtils.isNotBlank(req.getSampleName())) {
				businessQCCoefficientDataDO.setSampleName(req.getSampleName());
			}
			if (StringUtils.isNotBlank(req.getSampleCode())) {
				businessQCCoefficientDataDO.setSampleCode(req.getSampleCode());
			} else {
				if (StringUtils.isNotBlank(configQCSampleMethod.getCodeRule())) {
					String codeSample = sequenceUtil.genCode(configQCSampleMethod.getCodeRule());
					businessQCCoefficientDataDO.setSampleCode(codeSample);
				}
			}
			businessQCCoefficientDataDO.setDictionaryBusinessId(configQCSampleMethod.getDictionaryBusinessId());
			businessQCCoefficientDataDO.setDictionaryBusinessKey(configQCSampleMethod.getDictionaryBusinessKey());
			businessQCCoefficientDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
			businessQCCoefficientDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
			businessQCCoefficientDataDO.setAssayOperator(businessAssayTaskDO.getAssayOperator());
			businessQCCoefficientDataDO.setAssayOperatorId(businessAssayTaskDO.getAssayOperatorId());
			businessQCCoefficientDataDO.setAssayProject("");
			businessQCCoefficientDataDO.setAssignTaskTime(businessAssayTaskDO.getTaskAssignTime());
			businessQCCoefficientDataDO.setIsAssignTasked(QmsCommonConstant.YES);
			businessQCCoefficientDataDO.setIsReported(QmsCommonConstant.NO);
			
			businessQCCoefficientDataDOList.add(businessQCCoefficientDataDO);
			
			List<ConfigQCSampleMethodParameterExtendRespVO> configQCSampleMethodParameterExtendRespVOs = configQCSampleMethodParameterMapper.selectByConfigQCSampleMethodId(configQCSampleMethod.getId());
			for (ConfigQCSampleMethodParameterExtendRespVO configQCSampleMethodParameterExtendRespVO : configQCSampleMethodParameterExtendRespVOs) {
				BusinessQCCoefficientParameterDataDO businessQCCoefficientParameterDataDO = new BusinessQCCoefficientParameterDataDO();
				businessQCCoefficientParameterDataDO.setBusinessQCCoefficientDataId(businessQCCoefficientDataDO.getId());
				businessQCCoefficientParameterDataDO.setConfigQCSampleMethodParameterId(configQCSampleMethodParameterExtendRespVO.getId());
				businessQCCoefficientParameterDataDO.setDictionaryParameterId(configQCSampleMethodParameterExtendRespVO.getDictionaryParameterId());
				businessQCCoefficientParameterDataDO.setDataType(configQCSampleMethodParameterExtendRespVO.getDataType());
				businessQCCoefficientParameterDataDO.setDecimalPosition(configQCSampleMethodParameterExtendRespVO.getDecimalPosition());
				businessQCCoefficientParameterDataDO.setValue(configQCSampleMethodParameterExtendRespVO.getDefaultValue());					
				
				businessQCCoefficientParameterDataDOList.add(businessQCCoefficientParameterDataDO);
			}
			
		} else {//带检测项目的 如： 管理样、标准样、质控空白样、监控样
			//分析项目
			List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectExtendRespList = configAssayMethodProjectMapper.selectByConfigAssayMethodId(businessAssayTaskDO.getConfigAssayMethodId());
			//检测项目id
			List<Long> dictionaryProjectIdList = new ArrayList<>();
			//必须要检测的项目
			String mustProject = configQCSampleMethod.getMustProject();
			if (StringUtils.isNotBlank(mustProject)) {
				String[] mustProjects = mustProject.split(",");
				for (String dicProjectId : mustProjects) {
					dictionaryProjectIdList.add(Long.parseLong(dicProjectId));
				}
			}
			//当前任务的检测项目列表
			List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectByBusinessAssayTaskId(req.getBusinessAssayTaskId());
			//获取当前任务的检测项目id列表
			List<Long> busTaskDictionaryProjectIdList = businessAssayProjectDataList.stream().map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
			dictionaryProjectIdList.addAll(busTaskDictionaryProjectIdList);
			//获取需要创建的检测项目id
			List<Long> configAssayMethodProjectIds = configAssayMethodProjectExtendRespList.stream().filter(f -> dictionaryProjectIdList.contains(f.getDictionaryProjectId())).map(m -> m.getId()).collect(Collectors.toList());
			//分析项目参数
			List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList = configAssayMethodProjectParameterMapper.selectByConfigAssayMethodProjectIds(configAssayMethodProjectIds);
			//分析项目
			String assayProject = configAssayMethodProjectExtendRespList.stream().map(m -> m.getShowName()).collect(Collectors.joining(","));
			
			BusinessQCManagementDataDO businessQCManagementDataDO = new BusinessQCManagementDataDO();
			businessQCManagementDataDO.setId(IdWorker.getId());
			businessQCManagementDataDO.setBusinessAssayTaskId(req.getBusinessAssayTaskId());
			businessQCManagementDataDO.setConfigAssayMethodId(businessAssayTaskDO.getConfigAssayMethodId());
			businessQCManagementDataDO.setSampleName(req.getSampleName());
			if (StringUtils.isNotBlank(req.getSampleCode())) {
				businessQCManagementDataDO.setSampleCode(req.getSampleCode());
			}
			businessQCManagementDataDO.setBusinessStandardSampleId(req.getBusinessStandardSampleId());
			businessQCManagementDataDO.setDictionaryBusinessId(req.getDictionaryBusinessId());
			businessQCManagementDataDO.setDictionaryBusinessKey(req.getDictionaryBusinessKey());
			businessQCManagementDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
			businessQCManagementDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
			businessQCManagementDataDO.setAssayOperator(businessAssayTaskDO.getAssayOperator());
			businessQCManagementDataDO.setAssayOperatorId(businessAssayTaskDO.getAssayOperatorId());
			businessQCManagementDataDO.setAssayProject(assayProject);
			businessQCManagementDataDO.setAssignTaskTime(businessAssayTaskDO.getTaskAssignTime());
			businessQCManagementDataDO.setIsAssignTasked(QmsCommonConstant.YES);
			businessQCManagementDataDO.setIsReported(QmsCommonConstant.NO);
			
			businessQCManagementDataDOList.add(businessQCManagementDataDO);
			

			//分析方法参数配置
			List<ConfigAssayMethodParameterDO> configAssayMethodParameterDOList = configAssayMethodParameterMapper.selectByConfigAssayMethodId(businessAssayTaskDO.getConfigAssayMethodId());
			BusinessQCManagementSampleParameterDataDO businessQCManagementSampleParameterDataDO = null;
			for (ConfigAssayMethodParameterDO configAssayMethodParameterDO : configAssayMethodParameterDOList) {
				businessQCManagementSampleParameterDataDO = new BusinessQCManagementSampleParameterDataDO();
				businessQCManagementSampleParameterDataDO.setId(IdWorker.getId());
				businessQCManagementSampleParameterDataDO.setBusinessQCManagementDataId(businessQCManagementDataDO.getId());
				businessQCManagementSampleParameterDataDO.setConfigAssayMethodParameterId(configAssayMethodParameterDO.getId());
				businessQCManagementSampleParameterDataDO.setDataType(configAssayMethodParameterDO.getDataType());
				businessQCManagementSampleParameterDataDO.setDecimalPosition(configAssayMethodParameterDO.getDecimalPosition());	
				if (StringUtils.isNotBlank(configAssayMethodParameterDO.getDefaultValue())) {
					businessQCManagementSampleParameterDataDO.setValue(configAssayMethodParameterDO.getDefaultValue());
				}
				
				businessQCManagementSampleParameterDataDOList.add(businessQCManagementSampleParameterDataDO);
			}

			for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectExtendRespVO : configAssayMethodProjectExtendRespList) {
				if (!dictionaryProjectIdList.contains(configAssayMethodProjectExtendRespVO.getDictionaryProjectId())) {//不存在则跳过
					continue;
				}
				
				BusinessQCManagementProjectDataDO businessQCManagementProjectDataDO = new BusinessQCManagementProjectDataDO();
				businessQCManagementProjectDataDO.setId(IdWorker.getId());
				businessQCManagementProjectDataDO.setBusinessQCManagementDataId(businessQCManagementDataDO.getId());
				businessQCManagementProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectExtendRespVO.getId());
				businessQCManagementProjectDataDO.setDictionaryProjectId(configAssayMethodProjectExtendRespVO.getDictionaryProjectId());
				businessQCManagementProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_QUALITY_CONTROL);
				businessQCManagementProjectDataDO.setSymbol("=");
				businessQCManagementProjectDataDO.setDataType(configAssayMethodProjectExtendRespVO.getDataType());
				businessQCManagementProjectDataDO.setDecimalPosition(configAssayMethodProjectExtendRespVO.getDecimalPosition());
				businessQCManagementProjectDataDO.setEffectiveDigit(configAssayMethodProjectExtendRespVO.getEffectiveDigit());
				businessQCManagementProjectDataDO.setIsEnabled(configAssayMethodProjectExtendRespVO.getIsDefaultEnabled());
				
				businessQCManagementProjectDataDOList.add(businessQCManagementProjectDataDO);
				
				List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDoList = configAssayMethodProjectParameterList.stream().filter(f -> f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectExtendRespVO.getId())).collect(Collectors.toList());
				for (ConfigAssayMethodProjectParameterDO configAssayMethodProjectParameterDO : configAssayMethodProjectParameterDoList) {
					BusinessQCManagementParameterDataDO businessQCManagementParameterDataDO = new BusinessQCManagementParameterDataDO();
					businessQCManagementParameterDataDO.setBusinessQCManagementProjectDataId(businessQCManagementProjectDataDO.getId());
					businessQCManagementParameterDataDO.setConfigAssayMethodProjectParameterId(configAssayMethodProjectParameterDO.getId());
					businessQCManagementParameterDataDO.setDictionaryParameterId(configAssayMethodProjectParameterDO.getDictionaryParameterId());
					businessQCManagementParameterDataDO.setDataType(configAssayMethodProjectParameterDO.getDataType());
					businessQCManagementParameterDataDO.setDecimalPosition(configAssayMethodProjectParameterDO.getDecimalPosition());
					if (StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getDefaultValue())) {
						businessQCManagementParameterDataDO.setValue(configAssayMethodProjectParameterDO.getDefaultValue());
					}
					
					businessQCManagementParameterDataDOList.add(businessQCManagementParameterDataDO);
				}
				
			}
			
		}

		if (CollUtil.isNotEmpty(businessQCManagementDataDOList)) {
			businessQCManagementDataMapper.insertBatch(businessQCManagementDataDOList);
		}
		if (CollUtil.isNotEmpty(businessQCManagementSampleParameterDataDOList)) {
			businessQCManagementSampleParameterDataMapper.insertBatch(businessQCManagementSampleParameterDataDOList);
		}
		if (CollUtil.isNotEmpty(businessQCManagementProjectDataDOList)) {
			businessQCManagementProjectDataMapper.insertBatch(businessQCManagementProjectDataDOList);
		}
		if (CollUtil.isNotEmpty(businessQCManagementParameterDataDOList)) {
			businessQCManagementParameterDataMapper.insertBatch(businessQCManagementParameterDataDOList);
		}
		if (CollUtil.isNotEmpty(businessQCCoefficientDataDOList)) {
			businessQCCoefficientDataMapper.insertBatch(businessQCCoefficientDataDOList);
		}
		if (CollUtil.isNotEmpty(businessQCCoefficientParameterDataDOList)) {
			businessQCCoefficientParameterDataMapper.insertBatch(businessQCCoefficientParameterDataDOList);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteQcSample(DeleteQcSampleReqVO req) {
		if ("withoutProject".equals(req.getParentDictionaryBusinesskey())) {//不带检测项目的 空白样和标样
			businessQCCoefficientDataMapper.deleteByIds(req.getQcSampleIdList());
			businessQCCoefficientParameterDataMapper.delete(new LambdaQueryWrapperX<BusinessQCCoefficientParameterDataDO>().in(BusinessQCCoefficientParameterDataDO::getBusinessQCCoefficientDataId, req.getQcSampleIdList()));
		} else { //带检测项目的 管理样和标准样
			businessQCManagementDataMapper.deleteByIds(req.getQcSampleIdList());
			List<BusinessQCManagementProjectDataDO> businessQCManagementProjectDataList = businessQCManagementProjectDataMapper.selectByBusinessQCManagementDataIds(req.getQcSampleIdList());
			List<Long> businessQCManagementProjectDataIdList = businessQCManagementProjectDataList.stream().map(m -> m.getId()).collect(Collectors.toList());
			businessQCManagementProjectDataMapper.deleteByIds(businessQCManagementProjectDataIdList);
			businessQCManagementParameterDataMapper.delete(new LambdaQueryWrapperX<BusinessQCManagementParameterDataDO>().in(BusinessQCManagementParameterDataDO::getBusinessQCManagementProjectDataId, businessQCManagementProjectDataIdList));
		}
	}
}
