package cn.stylefeng.workflow.instance.modular.task.strategy.emptyprocess;

import cn.stylefeng.workflow.api.entity.design.Model;
import cn.stylefeng.workflow.api.entity.design.Node;
import cn.stylefeng.workflow.api.entity.instance.RuntimeNode;
import cn.stylefeng.workflow.api.enums.TaskTypeEnum;
import cn.stylefeng.workflow.api.enums.strategy.EmptyApproverStrategyEnum;
import cn.stylefeng.workflow.api.pojo.InstanceContext;
import cn.stylefeng.workflow.instance.modular.task.service.RuTaskService;
import cn.stylefeng.workflow.instance.modular.task.strategy.emptyprocess.base.EmptyApproverRule;
import cn.stylefeng.workflow.log.api.WorkflowLogApi;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PointFlowManagerRule implements EmptyApproverRule {

    @Resource
    private RuTaskService ruTaskService;

    @Resource
    private WorkflowLogApi workflowLogApi;

    @Override
    public EmptyApproverStrategyEnum getStrategyType() {
        return EmptyApproverStrategyEnum.TURN_TO_MANAGER;
    }

    @Override
    public void processEmptyApprover(Node currentNode, InstanceContext instanceContext, RuntimeNode runtimeNode) {

        Model model = instanceContext.getModel();
        Long managerUserId = model.getManagerUserId();

        if (managerUserId == null) {
            workflowLogApi.logFail(instanceContext, currentNode.getNodeId(), "流程审批人为空策略执行过程中，找不到配置的流程管理员！");
            return;
        }

        ruTaskService.createUserTask(managerUserId, TaskTypeEnum.PROCESSING, runtimeNode, instanceContext);
        workflowLogApi.logSuccess(instanceContext, currentNode.getNodeId(), "执行审批策略-审批人为空时，指定流程管理员进行办理");

    }

}
