package com.yeejoin.amos.boot.module.jyjc.biz.service.impl;

import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.module.jg.api.enums.BusinessTypeEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.WorkFlowStatusEnum;
import com.yeejoin.amos.boot.module.jyjc.api.entity.JyjcCmTask;
import com.yeejoin.amos.boot.module.jyjc.api.entity.JyjcInspectionApplication;
import com.yeejoin.amos.boot.module.jyjc.api.model.InstanceRuntimeData;
import com.yeejoin.amos.boot.module.jyjc.api.model.TaskMessageDto;
import com.yeejoin.amos.boot.module.jyjc.api.model.TaskModelDto;
import com.yeejoin.amos.boot.module.jyjc.api.model.WorkflowResultDto;
import com.yeejoin.amos.boot.module.jyjc.biz.service.TaskModelServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.enums.FlowStatusEnum;
import com.yeejoin.amos.feign.workflow.model.ActWorkflowBatchDTO;
import com.yeejoin.amos.feign.workflow.model.ActWorkflowStartDTO;
import com.yeejoin.amos.feign.workflow.model.ProcessTaskDTO;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.typroject.tyboot.core.foundation.context.RequestContext;

import java.util.*;

@Service
@RequiredArgsConstructor
public class WorkBenchTaskCreateServiceImpl extends InspectionPlanCommonServiceImpl {

    private final CmWorkflowServiceImpl cmWorkflowService;

    private final TaskModelServiceImpl taskModelService;

    private final JyjcCmTaskServiceImpl cmTaskService;

    private final CommonServiceImpl commonService;

    @Value("${inspect.plan.process.key:dddd}")
    private String processKey;

    public void createOneTask(JyjcInspectionApplication application, ReginParams reginParams) {
        // 启动流程
        List<WorkflowResultDto> workflowResultDtos = startProcess(application);
        // 保存数据
        JyjcCmTask cmTask = saveJyjcCmTask(application, workflowResultDtos);
        // 创建待办
        createdTaskModel(cmTask, reginParams, application);
        // 缓存执行节点信息
        commonService.saveExecuteFlowData2Redis(cmTask.getInstanceId(), this.buildInstanceRuntimeData(cmTask));
    }


    private List<WorkflowResultDto> startProcess(JyjcInspectionApplication application) {
        ActWorkflowBatchDTO actWorkflowBatchDTO = new ActWorkflowBatchDTO();
        List<ActWorkflowStartDTO> list = new ArrayList<>();
        ActWorkflowStartDTO dto = new ActWorkflowStartDTO();
        dto.setProcessDefinitionKey(processKey);
        dto.setBusinessKey(String.valueOf(new Date().getTime()));
        dto.setCompleteFirstTask(true);
        HashMap<String, Object> variables = new HashMap<>();
        // 指定下一节点执行人，谁受理谁执行
        variables.put("nextAssignee", RequestContext.getExeUserId());
        dto.setVariables(variables);
        list.add(dto);
        actWorkflowBatchDTO.setProcess(list);
        dto.setNextExecuteUserCompanyCode(application.getInspectionUnitCode());
        List<ProcessTaskDTO> processTasks = cmWorkflowService.startBatch(actWorkflowBatchDTO);
        return taskModelService.buildWorkFlowInfo(processTasks);
    }

    @NotNull
    private JyjcCmTask saveJyjcCmTask(JyjcInspectionApplication application, List<WorkflowResultDto> workflowResultDtos) {
        JyjcCmTask cmTask = new JyjcCmTask();
        cmTask.setPromoter("");
        cmTask.setNextTaskId(workflowResultDtos.get(0).getNextTaskId());
        cmTask.setNextTaskCode(workflowResultDtos.get(0).getNextTaskCode());
        cmTask.setNextExecuteUserIds(workflowResultDtos.get(0).getNextExecutorUserIds());
        cmTask.setInstanceId(workflowResultDtos.get(0).getInstanceId());
        cmTask.setProcessKey(processKey);
        cmTask.setSSeq(application.getSequenceNbr().toString());
        cmTask.setSType(application.getBizType());
        cmTask.setStatus(WorkFlowStatusEnum.INSPECTION_PLAN_ASSIGN_USER.getPass());
        cmTaskService.save(cmTask);
        return cmTask;
    }


    private InstanceRuntimeData buildInstanceRuntimeData(JyjcCmTask cmTask) {
        return InstanceRuntimeData.builder()
                .nextExecuteUserIds(cmTask.getNextExecuteUserIds())
                .promoter(cmTask.getPromoter())
                .nextTaskId(cmTask.getNextTaskId()).build();
    }

}
