package com.tourism.common.flow.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tourism.common.core.util.ApplicationContextHolder;
import com.tourism.common.flow.constant.FlowApprovalType;
import com.tourism.common.flow.exception.FlowEmptyUserException;
import com.tourism.common.flow.model.FlowTaskComment;
import com.tourism.common.flow.model.FlowTaskExt;
import com.tourism.common.flow.object.FlowUserTaskExtData;
import com.tourism.common.flow.service.FlowApiService;
import com.tourism.common.flow.service.FlowTaskExtService;
import com.tourism.common.flow.vo.FlowUserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.api.Task;
import org.flowable.task.service.delegate.DelegateTask;

import java.util.List;

/**
 * 空审批人处理监听器。
 *
 * @author 吃饭睡觉
 * @date 2024-09-06
 */
@Slf4j
public class FlowEmptyUserHandleListener implements TaskListener {

    private final transient FlowTaskExtService flowTaskExtService =
            ApplicationContextHolder.getBean(FlowTaskExtService.class);
    private final transient FlowApiService flowApiService =
            ApplicationContextHolder.getBean(FlowApiService.class);

    @Override
    public void notify(DelegateTask t) {
        FlowTaskExt flowTaskExt = flowTaskExtService.getByProcessDefinitionIdAndTaskId(
                t.getProcessDefinitionId(), t.getTaskDefinitionKey());
        Task task = flowApiService.getTaskById(t.getId());
        List<FlowUserInfoVo> userInfoVoList =
                flowTaskExtService.getCandidateUserInfoList(t.getProcessInstanceId(), flowTaskExt, task,
                        flowApiService.isMultiInstanceTask(t.getProcessDefinitionId(), t.getTaskDefinitionKey()), false);
        if (CollUtil.isNotEmpty(userInfoVoList)) {
            return;
        }
        FlowUserTaskExtData taskExtData =
                JSON.parseObject(flowTaskExt.getExtraDataJson(), FlowUserTaskExtData.class);
        //如果为空则默认是自动审批
        if (StrUtil.isBlank(taskExtData.getEmptyUserHandleWay())) {
            taskExtData.setEmptyUserHandleWay(FlowUserTaskExtData.EMPTY_USER_AUTO_COMPLETE);
        }
        switch (taskExtData.getEmptyUserHandleWay()) {
            case FlowUserTaskExtData.EMPTY_USER_TO_ASSIGNEE:
                t.setAssignee(taskExtData.getEmptyUserToAssignee());
                break;
            case FlowUserTaskExtData.EMPTY_USER_AUTO_REJECT:
            case FlowUserTaskExtData.EMPTY_USER_AUTO_COMPLETE:
                FlowTaskComment comment = new FlowTaskComment();
                comment.fillWith(task);
                if (taskExtData.getEmptyUserHandleWay().equals(FlowUserTaskExtData.EMPTY_USER_AUTO_REJECT)) {
                    comment.setApprovalType(FlowApprovalType.EMPTY_USER_AUTO_REJECT);
                    comment.setTaskComment("空审批人自动回退审批");
                    throw new FlowEmptyUserException(JSON.toJSONString(comment));
                } else {
                    comment.setApprovalType(FlowApprovalType.EMPTY_USER_AUTO_COMPLETE);
                    comment.setTaskComment("空审批人自动跳过审批");
                    flowApiService.completeTask(task, comment, null, null);
                }
                break;
            default:
                break;
        }
    }
}
