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

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessHandoverRecordSubDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessHandoverRecordSubMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleMapper;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowInfo;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSubSampleMapper;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleSubCheckCmp", name = "子样检查")
public class SampleSubCheckCmp extends NodeComponent {
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private BusinessHandoverRecordSubMapper businessHandoverRecordSubMapper;
	
	@Resource
	private ConfigSubSampleMapper configSubSampleMapper;

	@Override
	public void process() throws Exception {
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		String currentSampleFlowKey = sampleFlowContext.getCurrentSampleFlowKey();
		List<Long> sampleIdList = sampleFlowContext.getSampleIdList();
		if (CollUtil.isEmpty(sampleIdList)) {
			throw new ServiceException(1_032_100_000, "样品ID列表不允许为空");
		}
		List<BusinessSubSampleDO> businessSubSampleDOList = businessSubSampleMapper.selectByIds(sampleIdList);
		
		//赋值样品重量
		for (BusinessSubSampleDO businessSubSampleDO : businessSubSampleDOList) {
			SampleFlowInfo sampleFlowInfo = sampleFlowContext.getSampleFlowInfoById(businessSubSampleDO.getId());
			if (sampleFlowInfo != null) {
				businessSubSampleDO.setLastSampleWeight(sampleFlowInfo.getSampleWeight());
			}
		}
		
		boolean isSampleFlowKeyFail = businessSubSampleDOList.stream().anyMatch(p -> !p.getSampleFlowNodeKey().equals(currentSampleFlowKey));
		if (isSampleFlowKeyFail) {
			throw new ServiceException(1_032_100_000, "样品列表与当前流程不符");
		}
		
		List<Long> configSubSampleIdList = businessSubSampleDOList.stream().map(m -> m.getConfigSubSampleId()).distinct().collect(Collectors.toList());
		List<ConfigSubSampleDO> configSubSampleList = configSubSampleMapper.selectByIds(configSubSampleIdList);
		
		List<BusinessAssayTaskDataDO> businessAssayTaskDataList = new ArrayList<>();
		//如果是班组收样或者内部简码时，才需要更新分析任务节点
		if (QmsCommonConstant.FLOW_NODE_TEAM_RECEIVE.equals(currentSampleFlowKey) || QmsCommonConstant.FlOW_NODE_SAMPLE_ENCRYPT.equals(currentSampleFlowKey)) {
			businessAssayTaskDataList = businessAssayTaskDataMapper.selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>().in(BusinessAssayTaskDataDO::getBusinessSubSampleId, sampleIdList));
		}
		
		//上一次交接记录
		List<Long> upSampleRecordIdList = businessSubSampleDOList.stream().filter(f -> f.getUpSampleRecordId() != null).map(m -> m.getUpSampleRecordId()).collect(Collectors.toList());
		if (CollUtil.isNotEmpty(upSampleRecordIdList)) {
			List<BusinessHandoverRecordSubDO> businessHandoverRecordSubList = businessHandoverRecordSubMapper.selectByIds(upSampleRecordIdList);
			sampleFlowContext.setUpdateBusinessHandoverRecordSubList(businessHandoverRecordSubList);
		}
		
		
		sampleFlowContext.setBusinessSubSampleList(businessSubSampleDOList);
		sampleFlowContext.setBusinessAssayTaskDataList(businessAssayTaskDataList);
		sampleFlowContext.setConfigSubSampleList(configSubSampleList);
	}

}
