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

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

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
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.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskParameterDataDO;
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.BusinessSampleEntrustDepartmentDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDepartmentDetailDO;
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.BusinessSubParentSampleAssessmentDO;
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.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.BusinessAssayTaskParameterDataMapper;
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.BusinessSampleEntrustDepartmentDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustDepartmentMapper;
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.BusinessSubParentSampleAssessmentMapper;
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.BusinessSubSampleAssessmentMapper;
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.controller.vo.ConfigAssayMethodProjectExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodConfAdd;
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.dal.dataobject.BaseSampleDO;
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.dataobject.ConfigBaseSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigEntrustSourceDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleFlowDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleReportDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleDO;
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.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.ConfigBaseSampleMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSampleFlowMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSampleReportMapper;
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.config.dal.mapper.MaterialAssayStandardMethodMapper;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;
import com.zt.plat.module.qms.common.data.dal.dataobject.DataTemplateDO;
import com.zt.plat.module.qms.common.data.dal.mapper.DataTemplateMapper;
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;
import lombok.extern.slf4j.Slf4j;

/**
 * 委检样 是否有来样品位S值, 是： 综合分析室分析配料元素，试金分析室获取分析的配料元素 ；否：不送往综合分析室，配料时采用来样品位S值，如果是必须含有Cu、S的，则试金分析人员称样时，估一个Cu品位进行配料。
 * 
 * 商检分析样 这批样中第一个样品默认产生1个配料任务（综合分析室分析），最终这些配料元素对应到这批商检分析样的每一个试金样品的配料参数上；
 * 
 * 商检综合样 综合样，除了杂质元素，如果要分析Au、Ag，单个样品综合分析室分析配料元素会单独生成1个任务；这个结果匹配到综合样自身的试金任务的配料参数上；
 */
@Slf4j
@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 BusinessSampleEntrustDepartmentMapper businessSampleEntrustDepartmentMapper;
	
	@Resource
	private BusinessSampleEntrustDepartmentDetailMapper businessSampleEntrustDepartmentDetailMapper;
	
	@Resource
	private BusinessSampleEntrustProjectMapper businessSampleEntrustProjectMapper;
	
	@Resource
	private BusinessSampleAssayResultMapper businessSampleAssayResultMapper;
	
	@Resource
	private ConfigBaseSampleMapper configBaseSampleMapper;
	
	@Resource
	private BusinessBaseSampleMapper businessBaseSampleMapper;
	
	@Resource
	private ConfigSubSampleParentMapper configSubSampleParentMapper;
	
	@Resource
	private DataTemplateMapper dataTemplateMapper;
	
	@Resource
	private BusinessSubParentSampleMapper businessSubParentSampleMapper;
	
	@Resource
	private BusinessSubParentSampleAssessmentMapper businessSubParentSampleAssessmentMapper;
	
	@Resource
	private ConfigSubSampleMapper configSubSampleMapper;
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Resource
	private BusinessSubSampleAnalysisGroupMapper businessSubSampleAnalysisGroupMapper;
	
	@Resource
	private ConfigAssayMethodMapper configAssayMethodMapper;
	
	@Resource
	private ConfigSubSampleMethodMapper configSubSampleMethodMapper;//子样分析方法关联配置
	
	@Resource
	private ConfigAssayMethodParameterMapper configAssayMethodParameterMapper;
	
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private BusinessAssayTaskParameterDataMapper businessAssayTaskParameterDataMapper;
	
	@Resource
	private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;
	
	@Resource
	private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;
	
	@Resource
	private BusinessSubSampleAssessmentMapper businessSubSampleAssessmentMapper;
	
	@Resource
	private ConfigAssayMethodProjectParameterMapper configAssayMethodProjectParameterMapper;
	
	@Resource
	private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;
	
	@Resource
	private ConfigSampleFlowMapper configSampleFlowMapper;
	
	@Resource
	private BusinessAssayReportDataMapper businessAssayReportDataMapper;
	
	@Resource
	private ConfigSampleReportMapper configSampleReportMapper;
	
	@Resource
	private MaterialAssayStandardMethodMapper materialAssayStandardMethodMapper;

	@Override
	public void process() throws Exception {
		//获取样品委托上下文
		SampleEntrustContext sampleEntrustContext = this.getContextBean(SampleEntrustContext.class);
		
		//当前登录用户
		String loginRealname = sampleEntrustContext.getLoginRealname();
		
		LocalDateTime currentDateTime = sampleEntrustContext.getCurrentDateTime();
		//样品委托登记
		BusinessSampleEntrustRegistrationDO sampleEntrustRegistration = sampleEntrustContext.getSampleEntrustRegistration();
		//扩展信息
		String sampleEntrustRegistrationExtInfo = sampleEntrustRegistration.getExternalInfomation();
		JSONObject sampleEntrustRegistrationJsonObject = new JSONObject();
		if (StringUtils.isNotBlank(sampleEntrustRegistrationExtInfo)) {
			sampleEntrustRegistrationJsonObject = JSON.parseObject(sampleEntrustRegistrationExtInfo);
		}
		
		//样品委托登记明细
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailList = sampleEntrustContext.getSampleEntrustDetailList();
		//样品检测项目
		List<BusinessSampleEntrustProjectDO> sampleEntrustProjectList = sampleEntrustContext.getSampleEntrustProjectList();
		//样品来样品位
		List<BusinessSampleAssayResultDO> sampleAssayResultList = sampleEntrustContext.getSampleAssayResultList();
		
		List<Long> baseSampleIdList = sampleEntrustDetailList.stream().map(m -> m.getBaseSampleId()).distinct().collect(Collectors.toList());
		
		//样品大类列表
		List<BaseSampleDO> baseSampleList = sampleEntrustContext.getBaseSampleList();
		
		//主样配置
		List<ConfigBaseSampleDO> configBaseSampleList = configBaseSampleMapper.selectByBaseSampleIds(baseSampleIdList);
		List<Long> configBaseSampleIdList = configBaseSampleList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//分样配置
		List<ConfigSubSampleParentDO> configSubSampleParentList = configSubSampleParentMapper.selectByBaseSampleIds(baseSampleIdList);
		//子样配置
		List<ConfigSubSampleDO> configSubSampleList = configSubSampleMapper.selectByBaseSampleIds(baseSampleIdList);
		List<Long> configSubSampleIdList = configSubSampleList.stream().map(m -> m.getId()).collect(Collectors.toList());
		//子样分析方法配置
		List<ConfigSubSampleMethodExtendRespVO> configSubSampleMethodList = configSubSampleMethodMapper.selectByConfigSubSampleIds(configSubSampleIdList);
		List<Long> configAssayMethodIdList = configSubSampleMethodList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
		//分析方法配置
		List<ConfigAssayMethodDO> configAssayMethodList = configAssayMethodMapper.selectByIds(configAssayMethodIdList);
		//分析方法参数配置
		List<ConfigAssayMethodParameterDO> configAssayMethodParameterList = configAssayMethodParameterMapper.selectByConfigAssayMethodIds(configAssayMethodIdList);
		//分析方法检测项目配置
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigAssayMethodIds(configAssayMethodIdList);
		//分析方法检测项目参数配置
		List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList = configAssayMethodProjectParameterMapper.selectByConfigAssayMethodIds(configAssayMethodIdList);
		//检测项目字典
		List<DictionaryProjectDO> dictionaryProjectList = sampleEntrustContext.getDictionaryProjectList();
		
		//主样报表配置
		List<ConfigSampleReportDO> configSampleReportList = configSampleReportMapper.selectByConfigBaseSampleIds(configBaseSampleIdList);
		
		//样品流程配置
		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<BusinessSubParentSampleAssessmentDO> businessSubParentSampleAssessmentDOList = new ArrayList<>();
		//分样子样
		List<BusinessSubSampleDO> businessSubSampleDOList = new ArrayList<>();
		//分样子样分析班组
		List<BusinessSubSampleAnalysisGroupDO> businessSubSampleAnalysisGroupDOList = new ArrayList<>();
		//分样子样判定数据
		List<BusinessSubSampleAssessmentDO> businessSubSampleAssessmentDOList = new ArrayList<>();
		//子样检测任务
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = new ArrayList<>();
		//子样检测任务参数
		List<BusinessAssayTaskParameterDataDO> businessAssayTaskParameterDataDOList = new ArrayList<>();
		//检测任务的分析项目
		List<BusinessAssayProjectDataDO> businessAssayProjectDataDOList = new ArrayList<>();
		//分析项目对应的分析参数
		List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList = new ArrayList<>();
		//分析报表数据
		List<BusinessAssayReportDataDO> businessAssayReportDataDOList = new ArrayList<>();
		
		//子样流转信息列表
		List<SampleFlowInfo> sampleFlowInfoList = new ArrayList<>();

		//委托明细排序
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailListSort = sampleEntrustDetailList.stream().sorted(Comparator.comparingInt(BusinessSampleEntrustDetailDO::getSort)).collect(Collectors.toList());
		//主样
		BusinessBaseSampleDO businessBaseSampleDO = null;
		int sjfxyOne = -1;//商检分析样第一个是值为0，已经过商检分析样的值为1，默认值为-1；
		for (BusinessSampleEntrustDetailDO businessSampleEntrustDetailDO : sampleEntrustDetailListSort) {//委托样品明细
			
			//样品大类
			BaseSampleDO baseSampleDO = baseSampleList.stream().filter(f -> f.getId().equals(businessSampleEntrustDetailDO.getBaseSampleId())).findFirst().orElse(null);
			
			if (QmsCommonConstant.ENTRUST_INSPECTION_ANALYSIS_SAMPLE.equals(baseSampleDO.getDictionaryBusinessKey())){//如果是商检分析样
				if (sjfxyOne == -1) {
					sjfxyOne = 0;//第一个商检分析样
				} else {
					sjfxyOne = 1;//商检分析样已添加过
				}
			}

			//获取当前样品的检测项目
			List<BusinessSampleEntrustProjectDO> sampleEntrustProjectDOList = sampleEntrustProjectList.stream().filter(f -> businessSampleEntrustDetailDO.getId().equals(f.getBusinessSampleEntrustDetailId())).collect(Collectors.toList());
			
			//获取物料检测标准检测项目id列表
			List<Long> materialAssayStandardDetailIdList = sampleEntrustProjectDOList.stream().map(m -> m.getMaterialAssayStandardDetailId()).collect(Collectors.toList());
			
			//当前样品检测标准方法
			List<MaterialAssayStandardMethodDO> materialAssayStandardMethodList = sampleEntrustContext.getMaterialAssayStandardMethodDefaultByMaterialAssayStandardDetailIds(materialAssayStandardDetailIdList);
			
			//获取检测方法
			List<Long> materialAssayStandardConfigAssayMethodIdList = materialAssayStandardMethodList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
			
			//查找当前委托明细分析的子样配置
			List<Long> currentConfigSubSampleIdList = configSubSampleMethodList.stream().filter(f -> f.getIsDefaultUse().equals(QmsCommonConstant.YES) && materialAssayStandardConfigAssayMethodIdList.contains(f.getConfigAssayMethodId())).map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
			
			//查询当前委托样品对应的子样
			List<ConfigSubSampleDO> entrustConfigSubSampleList = configSubSampleList.stream().filter(f -> currentConfigSubSampleIdList.contains(f.getId()) && f.getBaseSampleId().equals(businessSampleEntrustDetailDO.getBaseSampleId())).distinct().collect(Collectors.toList());
			if (entrustConfigSubSampleList.size() != 1) {
				throw new ServiceException(1_032_100_000, "子样配置不正确");
			}
						
			//获取到子样配置
			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 = businessSampleEntrustDetailDO.getEntrustSampleCode();//默认取来样编号
			if (StringUtils.isNotBlank(businessSampleEntrustDetailDO.getSampleCode())) {//如果已经提前生成，
				sampleCode = businessSampleEntrustDetailDO.getSampleCode();
			} else if (StringUtils.isNotBlank(configBaseSample.getCodeRule())) {//如果配置了样品编号生成规则，则以生成规则为准
				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.setGroupId(businessSampleEntrustDetailDO.getEntrustGroupCode());
			businessSubSampleDO.setBatchCode(businessSampleEntrustDetailDO.getEntrustBatchCode());
			businessSubSampleDO.setDualFlag(businessSampleEntrustDetailDO.getEntrustDualFlag());
			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());
			businessSubSampleDO.setIsMaterialAssayStandard(sampleEntrustRegistration.getIsMaterialAssayStandard());
			businessSubSampleDO.setAssayLevel(businessSampleEntrustDetailDO.getAssayLevel());
			businessSubSampleDO.setRemark(sampleEntrustRegistrationJsonObject.getString("sampleCategory"));//子样备注设置样品类型
			
			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 : materialAssayStandardConfigAssayMethodIdList) {
				//获取分析方法对应的分析标准
				List<MaterialAssayStandardMethodDO> currentMaterialAssayStandardMethodList = materialAssayStandardMethodList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).collect(Collectors.toList());
				//获取分析标准的明细id
				List<Long> currentMaterialAssayStandardDetailIdList = currentMaterialAssayStandardMethodList.stream().map(m -> m.getMaterialAssayStandardDetailId()).collect(Collectors.toList());
				//获取当前分析方法对应的检测项目
				List<Long> dictionaryProjectIdList = sampleEntrustProjectDOList.stream().filter(f -> currentMaterialAssayStandardDetailIdList.contains(f.getMaterialAssayStandardDetailId())).map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
				//获取当前分析方法对应的检测项目显示名称
				List<String> dictionaryProjectShowNameList = dictionaryProjectList.stream().filter(f -> dictionaryProjectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());
				//查询分析方法
				ConfigAssayMethodDO configAssayMethodDO = configAssayMethodList.stream().filter(f -> f.getId().equals(configAssayMethodId)).findFirst().orElse(null);
				
				//查询子样对应的分析方法
				ConfigSubSampleMethodExtendRespVO configSubSampleMethod = configSubSampleMethodList.stream().filter(f -> f.getIsDefaultUse().equals(QmsCommonConstant.YES) && f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getConfigSubSampleId().equals(configSubSample.getId())).findFirst().orElse(null);
				
				if (configSubSampleMethod == null) {
					throw new ServiceException(1_032_100_000, "未找到子样对应的分析方法配置！");
				}
				
				//扩展配置信息
				String configInfomation = configSubSampleMethod.getConfigInfomation();
				List<ConfigSubSampleMethodConfItem> getParamList = null;
				if (StringUtils.isNotBlank(configInfomation)) {
					ConfigSubSampleMethodConfInfo configSubSampleMethodConfInfo = JSON.parseObject(configInfomation, ConfigSubSampleMethodConfInfo.class);
					ConfigSubSampleMethodConfAdd addAssayTask = configSubSampleMethodConfInfo.getAddAssayTask();
					getParamList = configSubSampleMethodConfInfo.getGetParam();
					if (addAssayTask != null) {//需要添加任务
						
						List<Long> conditionProjectIdList = addAssayTask.getConditionProjectIdList();
						Long methodId = addAssayTask.getMethodId();
						List<Long> projectIdList = addAssayTask.getProjectIdList();
						//判定检测项目包含条件中的检测项目
						Boolean isAssayConditionProjectList = sampleEntrustProjectList.stream().anyMatch(f -> conditionProjectIdList.contains(f.getDictionaryProjectId()));
						//是否预报S值
						Boolean isForecastS = false;
						String forecastResult = businessSampleEntrustDetailDO.getForecastResult();
						if (StringUtils.isNotBlank(forecastResult)) {
							JSONArray array = JSON.parseArray(forecastResult);
							for (int i = 0; i < array.size(); i++) {
							    JSONObject item = array.getJSONObject(i);
							    if ("S".equals(item.getString("simpleName"))) {
							    	isForecastS = true;
							    	break;
								}
							}
						}
						
						//如果是商检样分析，且为第一个商检分析样，并且包含要检测的项目，
						if (QmsCommonConstant.ENTRUST_INSPECTION_ANALYSIS_SAMPLE.equals(baseSampleDO.getDictionaryBusinessKey()) && isAssayConditionProjectList && sjfxyOne == 0) {
							addAssayTask(currentDateTime, sampleEntrustRegistrationJsonObject, configSubSampleMethodList, configAssayMethodProjectList,
									configAssayMethodProjectParameterList, dictionaryProjectList,
									businessSubParentSampleAssessmentDOList, businessSubSampleAnalysisGroupDOList,
									businessSubSampleAssessmentDOList, businessAssayTaskDataDOList,
									businessAssayProjectDataDOList, businessAssayParameterDataDOList, configSubSample,
									configSampleFlowSub, sampleFlowNodeSub, businessSubSampleDO, methodId,
									projectIdList, configAssayMethodParameterList, businessAssayTaskParameterDataDOList);
						} else if (QmsCommonConstant.ENTRUST_COMPREHENSIVE_INSPECTION_SAMPLE.equals(baseSampleDO.getDictionaryBusinessKey()) && isAssayConditionProjectList) {//如果是商检综合样，并且包含需检查的项目
							addAssayTask(currentDateTime, sampleEntrustRegistrationJsonObject, configSubSampleMethodList, configAssayMethodProjectList,
									configAssayMethodProjectParameterList, dictionaryProjectList,
									businessSubParentSampleAssessmentDOList, businessSubSampleAnalysisGroupDOList,
									businessSubSampleAssessmentDOList, businessAssayTaskDataDOList,
									businessAssayProjectDataDOList, businessAssayParameterDataDOList, configSubSample,
									configSampleFlowSub, sampleFlowNodeSub, businessSubSampleDO, methodId,
									projectIdList, configAssayMethodParameterList, businessAssayTaskParameterDataDOList);
							
						} else if (QmsCommonConstant.ENTRUST_COMMISSION_INSPECTION_SAMPLE.equals(baseSampleDO.getDictionaryBusinessKey()) && isAssayConditionProjectList && !isForecastS ) {//如果是委检样，包含检测项目，未预报S值
							addAssayTask(currentDateTime, sampleEntrustRegistrationJsonObject, configSubSampleMethodList, configAssayMethodProjectList,
									configAssayMethodProjectParameterList, dictionaryProjectList,
									businessSubParentSampleAssessmentDOList, businessSubSampleAnalysisGroupDOList,
									businessSubSampleAssessmentDOList, businessAssayTaskDataDOList,
									businessAssayProjectDataDOList, businessAssayParameterDataDOList, configSubSample,
									configSampleFlowSub, sampleFlowNodeSub, businessSubSampleDO, methodId,
									projectIdList, configAssayMethodParameterList, businessAssayTaskParameterDataDOList);
						}
						
					}
					
				}
				//分析班组
				businessSubSampleAnalysisGroupDO = businessSubSampleAnalysisGroupDOList.stream().filter(f -> f.getBusinessSubSampleId().equals(businessSubSampleDO.getId()) && 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);
				}
				
				//根据任务数判断是平行还是
				String assayType = configSubSampleMethod.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
				
				//分样判定数据
				BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubSampleDO.getBusinessSubParentSampleId()) && f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getAssayType().equals(assayType)).findFirst().orElse(null);//防止重复添加，先从列表过滤
				if (businessSubParentSampleAssessmentDO == null) {
					businessSubParentSampleAssessmentDO = new BusinessSubParentSampleAssessmentDO();
					businessSubParentSampleAssessmentDO.setId(IdWorker.getId());
					businessSubParentSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessSubParentSampleAssessmentDO.setConfigAssayMethodId(configAssayMethodId);
					businessSubParentSampleAssessmentDO.setAssayType(assayType);
					businessSubParentSampleAssessmentDO.setTaskType("常规");
					businessSubParentSampleAssessmentDOList.add(businessSubParentSampleAssessmentDO);
				}
				
				//子样判定数据
				BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubSampleId().equals(businessSubSampleDO.getId()) && f.getConfigAssayMethodId().equals(configAssayMethodId) && f.getAssayType().equals(assayType)).findFirst().orElse(null);//防止重复添加，先从列表过滤
				if (businessSubSampleAssessmentDO == null) {
					businessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();
					businessSubSampleAssessmentDO.setId(IdWorker.getId());	
					businessSubSampleAssessmentDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					businessSubSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleDO.getId());
					businessSubSampleAssessmentDO.setBusinessSubParentSampleAssessmentId(businessSubParentSampleAssessmentDO.getId());
					businessSubSampleAssessmentDO.setConfigAssayMethodId(configAssayMethodId);
					businessSubSampleAssessmentDO.setAssayType(assayType);
					businessSubSampleAssessmentDO.setTaskType("常规");
					
					businessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
				}

				//根据任务数循环
				for (int i = 0; i < configSubSampleMethod.getTaskCount(); i++) {
					Long businessSubSampleId = businessSubSampleDO.getId();
					Long businessSubSampleAssessmentId = businessSubSampleAssessmentDO.getId();
					BusinessAssayTaskDataDO businessAssayTaskDataDO = businessAssayTaskDataDOList.stream().filter(f -> {
						String assayProject = f.getAssayProject();
						if (StringUtils.isNotBlank(assayProject)) {
							List<String> assayProjectList = Arrays.asList(assayProject.split(","));
							return !assayProjectList.containsAll(dictionaryProjectShowNameList) 
									&& configAssayMethodId.equals(f.getConfigAssayMethodId())
									&& businessSubSampleId.equals(f.getBusinessSubSampleId()) 
									&& businessSubSampleAssessmentId.equals(f.getBusinessSubSampleAssessmentId()) ;//分析项目未完全存在，则返回
						}
						return false;
					}).findFirst().orElse(null);
					
					if (businessAssayTaskDataDO == null) {
						//子样检测任务
						businessAssayTaskDataDO = new BusinessAssayTaskDataDO();
						businessAssayTaskDataDO.setId(IdWorker.getId());
						businessAssayTaskDataDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
						businessAssayTaskDataDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
						businessAssayTaskDataDO.setBusinessSubSampleId(businessSubSampleDO.getId());
						businessAssayTaskDataDO.setBusinessSubSampleAssessmentId(businessSubSampleAssessmentDO.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<ConfigAssayMethodParameterDO> configAssayMethodParameterDOList = configAssayMethodParameterList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).collect(Collectors.toList());
						BusinessAssayTaskParameterDataDO businessAssayTaskParameterDataDO = null;
						for (ConfigAssayMethodParameterDO configAssayMethodParameterDO : configAssayMethodParameterDOList) {
							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 (StringUtils.isNotBlank(sampleEntrustRegistrationJsonObject.getString("sampleCategory")) && "remark".equals(configAssayMethodParameterDO.getKey())) {
								businessAssayTaskParameterDataDO.setValue(sampleEntrustRegistrationJsonObject.getString("sampleCategory"));//任务参数备注赋值样品类型
							}

							//来样品位赋值
							if (getParamList != null) {
								List<String> targetAttributeList = getParamList.stream().filter(f -> StringUtils.isNotBlank(f.getTarget().getAttribute()) && f.getTarget().getAttribute().contains(configAssayMethodParameterDO.getKey())).map(m -> m.getTarget().getAttribute()).collect(Collectors.toList());
								String forecastResult = businessSampleEntrustDetailDO.getForecastResult();
								if (StringUtils.isNotBlank(forecastResult)) {
									JSONArray array = JSON.parseArray(forecastResult);
									for (int forecIndex = 0; forecIndex < array.size(); forecIndex++) {
									    JSONObject item = array.getJSONObject(forecIndex);
									    if (targetAttributeList.contains(item.getString("simpleName")) && StringUtils.isNotBlank(configAssayMethodParameterDO.getKey()) && configAssayMethodParameterDO.getKey().equals(item.getString("simpleName"))) {
									    	businessAssayTaskParameterDataDO.setValue(item.getString("value"));
									    	break;
										}
									}
								}
							}
							
							businessAssayTaskParameterDataDOList.add(businessAssayTaskParameterDataDO);
						}
						
						//子样检测任务添加
						businessAssayTaskDataDOList.add(businessAssayTaskDataDO);
					}
					

					//子样检测任务检测项目
					List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(configAssayMethodId)).collect(Collectors.toList());
					
//					StringBuilder assayProjectBuilder = new StringBuilder();
					
					for (ConfigAssayMethodProjectExtendRespVO 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(",");
						

						//检测项目
						Long businessAssayTaskDataId = businessAssayTaskDataDO.getId();
						BusinessAssayProjectDataDO businessAssayProjectDataDO = businessAssayProjectDataDOList.stream().filter(f -> f.getBusinessAssayTaskDataId().equals(businessAssayTaskDataId) && f.getDictionaryProjectId().equals(configAssayMethodProjectDO.getDictionaryProjectId())).findFirst().orElse(null);
						if (businessAssayProjectDataDO == null) {
							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(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());
								if (StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getDefaultValue())) {
									businessAssayParameterDataDO.setValue(configAssayMethodProjectParameterDO.getDefaultValue());
								}

								if (StringUtils.isNotBlank(sampleEntrustRegistrationJsonObject.getString("sampleCategory")) && "remark".equals(configAssayMethodProjectParameterDO.getKey())) {
									businessAssayParameterDataDO.setValue(sampleEntrustRegistrationJsonObject.getString("sampleCategory"));//任务参数备注赋值样品类型
								}

								//来样品位赋值
								if (getParamList != null) {
									List<String> targetParameterList = getParamList.stream().filter(f -> StringUtils.isNotBlank(f.getTarget().getProject()) && f.getTarget().getProject().contains(configAssayMethodProjectDO.getSimpleName())).map(m -> m.getTarget().getParameter()).collect(Collectors.toList());
									String forecastResult = businessSampleEntrustDetailDO.getForecastResult();
									if (StringUtils.isNotBlank(forecastResult)) {
										JSONArray array = JSON.parseArray(forecastResult);
										for (int forecIndex = 0; forecIndex < array.size(); forecIndex++) {
										    JSONObject item = array.getJSONObject(forecIndex);
										    if (targetParameterList.contains(item.getString("simpleName")) && StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getKey()) && configAssayMethodProjectParameterDO.getKey().equals(item.getString("simpleName"))) {
										    	businessAssayParameterDataDO.setValue(item.getString("value"));
										    	break;
											}
										}
									}
								}

								businessAssayParameterDataDOList.add(businessAssayParameterDataDO);
							}
						} else {
							if (QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT.equals(businessAssayProjectDataDO.getUsage())) {
								businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT);
							}

							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());
								}

								//来样品位赋值
								if (getParamList != null) {
									List<String> targetParameterList = getParamList.stream().filter(f -> StringUtils.isNotBlank(f.getTarget().getProject()) && f.getTarget().getProject().contains(configAssayMethodProjectDO.getSimpleName())).map(m -> m.getTarget().getParameter()).collect(Collectors.toList());
									String forecastResult = businessSampleEntrustDetailDO.getForecastResult();
									if (StringUtils.isNotBlank(forecastResult)) {
										JSONArray array = JSON.parseArray(forecastResult);
										for (int forecIndex = 0; forecIndex < array.size(); forecIndex++) {
										    JSONObject item = array.getJSONObject(forecIndex);
										    if (targetParameterList.contains(item.getString("simpleName")) && StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getKey()) && configAssayMethodProjectParameterDO.getKey().equals(item.getString("simpleName"))) {
										    	businessAssayParameterDataDO.setValue(item.getString("value"));
										    	break;
											}
										}
									}
								}
								
								businessAssayParameterDataDOList.add(businessAssayParameterDataDO);
							}
						}
						
					}

//					if (assayProjectBuilder.length() > 1) {
//						assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
//					}
//					businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());

					Long currentBusinessAssayTaskDataId = businessAssayTaskDataDO.getId();
					List<Long> currentDictionaryProjectIdList = businessAssayProjectDataDOList.stream().filter(f -> f.getBusinessAssayTaskDataId().equals(currentBusinessAssayTaskDataId)).map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
					List<String> currentDictionaryProjectShowNameList = dictionaryProjectList.stream().filter(f -> currentDictionaryProjectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());
					businessAssayTaskDataDO.setAssayProject(CollUtil.join(currentDictionaryProjectShowNameList, ","));
				}

			}

			//=======================报表==========================
			List<ConfigSampleReportDO> configSampleReportDOList = configSampleReportList.stream().filter(f -> f.getConfigBaseSampleId().equals(configBaseSampleId)).collect(Collectors.toList());
			List<Long> currDataSourceConfigAssayMethodIdList = businessSubParentSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubSampleDO.getBusinessSubParentSampleId())).map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
			for (ConfigSampleReportDO configSampleReport : configSampleReportDOList) {
				BusinessAssayReportDataDO businessAssayReportDataDO = new BusinessAssayReportDataDO();
				businessAssayReportDataDO.setBusinessBaseSampleId(businessBaseSampleDO.getId());
//				if (StringUtils.isNotBlank(configSampleReport.getDataSource())){
//					businessAssayReportDataDO.setDataSource(configSampleReport.getDataSource());
//				} else {
					businessAssayReportDataDO.setDataSource(CollUtil.join(currDataSourceConfigAssayMethodIdList, ","));
//				}
				businessAssayReportDataDO.setConfigReportTypeId(configSampleReport.getConfigReportTypeId());
				businessAssayReportDataDO.setConfigSampleReportId(configSampleReport.getId());
				businessAssayReportDataDO.setSampleCode(businessBaseSampleDO.getSampleCode());
				
				businessAssayReportDataDOList.add(businessAssayReportDataDO);
			}
		}
		
		//处理委托登记下的分析部门及部门明细数据
//		ConfigEntrustSourceDO configEntrustSource = sampleEntrustContext.getConfigEntrustSourceById(sampleEntrustRegistration.getConfigEntrustSourceId());
		ConfigEntrustSourceDO configEntrustSource = sampleEntrustContext.getConfigEntrustSource();
		if (QmsCommonConstant.YES.equals(configEntrustSource.getIsGenerateDocument())) {//根据配置，只有配置了需要编制部门报告的数据才生成
			List<BusinessSampleEntrustDepartmentDO> businessSampleEntrustDepartmentDOList = new ArrayList<>();
			List<BusinessSampleEntrustDepartmentDetailDO> businessSampleEntrustDepartmentDetailDOList = new ArrayList<>();
			
			//查询表单设计器模板
			DataTemplateDO dataTemplateDO = dataTemplateMapper.selectLastByKey(configEntrustSource.getDataTemplateKey());
			
			Map<Long, List<BusinessAssayTaskDataDO>> businessAssayTaskDataAssayDepartmentMap = businessAssayTaskDataDOList.stream().collect(Collectors.groupingBy(BusinessAssayTaskDataDO::getAssayDepartmentId));
			for (Map.Entry<Long, List<BusinessAssayTaskDataDO>> entry : businessAssayTaskDataAssayDepartmentMap.entrySet()) {
				Long assayDepartmentId = entry.getKey();
				List<BusinessAssayTaskDataDO> businessAssayTaskDataAssayDepartmentList = entry.getValue();
				
				//获取当前分析任务的id
				List<Long> businessAssayTaskDataAssayDepartmentIdList = businessAssayTaskDataAssayDepartmentList.stream().map(m -> m.getId()).collect(Collectors.toList());
				
				//统计检测项目是上报的或者配料上报的数量
				long usageCount = businessAssayProjectDataDOList.stream().filter(f -> businessAssayTaskDataAssayDepartmentIdList.contains(f.getBusinessAssayTaskDataId()) && (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(f.getUsage()) || QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT.equals(f.getUsage()))).count();
				if (usageCount <= 0) {//如果没有上报检测项目，则跳出当前循环
					continue;
				}
				//需要上报的检测项目
				List<Long> usageReportDicProjectIdList = businessAssayProjectDataDOList.stream().filter(f -> businessAssayTaskDataAssayDepartmentIdList.contains(f.getBusinessAssayTaskDataId()) && (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(f.getUsage()) || QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT.equals(f.getUsage()))).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
				List<String> usageReportProjectNameList = dictionaryProjectList.stream().filter(f -> usageReportDicProjectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());
				
				BusinessSampleEntrustDepartmentDO businessSampleEntrustDepartmentDO = new BusinessSampleEntrustDepartmentDO();
				businessSampleEntrustDepartmentDO.setId(IdWorker.getId());
				businessSampleEntrustDepartmentDO.setBusinessSampleEntrustRegistrationId(sampleEntrustRegistration.getId());
				businessSampleEntrustDepartmentDO.setAssayDepartmentId(assayDepartmentId);
				businessSampleEntrustDepartmentDO.setAssayDepartmentName(businessAssayTaskDataAssayDepartmentList.get(0).getAssayDepartmentName());
				businessSampleEntrustDepartmentDO.setAssayStatus(QmsCommonConstant.UNCHECKED);
				businessSampleEntrustDepartmentDO.setDataTemplateId(dataTemplateDO.getId());
				businessSampleEntrustDepartmentDO.setDataTemplateKey(dataTemplateDO.getDataKey());
				
				List<String> assayProjectList = businessAssayTaskDataAssayDepartmentList.stream().flatMap(m -> {
					// 按逗号分割
	                String[] parts = m.getAssayProject().split(",");
	                
	                // 转为 Stream 并处理空格 (trim)，同时过滤掉分割后可能产生的空字符串
	                return Arrays.stream(parts)
	                    .map(String::trim)          // 去除每个ID前后的空格
	                    .filter(s -> !s.isEmpty()); // 过滤掉空项（例如 "101,,102" 中间的空项）
				}).filter(f -> usageReportProjectNameList.contains(f)).distinct().collect(Collectors.toList());
				businessSampleEntrustDepartmentDO.setAssayProject(CollUtil.join(assayProjectList, ","));

				List<Long> configAssayMethodIdAssayDepartmentIdList = businessAssayTaskDataAssayDepartmentList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
				List<String> assayProjectUnitList = new ArrayList<>();
				for (String assayProject : assayProjectList) {
					ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectExtendRespVO = configAssayMethodProjectList.stream().filter(f -> configAssayMethodIdAssayDepartmentIdList.contains(f.getConfigAssayMethodId()) && assayProject.equals(f.getShowName())).findFirst().orElse(null);
					assayProjectUnitList.add(configAssayMethodProjectExtendRespVO.getSimpleName() + "("+configAssayMethodProjectExtendRespVO.getDictionaryProjectUnit()+")");
				}
				businessSampleEntrustDepartmentDO.setAssayProjectUnit(CollUtil.join(assayProjectUnitList, ","));//这里为方便后续生成报告
				
				
				List<Long> businessBaseSampleIdList = businessAssayTaskDataAssayDepartmentList.stream().map(m -> m.getBusinessBaseSampleId()).distinct().collect(Collectors.toList());
				for (Long businessBaseSampleId : businessBaseSampleIdList) {
					//获取当前主样的分析任务id
					List<Long> currBaseBusinessAssayTaskDataAssayDepartmentIdList = businessAssayTaskDataAssayDepartmentList.stream().filter(f -> f.getBusinessBaseSampleId().equals(businessBaseSampleId)).map(m -> m.getId()).collect(Collectors.toList());
					//统计检测项目是上报的或者配料上报的数量
					usageCount = businessAssayProjectDataDOList.stream().filter(f -> currBaseBusinessAssayTaskDataAssayDepartmentIdList.contains(f.getBusinessAssayTaskDataId()) && (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(f.getUsage()) || QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT.equals(f.getUsage()))).count();
					if (usageCount <= 0) {//如果没有上报检测项目，则跳出当前循环
						continue;
					}
					List<Long> currBaseUsageReportDicProjectIdList = businessAssayProjectDataDOList.stream().filter(f -> currBaseBusinessAssayTaskDataAssayDepartmentIdList.contains(f.getBusinessAssayTaskDataId()) && (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(f.getUsage()) || QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT.equals(f.getUsage()))).map(m -> m.getDictionaryProjectId()).distinct().collect(Collectors.toList());
					List<String> currBaseUsageReportProjectNameList = dictionaryProjectList.stream().filter(f -> currBaseUsageReportDicProjectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());
					
					BusinessSampleEntrustDetailDO businessSampleEntrustDetailDO = sampleEntrustDetailList.stream().filter(f -> f.getBusinessBaseSampleId().equals(businessBaseSampleId)).findFirst().orElse(null);
					
					BusinessSampleEntrustDepartmentDetailDO businessSampleEntrustDepartmentDetailDO = new BusinessSampleEntrustDepartmentDetailDO();
					businessSampleEntrustDepartmentDetailDO.setBusinessSampleEntrustDepartmentId(businessSampleEntrustDepartmentDO.getId());
					businessSampleEntrustDepartmentDetailDO.setBusinessSampleEntrustRegistrationId(businessSampleEntrustDetailDO.getBusinessSampleEntrustRegistrationId());
					businessSampleEntrustDepartmentDetailDO.setBusinessBaseSampleId(businessBaseSampleId);
					businessSampleEntrustDepartmentDetailDO.setBaseSampleId(businessSampleEntrustDetailDO.getBaseSampleId());
					businessSampleEntrustDepartmentDetailDO.setSampleCode(businessSampleEntrustDetailDO.getSampleCode());
					businessSampleEntrustDepartmentDetailDO.setSampleName(businessSampleEntrustDetailDO.getSampleName());
					businessSampleEntrustDepartmentDetailDO.setEntrustSampleCode(businessSampleEntrustDetailDO.getEntrustSampleCode());
					businessSampleEntrustDepartmentDetailDO.setEntrustSampleName(businessSampleEntrustDetailDO.getEntrustSampleName());
					businessSampleEntrustDepartmentDetailDO.setSampleTypeDictionaryBusinessId(businessSampleEntrustDetailDO.getSampleTypeDictionaryBusinessId());
					businessSampleEntrustDepartmentDetailDO.setSort(businessSampleEntrustDetailDO.getSort());

					List<String> sampleAssayProjectList = businessAssayTaskDataAssayDepartmentList.stream().filter(f -> f.getBusinessBaseSampleId().equals(businessBaseSampleId)).flatMap(m -> {
						// 按逗号分割
		                String[] parts = m.getAssayProject().split(",");
		                
		                // 转为 Stream 并处理空格 (trim)，同时过滤掉分割后可能产生的空字符串
		                return Arrays.stream(parts)
		                    .map(String::trim)          // 去除每个ID前后的空格
		                    .filter(s -> !s.isEmpty()); // 过滤掉空项（例如 "101,,102" 中间的空项）
					}).filter(f -> currBaseUsageReportProjectNameList.contains(f)).distinct().collect(Collectors.toList());
					businessSampleEntrustDepartmentDetailDO.setAssayProject(CollUtil.join(sampleAssayProjectList, ","));
					
					
					businessSampleEntrustDepartmentDetailDOList.add(businessSampleEntrustDepartmentDetailDO);
				}
				
				
				businessSampleEntrustDepartmentDOList.add(businessSampleEntrustDepartmentDO);
			}
			
			if (CollUtil.isNotEmpty(businessSampleEntrustDepartmentDOList)) {
				businessSampleEntrustDepartmentMapper.insertBatch(businessSampleEntrustDepartmentDOList);
			}
			if (CollUtil.isNotEmpty(businessSampleEntrustDepartmentDetailDOList)) {
				businessSampleEntrustDepartmentDetailMapper.insertBatch(businessSampleEntrustDepartmentDetailDOList);
			}
		}
		
		
		
		sampleEntrustRegistration.setRegistrationStatus(QmsCommonConstant.SUBMITTED);
		sampleEntrustRegistration.setAssayStatus(QmsCommonConstant.UNCHECKED);
		sampleEntrustRegistration.setDataStatus(QmsCommonConstant.UNRETURNED);
		sampleEntrustRegistration.setTaskQuantity(businessAssayTaskDataDOList.size());
		
		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(businessSubParentSampleAssessmentDOList)) {
			businessSubParentSampleAssessmentMapper.insertBatch(businessSubParentSampleAssessmentDOList);
		}
		if (CollUtil.isNotEmpty(businessSubSampleDOList)) {
			businessSubSampleMapper.insertBatch(businessSubSampleDOList);
		}
		if (CollUtil.isNotEmpty(businessSubSampleAnalysisGroupDOList)) {
			businessSubSampleAnalysisGroupMapper.insertBatch(businessSubSampleAnalysisGroupDOList);
		}
		if (CollUtil.isNotEmpty(businessSubSampleAssessmentDOList)) {
			businessSubSampleAssessmentMapper.insertBatch(businessSubSampleAssessmentDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayTaskDataDOList)) {
			businessAssayTaskDataMapper.insertBatch(businessAssayTaskDataDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayTaskParameterDataDOList)) {
			businessAssayTaskParameterDataMapper.insertBatch(businessAssayTaskParameterDataDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayProjectDataDOList)) {
			businessAssayProjectDataMapper.insertBatch(businessAssayProjectDataDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayParameterDataDOList)) {
			businessAssayParameterDataMapper.insertBatch(businessAssayParameterDataDOList);
		}
		if (CollUtil.isNotEmpty(businessAssayReportDataDOList)) {
			businessAssayReportDataMapper.insertBatch(businessAssayReportDataDOList);
		}
	}

	private void addAssayTask(LocalDateTime currentDateTime, JSONObject sampleEntrustRegistrationJsonObject, List<ConfigSubSampleMethodExtendRespVO> configSubSampleMethodList,
			List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList,
			List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterList,
			List<DictionaryProjectDO> dictionaryProjectList,
			List<BusinessSubParentSampleAssessmentDO> businessSubParentSampleAssessmentDOList,
			List<BusinessSubSampleAnalysisGroupDO> businessSubSampleAnalysisGroupDOList,
			List<BusinessSubSampleAssessmentDO> businessSubSampleAssessmentDOList,
			List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList,
			List<BusinessAssayProjectDataDO> businessAssayProjectDataDOList,
			List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList, ConfigSubSampleDO configSubSample,
			ConfigSampleFlowDO configSampleFlowSub, SampleFlowNode sampleFlowNodeSub,
			BusinessSubSampleDO businessSubSampleDO, Long methodId, List<Long> projectIdList,
			List<ConfigAssayMethodParameterDO> configAssayMethodParameterList,
			List<BusinessAssayTaskParameterDataDO> businessAssayTaskParameterDataDOList) {
		BusinessSubSampleAnalysisGroupDO businessSubSampleAnalysisGroupDO;
		ConfigAssayMethodDO addAssayTaskConfigAssayMethod = configAssayMethodMapper.selectById(methodId);
		Long addAssayTaskAssayDepartmentId = addAssayTaskConfigAssayMethod.getAssayDepartmentId();

		//获取当前分析方法对应的检测项目显示名称
		List<String> dictionaryProjectShowNameList = dictionaryProjectList.stream().filter(f -> projectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());

		//查询子样对应的分析方法
		ConfigSubSampleMethodExtendRespVO addAssayTaskConfigSubSampleMethodDO = configSubSampleMethodList.stream().filter(f -> f.getIsDefaultUse().equals(QmsCommonConstant.YES) && f.getConfigAssayMethodId().equals(methodId) && f.getConfigSubSampleId().equals(configSubSample.getId())).findFirst().orElse(null);
		
		businessSubSampleAnalysisGroupDO = businessSubSampleAnalysisGroupDOList.stream().filter(f -> f.getBusinessSubSampleId().equals(businessSubSampleDO.getId()) && f.getAssayDepartmentId().equals(addAssayTaskAssayDepartmentId)).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(addAssayTaskConfigAssayMethod.getAssayDepartmentId());
			businessSubSampleAnalysisGroupDO.setAssayDepartmentName(addAssayTaskConfigAssayMethod.getAssayDepartmentName());
			businessSubSampleAnalysisGroupDO.setSampleStatus("待收样");
			
			businessSubSampleAnalysisGroupDOList.add(businessSubSampleAnalysisGroupDO);
		}
		
		//根据任务数判断是平行还是
		String assayType = addAssayTaskConfigSubSampleMethodDO.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
		
		//分样判定数据
		BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubSampleDO.getBusinessSubParentSampleId()) && f.getConfigAssayMethodId().equals(addAssayTaskConfigAssayMethod.getId()) && f.getAssayType().equals(assayType)).findFirst().orElse(null);//防止重复添加，先从列表过滤
		if (businessSubParentSampleAssessmentDO == null) {
			businessSubParentSampleAssessmentDO = new BusinessSubParentSampleAssessmentDO();
			businessSubParentSampleAssessmentDO.setId(IdWorker.getId());
			businessSubParentSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
			businessSubParentSampleAssessmentDO.setConfigAssayMethodId(addAssayTaskConfigAssayMethod.getId());
			businessSubParentSampleAssessmentDO.setAssayType(assayType);
			businessSubParentSampleAssessmentDO.setTaskType("常规");
			businessSubParentSampleAssessmentDOList.add(businessSubParentSampleAssessmentDO);
			
		}

		//子样判定数据
		BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubSampleId().equals(businessSubSampleDO.getId()) && f.getConfigAssayMethodId().equals(addAssayTaskConfigAssayMethod.getId()) && f.getAssayType().equals(assayType)).findFirst().orElse(null);//防止重复添加，先从列表过滤
		if (businessSubSampleAssessmentDO == null) {
			businessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();
			businessSubSampleAssessmentDO.setId(IdWorker.getId());	
			businessSubSampleAssessmentDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
			businessSubSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
			businessSubSampleAssessmentDO.setBusinessSubParentSampleAssessmentId(businessSubParentSampleAssessmentDO.getId());
			businessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleDO.getId());
			businessSubSampleAssessmentDO.setConfigAssayMethodId(addAssayTaskConfigAssayMethod.getId());
			businessSubSampleAssessmentDO.setAssayType(assayType);
			businessSubSampleAssessmentDO.setTaskType("常规");
			
			businessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
		}

		//根据任务数循环
		for (int i = 0; i < addAssayTaskConfigSubSampleMethodDO.getTaskCount(); i++) {
			
			//根据检测方法循环
			Long businessSubSampleId = businessSubSampleDO.getId();
			Long businessSubSampleAssessmentId = businessSubSampleAssessmentDO.getId();
			BusinessAssayTaskDataDO businessAssayTaskDataDO = businessAssayTaskDataDOList.stream().filter(f -> {
				String assayProject = f.getAssayProject();
				if (StringUtils.isNotBlank(assayProject)) {
					List<String> assayProjectList = Arrays.asList(assayProject.split(","));
					return !assayProjectList.containsAll(dictionaryProjectShowNameList) 
							&& addAssayTaskConfigAssayMethod.getId().equals(f.getConfigAssayMethodId())
							&& businessSubSampleId.equals(f.getBusinessSubSampleId()) 
							&& businessSubSampleAssessmentId.equals(f.getBusinessSubSampleAssessmentId()) ;//分析项目未完全存在，则返回
				}
				return false;
			}).findFirst().orElse(null);
			
			if (businessAssayTaskDataDO == null) {
				//子样检测任务
				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(addAssayTaskConfigAssayMethod.getId());
				businessAssayTaskDataDO.setAssayType(assayType);
				businessAssayTaskDataDO.setTaskType("常规");
//				businessAssayTaskDataDO.setConfigSampleFlowId(configSampleFlowSub.getId());
//				businessAssayTaskDataDO.setSampleFlowNodeKey(sampleFlowNodeSub.getNodeKey());
//				businessAssayTaskDataDO.setSampleFlowNodeTime(currentDateTime);
				businessAssayTaskDataDO.setAssayDepartmentId(addAssayTaskConfigAssayMethod.getAssayDepartmentId());
				businessAssayTaskDataDO.setAssayDepartmentName(addAssayTaskConfigAssayMethod.getAssayDepartmentName());
				

				//子样检测任务参数
				List<ConfigAssayMethodParameterDO> configAssayMethodParameterDOList = configAssayMethodParameterList.stream().filter(f -> f.getConfigAssayMethodId().equals(methodId)).collect(Collectors.toList());
				BusinessAssayTaskParameterDataDO businessAssayTaskParameterDataDO = null;
				for (ConfigAssayMethodParameterDO configAssayMethodParameterDO : configAssayMethodParameterDOList) {
					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 (StringUtils.isNotBlank(sampleEntrustRegistrationJsonObject.getString("sampleCategory")) && "remark".equals(configAssayMethodParameterDO.getKey())) {
						businessAssayTaskParameterDataDO.setValue(sampleEntrustRegistrationJsonObject.getString("sampleCategory"));//任务参数备注赋值样品类型
					}
					
					businessAssayTaskParameterDataDOList.add(businessAssayTaskParameterDataDO);
				}
				
				
				businessAssayTaskDataDOList.add(businessAssayTaskDataDO);
			}
			
			
			//子样检测任务检测项目
			List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectList.stream().filter(f -> f.getConfigAssayMethodId().equals(methodId)).collect(Collectors.toList());
			
//			StringBuilder assayProjectBuilder = new StringBuilder();
			
			for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO : configAssayMethodProjectDOList) {
				
				//如果当前分析方法的项目不在当前检测项目中，则跳出循环继续
				if (!projectIdList.contains(configAssayMethodProjectDO.getDictionaryProjectId())) {
					continue;
				}
				
//				DictionaryProjectDO dictionaryProject = dictionaryProjectList.stream().filter(f -> f.getId().equals(configAssayMethodProjectDO.getDictionaryProjectId())).findFirst().orElse(null);
//				assayProjectBuilder.append(dictionaryProject.getShowName()).append(",");
				

				//检测项目
				Long businessAssayTaskDataId = businessAssayTaskDataDO.getId();
				BusinessAssayProjectDataDO businessAssayProjectDataDO = businessAssayProjectDataDOList.stream().filter(f -> f.getBusinessAssayTaskDataId().equals(businessAssayTaskDataId) && f.getDictionaryProjectId().equals(configAssayMethodProjectDO.getDictionaryProjectId())).findFirst().orElse(null);
				if (businessAssayProjectDataDO == null) {
					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_INGREDIENT);

					businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
					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());
						if (StringUtils.isNotBlank(configAssayMethodProjectParameterDO.getDefaultValue())) {
							businessAssayParameterDataDO.setValue(configAssayMethodProjectParameterDO.getDefaultValue());
						}

						if (StringUtils.isNotBlank(sampleEntrustRegistrationJsonObject.getString("sampleCategory")) && "remark".equals(configAssayMethodProjectParameterDO.getKey())) {
							businessAssayParameterDataDO.setValue(sampleEntrustRegistrationJsonObject.getString("sampleCategory"));//任务参数备注赋值样品类型
						}
						
						businessAssayParameterDataDOList.add(businessAssayParameterDataDO);
					}
				} else {
					if (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(businessAssayProjectDataDO.getUsage())) {
						businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT);
					}

					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());
						}
						
						
						businessAssayParameterDataDOList.add(businessAssayParameterDataDO);
					}
				}
			}
//			if (assayProjectBuilder.length() > 1) {
//				assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
//			}
			Long currentBusinessAssayTaskDataId = businessAssayTaskDataDO.getId();
			List<Long> currentDictionaryProjectIdList = businessAssayProjectDataDOList.stream().filter(f -> f.getBusinessAssayTaskDataId().equals(currentBusinessAssayTaskDataId)).map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
			List<String> currentDictionaryProjectShowNameList = dictionaryProjectList.stream().filter(f -> currentDictionaryProjectIdList.contains(f.getId())).map(m -> m.getShowName()).collect(Collectors.toList());
			businessAssayTaskDataDO.setAssayProject(CollUtil.join(currentDictionaryProjectShowNameList, ","));
		}
	}


	@Override
	public boolean isAccess() {
		log.debug("标准执行生成样品");
		SampleEntrustContext sampleEntrustContext = this.getContextBean(SampleEntrustContext.class);
		return sampleEntrustContext.getIsUseMaterialAssayStandard();//标准的执行
	}
}
