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.bean.BeanUtil;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
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.liteflow.param.SampleEntrustDetail;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustDetailProject;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustParam;
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.MaterialAssayStandardDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardDetailDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.MaterialAssayStandardMethodDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigEntrustSourceMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardDetailMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.MaterialAssayStandardMethodMapper;
import jakarta.annotation.Resource;

/**
 * 样品委托上下文初始化
 */
@LiteflowComponent(id = "sampleEntrustContextInitCmp", name = "样品委托上下文初始化")
public class SampleEntrustContextInitCmp extends NodeComponent {
	
	@Resource
	private ConfigEntrustSourceMapper configEntrustSourceMapper;
	
	@Resource
	private MaterialAssayStandardMapper materialAssayStandardMapper;
	
	@Resource
	private MaterialAssayStandardDetailMapper materialAssayStandardDetailMapper;
	
	@Resource
	private MaterialAssayStandardMethodMapper materialAssayStandardMethodMapper;

	@Override
	public void process() throws Exception {
		//租户id
		Long tenantId = TenantContextHolder.getRequiredTenantId();
		
		//当前登录用户
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		
		//当前登录用户昵称
		String nickName = SecurityFrameworkUtils.getLoginUserNickname();
		
		//获取样品委托参数
		SampleEntrustParam sampleEntrustParam = this.getRequestData();
		
		if (sampleEntrustParam == null) {
			throw new ServiceException(500, "样品委托参数不允许为空");
		}
		
		//获取样品委托上下文
		SampleEntrustContext sampleEntrustContext = this.getContextBean(SampleEntrustContext.class);
		sampleEntrustContext.setTenantId(tenantId);
		sampleEntrustContext.setLoginUser(loginUser);
		sampleEntrustContext.setLoginRealname(nickName);
		
		//样品提交时是否收样
		sampleEntrustContext.setIsReceiveSample(sampleEntrustParam.getIsReceiveSample());
		//样品提交时是否送样
		sampleEntrustContext.setIsSendSample(sampleEntrustParam.getIsSendSample());
		
		//委托来源配置
		List<ConfigEntrustSourceDO> configEntrustSourceList = configEntrustSourceMapper.selectList();
		sampleEntrustContext.setConfigEntrustSourceList(configEntrustSourceList);
				
		//物料检验标准
		List<MaterialAssayStandardDO> materialAssayStandardList = materialAssayStandardMapper.selectList();
		sampleEntrustContext.setMaterialAssayStandardList(materialAssayStandardList);
		
		//物料检验标准项
		List<MaterialAssayStandardDetailDO> materialAssayStandardDetailList = materialAssayStandardDetailMapper.selectList();
		sampleEntrustContext.setMaterialAssayStandardDetailList(materialAssayStandardDetailList);
		
		//物料检验标准方法
		List<MaterialAssayStandardMethodDO> materialAssayStandardMethodList = materialAssayStandardMethodMapper.selectList();
		sampleEntrustContext.setMaterialAssayStandardMethodList(materialAssayStandardMethodList);
		
		//委托登记
		BusinessSampleEntrustRegistrationDO sampleEntrustRegistration = BeanUtil.copyProperties(sampleEntrustParam, BusinessSampleEntrustRegistrationDO.class);	
		
		//委托来源id为空并且委托来源key也为空
		if (sampleEntrustParam.getConfigEntrustSourceId() == null && StringUtils.isBlank(sampleEntrustParam.getConfigEntrustSourceKey())) {
			throw new ServiceException(500, "委托来源id或委托来源key为空");
		}
		
		//委托来源的id为空
		if (sampleEntrustParam.getConfigEntrustSourceId() == null && StringUtils.isNotBlank(sampleEntrustParam.getConfigEntrustSourceKey()) && StringUtils.isNotBlank(sampleEntrustParam.getEntrustType())) {
			ConfigEntrustSourceDO configEntrustSourceDO = configEntrustSourceList.stream().filter(f -> f.getKey().equals(sampleEntrustParam.getConfigEntrustSourceKey()) && f.getEntrustType().equals(sampleEntrustParam.getEntrustType())).findFirst().orElse(null);
			if (configEntrustSourceDO == null) {
				throw new ServiceException(500, "样品委托来源不正确");
			}
			sampleEntrustRegistration.setConfigEntrustSourceId(configEntrustSourceDO.getId());
		}
		
		//赋值委托来源id
		sampleEntrustContext.setSampleEntrustRegistration(sampleEntrustRegistration);
		
		List<SampleEntrustDetail> detailList = sampleEntrustParam.getSampleEntrustDetailList();
		
		//委托登记样品明细的检测项目明细
		List<BusinessSampleEntrustProjectDO> sampleEntrustProjectList = new ArrayList<>();		
		for (SampleEntrustDetail sampleEntrustDetail : detailList) {
			if (sampleEntrustDetail.getId() == null) {
				sampleEntrustDetail.setId(IdWorker.getId());
			}
			List<SampleEntrustDetailProject> projectList = sampleEntrustDetail.getSampleEntrustDetailProjectList();
			for (SampleEntrustDetailProject sampleEntrustDetailProject : projectList) {
				if (sampleEntrustDetailProject.getBusinessSampleEntrustDetailId() == null) {
					sampleEntrustDetailProject.setBusinessSampleEntrustDetailId(sampleEntrustDetail.getId());
				}
			}
			sampleEntrustProjectList.addAll(BeanUtil.copyToList(projectList, BusinessSampleEntrustProjectDO.class));
		}
		sampleEntrustContext.setSampleEntrustProjectList(sampleEntrustProjectList);

	    //委托登记样品明细
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailList = BeanUtil.copyToList(detailList, BusinessSampleEntrustDetailDO.class);
		sampleEntrustContext.setSampleEntrustDetailList(sampleEntrustDetailList);
	}

}
