package com.ruoyi.flowable.listener;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.IApproveUserService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 任务监听器
 *
 * create（创建）:在任务被创建且所有的任务属性设置完成后才触发
 * assignment（指派）：在任务被分配给某个办理人之后触发
 * complete（完成）：在配置了监听器的上一个任务完成时触发
 * delete（删除）：在任务即将被删除前触发。请注意任务由completeTask正常完成时也会触发
 *
 * @author Tony
 * @date 2021/4/20
 */
@Slf4j
@Component
public class DueTaskListener implements TaskListener{
    private static ISysUserService sysUserService;
    private static SysDeptMapper sysDeptMapper;
    private static IApproveUserService approveUserService;
    @Autowired
    public void setSysUserService(ISysUserService sysUserService) {
        DueTaskListener.sysUserService = sysUserService;
    }
    @Autowired
    public void setSysDeptMapper(SysDeptMapper sysDeptMapper) {
        DueTaskListener.sysDeptMapper = sysDeptMapper;
    }
    @Autowired
    public void setApproveUserMapper(IApproveUserService approveUserService) {
        DueTaskListener.approveUserService = approveUserService;
    }
    @Override
    public void notify(DelegateTask delegateTask) {

        log.info("任务监听器:{}", delegateTask);
        // TODO  获取事件类型 delegateTask.getEventName(),可以通过监听器给任务执行人发送相应的通知消息
        String eventName= delegateTask.getEventName();
        System.out.println("任务监听器触发事件：" + eventName);
        String taskDefKey= delegateTask.getTaskDefinitionKey();
        Object obj = delegateTask.getVariable("INITIATOR");
        SysUser user = sysUserService.selectUserById((Long) obj);
        String leader=sysDeptMapper.selectDeptLeader("投资部");
        SysUser leaderUser=sysUserService.selectUserByUserName(leader);
        if("create".equals(eventName)) {
            if ("deptLeader".equals(taskDefKey)) {//部门负责人、
                //部门负责人
                delegateTask.setAssignee(leaderUser.getUserId().toString());
            } else if ("confirm".equals(taskDefKey)) {
                //申请人本人确认
                delegateTask.setAssignee(user.getUserId().toString());
            } else if ("president".equals(taskDefKey)) {
                //总裁室固定杨总
                SysUser ylUser = sysUserService.selectUserByUserName("杨琳");
                delegateTask.setAssignee(ylUser.getUserId().toString());
            }
        }else if("complete".equals(eventName)){
            if ("deptLeader".equals(taskDefKey)) {
                List<Long> aUser = approveUserService.findApproveUser();
                if (aUser != null && aUser.size() > 0) {
                    delegateTask.setVariable("uploadList", aUser);
                } else {
                    List<Long> users = new ArrayList<>();
                    users.add(31L);
                    users.add(850L);
                    delegateTask.setVariable("uploadList", users);
                }
            }
        }

    }

}
