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

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

import org.apache.commons.lang3.StringUtils;

import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
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.MaterialAssayStandardDetailDO;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleEntrustUpdateDataCmp", name = "样品委托更新委托数据" )
public class SampleEntrustUpdateDataCmp extends NodeComponent {
	
	@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();
		
		//先删除旧的
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailOldList = businessSampleEntrustDetailMapper.selectList(new LambdaQueryWrapperX<BusinessSampleEntrustDetailDO>().eq(BusinessSampleEntrustDetailDO::getBusinessSampleEntrustRegistrationId, sampleEntrustRegistration.getId()));
		if (CollUtil.isNotEmpty(sampleEntrustDetailOldList)) {
			List<Long> sampleEntrustDetailIdList = sampleEntrustDetailOldList.stream().map(m -> m.getId()).collect(Collectors.toList());
			List<BusinessSampleEntrustProjectDO> sampleEntrustProjectList = businessSampleEntrustProjectMapper.selectList(new LambdaQueryWrapperX<BusinessSampleEntrustProjectDO>().in(BusinessSampleEntrustProjectDO::getBusinessSampleEntrustDetailId, sampleEntrustDetailIdList));
			if (CollUtil.isNotEmpty(sampleEntrustProjectList)) {
				List<Long> sampleEntrustProjectIdList = sampleEntrustProjectList.stream().map(m -> m.getId()).collect(Collectors.toList());
				//删除样品对应的检验项目
				businessSampleEntrustProjectMapper.physicalDeleteByIds(sampleEntrustProjectIdList);
			}
			List<BusinessSampleAssayResultDO> sampleAssayResultList = businessSampleAssayResultMapper.selectList(new LambdaQueryWrapperX<BusinessSampleAssayResultDO>().in(BusinessSampleAssayResultDO::getBusinessSampleEntrustDetailId, sampleEntrustDetailIdList));
			if (CollUtil.isNotEmpty(sampleAssayResultList)) {
				List<Long> sampleAssayResultIdList = sampleAssayResultList.stream().map(m -> m.getId()).collect(Collectors.toList());
				//删除来样品位
				businessSampleAssayResultMapper.physicalDeleteByIds(sampleAssayResultIdList);
			}
			
			//删除委托登记明细
			businessSampleEntrustDetailMapper.physicalDeleteByIds(sampleEntrustDetailIdList);
		}
		
		//保存更新新的
		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.updateById(sampleEntrustRegistration);
		
		//保存样品明细
		if (CollUtil.isNotEmpty(sampleEntrustDetailList)) {
			businessSampleEntrustDetailMapper.insertBatch(sampleEntrustDetailList);
		}

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

	}

}
