package com.zt.plat.module.qms.business.bus.liteflow.sample.entrust;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.exception.ServiceException;
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.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessBaseSampleDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleAssayResultDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustProjectDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubParentSampleDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAnalysisGroupDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.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.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessBaseSampleMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleAssayResultMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustProjectMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustRegistrationMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleAnalysisGroupMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleMapper;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowInfo;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleEntrustContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.BaseSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodProjectDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodProjectParameterDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigBaseSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleFlowDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleMethodDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleParentDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardMethodDO;
import com.zt.plat.module.qms.business.config.dal.mapper.BaseSampleMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodMapper;
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.ConfigBaseSampleMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSampleFlowMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleParentMapper;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;
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.core.sampleflow.SampleFlowDefinition;
import com.zt.plat.module.qms.core.sampleflow.SampleFlowNode;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleEntrustGenSampleDataCmp", name = "样品委托生成系统样品数据")
public class SampleEntrustGenSampleDataCmp extends NodeComponent {
	
	@Resource
	private SequenceUtil sequenceUtil;
	
	@Resource
	private BaseSampleMapper baseSampleMapper;
	
	@Resource
	private BusinessSampleEntrustRegistrationMapper businessSampleEntrustRegistrationMapper;
	
	@Resource
	private BusinessSampleEntrustDetailMapper businessSampleEntrustDetailMapper;
	
	@Resource
	private BusinessSampleEntrustProjectMapper businessSampleEntrustProjectMapper;
	
	@Resource
	private BusinessSampleAssayResultMapper businessSampleAssayResultMapper;
	
	@Resource
	private ConfigBaseSampleMapper configBaseSampleMapper;
	
	@Resource
	private BusinessBaseSampleMapper businessBaseSampleMapper;
	
	@Resource
	private ConfigSubSampleParentMapper configSubSampleParentMapper;
	
	@Resource
	private BusinessSubParentSampleMapper businessSubParentSampleMapper;
	
	@Resource
	private ConfigSubSampleMapper configSubSampleMapper;
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Resource
	private BusinessSubSampleAnalysisGroupMapper businessSubSampleAnalysisGroupMapper;
	
	@Resource
	private ConfigAssayMethodMapper configAssayMethodMapper;
	
	@Resource
	private ConfigSubSampleMethodMapper configSubSampleMethodMapper;//子样分析方法关联配置
	
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;
	
	@Resource
	private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;
	
	@Resource
	private ConfigAssayMethodProjectParameterMapper configAssayMethodProjectParameterMapper;
	
	@Resource
	private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;
	
	@Resource
	private DictionaryProjectMapper dictionaryProjectMapper;

	@Resource
	private ConfigSampleFlowMapper configSampleFlowMapper;

	@Override
	public void process() throws Exception {
		//获取样品委托上下文
		SampleEntrustContext sampleEntrustContext = this.getContextBean(SampleEntrustContext.class);
		
		//当前登录用户
		String loginRealname = sampleEntrustContext.getLoginRealname();
		
		LocalDateTime currentDateTime = sampleEntrustContext.getCurrentDateTime();
		//样品委托登记
		BusinessSampleEntrustRegistrationDO sampleEntrustRegistration = sampleEntrustContext.getSampleEntrustRegistration();
		//样品委托登记明细
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailList = sampleEntrustContext.getSampleEntrustDetailList();
		//样品检测项目
		List<BusinessSampleEntrustProjectDO> sampleEntrustProjectList = sampleEntrustContext.getSampleEntrustProjectList();
		//样品来样品位
		List<BusinessSampleAssayResultDO> sampleAssayResultList = sampleEntrustContext.getSampleAssayResultList();
		
		//样品大类列表
		List<BaseSampleDO> baseSampleList = baseSampleMapper.selectList();
		
		
		//主样配置
		List<ConfigBaseSampleDO> configBaseSampleList = configBaseSampleMapper.selectList();
		//分样配置
		List<ConfigSubSampleParentDO> configSubSampleParentList = configSubSampleParentMapper.selectList();
		//子样配置
		List<ConfigSubSampleDO> configSubSampleList = configSubSampleMapper.selectList();
		//子样分析方法配置
		List<ConfigSubSampleMethodDO> configSubSampleMethodList = configSubSampleMethodMapper.selectList();
		//分析方法配置
		List<ConfigAssayMethodDO> configAssayMethodList = configAssayMethodMapper.selectList();
		//分析方法检测项目配置
		List<ConfigAssayMethodProjectDO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectList();
		//分析方法检测项目参数配置
		List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList = configAssayMethodProjectParameterMapper.selectList();
		List<DictionaryProjectDO> dictionaryProjectList = dictionaryProjectMapper.selectList();
		
		//样品流程配置
		List<String> configSampleFlowKeyList = new ArrayList<>();
		List<String> configBaseSampleFlowKeyList = configBaseSampleList.stream().map(m -> m.getFlowKey()).distinct().collect(Collectors.toList());
		configSampleFlowKeyList.addAll(configBaseSampleFlowKeyList);
		List<String> configSubSampleFlowKeyList = configSubSampleList.stream().map(m -> m.getFlowKey()).distinct().collect(Collectors.toList());
		configSampleFlowKeyList.addAll(configSubSampleFlowKeyList);
		List<ConfigSampleFlowDO> configSampleFlowList = configSampleFlowMapper.selectLatestConfigSampleFlowListByKeys(configSampleFlowKeyList);
		
		List<BusinessBaseSampleDO> businessBaseSampleDOList = new ArrayList<>();
		List<BusinessSubParentSampleDO> businessSubParentSampleDOList = new ArrayList<>();
		List<BusinessSubSampleDO> businessSubSampleDOList = new ArrayList<>();
		List<BusinessSubSampleAnalysisGroupDO> businessSubSampleAnalysisGroupDOList = new ArrayList<>();
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = new ArrayList<>();
		List<BusinessAssayProjectDataDO> businessAssayProjectDataDOList = new ArrayList<>();
		List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList = new ArrayList<>();
		
		List<SampleFlowInfo> sampleFlowInfoList = new ArrayList<>();

		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailListSort = sampleEntrustDetailList.stream().sorted(Comparator.comparingInt(BusinessSampleEntrustDetailDO::getSort)).collect(Collectors.toList());
		BusinessBaseSampleDO businessBaseSampleDO = null;
		for (BusinessSampleEntrustDetailDO businessSampleEntrustDetailDO : sampleEntrustDetailListSort) {//委托样品明细
			
			//样品大类
			BaseSampleDO baseSampleDO = baseSampleList.stream().filter(f -> f.getId().equals(businessSampleEntrustDetailDO.getBaseSampleId())).findFirst().orElse(null);

			//获取当前样品的检测项目
			List<BusinessSampleEntrustProjectDO> sampleEntrustProjectDOList = sampleEntrustProjectList.stream().filter(f -> businessSampleEntrustDetailDO.getId().equals(f.getBusinessSampleEntrustDetailId())).collect(Collectors.toList());
			
			//当前样品检测项目字典id列表
			List<Long> dictionaryProjectIdList = sampleEntrustProjectDOList.stream().map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
			
			//获取物料检测标准检测项目id列表
			List<Long> materialAssayStandardDetailIdList = sampleEntrustProjectDOList.stream().map(m -> m.getMaterialAssayStandardDetailId()).collect(Collectors.toList());
			
			//当前样品检测标准方法
			List<MaterialAssayStandardMethodDO> materialAssayStandardMethodList = sampleEntrustContext.getMaterialAssayStandardMethodDefaultByMaterialAssayStandardDetailIds(materialAssayStandardDetailIdList);
			
			//获取检测方法
			List<Long> configAssayMethodIdList = materialAssayStandardMethodList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
			
			//查找子样配置
			List<Long> configSubSampleIdList = configSubSampleMethodList.stream().filter(f -> f.getIsDefaultUse().equals(QmsCommonConstant.YES) && configAssayMethodIdList.contains(f.getConfigAssayMethodId())).map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
			
			//查询当前委托样品对应的子样
			List<ConfigSubSampleDO> entrustConfigSubSampleList = configSubSampleList.stream().filter(f -> configSubSampleIdList.contains(f.getId()) && f.getBaseSampleId().equals(businessSampleEntrustDetailDO.getBaseSampleId())).distinct().collect(Collectors.toList());
			if (entrustConfigSubSampleList.size() != 1) {
				throw new ServiceException(500, "子样配置不正确");
			}
			//获取到子样配置
			ConfigSubSampleDO configSubSample = entrustConfigSubSampleList.get(0);
			Long configSubSampleParentId = configSubSample.getConfigSubSampleParentId();
			//获取到分样配置
			ConfigSubSampleParentDO configSubSampleParent = configSubSampleParentList.stream().filter(f -> configSubSampleParentId.equals(f.getId())).findFirst().orElse(null);
			Long configBaseSampleId = configSubSampleParent.getConfigBaseSampleId();
			//获取到主样配置
			ConfigBaseSampleDO configBaseSample = configBaseSampleList.stream().filter(f -> configBaseSampleId.equals(f.getId())).findFirst().orElse(null);
			
			//主样流程
			ConfigSampleFlowDO configSampleFlowBase = configSampleFlowList.stream().filter(f -> f.getKey().equals(configBaseSample.getFlowKey())).findFirst().orElse(null);
			//样品流程定义
			SampleFlowDefinition sampleFlowDefinitionBase = JSON.parseObject(configSampleFlowBase.getContent(), SampleFlowDefinition.class);
			List<SampleFlowNode> flowNodeBaseList = sampleFlowDefinitionBase.getFlowNodeList().stream().sorted(Comparator.comparing(SampleFlowNode::getSort)).collect(Collectors.toList());
			SampleFlowNode sampleFlowNodeBase = flowNodeBaseList.get(0);
			
			//=======================主样==========================
			businessBaseSampleDO = new BusinessBaseSampleDO();
			businessBaseSampleDO.setId(IdWorker.getId());
			//设置配置id
			businessBaseSampleDO.setConfigBaseSampleId(configBaseSampleId);
			//样品名称为委托登记来的样品名称
			businessBaseSampleDO.setSampleName(businessSampleEntrustDetailDO.getSampleName());
			//样品大类名称
			businessBaseSampleDO.setBaseSampleName(baseSampleDO.getName());
			//样品类型字典与委托一致
			businessBaseSampleDO.setDictionaryBusinessId(businessSampleEntrustDetailDO.getSampleTypeDictionaryBusinessId());
			//生成样品编号
			String sampleCode = sequenceUtil.genCode(configBaseSample.getCodeRule());
			businessBaseSampleDO.setSampleCode(sampleCode);
			//样品生成时间为当前时间
			businessBaseSampleDO.setSampleTime(currentDateTime);
			//样品状态正常
			businessBaseSampleDO.setSampleStatus(QmsCommonConstant.NORMAL);
			//主样流程配置id
			businessBaseSampleDO.setConfigSampleFlowId(configSampleFlowBase.getId());
			//主样当前流程节点key
			businessBaseSampleDO.setSampleFlowNodeKey(sampleFlowNodeBase.getNodeKey());
			//主样当前流程节点时间
			businessBaseSampleDO.setSampleFlowNodeTime(currentDateTime);
			//操作人
			businessBaseSampleDO.setOperator(loginRealname);
			
			//添加到列表
			businessBaseSampleDOList.add(businessBaseSampleDO);
			
			//=====================委托明细======================
			//设置主样id
			businessSampleEntrustDetailDO.setBusinessBaseSampleId(businessBaseSampleDO.getId());
			//设置主样编号
			businessSampleEntrustDetailDO.setSampleCode(businessBaseSampleDO.getSampleCode());

			//=======================分样=========================
			BusinessSubParentSampleDO businessSubParentSampleDO = new BusinessSubParentSampleDO();
			businessSubParentSampleDO.setId(IdWorker.getId());
			businessSubParentSampleDO.setDictionaryBusinessId(configSubSampleParent.getDictionaryBusinessId());
			businessSubParentSampleDO.setBusinessBaseSampleId(businessBaseSampleDO.getId());
			businessSubParentSampleDO.setConfigSubSampleParentId(configSubSampleParentId);
			businessSubParentSampleDO.setSampleName(businessBaseSampleDO.getSampleName());
			businessSubParentSampleDO.setSampleCode(businessBaseSampleDO.getSampleCode());
			businessSubParentSampleDO.setSubSampleCode(businessBaseSampleDO.getSampleCode());
			businessSubParentSampleDO.setSampleStatus(QmsCommonConstant.NORMAL);
			businessSubParentSampleDOList.add(businessSubParentSampleDO);

			//=======================子样=======================
			ConfigSampleFlowDO configSampleFlowSub = configSampleFlowList.stream().filter(f -> f.getKey().equals(configSubSample.getFlowKey())).findFirst().orElse(null);
			//样品流程定义
			SampleFlowDefinition sampleFlowDefinitionSub = JSON.parseObject(configSampleFlowSub.getContent(), SampleFlowDefinition.class);
			List<SampleFlowNode> flowNodeSubList = sampleFlowDefinitionSub.getFlowNodeList().stream().sorted(Comparator.comparing(SampleFlowNode::getSort)).collect(Collectors.toList());
			SampleFlowNode sampleFlowNodeSub = flowNodeSubList.get(0);
			
			BusinessSubSampleDO businessSubSampleDO = new BusinessSubSampleDO();
			businessSubSampleDO.setId(IdWorker.getId());
			businessSubSampleDO.setBusinessBaseSampleId(businessBaseSampleDO.getId());
			businessSubSampleDO.setBusinessSubParentSampleId(businessSubParentSampleDO.getId());
			businessSubSampleDO.setConfigSubSampleId(configSubSample.getId());
			businessSubSampleDO.setDictionaryBusinessId(configSubSample.getDictionaryBusinessId());
			businessSubSampleDO.setSampleName(businessBaseSampleDO.getSampleName());
			businessSubSampleDO.setSampleCode(businessBaseSampleDO.getSampleCode());
			businessSubSampleDO.setSampleAssayCode(businessBaseSampleDO.getSampleCode());
			businessSubSampleDO.setSampleReturnCode(businessBaseSampleDO.getSampleCode());
			businessSubSampleDO.setIsWeighing(businessSampleEntrustDetailDO.getIsWeighing());
			businessSubSampleDO.setSampleStatus(QmsCommonConstant.NORMAL);
			businessSubSampleDO.setConfigSampleFlowId(configSampleFlowSub.getId());
			businessSubSampleDO.setSampleFlowNodeKey(sampleFlowNodeSub.getNodeKey());
			businessSubSampleDO.setSampleFlowNodeTime(currentDateTime);
			businessSubSampleDO.setNextSampleFlowNode(sampleFlowNodeSub.getNextFlowNodeName());
			
			businessSubSampleDOList.add(businessSubSampleDO);
			
			//子样样品流转信息
			SampleFlowInfo sampleFlowInfo = new SampleFlowInfo();
			sampleFlowInfo.setId(businessSubSampleDO.getId());
			sampleFlowInfo.setSampleName(businessSubSampleDO.getSampleName());
			sampleFlowInfo.setSampleCode(businessSubSampleDO.getSampleCode());
			sampleFlowInfo.setIsWeighing(businessSubSampleDO.getIsWeighing());
			sampleFlowInfo.setSampleWeight(businessSubSampleDO.getLastSampleWeight());
			
			sampleFlowInfoList.add(sampleFlowInfo);
			
			//子样对应的分析班组
			BusinessSubSampleAnalysisGroupDO businessSubSampleAnalysisGroupDO = null;
			
			for (Long configAssayMethodId : configAssayMethodIdList) {
				//查询分析方法
				ConfigAssayMethodDO configAssayMethodDO = configAssayMethodList.stream().filter(f -> f.getId().equals(configAssayMethodId)).findFirst().orElse(null);
				
				//查询子样对应的分析方法
				ConfigSubSampleMethodDO configSubSampleMethodDO = configSubSampleMethodList.stream().filter(f -> f.getIsDefaultUse().equals(QmsCommonConstant.YES) && f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getConfigSubSampleId().equals(configSubSample.getId())).findFirst().orElse(null);
				
				businessSubSampleAnalysisGroupDO = businessSubSampleAnalysisGroupDOList.stream().filter(f -> f.getAssayDepartmentId().equals(configAssayMethodDO.getAssayDepartmentId())).findFirst().orElse(null);
				if (businessSubSampleAnalysisGroupDO == null) {
					businessSubSampleAnalysisGroupDO = new BusinessSubSampleAnalysisGroupDO();
					businessSubSampleAnalysisGroupDO.setId(IdWorker.getId());
					businessSubSampleAnalysisGroupDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					businessSubSampleAnalysisGroupDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessSubSampleAnalysisGroupDO.setBusinessSubSampleId(businessSubSampleDO.getId());
					businessSubSampleAnalysisGroupDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
					businessSubSampleAnalysisGroupDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
					businessSubSampleAnalysisGroupDO.setSampleStatus("待收样");
					
					businessSubSampleAnalysisGroupDOList.add(businessSubSampleAnalysisGroupDO);
				}

				//根据检测方法循环
				BusinessAssayTaskDataDO businessAssayTaskDataDO = null;
				
				//根据任务数判断是平行还是
				String assayType = configSubSampleMethodDO.getTaskCount() > 1 ? "平行" : "单杯";
				
				//根据任务数循环
				for (int i = 0; i < configSubSampleMethodDO.getTaskCount(); i++) {					
					//子样检测任务
					businessAssayTaskDataDO = new BusinessAssayTaskDataDO();
					businessAssayTaskDataDO.setId(IdWorker.getId());
					businessAssayTaskDataDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					businessAssayTaskDataDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessAssayTaskDataDO.setBusinessSubSampleId(businessSubSampleDO.getId());
					businessAssayTaskDataDO.setConfigAssayMethodId(configAssayMethodId);
					businessAssayTaskDataDO.setAssayType(assayType);
					businessAssayTaskDataDO.setTaskType("常规");
					businessAssayTaskDataDO.setConfigSampleFlowId(configSampleFlowSub.getId());
					businessAssayTaskDataDO.setSampleFlowNodeKey(sampleFlowNodeSub.getNodeKey());
					businessAssayTaskDataDO.setSampleFlowNodeTime(currentDateTime);
					businessAssayTaskDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
					businessAssayTaskDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
					
					
					List<ConfigAssayMethodProjectDO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).collect(Collectors.toList());
					
					StringBuilder assayProjectBuilder = new StringBuilder();
					
					for (ConfigAssayMethodProjectDO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
						
						//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
						if (!dictionaryProjectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
							continue;
						}
						
						DictionaryProjectDO dictionaryProject = dictionaryProjectList.stream().filter(f -> f.getId().equals(configAssayMethodProjectDO.getDictionaryProjectId())).findFirst().orElse(null);
						assayProjectBuilder.append(dictionaryProject.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.setIsEnabled(1);
						businessAssayProjectDataDO.setIsNotAssessment(0);
						
						businessAssayProjectDataDOList.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());
							
							
							businessAssayParameterDataDOList.add(businessAssayParameterDataDO);
						}
						
						
						
					}
					
					assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
					businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
					businessAssayTaskDataDOList.add(businessAssayTaskDataDO);
				}

			}
			
		}		
		
		sampleEntrustRegistration.setRegistrationStatus(QmsCommonConstant.SUBMITTED);
		
		sampleEntrustContext.setSampleFlowInfoList(sampleFlowInfoList);
		
		businessSampleEntrustRegistrationMapper.updateById(sampleEntrustRegistration);
		if (CollUtil.isNotEmpty(sampleEntrustDetailList)) {
			businessSampleEntrustDetailMapper.updateBatch(sampleEntrustDetailList);
		}
		if (CollUtil.isNotEmpty(sampleEntrustProjectList)) {
			businessSampleEntrustProjectMapper.updateBatch(sampleEntrustProjectList);
		}
		if (CollUtil.isNotEmpty(sampleAssayResultList)) {
			businessSampleAssayResultMapper.updateBatch(sampleAssayResultList);
		}
		if (CollUtil.isNotEmpty(businessBaseSampleDOList)) {
			businessBaseSampleMapper.insertBatch(businessBaseSampleDOList);
		}
		if (CollUtil.isNotEmpty(businessSubParentSampleDOList)) {
			businessSubParentSampleMapper.insertBatch(businessSubParentSampleDOList);
		}		
		if (CollUtil.isNotEmpty(businessSubSampleDOList)) {
			businessSubSampleMapper.insertBatch(businessSubSampleDOList);
		}
		if (CollUtil.isNotEmpty(businessSubSampleAnalysisGroupDOList)) {
			businessSubSampleAnalysisGroupMapper.insertBatch(businessSubSampleAnalysisGroupDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayTaskDataDOList)) {
			businessAssayTaskDataMapper.insertBatch(businessAssayTaskDataDOList);
		}		
		if (CollUtil.isNotEmpty(businessAssayProjectDataDOList)) {
			businessAssayProjectDataMapper.insertBatch(businessAssayProjectDataDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayParameterDataDOList)) {
			businessAssayParameterDataMapper.insertBatch(businessAssayParameterDataDOList);
		}
	}

}
