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

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

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.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDataDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayTaskDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskDataMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessAssayTaskMapper;
import com.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubSampleMapper;
import com.zt.plat.module.qms.business.bus.liteflow.param.AssignAssayUser;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleTaskAssignContext;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleTaskAssignTypeEnum;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigAssayMethodDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigReportTemplateDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigAssayMethodMapper;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigReportTemplateMapper;
import com.zt.plat.module.qms.core.code.SequenceUtil;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import jakarta.annotation.Resource;

@LiteflowComponent(id = "sampleTaskAssignMethodCmp", name = "按分析方法分配")
public class SampleTaskAssignMethodCmp extends NodeComponent {

	@Resource
	private SequenceUtil sequenceUtil;
	
	@Resource
	private ConfigAssayMethodMapper configAssayMethodMapper;
	
	@Resource
	private ConfigReportTemplateMapper configReportTemplateMapper;
	
	@Resource
	private BusinessAssayTaskDataMapper businessAssayTaskDataMapper;
	
	@Resource
	private BusinessAssayTaskMapper businessAssayTaskMapper;
	
	@Resource
	private BusinessSubSampleMapper businessSubSampleMapper;
	
	@Override
	public void process() throws Exception {
		SampleTaskAssignContext sampleTaskAssignContext = this.getContextBean(SampleTaskAssignContext.class);
		String loginRealname = sampleTaskAssignContext.getLoginRealname();
		LoginUser loginUser = sampleTaskAssignContext.getLoginUser();
		LocalDateTime currentDateTime = sampleTaskAssignContext.getCurrentDateTime();
		Long assignConfigAssayMethodId = sampleTaskAssignContext.getAssignConfigAssayMethodId();
		List<AssignAssayUser> assignAssayUserList = sampleTaskAssignContext.getAssignAssayUserList();
		//查询分析方法配置
		ConfigAssayMethodDO configAssayMethod = configAssayMethodMapper.selectById(assignConfigAssayMethodId);
		List<ConfigAssayMethodDO> configAssayMethodList = sampleTaskAssignContext.getConfigAssayMethodList();
		if (CollUtil.isEmpty(configAssayMethodList)) {
			configAssayMethodList = new ArrayList<>();
			configAssayMethodList.add(configAssayMethod);
		}
		//查询报表模板
		ConfigReportTemplateDO configReportTemplate = configReportTemplateMapper.selectLatestConfigReportTemplateByKey(configAssayMethod.getConfigReportTemplateKey());
		
		List<BusinessAssayTaskDataDO> businessAssayTaskDataDOList = businessAssayTaskDataMapper.selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>().eq(BusinessAssayTaskDataDO::getConfigAssayMethodId, assignConfigAssayMethodId).eq(BusinessAssayTaskDataDO::getIsAssignTasked, QmsCommonConstant.NO));

		List<Long> businessSubSampleIdList = businessAssayTaskDataDOList.stream().map(m -> m.getBusinessSubSampleId()).collect(Collectors.toList());
		List<BusinessSubSampleDO> businessSubSampleList = businessSubSampleMapper.selectByIds(businessSubSampleIdList);
		sampleTaskAssignContext.setBusinessSubSampleList(businessSubSampleList);
		
		List<BusinessAssayTaskDataDO> businessAssayTaskDataList = new ArrayList<>();

		//保存任务分配单
		List<BusinessAssayTaskDO> saveBusinessAssayTaskList = new ArrayList<>();
		//更新任务分配单
		List<BusinessAssayTaskDO> updateBusinessAssayTaskList = new ArrayList<>();
		//任务分配单明细
		List<BusinessAssayTaskDetailDO> businessAssayTaskDetailList = new ArrayList<>();
		
		//循环分析人员分配任务
		for (AssignAssayUser assignAssayUser : assignAssayUserList) {
			Integer sampleNum = assignAssayUser.getSampleNum();

			BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectAssignInProgressByAssayUserAndAssignUser(assignConfigAssayMethodId, assignAssayUser.getRealName(), loginRealname);
			
			if (businessAssayTaskDO == null) {
				String taskNo = sequenceUtil.genCode(configAssayMethod.getCodeRule());
				
				businessAssayTaskDO = new BusinessAssayTaskDO();
				businessAssayTaskDO.setId(IdWorker.getId());
				businessAssayTaskDO.setTaskNo(taskNo);
				businessAssayTaskDO.setTaskName(configAssayMethod.getName());
				businessAssayTaskDO.setConfigAssayMethodId(assignConfigAssayMethodId);			
				businessAssayTaskDO.setTaskSourceType(QmsCommonConstant.TASK_ASSIGN);
				businessAssayTaskDO.setConfigReportTemplateId(configReportTemplate.getId());
				businessAssayTaskDO.setConfigReportTemplateKey(configReportTemplate.getKey());
				businessAssayTaskDO.setAssayOperator(assignAssayUser.getRealName());
				businessAssayTaskDO.setAssayOperatorId(assignAssayUser.getUserId());
				businessAssayTaskDO.setIsIngredients(configAssayMethod.getIsIngredients());
				businessAssayTaskDO.setIngredientsStatus("initial");
				
				businessAssayTaskDO.setTaskAssignOperator(loginRealname);
				businessAssayTaskDO.setTaskAssignOperatorId(loginUser.getId());
				businessAssayTaskDO.setTaskAssignTime(currentDateTime);
				businessAssayTaskDO.setTaskAssignStatus(QmsCommonConstant.IN_PROGRESS);
				businessAssayTaskDO.setTaskAssayStatus(QmsCommonConstant.NOT_START);
				businessAssayTaskDO.setFinishStatus(QmsCommonConstant.NOT_START);
				businessAssayTaskDO.setFlowStatus(QmsCommonConstant.NOT_START);
				
				saveBusinessAssayTaskList.add(businessAssayTaskDO);
			} else {
				updateBusinessAssayTaskList.add(businessAssayTaskDO);
			}
			
			
			BusinessAssayTaskDetailDO businessAssayTaskDetailDO = null;
			for (int i = 0; i < sampleNum; i++) {
				BusinessAssayTaskDataDO businessAssayTaskDataDO = businessAssayTaskDataDOList.get(0);
				businessAssayTaskDataDO.setIsAssignTasked(QmsCommonConstant.YES);
				businessAssayTaskDataDO.setAssignTaskTime(currentDateTime);
				businessAssayTaskDataDO.setAssayOperator(assignAssayUser.getRealName());
				businessAssayTaskDataDO.setAssayOperatorId(assignAssayUser.getUserId());
				businessAssayTaskDataDO.setBusinessAssayTaskId(businessAssayTaskDO.getId());
				
				BusinessSubSampleDO businessSubSampleDO = sampleTaskAssignContext.getBusinessSubSampleById(businessAssayTaskDataDO.getBusinessSubSampleId());
				
				businessAssayTaskDetailDO = new BusinessAssayTaskDetailDO();
				businessAssayTaskDetailDO.setBusinessAssayTaskDataId(businessAssayTaskDataDO.getId());
				businessAssayTaskDetailDO.setBusinessAssayTaskId(businessAssayTaskDO.getId());
				businessAssayTaskDetailDO.setTaskNo(businessAssayTaskDO.getTaskNo());
				businessAssayTaskDetailDO.setSampleId(businessAssayTaskDataDO.getBusinessSubSampleId());
				businessAssayTaskDetailDO.setSampleCode(businessSubSampleDO.getSampleAssayCode());
				businessAssayTaskDetailDO.setSampleName(businessSubSampleDO.getSampleName());
				businessAssayTaskDetailDO.setDataSourceType(QmsCommonConstant.NORMAL);
				businessAssayTaskDetailDO.setSortNo(i+1);
				
				
				businessAssayTaskDetailList.add(businessAssayTaskDetailDO);
				
				businessAssayTaskDataList.add(businessAssayTaskDataDO);
				
				//移除当前对象
				businessAssayTaskDataDOList.remove(businessAssayTaskDataDO);
				
			}
		}
		sampleTaskAssignContext.setSaveBusinessAssayTaskList(saveBusinessAssayTaskList);
		sampleTaskAssignContext.setUpdateBusinessAssayTaskList(updateBusinessAssayTaskList);
		sampleTaskAssignContext.setBusinessAssayTaskDetailList(businessAssayTaskDetailList);
		sampleTaskAssignContext.setBusinessAssayTaskDataList(businessAssayTaskDataList);
		
	}

	@Override
	public boolean isAccess() {
		SampleTaskAssignContext sampleTaskAssignContext = this.getContextBean(SampleTaskAssignContext.class);
		return sampleTaskAssignContext.getSampleTaskAssignType().equals(SampleTaskAssignTypeEnum.ASSAY_METHOD);
	}

}
