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

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.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.liteflow.param.SampleFlowInfo;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleFlowParam;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSampleHandoverDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSampleHandoverMapper;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleFlowContextInitCmp", name = "样品流转上下文初始化")
public class SampleFlowContextInitCmp extends NodeComponent {
	
	@Resource
	private ConfigSampleHandoverMapper configSampleHandoverMapper;

	@Override
	public void process() throws Exception {
		//租户id
		Long tenantId = TenantContextHolder.getRequiredTenantId();
		
		//当前登录用户
		LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
		
		//当前登录用户昵称
		String nickName = SecurityFrameworkUtils.getLoginUserNickname();
		
		//样品流转请求参数
		SampleFlowParam sampleFlowParam = this.getRequestData();
		
		if (StringUtils.isBlank(sampleFlowParam.getCurrentSampleFlowKey())) {
			new ServiceException(1_032_100_000, "样品流程节点currentSampleFlowKey不允许为空");
		}
		List<SampleFlowInfo> sampleFlowInfoList = sampleFlowParam.getSampleFlowInfoList();
		if (CollUtil.isEmpty(sampleFlowInfoList)) {
			new ServiceException(1_032_100_000, "样品流转列表不允许为空");
		}
		List<Long> sampleIdList = sampleFlowInfoList.stream().map(m -> m.getId()).collect(Collectors.toList());
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		sampleFlowContext.setTenantId(tenantId);
		sampleFlowContext.setLoginUser(loginUser);
		sampleFlowContext.setLoginRealname(nickName);
		if (sampleFlowParam.getSpecifyDateTime() != null) {
			sampleFlowContext.setCurrentDateTime(sampleFlowParam.getSpecifyDateTime());
		}
		sampleFlowContext.setSampleSourceType(sampleFlowParam.getSampleSourceType());
		sampleFlowContext.setCurrentSampleFlowKey(sampleFlowParam.getCurrentSampleFlowKey());
		sampleFlowContext.setSampleIdList(sampleIdList);
		sampleFlowContext.setSampleFlowInfoList(sampleFlowInfoList);
		sampleFlowContext.setSendSampleOper(sampleFlowParam.getSendSampleOper());
		sampleFlowContext.setReceiveSampleOper(sampleFlowParam.getReceiveSampleOper());
		
		List<ConfigSampleHandoverDO> configSampleHandoverList = configSampleHandoverMapper.selectList();
		sampleFlowContext.setConfigSampleHandoverList(configSampleHandoverList);
	}

	@Override
	public boolean isAccess() {
		SampleFlowParam sampleFlowParam = this.getRequestData();
		if (sampleFlowParam == null) {
			return false;
		}
		return super.isAccess();
	}

}
