package digital.laboratory.platform.entrustment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.common.core.constant.CommonConstants;
import digital.laboratory.platform.sewage.entity.SewageJobIdentificationMaterial;
import digital.laboratory.platform.sys.entity.DrugLite;
import digital.laboratory.platform.entrustment.entity.*;
import digital.laboratory.platform.entrustment.handler.AppStartupRunner;
import digital.laboratory.platform.entrustment.mapper.SampleBoxMapper;
import digital.laboratory.platform.entrustment.mapper.SampleMapper;
import digital.laboratory.platform.entrustment.service.SampleService;
import digital.laboratory.platform.sys.entity.entrustment.IMAdditionalProperty;
import digital.laboratory.platform.sys.entity.entrustment.Sample;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 检验用的样本服务实现类
 *
 * @author Zhang Xiaolong created at  2022-07-26
 * @describe 检验用的样本 服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SampleServiceImpl extends ServiceImpl<SampleMapper, Sample> implements SampleService {
//	private final SampleBoxService sampleBoxService;
	private final SampleBoxMapper sampleBoxMapper;

	@Override
	public String getNewProvidedSampleNo(String IdentificationMaterialNo, int sampleSerialNumber) {
		String pureIMNo = StrUtil.removePrefixIgnoreCase(IdentificationMaterialNo, CommonConstants.CODE_PREFIX_IDENTIFICATION_MATERIAL);	// 去掉司法鉴定委托检材的前缀 JC
		pureIMNo = StrUtil.removePrefixIgnoreCase(pureIMNo, CommonConstants.CODE_PREFIX_SEWAGE_IDENTIFICATION_MATERIAL);	// 去掉污水任务检材的前缀 WJ

		String prefix = CommonConstants.CODE_PREFIX_PROVIDED_SAMPLE + pureIMNo;
		String newCode = prefix + String.format("%d", sampleSerialNumber);
		return newCode;
	}

	@Override
	public String getNewSampleNo(String IdentificationMaterialNo, int sampleSerialNumber) {
		String pureIMNo = StrUtil.removePrefixIgnoreCase(IdentificationMaterialNo, CommonConstants.CODE_PREFIX_IDENTIFICATION_MATERIAL);	// 去掉司法鉴定委托检材的前缀 JC
		pureIMNo = StrUtil.removePrefixIgnoreCase(pureIMNo, CommonConstants.CODE_PREFIX_SEWAGE_IDENTIFICATION_MATERIAL);	// 去掉污水任务检材的前缀 WJ

		String prefix = CommonConstants.CODE_PREFIX_SAMPLE + pureIMNo;
		String newCode = prefix + String.format("%d", sampleSerialNumber);
		return newCode;
	}
	//构建样本时的一些基本参数，污水检材转换为样本的参数，司法送检检材转化为样本的参数
	//复制对象属性

	/**
	 * 根据检材创建样本
	 *
	 * @param entrustmentIdentificationMaterial
	 *
	 * @return
	 */
	@Override
	public List<Sample> createSample(EntrustmentIdentificationMaterial entrustmentIdentificationMaterial,String orgId,String dlpUserId) {
		// 生成 样本1
		List<Sample> sampleList=new ArrayList<Sample>();
		//if ((entrustmentIdentificationMaterial.getSample1Present() != null) && (entrustmentIdentificationMaterial.getSample1Present())) {
			//SampleBox sb1 = sampleBoxMapper.selectById(entrustmentIdentificationMaterial.getSample1BoxId());
			Sample sample1 = new Sample();
			sample1.setId(IdWorker.get32UUID().toUpperCase());
			sample1.setIdentificationMaterialId(entrustmentIdentificationMaterial.getId());
			sample1.setSource("entrustment");
			//sample1.setSampleNo(entrustmentIdentificationMaterial.getSample1No());
			sample1.setSampleNo(entrustmentIdentificationMaterial.getAcceptNo());
			sample1.setCandidateDrugs(entrustmentIdentificationMaterial.getCandidateDrugs());
			sample1.setFundName(entrustmentIdentificationMaterial.getFundName());
			sample1.setFundQuantity(entrustmentIdentificationMaterial.getSample1FundQuantity());
			sample1.setFundUnit(entrustmentIdentificationMaterial.getFundUnit());
			sample1.setQuantity(entrustmentIdentificationMaterial.getSample1Quantity());
			sample1.setUnit(entrustmentIdentificationMaterial.getUnit());
			sample1.setAdditionalProperties(entrustmentIdentificationMaterial.getAdditionalProperties());
			sample1.setDrawWay(entrustmentIdentificationMaterial.getDrawWay());
			sample1.setBoxId(entrustmentIdentificationMaterial.getSample1BoxId());
			//sample1.setSampleType(sb1.getSampleType());
			sample1.setStorageMethod(entrustmentIdentificationMaterial.getStorageMethod());
			sample1.setAcceptTime(entrustmentIdentificationMaterial.getAcceptTime());

			sample1.setSampleType("A");//设置类型
			sample1.setEntrustmentId(entrustmentIdentificationMaterial.getEntrustmentId()); //设置任务ID
			sample1.setHolder(dlpUserId);
			sample1.setOrgId(orgId);
			sample1.setName(entrustmentIdentificationMaterial.getName());
			sample1.setAcceptNo(entrustmentIdentificationMaterial.getAcceptNo());
			this.save(sample1);
			sampleList.add(sample1);
		//}

		// 生成 样本2
		if ((entrustmentIdentificationMaterial.getSample2Present() != null) && (entrustmentIdentificationMaterial.getSample2Present())) {
			// hasSample = true;
			//SampleBox sb2 = sampleBoxMapper.selectById(entrustmentIdentificationMaterial.getSample2BoxId());
			Sample sample2 = new Sample();
			sample2.setId(IdWorker.get32UUID().toUpperCase());
			sample2.setIdentificationMaterialId(entrustmentIdentificationMaterial.getId());
			sample2.setSource("entrustment");
			//sample2.setSampleNo(entrustmentIdentificationMaterial.getSample2No());
			sample2.setSampleNo(entrustmentIdentificationMaterial.getAcceptNo());
			sample2.setCandidateDrugs(entrustmentIdentificationMaterial.getCandidateDrugs());
			sample2.setFundName(entrustmentIdentificationMaterial.getFundName());
			sample2.setFundQuantity(entrustmentIdentificationMaterial.getSample2FundQuantity());
			sample2.setFundUnit(entrustmentIdentificationMaterial.getFundUnit());
			sample2.setQuantity(entrustmentIdentificationMaterial.getSample2Quantity());
			sample2.setUnit(entrustmentIdentificationMaterial.getUnit());
			sample2.setAdditionalProperties(entrustmentIdentificationMaterial.getAdditionalProperties());
			sample2.setDrawWay(entrustmentIdentificationMaterial.getDrawWay());
			sample2.setBoxId(entrustmentIdentificationMaterial.getSample2BoxId());
			//sample2.setSampleType(sb2.getSampleType()); //取消了包装，所以这里没有盒子记录了，直接第一个样设置成A，第2个样设置成B
			sample2.setStorageMethod(entrustmentIdentificationMaterial.getStorageMethod());
			sample2.setAcceptTime(entrustmentIdentificationMaterial.getAcceptTime());


			sample2.setSampleType("B");//设置类型
			sample2.setEntrustmentId(entrustmentIdentificationMaterial.getEntrustmentId()); //设置任务ID
			sample2.setHolder(dlpUserId);
			sample2.setOrgId(orgId);
			sample2.setName(entrustmentIdentificationMaterial.getName());
			sample2.setAcceptNo(entrustmentIdentificationMaterial.getAcceptNo());
			this.save(sample2);
			sampleList.add(sample2);
		}

		return sampleList;
	}


	/**
	 * 根据污水检材创建样本
	 *
	 * @param sewageJobIdentificationMaterial
	 *
	 * @return
	 */
//	@Override
	public List<Sample> createSample(SewageJobIdentificationMaterial sewageJobIdentificationMaterial, String dlpUserId) {

		//污水的编号重新生成，逻辑发生改变，现在一个检材就对应一个样本  8-31

		List<Sample> sampleList=new ArrayList<Sample>();
		List<DrugLite> candidateDrugs = JSON.parseArray(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_CANDIDATE_DRUGS), DrugLite.class);
		List<IMAdditionalProperty> additionalProperties = JSON.parseArray(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_ADDITIONAL_PROPERTIES), IMAdditionalProperty.class);
		BigDecimal quantity = new BigDecimal(Double.parseDouble(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_QUANTITY)));
		// 生成 样本1
		//if ((sewageJobIdentificationMaterial.getSample1Present() != null) && (sewageJobIdentificationMaterial.getSample1Present())) {
//			SampleBox sb1 = sampleBoxMapper.selectById(sewageJobIdentificationMaterial.getSample1BoxId());
			Sample sample1 = new Sample();
			sample1.setId(IdWorker.get32UUID().toUpperCase());
			sample1.setIdentificationMaterialId(sewageJobIdentificationMaterial.getId());
			sample1.setSource("sewageJob");
			//sample1.setSampleNo(sewageJobIdentificationMaterial.getSample1No());
			sample1.setSampleNo(sewageJobIdentificationMaterial.getImNo());//编号规则改变，改成新的编号

			sample1.setCandidateDrugs(candidateDrugs);
			sample1.setFundName(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_NAME));
			sample1.setFundQuantity(Integer.parseInt(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_QUANTITY)));
			sample1.setFundUnit(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_UNIT));
			sample1.setQuantity(quantity);
			sample1.setUnit(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_UNIT));
			sample1.setAdditionalProperties(additionalProperties);
			sample1.setDrawWay(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_DRAWWAY));

			sample1.setBoxId(sewageJobIdentificationMaterial.getSample1BoxId());
//			sample1.setSampleType(sb1.getSampleType());
			sample1.setStorageMethod(sewageJobIdentificationMaterial.getStorageMethod());
			sample1.setAcceptTime(sewageJobIdentificationMaterial.getAcceptTime());

			sample1.setSampleType(sewageJobIdentificationMaterial.getSampleType().equals("1")?"A":"B");//设置类型
			sample1.setSewageJobId(sewageJobIdentificationMaterial.getJobId()); //设置任务ID
			sample1.setHolder(dlpUserId);
			sample1.setOrgId(sewageJobIdentificationMaterial.getDeliverOrgId());
			sample1.setName(sewageJobIdentificationMaterial.getSampleName());

			this.save(sample1);
			sampleList.add(sample1);
		//}

		// 生成 样本2
		/*if ((sewageJobIdentificationMaterial.getSample2Present() != null) && (sewageJobIdentificationMaterial.getSample2Present())) {
			// hasSample = true;
//			SampleBox sb2 = sampleBoxMapper.selectById(sewageJobIdentificationMaterial.getSample2BoxId());
			Sample sample2 = new Sample();
			sample2.setId(IdWorker.get32UUID().toUpperCase());
			sample2.setIdentificationMaterialId(sewageJobIdentificationMaterial.getId());
			sample2.setSource("sewageJob");
			sample2.setSampleNo(sewageJobIdentificationMaterial.getSample2No());

			sample2.setCandidateDrugs(candidateDrugs);
			sample2.setFundName(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_NAME));
			sample2.setFundQuantity(Integer.parseInt(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_QUANTITY)));
			sample2.setFundUnit(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_FUND_UNIT));
			sample2.setQuantity(quantity);
			sample2.setUnit(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_UNIT));
			sample2.setAdditionalProperties(additionalProperties);
			sample2.setDrawWay(AppStartupRunner.getCfg(CommonConstants.DLP_CODE_ENTRUSTMENT_SEWAGE_JOB_IDENTIFICATION_MATERIAL_DRAWWAY));

			sample2.setBoxId(sewageJobIdentificationMaterial.getSample2BoxId());
//			sample2.setSampleType(sb2.getSampleType());
			sample2.setStorageMethod(sewageJobIdentificationMaterial.getStorageMethod());
			sample2.setAcceptTime(sewageJobIdentificationMaterial.getAcceptTime());

			sample2.setSampleType("B");//设置类型
			sample2.setSewageJobId(sewageJobIdentificationMaterial.getJobId()); //设置任务ID
			sample2.setHolder(dlpUserId);
			sample2.setOrgId(sewageJobIdentificationMaterial.getDeliverOrgId());
			sample2.setName(sewageJobIdentificationMaterial.getSampleName());
			this.save(sample2);
			sampleList.add(sample2);
		}*/

		return sampleList;
	}

}
