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

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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleHandoverDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleHandoverDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportTemplateDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleHandoverDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigReportTemplateMapper;
import com.zt.plat.module.qms.common.dic.dal.dataobject.DictionaryBusinessDO;
import com.zt.plat.module.qms.common.dic.dal.mapper.DictionaryBusinessMapper;
import com.zt.plat.module.qms.core.code.SequenceUtil;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleSubHandoverCmp", name = "分样子样样品交接单")
public class SampleSubHandoverCmp extends NodeComponent {
	
	@Resource
	private SequenceUtil sequenceUtil;
	
	@Resource
	private ConfigReportTemplateMapper configReportTemplateMapper;
	
	@Resource
	private DictionaryBusinessMapper dictionaryBusinessMapper;
	

	@Override
	public void process() throws Exception {
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		String currentSampleFlowKey = sampleFlowContext.getCurrentSampleFlowKey();
		LocalDateTime currentDateTime = sampleFlowContext.getCurrentDateTime();
		LoginUser loginUser = sampleFlowContext.getLoginUser();
		String loginRealname = sampleFlowContext.getLoginRealname();
		List<BusinessSubSampleDO> busSubCsampleList = sampleFlowContext.getBusinessSubSampleList();
		ConfigSampleHandoverDO configSampleHandover = sampleFlowContext.getConfigSampleHandoverByFlowNodeKey(currentSampleFlowKey);
		
		List<Long> dictionaryBusinessIdList = busSubCsampleList.stream().map(m -> m.getDictionaryBusinessId()).distinct().collect(Collectors.toList());
		List<DictionaryBusinessDO> dictionaryBusinessList = dictionaryBusinessMapper.selectByIds(dictionaryBusinessIdList);
		
		BusinessSubSampleDO businessSubSampleDOFirst = busSubCsampleList.get(0);
		
		BusinessSampleHandoverDO businessSampleHandoverDO = new BusinessSampleHandoverDO();
		businessSampleHandoverDO.setId(IdWorker.getId());
		String handoverCode = sequenceUtil.genCode(configSampleHandover.getCodeRule());
		businessSampleHandoverDO.setCode(handoverCode);
		businessSampleHandoverDO.setName(configSampleHandover.getName());
		businessSampleHandoverDO.setConfigReportTemplateKey(configSampleHandover.getConfigReportTemplateKey());
		ConfigReportTemplateDO latestConfigSampleReportTemplate = configReportTemplateMapper.selectLatestConfigReportTemplateByKey(configSampleHandover.getConfigReportTemplateKey());
		businessSampleHandoverDO.setConfigReportTemplateId(latestConfigSampleReportTemplate.getId());
		businessSampleHandoverDO.setOperatorId(loginUser.getId());
		businessSampleHandoverDO.setOperator(loginRealname);
		businessSampleHandoverDO.setOperationTime(currentDateTime);
		businessSampleHandoverDO.setConfigSampleFlowId(businessSubSampleDOFirst.getConfigSampleFlowId());
		businessSampleHandoverDO.setSampleFlowNodeKey(currentSampleFlowKey);
		
		sampleFlowContext.setBusinessSampleHandover(businessSampleHandoverDO);
		
		List<BusinessSampleHandoverDetailDO> businessSampleHandoverDetailList = new ArrayList<>();
		BusinessSampleHandoverDetailDO businessSampleHandoverDetailDO = null;
		for (BusinessSubSampleDO businessSubSample : busSubCsampleList) {
			DictionaryBusinessDO dictionaryBusiness = dictionaryBusinessList.stream().filter(f -> f.getId().equals(businessSubSample.getDictionaryBusinessId())).findFirst().orElse(null);
			
			businessSampleHandoverDetailDO = new BusinessSampleHandoverDetailDO();
			businessSampleHandoverDetailDO.setId(IdWorker.getId());
			businessSampleHandoverDetailDO.setBusinessSampleHandoverId(businessSampleHandoverDO.getId());
			businessSampleHandoverDetailDO.setBusinessSubSampleId(businessSubSample.getId());
			businessSampleHandoverDetailDO.setSampleName(businessSubSample.getSampleName());
			businessSampleHandoverDetailDO.setSampleCode(businessSubSample.getSampleReturnCode());
			businessSampleHandoverDetailDO.setBalanceCode(businessSubSample.getLastBalanceCode());
			businessSampleHandoverDetailDO.setSampleWeight(businessSubSample.getLastSampleWeight());
			businessSampleHandoverDetailDO.setDictionaryBusinessId(businessSubSample.getDictionaryBusinessId());
			businessSampleHandoverDetailDO.setDictionaryBusinessName(dictionaryBusiness.getName());
			
			businessSampleHandoverDetailList.add(businessSampleHandoverDetailDO);
		}
		
		sampleFlowContext.setBusinessSampleHandoverDetailList(businessSampleHandoverDetailList);

	}

	@Override
	public boolean isAccess() {
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		String currentSampleFlowKey = sampleFlowContext.getCurrentSampleFlowKey();
		ConfigSampleHandoverDO configSampleHandover = sampleFlowContext.getConfigSampleHandoverByFlowNodeKey(currentSampleFlowKey);
		//交接单配置不为空，则生成交接单
		return configSampleHandover != null;
	}

}
