package com.ruoyi.flowable.listener;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysDeptMapper;
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.List;

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

        log.info("任务监听器:{}", delegateTask);
        // TODO  获取事件类型 delegateTask.getEventName(),可以通过监听器给任务执行人发送相应的通知消息
        String eventName= delegateTask.getEventName();
        String taskDefKey= delegateTask.getTaskDefinitionKey();
        Object obj = delegateTask.getVariable("INITIATOR");
        SysUser user = sysUserService.selectUserById((Long) obj);
        if("deptLeader".equals(taskDefKey)) {//部门负责人、
            //部门负责人
            String leader=sysDeptMapper.selectDeptLeader("投资部");
            SysUser leaderUser=sysUserService.selectUserByUserName(leader);
            delegateTask.setAssignee(leaderUser.getUserId().toString());
        }else if("meeting".equals(taskDefKey)){
            delegateTask.setAssignee(user.getUserId().toString());
        }else if("file".equals(taskDefKey)){
            delegateTask.setAssignee(user.getUserId().toString());
        }

    }

}
