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

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
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());
		ConfigReportTemplateDO configReportTemplate = configReportTemplateMapper.selectLatestConfigReportTemplateByKey(configEntrustSource.getConfigReportTemplateKey());
		
		String entrustNumber = null;
		if (StringUtils.isNotBlank(configEntrustSource.getCodeField())) {
			String externalInfomation = sampleEntrustRegistration.getExternalInfomation();
			JSONObject extJsonObject = JSON.parseObject(externalInfomation);
			entrustNumber = extJsonObject.getString(configEntrustSource.getCodeField());
		} else {
			String codeRule = configEntrustSource.getCodeRule();
			entrustNumber = sequenceUtil.genCode(codeRule);
		}
		
		//赋值id
		sampleEntrustRegistration.setId(IdWorker.getId());
		//设置委托单号
		sampleEntrustRegistration.setEntrustNumber(entrustNumber);
		
		sampleEntrustRegistration.setRegistrationStatus("in_progress");
		sampleEntrustRegistration.setConfigReportTemplateId(configReportTemplate.getId());
		sampleEntrustRegistration.setConfigReportTemplateKey(configReportTemplate.getKey());
		
		//sampleEntrustRegistration.setCreateOperator(sampleEntrustContext.getLoginRealname());
		
		List<BusinessSampleAssayResultDO> sampleAssayResultList = new ArrayList<>();

		//提取委托单号
	    String year = null;
	    String number = null;
        Pattern pattern = Pattern.compile("\\((\\d+)\\)第(\\d+)号");
        //一般委托或仲裁委托才提取
        if ("xsqywtybwt".equals(configEntrustSource.getKey()) || "xsqywtzcwt".equals(configEntrustSource.getKey())) {
    	    Matcher matcher = pattern.matcher(sampleEntrustRegistration.getEntrustNumber());
    	    if (matcher.find()) {
    	        year = matcher.group(1); // "2026"
    	        number = matcher.group(2); // "0027"
    	    }
		}
		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);
			}

			//一般委托
			if ("xsqywtybwt".equals(configEntrustSource.getKey())) {
				String sampleCode = "W" + year + number + "-" + StringUtils.leftPad(businessSampleEntrustDetail.getSort().toString(), 2, "0");
				businessSampleEntrustDetail.setSampleCode(sampleCode);
			} else if ("xsqywtzcwt".equals(configEntrustSource.getKey())) {//仲裁委托
				String sampleCode = "ZW" + year + number + "-" + StringUtils.leftPad(businessSampleEntrustDetail.getSort().toString(), 2, "0");
				businessSampleEntrustDetail.setSampleCode(sampleCode);
			} else if ("ztgmwt".equals(configEntrustSource.getKey())) {//中铜国贸商检委托，来样编号与样品编号一致
				businessSampleEntrustDetail.setSampleCode(businessSampleEntrustDetail.getEntrustSampleCode());//样品编号与来样编号一致
			}
		}
		
		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);
		}
	}

}
