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.Map;
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 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 = "sampleTaskAssignSampleCmp", name = "按样品分配")
public class SampleTaskAssignSampleCmp 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();
		AssignAssayUser assignAssayUser = sampleTaskAssignContext.getAssignAssayUser();
		List<Long> assignSampleIdList = sampleTaskAssignContext.getAssignSampleIdList();
		//根据分配的样品id查询分析任务数据
		List<BusinessAssayTaskDataDO> businessAssayTaskDataList = businessAssayTaskDataMapper.selectList(new LambdaQueryWrapperX<BusinessAssayTaskDataDO>().in(BusinessAssayTaskDataDO::getBusinessSubSampleId, assignSampleIdList).eq(BusinessAssayTaskDataDO::getIsAssignTasked, QmsCommonConstant.NO));
		List<Long> configAssayMethodIdList = businessAssayTaskDataList.stream().map(m -> m.getConfigAssayMethodId()).distinct().collect(Collectors.toList());
		List<ConfigAssayMethodDO> configAssayMethodList = configAssayMethodMapper.selectByIds(configAssayMethodIdList);
		sampleTaskAssignContext.setConfigAssayMethodList(configAssayMethodList);
		
		//查询子样
		List<BusinessSubSampleDO> businessSubSampleList = businessSubSampleMapper.selectByIds(assignSampleIdList);
		sampleTaskAssignContext.setBusinessSubSampleList(businessSubSampleList);
		
		//保存任务分配单
		List<BusinessAssayTaskDO> saveBusinessAssayTaskList = new ArrayList<>();
		//更新任务分配单
		List<BusinessAssayTaskDO> updateBusinessAssayTaskList = new ArrayList<>();
		//任务分配单明细
		List<BusinessAssayTaskDetailDO> businessAssayTaskDetailList = new ArrayList<>();
		
		//按分析方法分组
		Map<Long, List<BusinessAssayTaskDataDO>> groupBusinessAssayTaskDataListMap = businessAssayTaskDataList.stream().collect(Collectors.groupingBy(BusinessAssayTaskDataDO::getConfigAssayMethodId));
		for (Map.Entry<Long, List<BusinessAssayTaskDataDO>> entry : groupBusinessAssayTaskDataListMap.entrySet()) {
			long key = entry.getKey();
			ConfigAssayMethodDO configAssayMethod = sampleTaskAssignContext.getConfigAssayMethodById(key);
			//查询报表模板
			ConfigReportTemplateDO configReportTemplate = configReportTemplateMapper.selectLatestConfigReportTemplateByKey(configAssayMethod.getConfigReportTemplateKey());

			//根据分配
			BusinessAssayTaskDO businessAssayTaskDO = businessAssayTaskMapper.selectAssignInProgressByAssayUserAndAssignUser(configAssayMethod.getId(), 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.setAssayOperator(assignAssayUser.getRealName());
				businessAssayTaskDO.setAssayOperatorId(assignAssayUser.getUserId());
				businessAssayTaskDO.setConfigAssayMethodId(key);
				businessAssayTaskDO.setTaskSourceType(QmsCommonConstant.TASK_ASSIGN);
				businessAssayTaskDO.setConfigReportTemplateId(configReportTemplate.getId());
				businessAssayTaskDO.setConfigReportTemplateKey(configReportTemplate.getKey());
				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;
			List<BusinessAssayTaskDataDO> valList = entry.getValue();
			
			int sort = 1;
			for (BusinessAssayTaskDataDO val : valList) {
				val.setIsAssignTasked(QmsCommonConstant.YES);
				val.setAssignTaskTime(currentDateTime);
				val.setAssayOperator(assignAssayUser.getRealName());
				val.setAssayOperatorId(assignAssayUser.getUserId());
				val.setBusinessAssayTaskId(businessAssayTaskDO.getId());
				
				BusinessSubSampleDO businessSubSampleDO = sampleTaskAssignContext.getBusinessSubSampleById(val.getBusinessSubSampleId());
				
				businessAssayTaskDetailDO = new BusinessAssayTaskDetailDO();
				businessAssayTaskDetailDO.setBusinessAssayTaskDataId(val.getId());
				businessAssayTaskDetailDO.setBusinessAssayTaskId(businessAssayTaskDO.getId());
				businessAssayTaskDetailDO.setTaskNo(businessAssayTaskDO.getTaskNo());
				businessAssayTaskDetailDO.setSampleId(val.getBusinessSubSampleId());
				businessAssayTaskDetailDO.setSampleCode(businessSubSampleDO.getSampleAssayCode());
				businessAssayTaskDetailDO.setSampleName(businessSubSampleDO.getSampleName());
				businessAssayTaskDetailDO.setDataSourceType(QmsCommonConstant.NORMAL);
				businessAssayTaskDetailDO.setSortNo(sort);
				sort++;
				
				
				businessAssayTaskDetailList.add(businessAssayTaskDetailDO);
			}
			
		}

		sampleTaskAssignContext.setSaveBusinessAssayTaskList(saveBusinessAssayTaskList);
		sampleTaskAssignContext.setUpdateBusinessAssayTaskList(updateBusinessAssayTaskList);
		sampleTaskAssignContext.setBusinessAssayTaskDataList(businessAssayTaskDataList);
		sampleTaskAssignContext.setBusinessAssayTaskDetailList(businessAssayTaskDetailList);
	}

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

}
