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

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.alibaba.qlexpress4.Express4Runner;
import com.alibaba.qlexpress4.QLOptions;
import com.alibaba.qlexpress4.QLResult;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.infra.api.businessfile.BusinessFileApi;
import com.zt.plat.module.infra.api.businessfile.dto.BusinessFileSaveReqDTO;
import com.zt.plat.module.infra.api.file.FileApi;
import com.zt.plat.module.infra.api.file.dto.FileCreateReqDTO;
import com.zt.plat.module.infra.api.file.dto.FileRespDTO;
import com.zt.plat.module.qms.business.bus.controller.vo.BatchResultDataReportingReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BatchResultDataReportingReqVO.BusinessSubParentSampleIdAndRetestCount;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayProjectDataExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleAssessmentProjectExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleAssessmentGroupReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleAssessmentGroupRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAssessmentProjectExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.ModifySampleResultReportingReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.NoReportSubParentSampleAssessmentRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.RecheckSubSampleParentCreateReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.RecheckSubSampleParentMethodRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.ReportFieldValueData;
import com.zt.plat.module.qms.business.bus.controller.vo.ReportedDataSource;
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.BusinessBaseSampleDO;
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.BusinessSampleEntrustFileDO;
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.BusinessSubParentSampleAssessmentProjectDO;
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.BusinessSubSampleAssessmentProjectDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleParentRetestDO;
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.BusinessBaseSampleMapper;
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.BusinessSampleEntrustFileMapper;
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.BusinessSubParentSampleAssessmentProjectMapper;
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.BusinessSubSampleAssessmentProjectMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleParentRetestMapper;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowInfo;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowParam;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigAssayMethodProjectExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigAssayMethodRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigProjectExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleReportExtendRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleReportReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodExtendRespVO;
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.ConfigEntrustSourceDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportFieldDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportTemplateDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleMethodDO;
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.ConfigEntrustSourceMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigProjectMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigReportFieldMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSampleReportMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleMethodMapper;
import com.zt.plat.module.qms.business.config.service.ConfigReportTemplateService;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import com.zt.plat.module.qms.framework.gridreport.GridReport;
import com.zt.plat.module.qms.framework.gridreport.PdfHelper;
import com.zt.plat.module.qms.mq.SampleEntrustRegistrationAssayCompleteMessage;
import com.zt.plat.module.qms.mq.producer.SampleEntrustRegistrationAssayCompleteProducer;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import gridreport.jni.BinaryObject;
import gridreport.jni.ExportType;
import jakarta.annotation.Resource;

@Service
public class SampleResultReportingServiceImpl implements SampleResultReportingService {

	@Resource
	private GridReport gridReport;
	
    @Resource 
    private FileApi fileApi;
    
    @Resource 
    private BusinessFileApi businessFileApi;
    
	@Resource
	private ConfigReportTemplateService configReportTemplateService;

	@Resource
	private SampleAnalysisAuditService sampleAnalysisAuditService;
	    
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private BusinessSubParentSampleAssessmentMapper businessSubParentSampleAssessmentMapper;
	
	@Resource
	private BusinessSubParentSampleAssessmentProjectMapper businessSubParentSampleAssessmentProjectMapper;
	
	@Resource
	private BusinessSubSampleAssessmentMapper businessSubSampleAssessmentMapper;
	
	@Resource
	private BusinessSubSampleAssessmentProjectMapper businessSubSampleAssessmentProjectMapper;
	
	@Resource
	private BusinessSubParentSampleMapper businessSubParentSampleMapper;
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Resource
	private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;
	
	@Resource
	private SampleEntrustRegistrationAssayCompleteProducer sampleEntrustRegistrationAssayCompleteProducer;

	@Override
	public List<BusinessSubParentSampleAssessmentGroupRespVO> getUnReportMethodGroupList(BusinessSubParentSampleAssessmentGroupReqVO reqVO) {
		List<BusinessSubParentSampleAssessmentGroupRespVO> list = businessSubParentSampleAssessmentProjectMapper.selectUnReportMethodGroupList(reqVO);
		return list;
	}

	@Override
	public JSONObject getSampleResultReportingList(BusinessSubParentSampleAssessmentGroupReqVO reqVO) {
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectMapper.selectByConfigAssayMethodId(reqVO.getConfigAssayMethodId());
		List<Map<String, Object>> columnList = new ArrayList<>();

		//动态字段
		for (ConfigAssayMethodProjectExtendRespVO configAssayMethodProject : configAssayMethodProjectDOList) {
			columnList.add(new HashMap<String, Object>() {
				private static final long serialVersionUID = 2100402322138923549L;

			{
				put("title", configAssayMethodProject.getShowName() + "(" + configAssayMethodProject.getDictionaryProjectUnit() + ")");
				put("field", configAssayMethodProject.getSimpleName());
				put("dataType", configAssayMethodProject.getDataType());
				put("decimalPosition", configAssayMethodProject.getDecimalPosition());
				put("isEdit", true);
			}});
		}

		List<Map<String, Object>> dataList = new ArrayList<>();
		
		//查询结果报送未上报的样品
		List<NoReportSubParentSampleAssessmentRespVO> noReportSubParentSampleAssessmentRespList = businessSubParentSampleAssessmentProjectMapper.selectNoReportSubParentSampleAssessment(reqVO);
		
		if (CollUtil.isEmpty(noReportSubParentSampleAssessmentRespList)) {//没有查询到未上报的数据
			JSONObject json = new JSONObject();
			json.put("columns", columnList);
			json.put("datas", dataList);
			return json;
		}
		
		//分样id列表
		List<Long> businessSubParentSampleIds = noReportSubParentSampleAssessmentRespList.stream().map(m -> m.getBusinessSubParentSampleId()).collect(Collectors.toList());
		//报送的检测项目数据
		List<BusinessSubParentSampleAssessmentProjectExtendRespVO> businessSubParentSampleAssessmentDOList = businessSubParentSampleAssessmentProjectMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIds, reqVO.getConfigAssayMethodId());
		//获取分析任务数
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIds, reqVO.getConfigAssayMethodId());
		List<Long> businessSubSampleIds = businessAssayTaskDataDOList.stream().map(m -> m.getBusinessSubSampleId()).distinct().collect(Collectors.toList());
		//获取子样数据
		List<BusinessSubSampleDO> businessSubSampleDOList = businessSubSampleMapper.selectByIds(businessSubSampleIds);
		
		//获取结果判定数据
		List<BusinessSubSampleAssessmentProjectExtendRespVO> businessSubSampleAssessmentExtendRespVOList = businessSubSampleAssessmentProjectMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIds, reqVO.getConfigAssayMethodId());
		
		//获取复测业务数据
		List<BusinessSubSampleParentRetestDO> businessSubSampleParentRecheckDOList = businessSubSampleParentRetestMapper.selectByRetestBusinessSubParentSampleIdsAndRetestConfigAssayMethodId(businessSubParentSampleIds, reqVO.getConfigAssayMethodId());
		
		//循环未上报的样品
		for (NoReportSubParentSampleAssessmentRespVO noReportSubParentSampleAssessmentRespVO : noReportSubParentSampleAssessmentRespList) {
			Map<String, Object> noReportSubParentSampleAssessmentMap = BeanUtil.beanToMap(noReportSubParentSampleAssessmentRespVO);
			String sampleCode = businessSubSampleDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(noReportSubParentSampleAssessmentRespVO.getBusinessSubParentSampleId())).map(m -> m.getSampleAssayCode()).distinct().collect(Collectors.joining(" | "));
			noReportSubParentSampleAssessmentMap.put("sampleCode", sampleCode);

			//报出结果
			Map<String, Object> subSampleAssessmentBeforeMap = BeanUtil.copyProperties(noReportSubParentSampleAssessmentMap, Map.class);
			Map<String, Object> subSampleAssessmentMap = BeanUtil.copyProperties(noReportSubParentSampleAssessmentMap, Map.class);
			List<BusinessSubParentSampleAssessmentProjectExtendRespVO> curBusinessSubParentSampleAssessmentDOList = businessSubParentSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(noReportSubParentSampleAssessmentRespVO.getBusinessSubParentSampleId()) && f.getRetestCount().equals(noReportSubParentSampleAssessmentRespVO.getRetestCount())).collect(Collectors.toList());
			List<Long> curBusinessSubParentSampleAssessmentDictionaryProjectIdList = curBusinessSubParentSampleAssessmentDOList.stream().map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());
			noReportSubParentSampleAssessmentMap.put("sampleReceiveTime", noReportSubParentSampleAssessmentRespVO.getSampleReceiveTime());
			noReportSubParentSampleAssessmentMap.put("configAssayMethodName", "报出结果");
			StringBuilder businessSubParentSampleAssessmentProjectIds = new StringBuilder();
			for (BusinessSubParentSampleAssessmentProjectExtendRespVO businessSubParentSampleAssessmentProjectDO : curBusinessSubParentSampleAssessmentDOList) {
				String assessmentValue = businessSubParentSampleAssessmentProjectDO.getAssessmentValue();
				if (StringUtils.isNotBlank(businessSubParentSampleAssessmentProjectDO.getSymbol()) && !"=".equals(businessSubParentSampleAssessmentProjectDO.getSymbol())) {//符号不为空，并且不为等于号。结果添加上符号
					assessmentValue = businessSubParentSampleAssessmentProjectDO.getSymbol() + assessmentValue;
				}
				noReportSubParentSampleAssessmentMap.put(businessSubParentSampleAssessmentProjectDO.getSimpleName(), assessmentValue);
				businessSubParentSampleAssessmentProjectIds.append(businessSubParentSampleAssessmentProjectDO.getId()).append(",");
			}
			if (businessSubParentSampleAssessmentProjectIds.length() > 0) {
				businessSubParentSampleAssessmentProjectIds.delete(businessSubParentSampleAssessmentProjectIds.length() -1,  businessSubParentSampleAssessmentProjectIds.length());
			}
			noReportSubParentSampleAssessmentMap.put("businessSubParentSampleAssessmentProjectIds", businessSubParentSampleAssessmentProjectIds.toString());
			noReportSubParentSampleAssessmentMap.put("businessSubParentSampleAssessmentId", noReportSubParentSampleAssessmentRespVO.getBusinessSubParentSampleAssessmentId());
			dataList.add(noReportSubParentSampleAssessmentMap);
			
			//当前判定数据
			List<BusinessSubSampleAssessmentProjectExtendRespVO> businessSubSampleAssessmentList = businessSubSampleAssessmentExtendRespVOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(noReportSubParentSampleAssessmentRespVO.getBusinessSubParentSampleId()) && f.getRetestCount().equals(noReportSubParentSampleAssessmentRespVO.getRetestCount()) ).collect(Collectors.toList());
			StringBuilder businessSubSampleAssessmentIds = new StringBuilder();
			for (BusinessSubSampleAssessmentProjectExtendRespVO businessSubSampleAssessmentProjectExtendRespVO : businessSubSampleAssessmentList) {
				subSampleAssessmentMap.put("sampleReceiveTime", noReportSubParentSampleAssessmentRespVO.getSampleReceiveTime());
				subSampleAssessmentMap.put("configAssayMethodName", businessSubSampleAssessmentProjectExtendRespVO.getConfigAssayMethodName());
				String assessmentValue = businessSubSampleAssessmentProjectExtendRespVO.getAssessmentValue();
				if (StringUtils.isNotBlank(businessSubSampleAssessmentProjectExtendRespVO.getSymbol()) && !"=".equals(businessSubSampleAssessmentProjectExtendRespVO.getSymbol())) {//符号不为空，并且不为等于号。结果添加上符号
					assessmentValue = businessSubSampleAssessmentProjectExtendRespVO.getSymbol() + assessmentValue;
				}
				subSampleAssessmentMap.put(businessSubSampleAssessmentProjectExtendRespVO.getSimpleName(), assessmentValue);
				businessSubSampleAssessmentIds.append(businessSubSampleAssessmentProjectExtendRespVO.getId()).append(",");
			}
			businessSubSampleAssessmentIds.delete(businessSubSampleAssessmentIds.length() -1,  businessSubSampleAssessmentIds.length());
			subSampleAssessmentMap.put("businessSubParentSampleAssessmentProjectIds", businessSubParentSampleAssessmentProjectIds.toString());
			subSampleAssessmentMap.put("businessSubSampleAssessmentIds", businessSubSampleAssessmentIds.toString());

			dataList.add(subSampleAssessmentMap);

			//复测之前的判定数据
			BusinessSubSampleParentRetestDO businessSubSampleParentRecheckDO = businessSubSampleParentRecheckDOList.stream().filter(f -> f.getRetestBusinessSubParentSampleId().equals(noReportSubParentSampleAssessmentRespVO.getBusinessSubParentSampleId()) && f.getRetestCount().equals(noReportSubParentSampleAssessmentRespVO.getRetestCount())).findFirst().orElse(null);
			if (businessSubSampleParentRecheckDO != null) {
				
				List<BusinessSubSampleAssessmentProjectExtendRespVO> businessSubSampleAssessmentExtendBeforeList = businessSubSampleAssessmentProjectMapper.selectByBusinessSubParentSampleIdAndBusinessSubParentSampleAssessmentId(businessSubSampleParentRecheckDO.getBusinessSubParentSampleId(), businessSubSampleParentRecheckDO.getBusinessSubParentSampleAssessmentId());
				StringBuilder businessSubSampleAssessmentIdsBefore = new StringBuilder();
				for (BusinessSubSampleAssessmentProjectExtendRespVO businessSubSampleAssessmentProjectExtendRespVO : businessSubSampleAssessmentExtendBeforeList) {
					if (!curBusinessSubParentSampleAssessmentDictionaryProjectIdList.contains(businessSubSampleAssessmentProjectExtendRespVO.getDictionaryProjectId())) {
						continue;
					}
					subSampleAssessmentBeforeMap.put("sampleReceiveTime", noReportSubParentSampleAssessmentRespVO.getSampleReceiveTime());
					subSampleAssessmentBeforeMap.put("configAssayMethodName", businessSubSampleAssessmentProjectExtendRespVO.getConfigAssayMethodName());
					String assessmentValue = businessSubSampleAssessmentProjectExtendRespVO.getAssessmentValue();
					if (StringUtils.isNotBlank(businessSubSampleAssessmentProjectExtendRespVO.getSymbol()) && !"=".equals(businessSubSampleAssessmentProjectExtendRespVO.getSymbol())) {//符号不为空，并且不为等于号。结果添加上符号
						assessmentValue = businessSubSampleAssessmentProjectExtendRespVO.getSymbol() + assessmentValue;
					}
					subSampleAssessmentBeforeMap.put(businessSubSampleAssessmentProjectExtendRespVO.getSimpleName(), assessmentValue);
					businessSubSampleAssessmentIdsBefore.append(businessSubSampleAssessmentProjectExtendRespVO.getId()).append(",");
				}
				if (businessSubSampleAssessmentIdsBefore.length() > 0) {
					businessSubSampleAssessmentIdsBefore.delete(businessSubSampleAssessmentIdsBefore.length() -1,  businessSubSampleAssessmentIdsBefore.length());
					subSampleAssessmentBeforeMap.put("businessSubParentSampleAssessmentProjectIds", businessSubParentSampleAssessmentProjectIds.toString());
					subSampleAssessmentBeforeMap.put("businessSubSampleAssessmentIds", businessSubSampleAssessmentIdsBefore.toString());

					dataList.add(subSampleAssessmentBeforeMap);
					
				}
			}
			
		}
		
		
		/**
		
		List<Long> businessSubParentSampleIdList = businessSubParentSampleAssessmentDOList.stream().map(m -> m.getBusinessSubParentSampleId()).distinct().collect(Collectors.toList());
		
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(businessSubParentSampleIdList, configAssayMethodId);
		
		
		
		for (BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO : businessSubParentSampleAssessmentDOList) {
			//Map<String, Object> dataMap = new HashMap<>();
			Map<String, Object> dataMap = BeanUtil.beanToMap(businessSubParentSampleAssessmentDO);
			
			
			dataList.add(dataMap);
		}
		**/

		

		JSONObject json = new JSONObject();
		json.put("columns", columnList);
		json.put("datas", dataList);
		return json;
	}


	@Override
	public List<RecheckSubSampleParentMethodRespVO> getRecheckAssayMethodList(Long baseSampleId, Long businessSubParentSampleId, Long configAssayMethodId) {
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		//查询检查项目
		List<RecheckSubSampleParentMethodRespVO> list = businessSubParentSampleAssessmentProjectMapper.getRecheckAssayMethodList(baseSampleId, businessSubParentSampleId, configAssayMethodId, loginUser.getVisitDeptId());
		//过滤默认的
		List<RecheckSubSampleParentMethodRespVO> resultList = list.stream().filter(f -> f.getIsRecheckDefault() != null && f.getIsRecheckDefault().equals(QmsCommonConstant.YES)).collect(Collectors.toList());
		for (RecheckSubSampleParentMethodRespVO recheckSubSampleParentMethodRespVO : resultList) {
			List<ConfigAssayMethodRespVO> methodList = new ArrayList<>();
			//根据检查项目过滤方法
			List<RecheckSubSampleParentMethodRespVO> projectMethodList = list.stream().filter(f -> f.getDictionaryProjectId().equals(recheckSubSampleParentMethodRespVO.getDictionaryProjectId())).collect(Collectors.toList());
			for (RecheckSubSampleParentMethodRespVO recheckSubSampleParentMethod : projectMethodList) {
				ConfigAssayMethodRespVO configAssayMethodRespVO = new ConfigAssayMethodRespVO();
				configAssayMethodRespVO.setId(recheckSubSampleParentMethod.getConfigAssayMethodId());
				configAssayMethodRespVO.setName(recheckSubSampleParentMethod.getConfigAssayMethodName());
				configAssayMethodRespVO.setMethodNameCategory(recheckSubSampleParentMethod.getConfigAssayMethodNameAndCategory());
				methodList.add(configAssayMethodRespVO);
			}
			recheckSubSampleParentMethodRespVO.setMethodList(methodList);
		}
		
		return resultList;
	}
	
	@Resource
	private BaseSampleMapper baseSampleMapper;
	
	@Resource
	private ConfigSubSampleMethodMapper configSubSampleMethodMapper;
	
	@Resource
	private ConfigAssayMethodMapper configAssayMethodMapper;
		
	@Resource
	private ConfigAssayMethodProjectParameterMapper configAssayMethodProjectParameterMapper;
	
	@Resource
	private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;
	
	@Resource
	private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;
	
	@Resource
	private BusinessSubSampleParentRetestMapper businessSubSampleParentRetestMapper;
	

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createRecheckSample(RecheckSubSampleParentCreateReqVO reqVO) {

		List<BusinessSubParentSampleAssessmentDO> newSubParentSampleAssessmentDOList = new ArrayList<>();
		List<BusinessSubSampleAssessmentDO> newSubSampleAssessmentDOList = new ArrayList<>();
		List<BusinessAssayTaskDataDO> newBusinessAssayTaskDataDOList = new ArrayList<>();
		List<BusinessAssayProjectDataDO> newBusinessAssayProjectDataDOList = new ArrayList<>();
		List<BusinessAssayParameterDataDO> newBusinessAssayParameterDataDOList = new ArrayList<>();	
		List<BusinessSubSampleParentRetestDO> newBusinessSubSampleParentRetestDOList = new ArrayList<>();
		
		BusinessSubParentSampleAssessmentDO olBusinessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentMapper.selectById(reqVO.getBusinessSubParentSampleAssessmentId());
		
		//查询分样主样判定
		//BusinessSubParentSampleAssessmentDO businessSubParentSampleAssessmentDO = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(reqVO.getBusinessSubParentSampleId(), reqVO.getConfigAssayMethodId());
		if (olBusinessSubParentSampleAssessmentDO == null) {
			throw new ServiceException(1_032_050_000, "结果报送数据未找到");
		}

		//复检的检测项目
		List<RecheckSubSampleParentMethodRespVO> recheckProjectList = reqVO.getRecheckProjectList();
		List<Long> recheckDictionaryProjectIdList = recheckProjectList.stream().map(m -> m.getDictionaryProjectId()).collect(Collectors.toList());	
		List<BusinessSubParentSampleAssessmentProjectDO> businessSubParentSampleAssessmentProjectDOList = businessSubParentSampleAssessmentProjectMapper.selectByBusinessSubParentSampleAssessmentIdAndDictionaryProjectIds(olBusinessSubParentSampleAssessmentDO.getId(), recheckDictionaryProjectIdList);
		for (BusinessSubParentSampleAssessmentProjectDO businessSubParentSampleAssessmentProjectDO : businessSubParentSampleAssessmentProjectDOList) {
			businessSubParentSampleAssessmentProjectDO.setIsReported(QmsCommonConstant.NO);
			businessSubParentSampleAssessmentProjectDO.setAssessmentStatus(QmsCommonConstant.VOID);
			businessSubParentSampleAssessmentProjectDO.setAssessmentValue(null);
		}
		
		//获取分析任务数
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessSubParentSampleIdAndConfigAssayMethodId(reqVO.getBusinessSubParentSampleId(), reqVO.getConfigAssayMethodId());
		List<Long> businessSubSampleIds = businessAssayTaskDataDOList.stream().map(m -> m.getBusinessSubSampleId()).distinct().collect(Collectors.toList());
		
		//获取子样
		List<BusinessSubSampleDO> businessSubSampleDOList = businessSubSampleMapper.selectByIds(businessSubSampleIds);

		for (BusinessSubSampleDO businessSubSampleDO : businessSubSampleDOList) {
			
			
			//根据方法分组
			Map<Long, List<RecheckSubSampleParentMethodRespVO>> recheckProjectMap = recheckProjectList.stream().collect(Collectors.groupingBy(RecheckSubSampleParentMethodRespVO::getConfigAssayMethodId));

			BusinessAssayTaskDataDO businessAssayTaskDataDO = null;
			for (Map.Entry<Long, List<RecheckSubSampleParentMethodRespVO>> entry : recheckProjectMap.entrySet()) {
				Long configAssayMethodId = entry.getKey();
				List<RecheckSubSampleParentMethodRespVO> projectList = entry.getValue();
				
				
				//查询分析方法
				ConfigAssayMethodDO configAssayMethodDO = configAssayMethodMapper.selectById(configAssayMethodId);
				ConfigSubSampleMethodExtendRespVO configSubSampleMethodDO = configSubSampleMethodMapper.selectByConfigSubSampleIdAndConfigAssayMethodId(businessSubSampleDO.getConfigSubSampleId(), configAssayMethodId);

				List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectList = configAssayMethodProjectMapper.selectByConfigAssayMethodId(configAssayMethodId);
				
				//根据任务数判断是平行还是
				String assayType = configSubSampleMethodDO.getTaskCount() > 1 ? QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL : QmsCommonConstant.ASSAY_TYPE_SINGLE_CUP;
				
				BusinessSubParentSampleAssessmentDO newBusinessSubParentSampleAssessmentDO = newSubParentSampleAssessmentDOList.stream().filter(f -> businessSubSampleDO.getBusinessSubParentSampleId().equals(f.getBusinessSubParentSampleId()) && configAssayMethodId.equals(f.getConfigAssayMethodId()) && "复测".equals(f.getTaskType())).findFirst().orElse(null);
				if (newBusinessSubParentSampleAssessmentDO == null) {
					newBusinessSubParentSampleAssessmentDO = new BusinessSubParentSampleAssessmentDO();
					newBusinessSubParentSampleAssessmentDO.setId(IdWorker.getId());
					newBusinessSubParentSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					newBusinessSubParentSampleAssessmentDO.setConfigAssayMethodId(configAssayMethodId);
					newBusinessSubParentSampleAssessmentDO.setAssayType(assayType);
					newBusinessSubParentSampleAssessmentDO.setTaskType("复测");
					newBusinessSubParentSampleAssessmentDO.setRetestCount(olBusinessSubParentSampleAssessmentDO.getRetestCount() + 1);
					newSubParentSampleAssessmentDOList.add(newBusinessSubParentSampleAssessmentDO);
					

					BusinessSubSampleParentRetestDO businessSubSampleParentRetestDO = new BusinessSubSampleParentRetestDO();
					businessSubSampleParentRetestDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					businessSubSampleParentRetestDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessSubSampleParentRetestDO.setBusinessSubParentSampleAssessmentId(reqVO.getBusinessSubParentSampleAssessmentId());
					businessSubSampleParentRetestDO.setConfigAssayMethodId(reqVO.getConfigAssayMethodId());
					businessSubSampleParentRetestDO.setRetestBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					businessSubSampleParentRetestDO.setRetestBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					businessSubSampleParentRetestDO.setRetestBusinessSubParentSampleAssessmentId(newBusinessSubParentSampleAssessmentDO.getId());
					businessSubSampleParentRetestDO.setRetestCount(newBusinessSubParentSampleAssessmentDO.getRetestCount());
					businessSubSampleParentRetestDO.setRetestConfigAssayMethodId(configAssayMethodId);
					
					newBusinessSubSampleParentRetestDOList.add(businessSubSampleParentRetestDO );
				}
				
				Long newBusinessSubParentSampleAssessmentId = newBusinessSubParentSampleAssessmentDO.getId();
				BusinessSubSampleAssessmentDO newBusinessSubSampleAssessmentDO = newSubSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleAssessmentId().equals(newBusinessSubParentSampleAssessmentId)).findFirst().orElse(null);
				if (newBusinessSubSampleAssessmentDO == null) {
					newBusinessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();
					newBusinessSubSampleAssessmentDO.setId(IdWorker.getId());	
					newBusinessSubSampleAssessmentDO.setBusinessBaseSampleId(businessSubSampleDO.getBusinessBaseSampleId());
					newBusinessSubSampleAssessmentDO.setBusinessSubParentSampleId(businessSubSampleDO.getBusinessSubParentSampleId());
					newBusinessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleDO.getId());
					newBusinessSubSampleAssessmentDO.setBusinessSubParentSampleAssessmentId(newBusinessSubParentSampleAssessmentDO.getId());
					newBusinessSubSampleAssessmentDO.setConfigAssayMethodId(configAssayMethodId);
					newBusinessSubSampleAssessmentDO.setAssayType(assayType);
					newBusinessSubSampleAssessmentDO.setTaskType("复测");
					newBusinessSubSampleAssessmentDO.setRetestCount(newBusinessSubParentSampleAssessmentDO.getRetestCount());
					
					newSubSampleAssessmentDOList.add(newBusinessSubSampleAssessmentDO);
				}
				

				//根据任务数循环
				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.setBusinessSubSampleAssessmentId(newBusinessSubSampleAssessmentDO.getId());
					businessAssayTaskDataDO.setConfigAssayMethodId(configAssayMethodId);
					businessAssayTaskDataDO.setAssayType(assayType);
					businessAssayTaskDataDO.setTaskType("复测");
					businessAssayTaskDataDO.setConfigSampleFlowId(businessSubSampleDO.getConfigSampleFlowId());
					businessAssayTaskDataDO.setSampleFlowNodeKey(QmsCommonConstant.FLOW_NODE_ANALYSIS);
					businessAssayTaskDataDO.setSampleFlowNodeTime(LocalDateTime.now());
					businessAssayTaskDataDO.setAssayDepartmentId(configAssayMethodDO.getAssayDepartmentId());
					businessAssayTaskDataDO.setAssayDepartmentName(configAssayMethodDO.getAssayDepartmentName());
					businessAssayTaskDataDO.setRetestCount(newBusinessSubSampleAssessmentDO.getRetestCount());
					

					StringBuilder assayProjectBuilder = new StringBuilder();
					
					for (RecheckSubSampleParentMethodRespVO recheckSubSampleParentMethodRespVO : projectList) {
						ConfigAssayMethodProjectExtendRespVO configAssayMethodProjectDO = configAssayMethodProjectList.stream().filter(f -> f.getDictionaryProjectId().equals(recheckSubSampleParentMethodRespVO.getDictionaryProjectId())).findFirst().orElse(null);
						
						assayProjectBuilder.append(configAssayMethodProjectDO.getShowName()).append(",");

						//检测项目
						BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
						businessAssayProjectDataDO.setId(IdWorker.getId());
						businessAssayProjectDataDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
						businessAssayProjectDataDO.setConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
						businessAssayProjectDataDO.setDictionaryProjectId(configAssayMethodProjectDO.getDictionaryProjectId());
						businessAssayProjectDataDO.setDataType(configAssayMethodProjectDO.getDataType());
						businessAssayProjectDataDO.setDecimalPosition(configAssayMethodProjectDO.getDecimalPosition());
						businessAssayProjectDataDO.setEffectiveDigit(configAssayMethodProjectDO.getEffectiveDigit());
						businessAssayProjectDataDO.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);

						businessAssayProjectDataDO.setMinimumLimitValue(configAssayMethodProjectDO.getMinimumLimitValue());
						businessAssayProjectDataDO.setIsEnabled(QmsCommonConstant.YES);
						businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
						
						newBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
						
						List<ConfigAssayMethodProjectParameterDO> configAssayMethodProjectParameterDOList = configAssayMethodProjectParameterMapper.selectByConfigAssayMethodProjectId(configAssayMethodProjectDO.getId());
						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());
							
							
							newBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
						}
					}
					
					assayProjectBuilder.delete(assayProjectBuilder.length() - 1 , assayProjectBuilder.length());
					businessAssayTaskDataDO.setAssayProject(assayProjectBuilder.toString());
					newBusinessAssayTaskDataDOList.add(businessAssayTaskDataDO);
				}
				
			}
		}
		
		List<BusinessSubParentSampleAssessmentProjectDO> allBusinessSubParentSampleAssessmentProjectList = businessSubParentSampleAssessmentProjectMapper.selectByBusinessSubParentSampleAssessmentId(olBusinessSubParentSampleAssessmentDO.getId());
		if (allBusinessSubParentSampleAssessmentProjectList.size() == businessSubParentSampleAssessmentProjectDOList.size()) {//如果复测项目都全部完成，则删除旧的结果报送
			businessSubParentSampleAssessmentMapper.deleteById(olBusinessSubParentSampleAssessmentDO.getId());
		}
		
		if (CollUtil.isNotEmpty(businessSubParentSampleAssessmentProjectDOList)) {
			businessSubParentSampleAssessmentProjectMapper.updateBatch(businessSubParentSampleAssessmentProjectDOList);
		}
		
		if (CollUtil.isNotEmpty(newSubParentSampleAssessmentDOList)) {
			businessSubParentSampleAssessmentMapper.insertBatch(newSubParentSampleAssessmentDOList);
		}
		
		if (CollUtil.isNotEmpty(newSubSampleAssessmentDOList)) {
			businessSubSampleAssessmentMapper.insertBatch(newSubSampleAssessmentDOList);
		}
		
		if (CollUtil.isNotEmpty(newBusinessAssayTaskDataDOList)) {
			businessAssayTaskDataMapper.insertBatch(newBusinessAssayTaskDataDOList);
		}
		if (CollUtil.isNotEmpty(newBusinessAssayProjectDataDOList)) {
			businessAssayProjectDataMapper.insertBatch(newBusinessAssayProjectDataDOList);
		}
		if (CollUtil.isNotEmpty(newBusinessAssayParameterDataDOList)) {
			businessAssayParameterDataMapper.insertBatch(newBusinessAssayParameterDataDOList);
		}
		if (CollUtil.isNotEmpty(newBusinessSubSampleParentRetestDOList)) {
			businessSubSampleParentRetestMapper.insertBatch(newBusinessSubSampleParentRetestDOList);
		}
	}
	
	@Resource
	private Express4Runner express4Runner;

	@Resource
	private BusinessAssayReportDataMapper businessAssayReportDataMapper;
	
	@Resource
	private ConfigSampleReportMapper configSampleReportMapper;
	
	@Resource
	private ConfigReportFieldMapper configReportFieldMapper;
	
	@Resource
	private ConfigProjectMapper configProjectMapper;
	
	@Resource
	private BusinessBaseSampleMapper businessBaseSampleMapper;
		
	@Resource
	private BusinessSubSampleAnalysisGroupMapper businessSubSampleAnalysisGroupMapper;
	
	@Resource
	private BusinessSampleEntrustRegistrationMapper businessSampleEntrustRegistrationMapper;
	
	@Resource
	private BusinessSampleEntrustDetailMapper businessSampleEntrustDetailMapper;
	
	@Resource
	private BusinessSampleEntrustFileMapper businessSampleEntrustFileMapper;
	
	@Resource
	private BusinessSampleEntrustDepartmentMapper businessSampleEntrustDepartmentMapper;
	
	@Resource
	private BusinessSampleEntrustDepartmentDetailMapper businessSampleEntrustDepartmentDetailMapper;

	@Resource
	private SampleFlowService sampleFlowService;
	
	@Resource
	private ConfigEntrustSourceMapper configEntrustSourceMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchResultDataReporting(BatchResultDataReportingReqVO reqVO) {

		//当前登录用户昵称
		String nickName = SecurityFrameworkUtils.getLoginUserNickname();
		
		List<BusinessSampleEntrustRegistrationDO> updatebBusinessSampleEntrustRegistrationDOList = new ArrayList<>();
		List<BusinessSampleEntrustDepartmentDO> updateBusinessSampleEntrustDepartmentDOList = new ArrayList<>();
		List<BusinessSampleEntrustDepartmentDetailDO> updateBusinessSampleEntrustDepartmentDetailDOList = new ArrayList<>();
		List<BusinessSampleEntrustFileDO> businessSampleEntrustFileDOList = new ArrayList<>();
		
		List<BusinessSubSampleDO> updateBusinessSubSampleDOList = new ArrayList<>();
		List<BusinessSubSampleAnalysisGroupDO> updateBusinessSubSampleAnalysisGroupDOList = new ArrayList<>();
		
		List<BusinessAssayReportDataDO> saveBusinessAssayReportDataDOList = new ArrayList<>();
		List<BusinessAssayReportDataDO> updateBusinessAssayReportDataDOList = new ArrayList<>();
		
		//分样判定id
		List<Long> businessSubParentSampleAssessmentIdList = new ArrayList<>();
		List<Long> businessSubParentSampleAssessmentProjectIdList = new ArrayList<>();
		
		List<BusinessSubParentSampleAssessmentProjectExtendRespVO> businessSubParentSampleAssessmentExtendList = businessSubParentSampleAssessmentProjectMapper.selectByBusinessSubParentSampleIdsAndConfigAssayMethodId(reqVO.getBusinessSubParentSampleIds(), reqVO.getConfigAssayMethodId());
		
		List<BusinessSubParentSampleDO> businessSubParentSampleDOList = businessSubParentSampleMapper.selectByIds(reqVO.getBusinessSubParentSampleIds());
		
		//循环分样主样数据
		for (BusinessSubParentSampleDO businessSubParentSampleDO : businessSubParentSampleDOList) {
			
			BusinessBaseSampleDO businessBaseSampleDO = businessBaseSampleMapper.selectById(businessSubParentSampleDO.getBusinessBaseSampleId());

			//查询报表
			List<BusinessAssayReportDataDO> businessAssayReportDataDOList = businessAssayReportDataMapper.selectBytBusinessBaseSampleId(businessSubParentSampleDO.getBusinessBaseSampleId());
			
			ConfigSampleReportReqVO configSampleReportSearch = new ConfigSampleReportReqVO();
			configSampleReportSearch.setConfigBaseSampleId(businessBaseSampleDO.getConfigBaseSampleId());
			List<ConfigSampleReportExtendRespVO> configSampleReportList = configSampleReportMapper.selectList(configSampleReportSearch);
			
			List<Long> configReportTypeIdList = configSampleReportList.stream().map(m -> m.getConfigReportTypeId()).collect(Collectors.toList());
			
			//查询动态和计算的报表字段
			List<ConfigReportFieldDO> configReportFieldList = configReportFieldMapper.selectByConfigReportTypeIds(configReportTypeIdList, Arrays.asList(QmsCommonConstant.FIELD_DYNAMIC, QmsCommonConstant.FIELD_CALCULATED));
			//动态报表字段
			List<ConfigReportFieldDO> configReportFieldDynamicList = configReportFieldList.stream().filter(f -> QmsCommonConstant.FIELD_DYNAMIC.equals(f.getFieldType())).collect(Collectors.toList());
			//根据检测方法查询字段配置
			List<ConfigProjectExtendRespVO> configProjectList = configProjectMapper.selectByConfigAssayMethodId(reqVO.getConfigAssayMethodId());
			
			//处理报表数据
			for (ConfigSampleReportExtendRespVO configSampleReport : configSampleReportList) {
				BusinessAssayReportDataDO businessAssayReportDataDO = businessAssayReportDataDOList.stream().filter(f -> f.getConfigReportTypeId().equals(configSampleReport.getConfigReportTypeId()) && f.getConfigSampleReportId().equals(configSampleReport.getId())).findFirst().orElse(null);
				if (businessAssayReportDataDO == null) {
					businessAssayReportDataDO = new BusinessAssayReportDataDO();
					businessAssayReportDataDO.setBusinessBaseSampleId(businessSubParentSampleDO.getBusinessBaseSampleId());
					businessAssayReportDataDO.setConfigReportTypeId(configSampleReport.getConfigReportTypeId());
					businessAssayReportDataDO.setConfigSampleReportId(configSampleReport.getId());
					businessAssayReportDataDO.setSampleCode(businessBaseSampleDO.getSampleCode());
					
					saveBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
				} else {
					updateBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
				}
				Map<String, ReportFieldValueData> assayDataJson = new HashMap<>();
				String assayData = businessAssayReportDataDO.getAssayData();
				if (StringUtils.isNotBlank(assayData)) {
					assayDataJson = JSON.parseObject(assayData, new TypeReference<Map<String, ReportFieldValueData>>() {});
				}
				
				//过滤分样id，根据多个复测次数处理
				List<BusinessSubParentSampleIdAndRetestCount> businessSubParentSampleIdAndRetestCountList = reqVO.getBusinessSubParentSampleIdAndRetestCounts().stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleDO.getId())).collect(Collectors.toList());
				for (BusinessSubParentSampleIdAndRetestCount businessSubParentSampleIdAndRetestCount : businessSubParentSampleIdAndRetestCountList) {

					//循环判定值
					List<BusinessSubParentSampleAssessmentProjectExtendRespVO> businessSubParentSampleAssessmentList = businessSubParentSampleAssessmentExtendList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubParentSampleDO.getId()) && f.getRetestCount().equals(businessSubParentSampleIdAndRetestCount.getRetestCount()) && "normal".equals(f.getAssessmentStatus())).collect(Collectors.toList());
					for (BusinessSubParentSampleAssessmentProjectExtendRespVO businessSubParentSampleAssessment : businessSubParentSampleAssessmentList) {
						businessSubParentSampleAssessmentProjectIdList.add(businessSubParentSampleAssessment.getId());
						businessSubParentSampleAssessmentIdList.add(businessSubParentSampleAssessment.getBusinessSubParentSampleAssessmentId());
						
						Long configAssayMethodProjectId = businessSubParentSampleAssessment.getConfigAssayMethodProjectId();
						List<ConfigProjectExtendRespVO> configProjectFeildList = configProjectList.stream().filter(f -> f.getConfigAssayMethodProjectId() != null && f.getConfigAssayMethodProjectId().equals(configAssayMethodProjectId)).collect(Collectors.toList());
						for (ConfigProjectExtendRespVO configProjectFeild : configProjectFeildList) {
							//查询动态报表字段
							ConfigReportFieldDO configReportField = configReportFieldDynamicList.stream().filter(f -> f.getConfigReportTypeId().equals(configSampleReport.getConfigReportTypeId()) && f.getField().equals(configProjectFeild.getSaveColumn())).findFirst().orElse(null);
							
							if (configReportField == null) {
								continue;
							}
							
							ReportFieldValueData reportFieldValueData = new ReportFieldValueData();
							reportFieldValueData.setFieldName(configReportField.getFieldName());
							reportFieldValueData.setFieldValue(businessSubParentSampleAssessment.getAssessmentValue());
							reportFieldValueData.setDataType(configReportField.getDataType());
							reportFieldValueData.setDecimalPosition(configReportField.getDecimalPosition());
							reportFieldValueData.setMathSymbol(businessSubParentSampleAssessment.getSymbol());
							reportFieldValueData.setUnit(configProjectFeild.getDictionaryProjectUnit());
							reportFieldValueData.setUsage(businessSubParentSampleAssessment.getUsage());
							reportFieldValueData.setMinLimitValue(businessSubParentSampleAssessment.getMinimumLimitValue());
							reportFieldValueData.setMethodName(businessSubParentSampleAssessment.getConfigAssayMethodName());
							assayDataJson.put(configReportField.getField(), reportFieldValueData);
						}				
					}
				}
				
				
				//修改已上报数据来源
				String dataSource = businessAssayReportDataDO.getDataSource();
	            Set<String> dataSources = StringUtils.isBlank(dataSource) ? new HashSet<>() : new HashSet<>(Arrays.asList(dataSource.split(",")));
				String reportedSource = businessAssayReportDataDO.getReportedSource();
				ReportedDataSource reportedDataSource = null;
				if (StringUtils.isNotBlank(reportedSource)) {
					reportedDataSource = JSON.parseObject(reportedSource, ReportedDataSource.class);
				} else {
					reportedDataSource = new ReportedDataSource();
				}
				reportedDataSource.addDataSource(reqVO.getConfigAssayMethodId().toString(), nickName, LocalDateTime.now());
				Set<String> busDataSources = reportedDataSource.getDetails().stream().map(m -> m.getSourceCode()).collect(Collectors.toSet());
				if (busDataSources.size() == dataSources.size() && busDataSources.equals(dataSources)) {
					businessAssayReportDataDO.setIsAllReported(QmsCommonConstant.YES);
				}
				if (busDataSources.size() > 0) {//计算					
					Map<String, Object> calcContext = assayDataJson.entrySet().stream().collect(Collectors.toMap(
			                Map.Entry::getKey,
			                entry -> {
			                	ReportFieldValueData data = entry.getValue();
			                    String fieldValue = data != null ? data.getFieldValue() : null;
			                    if (fieldValue == null || fieldValue.isEmpty()) {
			                        return BigDecimal.ZERO;
			                    }
			                    return new BigDecimal(fieldValue);
			                }
			            ));
					
					//计算报表字段
					List<ConfigReportFieldDO> configReportFieldCalculatedList = configReportFieldList.stream().filter(f -> QmsCommonConstant.FIELD_CALCULATED.equals(f.getFieldType())).collect(Collectors.toList());
					for (ConfigReportFieldDO configReportFieldCalculated : configReportFieldCalculatedList) {
						if (StringUtils.isNotBlank(configReportFieldCalculated.getFormula())) {
							//获取公式中的参数
							Set<String> outVarNames = express4Runner.getOutVarNames(configReportFieldCalculated.getFormula());
							for (String calcField : outVarNames) {
								if (!calcContext.containsKey(calcField)) {
									calcContext.put(calcField, BigDecimal.ZERO);
								}
							}
							
							QLResult expressResult = express4Runner.execute(configReportFieldCalculated.getFormula(), calcContext, QLOptions.builder().precise(true).build());//高精度计算
							BigDecimal calcValue = (BigDecimal) expressResult.getResult();
							
							String mathSymbol = "=";
							List<String> mathSymbolList = assayDataJson.entrySet().stream().filter(f -> outVarNames.contains(f.getKey()) && !"=".equals(((ReportFieldValueData)f.getValue()).getMathSymbol())).map(f -> ((ReportFieldValueData)f.getValue()).getMathSymbol()).distinct().collect(Collectors.toList());
							if (CollUtil.isNotEmpty(mathSymbolList)) {
								mathSymbol = mathSymbolList.get(0);
							}
							String unit = null;
							List<String> unitList = assayDataJson.entrySet().stream().filter(f -> outVarNames.contains(f.getKey())).map(f -> ((ReportFieldValueData)f.getValue()).getUnit()).distinct().collect(Collectors.toList());
							if (CollUtil.isNotEmpty(mathSymbolList)) {
								unit = unitList.get(0);
							}
							
							ReportFieldValueData reportFieldValueData = new ReportFieldValueData();
							reportFieldValueData.setFieldName(configReportFieldCalculated.getFieldName());
							reportFieldValueData.setFieldValue(calcValue.toPlainString());
							reportFieldValueData.setDataType(configReportFieldCalculated.getDataType());
							reportFieldValueData.setDecimalPosition(configReportFieldCalculated.getDecimalPosition());
							reportFieldValueData.setMathSymbol(mathSymbol);
							reportFieldValueData.setUnit(unit);
							reportFieldValueData.setUsage(QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT);
							reportFieldValueData.setMinLimitValue(null);
							reportFieldValueData.setMethodName(null);
							assayDataJson.put(configReportFieldCalculated.getField(), reportFieldValueData);
							
						}
						
					}
					
				}
				businessAssayReportDataDO.setAssayData(JSON.toJSONString(assayDataJson));
				businessAssayReportDataDO.setReportedSource(JSON.toJSONString(reportedDataSource));

			}
		}

		//分析方法信息
		ConfigAssayMethodDO configAssayMethodDO = configAssayMethodMapper.selectById(reqVO.getConfigAssayMethodId());
		//查询当前分析部门下的分样判定信息
		List<BusinessSubParentSampleAssessmentDO> businessSubParentSampleAssessmentDOList = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIdsAndAssayDepartmentId(reqVO.getBusinessSubParentSampleIds(), configAssayMethodDO.getAssayDepartmentId());
		//查询当前分析部门下的子样分析班组信息
		List<BusinessSubSampleAnalysisGroupDO> businessSubSampleAnalysisGroupDOList = businessSubSampleAnalysisGroupMapper.selectByBusinessSubParentSampleIdsAndAssayDepartmentId(reqVO.getBusinessSubParentSampleIds(), configAssayMethodDO.getAssayDepartmentId());
		for (BusinessSubSampleAnalysisGroupDO businessSubSampleAnalysisGroupDO : businessSubSampleAnalysisGroupDOList) {//循环分析班组信息
			//查询当前分样判定上报未上报的，并排除当前(上报方法和复测次数），的数量
			List<Integer> retestCountList = reqVO.getBusinessSubParentSampleIdAndRetestCounts().stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubSampleAnalysisGroupDO.getBusinessSubParentSampleId())).map(m -> m.getRetestCount()).collect(Collectors.toList());
			long count = businessSubParentSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleId().equals(businessSubSampleAnalysisGroupDO.getBusinessSubParentSampleId()) && !( f.getConfigAssayMethodId().equals(reqVO.getConfigAssayMethodId()) && retestCountList.contains(f.getRetestCount()) ) && f.getIsReported().equals(QmsCommonConstant.NO)).count();
			if (count > 0) {//如果还存在未上报的数据，则继续
				continue;
			}
			businessSubSampleAnalysisGroupDO.setSampleStatus("已完成");//当前班组分析已完成
			updateBusinessSubSampleAnalysisGroupDOList.add(businessSubSampleAnalysisGroupDO);
			//查询子样
			BusinessSubSampleDO businessSubSampleDO = businessSubSampleMapper.selectById(businessSubSampleAnalysisGroupDO.getBusinessSubSampleId());
			updateBusinessSubSampleDOList.add(businessSubSampleDO);
		}
		
		//处理委托登记信息
		List<Long> businessBaseSampleIdList = businessSubParentSampleDOList.stream().map(m -> m.getBusinessBaseSampleId()).distinct().collect(Collectors.toList());
		
		List<BusinessSampleEntrustDetailDO> businessSampleEntrustDetailDOList = businessSampleEntrustDetailMapper.selectByBusinessBaseSampleIds(businessBaseSampleIdList);
		List<Long> businessSampleEntrustRegistrationIdList = businessSampleEntrustDetailDOList.stream().map(m -> m.getBusinessSampleEntrustRegistrationId()).distinct().collect(Collectors.toList());
		
		List<BusinessSampleEntrustRegistrationDO> businessSampleEntrustRegistrationDOList = businessSampleEntrustRegistrationMapper.selectByIds(businessSampleEntrustRegistrationIdList);
		
		List<BusinessSampleEntrustDetailDO> allBusinessSampleEntrustDetailDOList = businessSampleEntrustDetailMapper.selectByBusinessSampleEntrustRegistrationIds(businessSampleEntrustRegistrationIdList);
		
		List<BusinessSampleEntrustDepartmentDO> allBusinessSampleEntrustDepartmentDOList = businessSampleEntrustDepartmentMapper.selectByBusinessSampleEntrustRegistrationIds(businessSampleEntrustRegistrationIdList);
		
		List<BusinessSampleEntrustDepartmentDetailDO> allBusinessSampleEntrustDepartmentDetailDOList = businessSampleEntrustDepartmentDetailMapper.selectByBusinessSampleEntrustRegistrationIds(businessSampleEntrustRegistrationIdList);
		
		for (BusinessSampleEntrustRegistrationDO businessSampleEntrustRegistrationDO : businessSampleEntrustRegistrationDOList) {
			ConfigEntrustSourceDO configEntrustSourceDO = configEntrustSourceMapper.selectById(businessSampleEntrustRegistrationDO.getConfigEntrustSourceId());
			//过滤当前委托下的分析部门
			List<BusinessSampleEntrustDepartmentDO> currBusinessSampleEntrustDepartmentDOList = allBusinessSampleEntrustDepartmentDOList.stream().filter(f -> f.getBusinessSampleEntrustRegistrationId().equals(businessSampleEntrustRegistrationDO.getId()) && !QmsCommonConstant.CHECKED.equals(f.getAssayStatus())).collect(Collectors.toList());
			if (QmsCommonConstant.YES.equals(configEntrustSourceDO.getIsGenerateDocument()) && CollUtil.isNotEmpty(currBusinessSampleEntrustDepartmentDOList)) {//需要生成部门编制报告,并且当前委托部门不为空
				for (BusinessSampleEntrustDepartmentDO businessSampleEntrustDepartmentDO : currBusinessSampleEntrustDepartmentDOList) {
					//过滤当前分析部门下的样品
					List<BusinessSampleEntrustDepartmentDetailDO> currBusinessSampleEntrustDepartmentDetailDOList = allBusinessSampleEntrustDepartmentDetailDOList.stream().filter(f -> f.getBusinessSampleEntrustDepartmentId().equals(businessSampleEntrustDepartmentDO.getId()) && !QmsCommonConstant.CHECKED.equals(f.getAssayStatus())).collect(Collectors.toList());
					for (BusinessSampleEntrustDepartmentDetailDO businessSampleEntrustDepartmentDetailDO : currBusinessSampleEntrustDepartmentDetailDOList) {
						List<BusinessAssayTaskDataDO> currBusinessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessBaseSampleIdAndAssayDepartmentId(businessSampleEntrustDepartmentDetailDO.getBusinessBaseSampleId(), businessSampleEntrustDepartmentDO.getAssayDepartmentId());
						List<Long> currBusinessSubSampleAssessmentIdList = currBusinessAssayTaskDataDOList.stream().filter(f -> f.getBusinessSubSampleAssessmentId() != null).map(m -> m.getBusinessSubSampleAssessmentId()).distinct().collect(Collectors.toList());
						List<BusinessSubSampleAssessmentDO> currBusinessSubSampleAssessmentDOList = businessSubSampleAssessmentMapper.selectByIds(currBusinessSubSampleAssessmentIdList);
						List<Long> currBusinessSubParentSampleAssessmentIdList = currBusinessSubSampleAssessmentDOList.stream().filter(f -> f.getBusinessSubParentSampleAssessmentId() != null).map(m -> m.getBusinessSubParentSampleAssessmentId()).distinct().collect(Collectors.toList());
						List<Long> otherBusinessSubParentSampleAssessmentIdList = currBusinessSubParentSampleAssessmentIdList.stream().filter(f -> !businessSubParentSampleAssessmentIdList.contains(f)).collect(Collectors.toList());						
						if (otherBusinessSubParentSampleAssessmentIdList.size() <= 0) {
							businessSampleEntrustDepartmentDetailDO.setAssayStatus(QmsCommonConstant.CHECKED);//设置分析样品检验状态为完成
							updateBusinessSampleEntrustDepartmentDetailDOList.add(businessSampleEntrustDepartmentDetailDO);
						} else {
							List<BusinessSubParentSampleAssessmentDO> otherBusinessSubParentSampleAssessmentList = businessSubParentSampleAssessmentMapper.selectByIds(otherBusinessSubParentSampleAssessmentIdList);
							long count = otherBusinessSubParentSampleAssessmentList.stream().filter(f -> QmsCommonConstant.NO.equals(f.getIsReported())).count();//过滤未上报的数量
							if (count > 0) {//如果大于0，则还存在未上报的
								continue;
							}
							businessSampleEntrustDepartmentDetailDO.setAssayStatus(QmsCommonConstant.CHECKED);//设置分析样品检验状态为完成
							updateBusinessSampleEntrustDepartmentDetailDOList.add(businessSampleEntrustDepartmentDetailDO);//添加到更新列表
						}
					}
					long count = currBusinessSampleEntrustDepartmentDetailDOList.stream().filter(f -> !QmsCommonConstant.CHECKED.equals(f.getAssayStatus())).count();
					if (count > 0) {//如果大于0，则还存在样品未上报
						continue;
					}
					businessSampleEntrustDepartmentDO.setAssayStatus(QmsCommonConstant.CHECKED);//修改完成状态
					updateBusinessSampleEntrustDepartmentDOList.add(businessSampleEntrustDepartmentDO);//添加到更新列表
				}
				
			} else {
				List<Long> currentBusinessBaseSampleIdList = allBusinessSampleEntrustDetailDOList.stream().filter(f -> f.getBusinessSampleEntrustRegistrationId().equals(businessSampleEntrustRegistrationDO.getId())).map(m -> m.getBusinessBaseSampleId()).collect(Collectors.toList());
				List<BusinessSubParentSampleDO> currentBusinessSubParentSampleDOList = businessSubParentSampleMapper.selectByBusinessBaseSampleIds(currentBusinessBaseSampleIdList);
				List<Long> currentBusinessSubParentSampleIdList = currentBusinessSubParentSampleDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
				List<BusinessSubParentSampleAssessmentDO> currentBusinessSubParentSampleAssessmentDOList = businessSubParentSampleAssessmentMapper.selectByBusinessSubParentSampleIds(currentBusinessSubParentSampleIdList);
				
				//排除不在reqVO.getBusinessSubParentSampleIds()并且方法reqVO.getConfigAssayMethodId()的数据，其他数据校验是否已上报
				long count = currentBusinessSubParentSampleAssessmentDOList.stream().filter(f -> !(reqVO.getBusinessSubParentSampleIds().contains(f.getBusinessSubParentSampleId()) && f.getConfigAssayMethodId().equals(reqVO.getConfigAssayMethodId())) && f.getIsReported().equals(QmsCommonConstant.NO)).count();
				if (count > 0) {//如果还存在未上报的数据，则继续
					continue;
				}
				businessSampleEntrustRegistrationDO.setAssayStatus(QmsCommonConstant.CHECKED);

				//TODO 新增个配置是否发送到消息队列
				if (true) {
					SampleEntrustRegistrationAssayCompleteMessage message = new SampleEntrustRegistrationAssayCompleteMessage();
					message.setBusinessSampleEntrustRegistrationId(businessSampleEntrustRegistrationDO.getId())
						   .setConfigEntrustSourceId(businessSampleEntrustRegistrationDO.getConfigEntrustSourceId())
						   .setEntrustNumber(businessSampleEntrustRegistrationDO.getEntrustNumber())
						   .setEntrustUnit(businessSampleEntrustRegistrationDO.getEntrustUnit());
					sampleEntrustRegistrationAssayCompleteProducer.sendMessage(message);
				}
				
				//处理原始记录附件，原始记录需要过滤掉
				//过滤当前
//				List<Long> currEntrustBusinessBaseSampleIdList = allBusinessSampleEntrustDetailDOList.stream().filter(f -> businessSampleEntrustRegistrationDO.getId().equals(f.getBusinessSampleEntrustRegistrationId())).map(m -> m.getBusinessBaseSampleId()).collect(Collectors.toList());
				
				//查询分析任务
				List<BusinessAssayTaskDataDO> currEntrustBusinessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessBaseSampleIds(currentBusinessBaseSampleIdList);
				List<Long> currEntrustBusinessAssayTaskDataDOIdList = currEntrustBusinessAssayTaskDataDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
				
				//查询分析项目
				List<BusinessAssayProjectDataExtendRespVO> currEntrustBusinessAssayProjectDataList = businessAssayProjectDataMapper.selectByBusinessAssayTaskDataIds(currEntrustBusinessAssayTaskDataDOIdList);
				//过滤出需要上报的分析任务id
				List<Long> usageReportBusinessAssayTaskDataDOIdList = currEntrustBusinessAssayProjectDataList.stream().filter(f -> (QmsCommonConstant.ASSAY_PROJECT_USAGE_REPORT.equals(f.getUsage()) || QmsCommonConstant.ASSAY_PROJECT_USAGE_INGREDIENT_REPORT.equals(f.getUsage()))).map(m -> m.getBusinessAssayTaskDataId()).distinct().collect(Collectors.toList());
				
				//通过要上报的进行任务id分组
				Map<Long, List<BusinessAssayTaskDataDO>> businessAssayTaskDataByTaskMap = currEntrustBusinessAssayTaskDataDOList.stream().filter(f -> usageReportBusinessAssayTaskDataDOIdList.contains(f.getId())).collect(Collectors.groupingBy(BusinessAssayTaskDataDO::getBusinessAssayTaskId));
//				List<Long> businessAssayTaskIdList = businessAssayTaskDataDOList.stream().map(m -> m.getBusinessAssayTaskId()).distinct().collect(Collectors.toList());
				for (Map.Entry<Long, List<BusinessAssayTaskDataDO>> entry : businessAssayTaskDataByTaskMap.entrySet()) {
					Long businessAssayTaskId = entry.getKey();
					List<Long> currBusinessAssayTaskDataIdList = entry.getValue().stream().map(m -> m.getId()).collect(Collectors.toList());
					JSONObject json = sampleAnalysisAuditService.getCrossAuditDataByTaskId(businessAssayTaskId, currBusinessAssayTaskDataIdList);
					JSONObject businessAssayTaskJson = json.getJSONArray("businessAssayTask").getJSONObject(0);
					String configReportTemplateKey = businessAssayTaskJson.getString("configReportTemplateKey");
					String taskNo = businessAssayTaskJson.getString("taskNo");
					String taskName = businessAssayTaskJson.getString("taskName");
					String fileName = taskNo + "-" + taskName;
//					String assayOperatorId = businessAssayTaskJson.getString("assayOperatorId");
//					String assayOperator = businessAssayTaskJson.getString("assayOperator");
					
					ConfigReportTemplateDO gridppReportFile = configReportTemplateService.getConfigReportTemplateByKey(configReportTemplateKey);

			        //报表参数
			        Map<String, String> reportParams = new HashMap<>();
			        BinaryObject binaryObject;
					try {
						binaryObject = gridReport.genReport(gridppReportFile.getContent(), json.toJSONString(), reportParams, ExportType.PDF);
					} catch (Exception e) {
						throw new ServiceException(1_032_100_000, taskNo + "原始记录生成失败！");
					}
			        byte[] docBytes = PdfHelper.modifyDocInfo(binaryObject.getDataBuf());
			        //上传到文件服务
			        FileCreateReqDTO fileCreateReqDTO = new FileCreateReqDTO();
			        fileCreateReqDTO.setName(fileName);
			        fileCreateReqDTO.setDirectory(null);
			        fileCreateReqDTO.setType(null);
			        fileCreateReqDTO.setContent(docBytes);
			        CommonResult<FileRespDTO> fileResult = fileApi.createFileWithReturn(fileCreateReqDTO);
			        if (fileResult == null || !fileResult.isSuccess() || fileResult.getData() == null) {
			            throw new ServiceException(1_032_100_000, "通过文件服务创建附件失败: " + Optional.ofNullable(fileResult).map(CommonResult::getMsg).orElse("未知错误"));
			        }
			        FileRespDTO fileRespDTO = fileResult.getData();
			        //创建businessFile
			        BusinessFileSaveReqDTO businessFileSaveReqDTO = BusinessFileSaveReqDTO.builder()
			                .businessId(businessSampleEntrustRegistrationDO.getId())
			                .businessCode(taskNo)
			                .fileName(fileRespDTO.getName())
			                .fileId(fileRespDTO.getId())
			                .source(null)
			                .status(1)
			                .build();
			        businessFileApi.createBusinessFile(businessFileSaveReqDTO);

			        //设置附件
			        JSONObject fileExtInfoJSON = new JSONObject();
			        fileExtInfoJSON.put("taskNo", businessAssayTaskJson.getString("taskNo"));
			        fileExtInfoJSON.put("taskName", businessAssayTaskJson.getString("taskName"));
			        fileExtInfoJSON.put("assayOperator", businessAssayTaskJson.getString("assayOperator"));
			        fileExtInfoJSON.put("assayTime", businessAssayTaskJson.getString("assayTime"));
			        fileExtInfoJSON.put("reportOperator", businessAssayTaskJson.getString("reportOperator"));
			        fileExtInfoJSON.put("reportTime", businessAssayTaskJson.getString("reportTime"));
			        BusinessSampleEntrustFileDO businessSampleEntrustFileDO = new BusinessSampleEntrustFileDO();
			        businessSampleEntrustFileDO.setBusinessSampleEntrustRegistrationId(businessSampleEntrustRegistrationDO.getId());
			        businessSampleEntrustFileDO.setFileCategory(QmsCommonConstant.ENTRUST_ATTACHMENT_CATEGORY_ASSAY_TASK);
			        businessSampleEntrustFileDO.setFileId(fileRespDTO.getId());
			        businessSampleEntrustFileDO.setFileName(fileRespDTO.getName());
			        businessSampleEntrustFileDO.setFilePath(fileRespDTO.getDirectory());
			        businessSampleEntrustFileDO.setExternalInfomation(fileExtInfoJSON.toJSONString());
			        
			        businessSampleEntrustFileDOList.add(businessSampleEntrustFileDO);
					
				}
				
				updatebBusinessSampleEntrustRegistrationDOList.add(businessSampleEntrustRegistrationDO);
			}
		}
		
		//保存分析报表数据
		if (saveBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.insertBatch(saveBusinessAssayReportDataDOList);
		}
		//更新分析报表数据
		if (updateBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.updateBatch(updateBusinessAssayReportDataDOList);
		}
		
		//更新分析班组
		if (updateBusinessSubSampleAnalysisGroupDOList.size()> 0) {
			businessSubSampleAnalysisGroupMapper.updateBatch(updateBusinessSubSampleAnalysisGroupDOList);
		}
		
		//查询正常上报的
//		List<Long> businessSubParentSampleAssessmentIdList = businessSubParentSampleAssessmentExtendList.stream().filter(f -> "normal".equals(f.getAssessmentStatus()) ).map(m -> m.getId()).collect(Collectors.toList());
		
		//更新分样判定检测项目
		businessSubParentSampleAssessmentProjectMapper.update(new LambdaUpdateWrapper<BusinessSubParentSampleAssessmentProjectDO>()
				.set(BusinessSubParentSampleAssessmentProjectDO::getIsReported, QmsCommonConstant.YES)
				.set(BusinessSubParentSampleAssessmentProjectDO::getReporter, nickName)
				.set(BusinessSubParentSampleAssessmentProjectDO::getReportTime, LocalDateTime.now())
				.in(BusinessSubParentSampleAssessmentProjectDO::getId, businessSubParentSampleAssessmentProjectIdList));

		//更新分样判定
		businessSubParentSampleAssessmentMapper.update(new LambdaUpdateWrapper<BusinessSubParentSampleAssessmentDO>()
				.set(BusinessSubParentSampleAssessmentDO::getIsReported, QmsCommonConstant.YES)
				.set(BusinessSubParentSampleAssessmentDO::getReporter, nickName)
				.set(BusinessSubParentSampleAssessmentDO::getReportTime, LocalDateTime.now())
				.in(BusinessSubParentSampleAssessmentDO::getId, businessSubParentSampleAssessmentIdList));
		
		if (updatebBusinessSampleEntrustRegistrationDOList.size() > 0) {
			businessSampleEntrustRegistrationMapper.updateBatch(updatebBusinessSampleEntrustRegistrationDOList);
		}
		
		if (updateBusinessSampleEntrustDepartmentDOList.size() > 0) {
			businessSampleEntrustDepartmentMapper.updateBatch(updateBusinessSampleEntrustDepartmentDOList);
		}
		
		if (businessSampleEntrustFileDOList.size() > 0) {
			businessSampleEntrustFileMapper.insertBatch(businessSampleEntrustFileDOList);
		}
		
		if (updateBusinessSampleEntrustDepartmentDetailDOList.size() > 0) {
			businessSampleEntrustDepartmentDetailMapper.updateBatch(updateBusinessSampleEntrustDepartmentDetailDOList);
		}
		
		if (updateBusinessSubSampleDOList.size() > 0 && QmsCommonConstant.FLOW_NODE_ANALYSIS.equals(updateBusinessSubSampleDOList.get(0).getSampleFlowNodeKey())) {//仅当前节点在分析时，才操作
			SampleFlowParam sampleFlowParam = new SampleFlowParam();
			sampleFlowParam.setCurrentSampleFlowKey(QmsCommonConstant.FLOW_NODE_ANALYSIS);
			sampleFlowParam.setSampleSourceType(2);
			sampleFlowParam.setSampleFlowInfoList(BeanUtils.toBean(updateBusinessSubSampleDOList, SampleFlowInfo.class));
			sampleFlowService.sampleFlow(sampleFlowParam);
		}
	}

	@Override
	public JSONObject getSingleSampleResultReportingList(Long businessSubParentSampleId, Long configAssayMethodId) {
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void modifySampleResultReporting(ModifySampleResultReportingReqVO reqVO) {
		List<String> businessSubParentSampleAssessmentProjectIdList = Arrays.asList(reqVO.getBusinessSubParentSampleAssessmentProjectIds().split(","));
		List<String> businessSubSampleAssessmentIdList = Arrays.asList(reqVO.getBusinessSubSampleAssessmentIds().split(","));
		List<BusinessSubParentSampleAssessmentProjectDO> businessSubParentSampleAssessmentDOList = businessSubParentSampleAssessmentProjectMapper.selectByIds(businessSubParentSampleAssessmentProjectIdList);
		List<BusinessSubSampleAssessmentProjectDO> businessSubSampleAssessmentDOList = businessSubSampleAssessmentProjectMapper.selectByIds(businessSubSampleAssessmentIdList);
		for (BusinessSubParentSampleAssessmentProjectDO businessSubParentSampleAssessmentProjectDO : businessSubParentSampleAssessmentDOList) {
			BusinessSubSampleAssessmentProjectDO businessSubSampleAssessmentProjectDO = businessSubSampleAssessmentDOList.stream().filter(f -> f.getDictionaryProjectId().equals(businessSubParentSampleAssessmentProjectDO.getDictionaryProjectId())).findFirst().orElse(null);
			businessSubParentSampleAssessmentProjectDO.setConfigAssayMethodId(businessSubSampleAssessmentProjectDO.getConfigAssayMethodId());
			businessSubParentSampleAssessmentProjectDO.setConfigAssayMethodProjectId(businessSubSampleAssessmentProjectDO.getConfigAssayMethodProjectId());
			businessSubParentSampleAssessmentProjectDO.setAssessmentValue(businessSubSampleAssessmentProjectDO.getAssessmentValue());
			businessSubParentSampleAssessmentProjectDO.setSymbol(businessSubSampleAssessmentProjectDO.getSymbol());
		}
		businessSubParentSampleAssessmentProjectMapper.updateBatch(businessSubParentSampleAssessmentDOList);
	}


}
