package cn.iocoder.yudao.module.bpm.framework.flowable.core.behavior.script.impl;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskRespVO;
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskRuleScriptEnum;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.behavior.script.BpmTaskAssignScript;
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
import cn.iocoder.yudao.module.bpm.service.task.BpmTaskService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
import static java.util.Collections.emptySet;

/**
 * 分配给审批人的上级领导 审批的 Script 实现类
 */
@Component
public class BpmTaskAssignLeaderScript implements BpmTaskAssignScript {

    @Resource
    @Lazy // 解决循环依赖
    private DeptApi deptApi;
    @Resource
    @Lazy // 解决循环依赖
    private AdminUserApi userApi;

    @Resource
    @Lazy // 解决循环依赖
    private BpmProcessInstanceService bpmProcessInstanceService;

    @Resource
    @Lazy // 解决循环依赖
    private BpmTaskService bpmTaskService ;

    @Override
    public Set<Long> calculateTaskCandidateUsers(DelegateExecution execution) {

        ProcessInstance processInstance = bpmProcessInstanceService.getProcessInstance(execution.getProcessInstanceId());
        List<BpmTaskRespVO> bpmTaskRespVOs = bpmTaskService.getTaskListByProcessInstanceId(processInstance.getProcessInstanceId());
        if (CollUtil.isEmpty(bpmTaskRespVOs)) {
            return emptySet();
        }

        // 获取最后一个Task任务相关数据
        BpmTaskRespVO bpmTaskRespVO = bpmTaskRespVOs.get(0) ;
        Long assigneeUserId = bpmTaskRespVO.getAssigneeUser().getId();

        // 获取审批人部门信息
        DeptRespDTO dept = getUserDept(assigneeUserId);
        if (dept == null || dept.getParentId() == null) {
            return emptySet();
        }
        // 获取审批人的上级部门信息
        DeptRespDTO assignDept = deptApi.getDept(dept.getParentId()).getCheckedData();

        return assignDept != null && assignDept.getLeaderUserId() != null ? asSet(assignDept.getLeaderUserId()) : emptySet();
    }

    @Override
    public BpmTaskRuleScriptEnum getEnum() {
        return BpmTaskRuleScriptEnum.LEADER_X11;
    }

    private DeptRespDTO getUserDept(Long userId) {
        AdminUserRespDTO user = userApi.getUser(userId).getCheckedData();
        if (user.getDeptId() == null) { // 找不到部门，所以无法使用该规则
            return null;
        }
        return deptApi.getDept(user.getDeptId()).getCheckedData();
    }
}
