package com.testor.module.hazard.task;


import com.testor.common.core.utils.StringUtils;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;

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

public class CrossDayProcessListener implements TaskListener {


    @Override
    public void notify(DelegateTask delegateTask) {
        // 动态为任务设置候选用户
        // 获取流程变量，确定它是否是一个 List
        Object workSiteResponsible = delegateTask.getVariable("workSiteResponsible");
        List<String> participants = new ArrayList<>();
        if (workSiteResponsible != null && StringUtils.isNotBlank(workSiteResponsible.toString())) {
            participants = Arrays.asList(workSiteResponsible.toString().replace("0-", "").split(","));
        }
        Object checkUser = delegateTask.getVariable("checkUser");
        if (checkUser != null && StringUtils.isNotBlank(checkUser.toString())) {
            participants = Arrays.asList((checkUser.toString().replace("0-", "")  // 替换掉所有的 "0-" 为 ""
                    .split(",")));
        }
        // 动态为任务设置候选用户
        for (String userId : participants) {
            delegateTask.addCandidateUser(userId);
        }
    }
}
