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

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleAssayResultDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustProjectDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleAssayResultMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustDetailMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustProjectMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSampleEntrustRegistrationMapper;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleEntrustContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigEntrustSourceDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportTemplateDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDetailDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigReportTemplateMapper;
import com.zt.plat.module.qms.core.code.SequenceUtil;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleEntrustCreateDataCmp", name = "样品委托创建委托数据" )
public class SampleEntrustCreateDataCmp extends NodeComponent {
	
	@Resource
	private SequenceUtil sequenceUtil;
	
	@Resource
	private ConfigReportTemplateMapper configReportTemplateMapper;
	
	@Resource
	private BusinessSampleEntrustRegistrationMapper businessSampleEntrustRegistrationMapper;
	
	@Resource
	private BusinessSampleEntrustDetailMapper businessSampleEntrustDetailMapper;
	
	@Resource
	private BusinessSampleEntrustProjectMapper businessSampleEntrustProjectMapper;
	
	@Resource
	private BusinessSampleAssayResultMapper businessSampleAssayResultMapper;

	@Override
	public void process() throws Exception {
		//获取样品委托上下文
		SampleEntrustContext sampleEntrustContext = this.getContextBean(SampleEntrustContext.class);
		//样品委托
		BusinessSampleEntrustRegistrationDO sampleEntrustRegistration = sampleEntrustContext.getSampleEntrustRegistration();
		
		//获取委托来源配置
		ConfigEntrustSourceDO configEntrustSource = sampleEntrustContext.getConfigEntrustSourceById(sampleEntrustRegistration.getConfigEntrustSourceId());
		String codeRule = configEntrustSource.getCodeRule();
		ConfigReportTemplateDO configReportTemplate = configReportTemplateMapper.selectLatestConfigReportTemplateByKey(configEntrustSource.getConfigReportTemplateKey());
		
		//赋值id
		sampleEntrustRegistration.setId(IdWorker.getId());
		//设置委托单号
		sampleEntrustRegistration.setEntrustNumber(sequenceUtil.genCode(codeRule));
		sampleEntrustRegistration.setRegistrationStatus("in_progress");
		sampleEntrustRegistration.setConfigReportTemplateId(configReportTemplate.getId());
		sampleEntrustRegistration.setConfigReportTemplateKey(configReportTemplate.getKey());
		
		//sampleEntrustRegistration.setCreateOperator(sampleEntrustContext.getLoginRealname());
		
		List<BusinessSampleAssayResultDO> sampleAssayResultList = new ArrayList<>();

		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailList = sampleEntrustContext.getSampleEntrustDetailList();
		for (BusinessSampleEntrustDetailDO businessSampleEntrustDetail : sampleEntrustDetailList) {
			businessSampleEntrustDetail.setBusinessSampleEntrustRegistrationId(sampleEntrustRegistration.getId());

			if (StringUtils.isNotBlank(businessSampleEntrustDetail.getForecastResult())) {
				BusinessSampleAssayResultDO sampleAssayResult = new BusinessSampleAssayResultDO();
				sampleAssayResult.setBusinessSampleEntrustDetailId(businessSampleEntrustDetail.getId());
				sampleAssayResult.setData(businessSampleEntrustDetail.getForecastResult());
				sampleAssayResultList.add(sampleAssayResult);
			}
		}
		
		List<BusinessSampleEntrustProjectDO> sampleEntrustProjectList = sampleEntrustContext.getSampleEntrustProjectList();	
		for (BusinessSampleEntrustProjectDO businessSampleEntrustProject : sampleEntrustProjectList) {
			MaterialAssayStandardDetailDO materialAssayStandardDetail = sampleEntrustContext.getMaterialAssayStandardDetailById(businessSampleEntrustProject.getMaterialAssayStandardDetailId());
			businessSampleEntrustProject.setDictionaryProjectId(materialAssayStandardDetail.getDictionaryProjectId());
		}
		
		//保存样品委托登记
		businessSampleEntrustRegistrationMapper.insert(sampleEntrustRegistration);
		
		//保存样品明细
		if (CollUtil.isNotEmpty(sampleEntrustDetailList)) {
			businessSampleEntrustDetailMapper.insertBatch(sampleEntrustDetailList);
		}

		//保存检测项目
		if (CollUtil.isNotEmpty(sampleEntrustProjectList)) {
			businessSampleEntrustProjectMapper.insertBatch(sampleEntrustProjectList);
		}
		
		//保存来样元素
		if (CollUtil.isNotEmpty(sampleAssayResultList)) {
			businessSampleAssayResultMapper.insertBatch(sampleAssayResultList);
		}
	}

}
