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

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

import com.alibaba.fastjson2.JSON;
import com.ql.util.express.ExpressRunner;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

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.BusinessHandoverRecordSubMapper;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleFlowDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigRuleMapper;
import com.zt.plat.module.qms.business.config.service.ConfigSampleFlowService;
//import com.zt.plat.module.qms.business.config.service.ConfigSubSampleService;
import com.zt.plat.module.qms.core.sampleflow.SampleFlowDefinition;
import com.zt.plat.module.qms.core.sampleflow.SampleFlowNode;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleSubProcessUpdateCmp", name = "子样流程节点更新")
public class SampleSubProcessUpdateCmp extends NodeComponent {
	
//	@Resource
//	private ConfigSubSampleService configSubSampleService;
	
	@Resource
	private ExpressRunner expressRunner;
	
	@Resource
	private ConfigRuleMapper configRuleMapper;
	
	@Resource
	private ConfigSampleFlowService configSampleFlowService;
	
	@Resource
	private BusinessHandoverRecordSubMapper businessHandoverRecordSubMapper;

	@Override
	public void process() throws Exception {
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		String currentSampleFlowKey = sampleFlowContext.getCurrentSampleFlowKey();
//		LoginUser loginUser = sampleFlowContext.getLoginUser();
//		String loginRealname = sampleFlowContext.getLoginRealname();
		LocalDateTime currentDateTime = sampleFlowContext.getCurrentDateTime();
		List<Long> sampleIdList = sampleFlowContext.getSampleIdList();
		List<BusinessSubSampleDO> busSubCsampleList = sampleFlowContext.getBusinessSubSampleList();
		List<BusinessAssayTaskDataDO> businessAssayTaskDataList = sampleFlowContext.getBusinessAssayTaskDataList();
		//查询历史交接记录
		List<BusinessHandoverRecordSubDO> businessHandoverRecordSubDOList = businessHandoverRecordSubMapper.selectByBusinessSubSampleIds(sampleIdList);
		for (BusinessSubSampleDO businessSubSample : busSubCsampleList) {
			ConfigSampleFlowDO configSampleFlow = configSampleFlowService.getConfigSampleFlow(businessSubSample.getConfigSampleFlowId());
			String configSampleFlowContent = configSampleFlow.getContent();
			//样品流程定义
			SampleFlowDefinition sampleFlowDefinition = JSON.parseObject(configSampleFlowContent, SampleFlowDefinition.class);
			List<SampleFlowNode> flowNodeList = sampleFlowDefinition.getFlowNodeList();
			SampleFlowNode flowNode = flowNodeList.stream().filter(f -> f.getNodeKey().equals(currentSampleFlowKey)).findFirst().orElse(null);
			List<SampleFlowNode> nextFlowNodeList = flowNode.getNextFlowNodeList();
			SampleFlowNode nextFlowNode = nextFlowNodeList.stream().filter(f -> f.getIsDefault()).findFirst().orElse(null);
			businessSubSample.setSampleFlowNodeKey(nextFlowNode.getNodeKey());
			businessSubSample.setSampleFlowNodeTime(currentDateTime);
			
			List<BusinessAssayTaskDataDO> subSampleAssayTaskDataList = businessAssayTaskDataList.stream().filter(f -> f.getBusinessSubSampleId().equals(businessSubSample.getId())).collect(Collectors.toList());
			for (BusinessAssayTaskDataDO businessAssayTaskDataDO : subSampleAssayTaskDataList) {
				businessAssayTaskDataDO.setSampleFlowNodeKey(nextFlowNode.getNodeKey());
				businessAssayTaskDataDO.setSampleFlowNodeTime(currentDateTime);

			}
		}

	}

}
