package org.springblade.flow.engine.custom.cmd.multi;


import java.util.List;

import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;
import org.springblade.flow.base.util.SpringContextHolder;

/**
 * @author LoveMyOrange
 * @create 2022-01-26 13:43
 */
public class SimpleUserTaskSignCmd implements Command<Void> {
    protected  String taskId;
    protected List<String> candidateUsers;
    protected String operator;

    public SimpleUserTaskSignCmd(String taskId, List<String> candidateUsers, String operator) {
        this.taskId = taskId;
        this.candidateUsers = candidateUsers;
        this.operator = operator;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        SpringProcessEngineConfiguration springProcessEngineConfiguration = SpringContextHolder.getBean(SpringProcessEngineConfiguration.class);
        TaskEntityManager taskEntityManager = springProcessEngineConfiguration.getTaskServiceConfiguration().getTaskEntityManager();
        TaskEntity taskEntity = taskEntityManager.findById(taskId);
        if("add".equals(operator)){
            taskEntity.addCandidateUsers(candidateUsers);
        }
        else{
            for (String candidateUser : candidateUsers) {
                taskEntity.deleteCandidateUser(candidateUser);
            }
        }
        return null;
    }
}
