package com.codvision.workflow.listener.task;

import com.codvision.base.support.WebSocketServer;
import com.codvision.workflow.service.WorkFlowService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 待办消息WebSocket通知监听器（任务监听器 事件类型 -> create）
 * @author hxl
 */
@RequiredArgsConstructor
@Component(value = "workFlowTodoMsgNoticeListener")
public class WorkFlowTodoMsgNoticeListener implements TaskListener {

    private final WorkFlowService workFlowService;

    @Override
    @SneakyThrows
    public void notify(DelegateTask delegateTask) {
        List<Integer> taskRelatedUserIds = workFlowService.getTaskRelatedUserIds(delegateTask.getId());
        if (CollectionUtils.isEmpty(taskRelatedUserIds)) {
            return;
        }

        for (Integer uid : taskRelatedUserIds) {
            WebSocketServer.sendInfo("EVENT_TODO_MSG", uid);
        }
    }
}
