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

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.api.pojo.config.node.NodeData;
import cn.stylefeng.workflow.api.pojo.config.node.approver.ApproverJumpStrategy;
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 PointUserApproveRule implements EmptyApproverRule {

    @Resource
    private RuTaskService ruTaskService;

    @Resource
    private WorkflowLogApi workflowLogApi;

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

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

        NodeData nodeData = currentNode.getNodeData();
        ApproverJumpStrategy approverJumpStrategy = nodeData.getApproverConfig().getApproverJumpStrategy();
        Long emptyApproverPointUserId = approverJumpStrategy.getEmptyApproverPointUserId();

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

        ruTaskService.createUserTask(emptyApproverPointUserId, TaskTypeEnum.PROCESSING, runtimeNode, instanceContext);
        workflowLogApi.logSuccess(instanceContext, currentNode.getNodeId(), "执行审批策略-审批人为空时，指定人员审批");

    }

}
