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

import java.math.BigDecimal;
import java.math.RoundingMode;
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.Objects;
import java.util.Set;
import java.util.stream.Collectors;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayParameterDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayProjectDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessBaseSampleDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAssessmentDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayParameterDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayProjectDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayReportDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessBaseSampleMapper;
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.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodProjectAssessmentDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportFieldDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigRuleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodProjectAssessmentMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodProjectMapper;
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.ConfigReportTypeMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigRuleMapper;
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.core.qlexpress.cmp.AllowanceCalculatorComponent;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;

/**
 * <b>SampleAnalysisAuditServiceImpl</b>
 * <p>
 * 更新历史：
 * <pre> 版本         更新时间            更新者        更新内容<hr/>
 * V1.0      2025年9月29日          wxr         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * </p>
 * @author 王兴荣<wxr@wangxingrong.com>
 * @version V1.0 
 * @since 2025年9月29日
 */
@Service
public class SampleAnalysisAuditServiceImpl implements SampleAnalysisAuditService {
	
	@Resource
	private ExpressRunner expressRunner;
	
	@Resource
	private AllowanceCalculatorComponent allowanceCalculatorComponent;
	
	@Resource
	private BusinessAssayTaskMapper businessAssayTaskMapper;
	
	@Resource
	private BusinessAssayTaskDetailMapper businessAssayTaskDetailMapper;
	
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private BusinessAssayProjectDataMapper businessAssayProjectDataMapper;
	
	@Resource
	private ConfigAssayMethodProjectAssessmentMapper configAssayMethodProjectAssessmentMapper;
	
//	@Resource
//	private BusinessTeamAssessmentMapper businessTeamAssessmentMapper;
	
	@Resource
	private BusinessSubSampleAssessmentMapper businessSubSampleAssessmentMapper;
	
	@Resource
	private ConfigAssayMethodProjectMapper configAssayMethodProjectMapper;

	@Override
	public JSONObject getSampleResultAssessmentList(Long configAssayMethodId, String assessmentStatus) {
		List<ConfigAssayMethodProjectExtendRespVO> configAssayMethodProjectDOList = configAssayMethodProjectMapper.selectByConfigAssayMethodId(configAssayMethodId);
		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<>();
		BusinessAssayTaskDataReqVO search = new BusinessAssayTaskDataReqVO();
		search.setConfigAssayMethodId(configAssayMethodId);
		List<BusinessAssayTaskDataExtendRespVO> resultAssessmentList = businessAssayTaskDataMapper.selectResultAssessmentList(search);
		for (BusinessAssayTaskDataExtendRespVO businessAssayTaskDataExtendRespVO : resultAssessmentList) {
			Map<String,Object> businessAssayTaskDataMap = BeanUtil.beanToMap(businessAssayTaskDataExtendRespVO);
			Map<String,Object> businessAssayTaskDataAssessmentMap = BeanUtil.copyProperties(businessAssayTaskDataMap, Map.class);
			businessAssayTaskDataAssessmentMap.put("assayOperator", "判定结果");
			businessAssayTaskDataAssessmentMap.put("reportTime", null);
			businessAssayTaskDataAssessmentMap.put("assessmentStatus", QmsCommonConstant.IN_PROGRESS);

			//查询是否已添加过判定记录
			Map<String,Object> assessmentMap = dataList.stream().filter(f -> 
							    Objects.equals(businessAssayTaskDataExtendRespVO.getBusinessSubSampleId(), f.get("businessSubSampleId")) &&
							    Objects.equals(businessAssayTaskDataExtendRespVO.getConfigAssayMethodId(), f.get("configAssayMethodId")) &&
							    "判定结果".equals(f.get("assayOperator"))
							).findFirst().orElse(null);
			//为空则未添加过判定记录
			if (assessmentMap == null) {
				if ("平行".equals(businessAssayTaskDataExtendRespVO.getAssayType())) {
					List<BusinessSubSampleAssessmentExtendRespVO> businessSubSampleAssessmentList = businessSubSampleAssessmentMapper.selectByBusinessSubSampleIdAndConfigAssayMethodId(businessAssayTaskDataExtendRespVO.getBusinessSubSampleId(), configAssayMethodId);
					for (BusinessSubSampleAssessmentExtendRespVO businessSubSampleAssessment : businessSubSampleAssessmentList) {
						businessAssayTaskDataAssessmentMap.put("id", businessSubSampleAssessment.getId());
						businessAssayTaskDataAssessmentMap.put("assessmentStatus", businessSubSampleAssessment.getAssessmentStatus());
						businessAssayTaskDataAssessmentMap.put("assessmentStatus_" + businessSubSampleAssessment.getSimpleName(), businessSubSampleAssessment.getAssessmentStatus());
						businessAssayTaskDataAssessmentMap.put(businessSubSampleAssessment.getSimpleName(), businessSubSampleAssessment.getAssessmentValue());
						businessAssayTaskDataAssessmentMap.put("assessmentValue", businessSubSampleAssessment.getAssessmentValue());
					}
				} else if ("双杯".equals(businessAssayTaskDataExtendRespVO.getAssayType())) {
					//businessTeamAssessmentMapper.selectBy
				}
				dataList.add(businessAssayTaskDataAssessmentMap);
				//赋值
				assessmentMap = businessAssayTaskDataAssessmentMap;
			}
			
			
			List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectByBusinessAssayTaskDataId(businessAssayTaskDataExtendRespVO.getId());
			for (BusinessAssayProjectDataExtendRespVO businessAssayProjectData : businessAssayProjectDataList) {
				businessAssayTaskDataMap.put(businessAssayProjectData.getSimpleName(), businessAssayProjectData.getValue());
				businessAssayTaskDataMap.put("isNotAssessment_" + businessAssayProjectData.getSimpleName(), businessAssayProjectData.getIsNotAssessment());
				//添加判定状态
				businessAssayTaskDataMap.put("assessmentStatus", assessmentMap.get("assessmentStatus"));
				
			}
			dataList.add(businessAssayTaskDataMap);
			
		}
		
		if (StringUtils.isNotBlank(assessmentStatus)) {
			dataList = dataList.stream().filter(f -> assessmentStatus.equals(f.get("assessmentStatus"))).collect(Collectors.toList());
		}
		
		
		JSONObject json = new JSONObject();
		json.put("columns", columnList);
		json.put("datas", dataList);
		return json;
	}

	@Override
	public JSONObject getParallelResultAssessment(Long businessSubSampleId, Long configAssayMethodId) {
		List<Map<String, Object>> columnList = new ArrayList<>();
		List<Map<String, Object>> dataList = new ArrayList<>();
		
		//查询子样平行分析结果
		BusinessAssayProjectDataReqVO search = new BusinessAssayProjectDataReqVO();
		search.setBusinessSubSampleId(businessSubSampleId);
		search.setConfigAssayMethodId(configAssayMethodId);
		List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectAnalysisBy(search);
		
		//已检测项目分组，取第一个
		Map<Long, List<BusinessAssayProjectDataExtendRespVO>> businessAssayProjectDataMap = businessAssayProjectDataList.stream().collect(Collectors.groupingBy(BusinessAssayProjectDataExtendRespVO::getConfigAssayMethodProjectId));
		for (Map.Entry<Long, List<BusinessAssayProjectDataExtendRespVO>> businessAssayProjectDataEntry : businessAssayProjectDataMap.entrySet()) {
			//单个检测项目的分析结果
			List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataSingleList = businessAssayProjectDataEntry.getValue();
			for (BusinessAssayProjectDataExtendRespVO businessAssayProjectDataExtendRespVO : businessAssayProjectDataSingleList) {//每个检测结果作为一个动态列
				columnList.add(new HashMap<String, Object>() {
					private static final long serialVersionUID = -1844858000003248901L;

				{
					put("title", businessAssayProjectDataExtendRespVO.getSampleAssayCode() + "(" + businessAssayProjectDataExtendRespVO.getAssayOperator() + ")");
					put("field", businessAssayProjectDataExtendRespVO.getBusinessAssayTaskDataId() + ";" + businessAssayProjectDataExtendRespVO.getBusinessSubSampleId() + ";" + businessAssayProjectDataExtendRespVO.getAnalysisCount());
				}});
			}
			
			//取第一个后跳出循环
			break;
		}
		
		//查询判定值
		List<BusinessSubSampleAssessmentExtendRespVO> businessSubSampleAssessmentList = businessSubSampleAssessmentMapper.selectByBusinessSubSampleIdAndConfigAssayMethodId(businessSubSampleId, configAssayMethodId);
		
		for (BusinessSubSampleAssessmentExtendRespVO businessSubSampleAssessment : businessSubSampleAssessmentList) {
			Map<String, Object> businessSubSampleAssessmentMap = BeanUtil.beanToMap(businessSubSampleAssessment);
			List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataSingleList = businessAssayProjectDataList.stream().filter(f -> f.getDictionaryProjectId().equals(businessSubSampleAssessment.getDictionaryProjectId())).collect(Collectors.toList());
			for (BusinessAssayProjectDataExtendRespVO businessAssayProjectDataExtendRespVO : businessAssayProjectDataSingleList) {
				businessSubSampleAssessmentMap.put(businessAssayProjectDataExtendRespVO.getBusinessAssayTaskDataId() + ";" + businessAssayProjectDataExtendRespVO.getBusinessSubSampleId() + ";" + businessAssayProjectDataExtendRespVO.getAnalysisCount(), businessAssayProjectDataExtendRespVO.getValue());
			}
			dataList.add(businessSubSampleAssessmentMap);
		}
		
		
		JSONObject json = new JSONObject();
		json.put("columns", columnList);
		json.put("datas", dataList);
		json.put("subDatas", businessAssayProjectDataList);
		return json;
	}

	@Override
	public JSONObject getDoubleCupResultAssessment(Long businessSubParentSampleId, Long configAssayMethodId) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void crossAuditByByTaskId(Long businessAssayTaskId, String auditStatus) {

		//当前登录用户昵称
		String nickName = SecurityFrameworkUtils.getLoginUserNickname();
		
		//班组长双杯判定记录
//		List<BusinessTeamAssessmentDO> businessTeamAssessmentDOList = new ArrayList<>();
		
		//平行样判定记录
		List<BusinessSubSampleAssessmentDO> saveBusinessSubSampleAssessmentDOList = new ArrayList<>();
		List<BusinessSubSampleAssessmentDO> updateBusinessSubSampleAssessmentDOList = new ArrayList<>();
		
		BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectById(businessAssayTaskId);
		if (QmsCommonConstant.APPROVE.equals(auditStatus)) {
			businessAssayTaskDO.setFinishStatus(QmsCommonConstant.APPROVED);
			businessAssayTaskDO.setFlowStatus(QmsCommonConstant.APPROVED);
			businessAssayTaskDO.setReportTime(LocalDateTime.now());
			businessAssayTaskDO.setReportOperator(SecurityFrameworkUtils.getLoginUserNickname());
			
			//List<BusinessAssayTaskDetailDO> businessAssayTaskDetailDOList = businessAssayTaskDetailMapper.selectByBusinessAssayTaskId(businessAssayTaskId);
			
			//查询分析任务明细
			List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectByBusinessAssayTaskId(businessAssayTaskId);
			
			Map<Long, List<BusinessAssayTaskDataDO>> businessAssayTaskDataDOMap = businessAssayTaskDataDOList.stream().collect(Collectors.groupingBy(BusinessAssayTaskDataDO::getBusinessSubSampleId));
			for (Map.Entry<Long, List<BusinessAssayTaskDataDO>> businessAssayTaskDataDOMapEntry : businessAssayTaskDataDOMap.entrySet()) {
				Long businessSubSampleId = businessAssayTaskDataDOMapEntry.getKey();
				List<BusinessAssayTaskDataDO> businessAssayTaskDataList = businessAssayTaskDataDOMapEntry.getValue();
				List<Long> businessAssayTaskDataIdList = businessAssayTaskDataList.stream().map(m -> m.getId()).collect(Collectors.toList());
				BusinessAssayTaskDataDO businessAssayTaskDataDO = businessAssayTaskDataList.get(0);
				
				if ("平行".equals(businessAssayTaskDataDO.getAssayType())) {
					//根据子样id及分析方法id，查询未上报的分析任务
					List<BusinessAssayTaskDataDO> unReportedList = businessAssayTaskDataMapper.selectIsReportedList(businessAssayTaskDataIdList, businessAssayTaskDataDO.getBusinessSubSampleId(), businessAssayTaskDataDO.getConfigAssayMethodId(), QmsCommonConstant.NO);
					if (unReportedList.size() > 0) {//还有未上报数据，直接处理，无需判定
						continue;
					} else {//做超差判定
						//根据子样id及分析方法id，查询已上报的分析任务
						List<BusinessAssayTaskDataDO> reportedList = businessAssayTaskDataMapper.selectIsReportedList(businessAssayTaskDataIdList, businessAssayTaskDataDO.getBusinessSubSampleId(), businessAssayTaskDataDO.getConfigAssayMethodId(), QmsCommonConstant.YES);
						if (reportedList.size() > 0) {
							List<Long> reportedBusinessAssayTaskDataIdList = reportedList.stream().map(m -> m.getId()).collect(Collectors.toList());
							businessAssayTaskDataIdList.addAll(reportedBusinessAssayTaskDataIdList);
						}
						
						//查询分析项目
						List<BusinessAssayProjectDataDO> businessAssayProjectDataDOList = businessAssayProjectDataMapper.selectList(new LambdaQueryWrapperX<BusinessAssayProjectDataDO>().in(BusinessAssayProjectDataDO::getBusinessAssayTaskDataId, businessAssayTaskDataIdList));
						Map<Long, List<BusinessAssayProjectDataDO>> businessAssayProjectDataDOMap = businessAssayProjectDataDOList.stream().collect(Collectors.groupingBy(BusinessAssayProjectDataDO::getDictionaryProjectId));
						for (Map.Entry<Long, List<BusinessAssayProjectDataDO>> businessAssayProjectDataDOEntry : businessAssayProjectDataDOMap.entrySet()) {
							Long dictionaryProjectId = businessAssayProjectDataDOEntry.getKey();
							List<BusinessAssayProjectDataDO> val1 = businessAssayProjectDataDOEntry.getValue();
							BusinessAssayProjectDataDO businessAssayProjectDataDO = val1.get(0);
							List<ConfigAssayMethodProjectAssessmentDO> configAssayMethodProjectAssessmentDOList = configAssayMethodProjectAssessmentMapper.selectList(new LambdaQueryWrapperX<ConfigAssayMethodProjectAssessmentDO>().eq(ConfigAssayMethodProjectAssessmentDO::getConfigAssayMethodId, businessAssayTaskDataDO.getConfigAssayMethodId()).eq(ConfigAssayMethodProjectAssessmentDO::getConfigAssayMethodProjectId, businessAssayProjectDataDO.getConfigAssayMethodProjectId()));

							//查询判定结果数据
							BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentMapper.selectByBusinessSubSampleIdAndConfigAssayMethodIdAndConfigAssayMethodProjectId(businessSubSampleId, businessAssayTaskDataDO.getConfigAssayMethodId(), businessAssayProjectDataDO.getConfigAssayMethodProjectId());
							if (businessSubSampleAssessmentDO == null) {
								businessSubSampleAssessmentDO = new BusinessSubSampleAssessmentDO();

						        saveBusinessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
							} else {
								updateBusinessSubSampleAssessmentDOList.add(businessSubSampleAssessmentDO);
							}
							
							businessSubSampleAssessmentDO.setBusinessSubSampleId(businessSubSampleId);
							businessSubSampleAssessmentDO.setConfigAssayMethodId(businessAssayTaskDataDO.getConfigAssayMethodId());
							businessSubSampleAssessmentDO.setConfigAssayMethodProjectId(businessAssayProjectDataDO.getConfigAssayMethodProjectId());
							businessSubSampleAssessmentDO.setDictionaryProjectId(dictionaryProjectId);
							businessSubSampleAssessmentDO.setDataType(businessAssayProjectDataDO.getDataType());
							businessSubSampleAssessmentDO.setDecimalPosition(businessAssayProjectDataDO.getDecimalPosition());
							businessSubSampleAssessmentDO.setAssayType(businessAssayTaskDataDO.getAssayType());
							businessSubSampleAssessmentDO.setTaskType(businessAssayTaskDataDO.getTaskType());
							businessSubSampleAssessmentDO.setAssessmentStatus(QmsCommonConstant.NORMAL);
							
							 // 排序并处理 null
					        List<BigDecimal> sortedValues = val1.stream()
					                .map(v -> {
					                    String val = v.getValue();
					                    return val == null || val.trim().isEmpty() ? BigDecimal.ZERO : new BigDecimal(val.trim());
					                })
					                .sorted()
					                .collect(Collectors.toList());
							
					        // 获取小数精度
					        int elementScale = businessAssayProjectDataDO.getDecimalPosition();
					        // 计算代表值（均值或差值）
					        BigDecimal representativeValue = calculateRepresentativeValue(sortedValues, elementScale);
					        
					        //设置判定值
					        businessSubSampleAssessmentDO.setAssessmentValue(representativeValue.toPlainString());
					        
					        // 查找匹配的允差规则
					        ConfigAssayMethodProjectAssessmentDO matchedRule = findMatchingRule(configAssayMethodProjectAssessmentDOList, representativeValue);
					        if (matchedRule == null) {
					            throw new ServiceException(500, "未找到超差判定区间范围，元素ID: " + dictionaryProjectId + ", 值: " + representativeValue);
					        }

					        // 计算允差值
					        BigDecimal allowValue;
					        try {
					            allowValue = allowanceCalculatorComponent.calculateAllowableValue(new BigDecimal(matchedRule.getMinimumValue()), new BigDecimal(matchedRule.getMaximumValue()), new BigDecimal(matchedRule.getMinimumToleraanceValue()), new BigDecimal(matchedRule.getMaximumToleraanceValue()), new BigDecimal(matchedRule.getToleraanceAdjustmentValue()), representativeValue, matchedRule.getIsUseFormula(), matchedRule.getFormula(), elementScale);
					        } catch (Exception e) {
					            throw new ServiceException(500, "超差判定计算出错，检测项目ID: " + dictionaryProjectId);
					        }

					        // 判断相邻差值是否超差
					        for (int i = 0; i < sortedValues.size() - 1; i++) {
					            BigDecimal diff = sortedValues.get(i + 1).subtract(sortedValues.get(i)).abs();
					            if (diff.compareTo(allowValue) > 0) {
					            	businessSubSampleAssessmentDO.setAssessmentStatus(QmsCommonConstant.EXCEEDS_TOLERANCE);
					            	break;
					            }
					        }
					        
						}
						
						
						
						
					}
					
					
				} else if ("双杯".equals(businessAssayTaskDataDO.getAssayType())) {
					
				} else {//单杯
					
				}
				
				
			}
			
			for (BusinessAssayTaskDataDO businessAssayTaskDataDO : businessAssayTaskDataDOList) {
				businessAssayTaskDataDO.setIsReported(1);
				businessAssayTaskDataDO.setReportTime(LocalDateTime.now());
				businessAssayTaskDataDO.setReporter(nickName);
			}
			
			businessAssayTaskDataMapper.updateBatch(businessAssayTaskDataDOList);
			
			
			
			
			
			
			
			
			
			
			
			
		} else {
			businessAssayTaskDO.setFinishStatus(QmsCommonConstant.PENDING);
			businessAssayTaskDO.setFlowStatus(QmsCommonConstant.NOT_START);
		}
		businessAssayTaskMapper.updateById(businessAssayTaskDO);
		
		//插入平行判定记录
		if (saveBusinessSubSampleAssessmentDOList.size() > 0) {
			businessSubSampleAssessmentMapper.insertBatch(saveBusinessSubSampleAssessmentDOList);
		}
		
		//插入双杯判定记录
//		if (businessTeamAssessmentDOList.size() > 0) {
//			businessTeamAssessmentMapper.insertBatch(businessTeamAssessmentDOList);
//		}
	}
	
	// 查找匹配的允差区间
	private ConfigAssayMethodProjectAssessmentDO findMatchingRule(List<ConfigAssayMethodProjectAssessmentDO> rules, BigDecimal value) {
	    if (rules == null || rules.isEmpty()) return null;

	    // 特殊处理 level=0 和 level=4
	    for (ConfigAssayMethodProjectAssessmentDO rule : rules) {
	        if (rule.getAssessmentLevel() == 0) {
	        	ConfigAssayMethodProjectAssessmentDO next = rules.stream()
	                    .filter(r -> r.getAssessmentLevel() == 1)
	                    .findFirst()
	                    .orElse(rule);
	            if (value.compareTo(new BigDecimal(next.getMinimumValue())) >= 0 && value.compareTo(new BigDecimal(next.getMaximumValue())) <= 0) {
	                return next;
	            }
	        } else if (rule.getAssessmentLevel() == 4) {
	        	ConfigAssayMethodProjectAssessmentDO prev = rules.stream()
	                    .filter(r -> r.getAssessmentLevel() == 3)
	                    .findFirst()
	                    .orElse(rule);
	            if (value.compareTo(new BigDecimal(prev.getMinimumValue())) >= 0 && value.compareTo(new BigDecimal(prev.getMaximumValue())) <= 0) {
	                return prev;
	            }
	        } else {
	            if (value.compareTo(new BigDecimal(rule.getMinimumValue())) >= 0 && value.compareTo(new BigDecimal(rule.getMaximumValue())) <= 0) {
	                return rule;
	            }
	        }
	    }
	    return null;
	}

	// 计算代表值：1个取本身，2个取均值，>2取平均
	private BigDecimal calculateRepresentativeValue(List<BigDecimal> values, int elementScale) {
	    int size = values.size();
	    if (size == 0) return BigDecimal.ZERO;
	    if (size == 1) return values.get(0).setScale(elementScale, RoundingMode.HALF_EVEN);
	    if (size == 2) {
	        return values.get(0).add(values.get(1)).divide(BigDecimal.valueOf(2), elementScale, RoundingMode.HALF_EVEN);
	    }
	    BigDecimal sum = values.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
	    return sum.divide(BigDecimal.valueOf(size), elementScale, RoundingMode.HALF_EVEN);
	}

	@Resource
	private ConfigSampleReportMapper configSampleReportMapper;
	
	@Resource
	private ConfigReportFieldMapper configReportFieldMapper;
	
	@Resource
	private ConfigReportTypeMapper configReportTypeMapper;
	
	@Resource
	private ConfigProjectMapper configProjectMapper;
	
	@Resource
	private BusinessAssayReportDataMapper businessAssayReportDataMapper;
	
	@Resource
	private BusinessBaseSampleMapper businessBaseSampleMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void assessmentDataReporting(Long businessSubSampleId, Long configAssayMethodId) {
		//当前登录用户昵称
		String nickName = SecurityFrameworkUtils.getLoginUserNickname();
		List<BusinessAssayTaskDataDO> unReportedList = businessAssayTaskDataMapper.selectIsReportedList(null, businessSubSampleId, configAssayMethodId, QmsCommonConstant.NO);
		if (unReportedList.size() > 0) {
			throw new ServiceException(500, "存在未上报的数据");
		}
		List<BusinessAssayReportDataDO> saveBusinessAssayReportDataDOList = new ArrayList<>();
		List<BusinessAssayReportDataDO> updateBusinessAssayReportDataDOList = new ArrayList<>();
		//分析任务已全部上报
		List<BusinessAssayTaskDataDO> reportedList = businessAssayTaskDataMapper.selectIsReportedList(null, businessSubSampleId, configAssayMethodId, QmsCommonConstant.YES);
		//平行样判定结果
		List<BusinessSubSampleAssessmentExtendRespVO> businessSubSampleAssessmentList = businessSubSampleAssessmentMapper.selectByBusinessSubSampleIdAndConfigAssayMethodId(businessSubSampleId, configAssayMethodId);
		
		List<Long> businessSubSampleAssessmentIdList = businessSubSampleAssessmentList.stream().map(m -> m.getId()).collect(Collectors.toList());
		
		BusinessAssayTaskDataDO businessAssayTaskDataDO = reportedList.get(0);
		
		//查询主样
		BusinessBaseSampleDO businessBaseSampleDO = businessBaseSampleMapper.selectById(businessAssayTaskDataDO.getBusinessBaseSampleId());
		
		//查询报表
		List<BusinessAssayReportDataDO> businessAssayReportDataDOList = businessAssayReportDataMapper.selectBytBusinessBaseSampleId(businessAssayTaskDataDO.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(configAssayMethodId);
		
		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(businessAssayTaskDataDO.getBusinessBaseSampleId());
				businessAssayReportDataDO.setConfigReportTypeId(configSampleReport.getConfigReportTypeId());
				businessAssayReportDataDO.setConfigSampleReportId(configSampleReport.getId());
				businessAssayReportDataDO.setSampleCode(businessBaseSampleDO.getSampleCode());
				
				saveBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
			} else {
				updateBusinessAssayReportDataDOList.add(businessAssayReportDataDO);
			}
			JSONObject assayDataJson = new JSONObject();
			String assayData = businessAssayReportDataDO.getAssayData();
			if (StringUtils.isNotBlank(assayData)) {
				assayDataJson = JSON.parseObject(assayData);
			}
			//循环判定值
			for (BusinessSubSampleAssessmentExtendRespVO businessSubSampleAssessment : businessSubSampleAssessmentList) {
				Long configAssayMethodProjectId = businessSubSampleAssessment.getConfigAssayMethodProjectId();
				List<ConfigProjectExtendRespVO> configProjectFeildList = configProjectList.stream().filter(f -> 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(businessSubSampleAssessment.getAssessmentValue());
					reportFieldValueData.setDataType(configReportField.getDataType());
					reportFieldValueData.setMathSymbol("=");
					reportFieldValueData.setUnit(configProjectFeild.getDictionaryProjectUnit());					
					assayDataJson.put(configReportField.getField(), reportFieldValueData);
				}				
			}
			businessAssayReportDataDO.setAssayData(assayDataJson.toJSONString());
			
			//修改已上报数据来源
			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(configAssayMethodId.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) {
				//计算
				// TODO
			}
			businessAssayReportDataDO.setReportedSource(JSON.toJSONString(reportedDataSource));

		}
		
		
		if (saveBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.insertBatch(saveBusinessAssayReportDataDOList);
		}
		if (updateBusinessAssayReportDataDOList.size() > 0) {
			businessAssayReportDataMapper.updateBatch(updateBusinessAssayReportDataDOList);
		}
		businessSubSampleAssessmentMapper.update(new LambdaUpdateWrapper<BusinessSubSampleAssessmentDO>()
				.set(BusinessSubSampleAssessmentDO::getIsReported, QmsCommonConstant.YES)
				.set(BusinessSubSampleAssessmentDO::getReporter, nickName)
				.set(BusinessSubSampleAssessmentDO::getReportTime, LocalDateTime.now())
				.in(BusinessSubSampleAssessmentDO::getId, businessSubSampleAssessmentIdList));
	}

	@Resource
	private ConfigSubSampleMapper configSubSampleMapper;
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Resource
	private BusinessAssayParameterDataMapper businessAssayParameterDataMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createReAnalysis(Long businessSubSampleId, Long configAssayMethodId) {
		//子样数据
		BusinessSubSampleDO businessSubSampleDO = businessSubSampleMapper.selectById(businessSubSampleId);
		
		//子样配置数据
		ConfigSubSampleDO configSubSampleDO = configSubSampleMapper.selectById(businessSubSampleDO.getConfigSubSampleId());
		if (StringUtils.isBlank(configSubSampleDO.getRecheckFlowNodeKey())) {
			throw new ServiceException(500, "未配置复检流程，不能发起复检委托！");
		}
		
		List<BusinessAssayTaskDataDO> newBusinessAssayTaskDataDOList = new ArrayList<>();
		List<BusinessAssayProjectDataDO> newBusinessAssayProjectDataDOList = new ArrayList<>();
		List<BusinessAssayParameterDataDO> newBusinessAssayParameterDataDOList  = new ArrayList<>();

		BusinessAssayTaskDataDO newBusinessAssayTaskDataDO = null;
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectMaxAnalysisCountByBusinessSubSampleIdAndConfigAssayMethodId(businessSubSampleId,	configAssayMethodId);
		List<Long> businessAssayTaskDataDOIdList = businessAssayTaskDataDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
		List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataDOList = businessAssayProjectDataMapper.selectByBusinessAssayTaskDataIds(businessAssayTaskDataDOIdList);
		List<Long> businessAssayProjectDataDOIdList = businessAssayProjectDataDOList.stream().map(m -> m.getId()).collect(Collectors.toList());
		List<BusinessAssayParameterDataDO> businessAssayParameterDataDOList = businessAssayParameterDataMapper.selectByBusinessAssayProjectDataIds(businessAssayProjectDataDOIdList);
		
		for (BusinessAssayTaskDataDO businessAssayTaskDataDO : businessAssayTaskDataDOList) {
			// 子样检测任务
			newBusinessAssayTaskDataDO = new BusinessAssayTaskDataDO();
			newBusinessAssayTaskDataDO.setId(IdWorker.getId());
			newBusinessAssayTaskDataDO.setBusinessBaseSampleId(businessAssayTaskDataDO.getBusinessBaseSampleId());
			newBusinessAssayTaskDataDO.setBusinessSubParentSampleId(businessAssayTaskDataDO.getBusinessSubParentSampleId());
			newBusinessAssayTaskDataDO.setBusinessSubSampleId(businessAssayTaskDataDO.getBusinessSubSampleId());
			newBusinessAssayTaskDataDO.setConfigAssayMethodId(businessAssayTaskDataDO.getConfigAssayMethodId());
			newBusinessAssayTaskDataDO.setAssayType(businessAssayTaskDataDO.getAssayType());
			newBusinessAssayTaskDataDO.setTaskType(businessAssayTaskDataDO.getTaskType());
			newBusinessAssayTaskDataDO.setConfigSampleFlowId(businessAssayTaskDataDO.getConfigSampleFlowId());
			newBusinessAssayTaskDataDO.setSampleFlowNodeKey(configSubSampleDO.getRecheckFlowNodeKey());
			newBusinessAssayTaskDataDO.setSampleFlowNodeTime(LocalDateTime.now());
			newBusinessAssayTaskDataDO.setAssayDepartmentId(businessAssayTaskDataDO.getAssayDepartmentId());
			newBusinessAssayTaskDataDO.setAssayDepartmentName(businessAssayTaskDataDO.getAssayDepartmentName());
			newBusinessAssayTaskDataDO.setAnalysisCount(businessAssayTaskDataDO.getAnalysisCount() + 1);
			newBusinessAssayTaskDataDO.setAssayProject(businessAssayTaskDataDO.getAssayProject());
			
			List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataDOList.stream().filter(f -> f.getBusinessAssayTaskDataId().equals(businessAssayTaskDataDO.getId())).collect(Collectors.toList());
			for (BusinessAssayProjectDataExtendRespVO businessAssayProjectData : businessAssayProjectDataList) {
				//检测项目
				BusinessAssayProjectDataDO businessAssayProjectDataDO = new BusinessAssayProjectDataDO();
				businessAssayProjectDataDO.setId(IdWorker.getId());
				businessAssayProjectDataDO.setBusinessAssayTaskDataId(newBusinessAssayTaskDataDO.getId());
				businessAssayProjectDataDO.setConfigAssayMethodProjectId(businessAssayProjectData.getConfigAssayMethodProjectId());
				businessAssayProjectDataDO.setDictionaryProjectId(businessAssayProjectData.getDictionaryProjectId());
				businessAssayProjectDataDO.setDataType(businessAssayProjectData.getDataType());
				businessAssayProjectDataDO.setDecimalPosition(businessAssayProjectData.getDecimalPosition());
				businessAssayProjectDataDO.setIsEnabled(businessAssayProjectData.getIsEnabled());
				businessAssayProjectDataDO.setIsNotAssessment(0);
				
				newBusinessAssayProjectDataDOList.add(businessAssayProjectDataDO);
				
				List<BusinessAssayParameterDataDO> businessAssayParameterDataList = businessAssayParameterDataDOList.stream().filter(f -> f.getBusinessAssayProjectDataId().equals(businessAssayProjectData.getId())).collect(Collectors.toList());
				for (BusinessAssayParameterDataDO businessAssayParameterData : businessAssayParameterDataList) {
					BusinessAssayParameterDataDO businessAssayParameterDataDO = new BusinessAssayParameterDataDO();
					businessAssayParameterDataDO.setId(IdWorker.getId());
					businessAssayParameterDataDO.setBusinessAssayProjectDataId(businessAssayProjectDataDO.getId());
					businessAssayParameterDataDO.setConfigAssayMethodProjectParameterId(businessAssayParameterData.getConfigAssayMethodProjectParameterId());
					businessAssayParameterDataDO.setDictionaryParameterId(businessAssayParameterData.getDictionaryParameterId());
					businessAssayParameterDataDO.setDataType(businessAssayParameterData.getDataType());
					businessAssayParameterDataDO.setDecimalPosition(businessAssayParameterData.getDecimalPosition());
					
					newBusinessAssayParameterDataDOList.add(businessAssayParameterDataDO);
				}
			}
			
			newBusinessAssayTaskDataDOList.add(newBusinessAssayTaskDataDO);
			
		}
		
		//修改判定状态
		//查询判定值
		List<BusinessSubSampleAssessmentExtendRespVO> businessSubSampleAssessmentList = businessSubSampleAssessmentMapper.selectByBusinessSubSampleIdAndConfigAssayMethodId(businessSubSampleId, configAssayMethodId);
		for (BusinessSubSampleAssessmentExtendRespVO businessSubSampleAssessmentExtendRespVO : businessSubSampleAssessmentList) {
			businessSubSampleAssessmentExtendRespVO.setAssessmentValue(null);
			businessSubSampleAssessmentExtendRespVO.setAssessmentStatus(QmsCommonConstant.IN_PROGRESS);
		}
		
		if (newBusinessAssayTaskDataDOList.size() > 0) {
			businessAssayTaskDataMapper.insertBatch(newBusinessAssayTaskDataDOList);
		}
		if (newBusinessAssayProjectDataDOList.size() > 0) {
			businessAssayProjectDataMapper.insertBatch(newBusinessAssayProjectDataDOList);
		}
		if (newBusinessAssayParameterDataDOList.size() > 0) {
			businessAssayParameterDataMapper.insertBatch(newBusinessAssayParameterDataDOList);
		}
		if (businessSubSampleAssessmentList.size() > 0) {
			List<BusinessSubSampleAssessmentDO> businessSubSampleAssessmentDOList = BeanUtil.copyToList(businessSubSampleAssessmentList, BusinessSubSampleAssessmentDO.class);
			businessSubSampleAssessmentMapper.updateBatch(businessSubSampleAssessmentDOList);
		}
		
	}

	@Resource
	private ConfigRuleMapper configRuleMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public JSONObject modifyParallelResultAssessment(Long businessSubSampleId, Long configAssayMethodId, Long businessSubSampleAssessmentId, List<Long> businessAssayProjectDataIds) {
		BusinessSubSampleAssessmentDO businessSubSampleAssessmentDO = businessSubSampleAssessmentMapper.selectById(businessSubSampleAssessmentId);
		
		//计算平均值
		ConfigRuleDO calculateAverageValue = configRuleMapper.selectLatestConfigRuleByCode("calculateAverageValue");
		
		BusinessAssayProjectDataReqVO businessAssayProjectDataSearch = new BusinessAssayProjectDataReqVO();
		businessAssayProjectDataSearch.setBusinessSubSampleId(businessSubSampleId);
		businessAssayProjectDataSearch.setConfigAssayMethodId(configAssayMethodId);
		businessAssayProjectDataSearch.setConfigAssayMethodProjectId(businessSubSampleAssessmentDO.getConfigAssayMethodProjectId());
		List<BusinessAssayProjectDataExtendRespVO> businessAssayProjectDataList = businessAssayProjectDataMapper.selectAnalysisBy(businessAssayProjectDataSearch);
		List<BusinessAssayProjectDataDO> businessAssayProjectDataDOList = BeanUtil.copyToList(businessAssayProjectDataList, BusinessAssayProjectDataDO.class);
		for (BusinessAssayProjectDataDO businessAssayProjectDataDO : businessAssayProjectDataDOList) {
			if (businessAssayProjectDataIds.contains(businessAssayProjectDataDO.getId())) {
				businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.NO);
			} else {
				businessAssayProjectDataDO.setIsNotAssessment(QmsCommonConstant.YES);
			}
		}
		// 排序并处理 null
        List<BigDecimal> sortedValues = businessAssayProjectDataDOList.stream()
        		.filter(f -> f.getIsNotAssessment().equals(QmsCommonConstant.NO))
                .map(v -> {
                    String val = v.getValue();
                    return val == null || val.trim().isEmpty() ? BigDecimal.ZERO : new BigDecimal(val.trim());
                })
                .sorted()
                .collect(Collectors.toList());
		
        // 获取小数精度
        int elementScale = businessSubSampleAssessmentDO.getDecimalPosition();
        // 计算代表值（均值\）
//        BigDecimal representativeValue = calculateRepresentativeValue(sortedValues, elementScale);
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("values", sortedValues);
        context.put("scale", elementScale);
        BigDecimal representativeValue = null;
		try {
			representativeValue = (BigDecimal) expressRunner.execute(calculateAverageValue.getExpression(), context, null, false, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        //设置判定值
        businessSubSampleAssessmentDO.setAssessmentValue(representativeValue.toPlainString());
        
		List<ConfigAssayMethodProjectAssessmentDO> configAssayMethodProjectAssessmentDOList = configAssayMethodProjectAssessmentMapper.selectList(new LambdaQueryWrapperX<ConfigAssayMethodProjectAssessmentDO>()
				.eq(ConfigAssayMethodProjectAssessmentDO::getConfigAssayMethodId, businessSubSampleAssessmentDO.getConfigAssayMethodId())
				.eq(ConfigAssayMethodProjectAssessmentDO::getConfigAssayMethodProjectId, businessSubSampleAssessmentDO.getConfigAssayMethodProjectId()));
        
        // 查找匹配的允差规则
        ConfigAssayMethodProjectAssessmentDO matchedRule = findMatchingRule(configAssayMethodProjectAssessmentDOList, representativeValue);
        if (matchedRule == null) {
            throw new ServiceException(500, "未找到超差判定区间范围，元素ID: " + businessSubSampleAssessmentDO.getDictionaryProjectId() + ", 值: " + representativeValue);
        }

        // 计算允差值
        BigDecimal allowValue;
        try {
            allowValue = allowanceCalculatorComponent.calculateAllowableValue(new BigDecimal(matchedRule.getMinimumValue()), new BigDecimal(matchedRule.getMaximumValue()), new BigDecimal(matchedRule.getMinimumToleraanceValue()), new BigDecimal(matchedRule.getMaximumToleraanceValue()), new BigDecimal(matchedRule.getToleraanceAdjustmentValue()), representativeValue, matchedRule.getIsUseFormula(), matchedRule.getFormula(), elementScale);
        } catch (Exception e) {
            throw new ServiceException(500, "超差判定计算出错，检测项目ID: " + businessSubSampleAssessmentDO.getDictionaryProjectId());
        }

        // 判断相邻差值是否超差
        for (int i = 0; i < sortedValues.size() - 1; i++) {
            BigDecimal diff = sortedValues.get(i + 1).subtract(sortedValues.get(i)).abs();
            if (diff.compareTo(allowValue) > 0) {
            	businessSubSampleAssessmentDO.setAssessmentStatus(QmsCommonConstant.EXCEEDS_TOLERANCE);
            	break;
            }
        }
		
        //更新判定值
        businessSubSampleAssessmentMapper.updateById(businessSubSampleAssessmentDO);
        //更新检测项目
        businessAssayProjectDataMapper.updateBatch(businessAssayProjectDataDOList);
		
		
		return getParallelResultAssessment(businessSubSampleId, configAssayMethodId);
	}


	
}
