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

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-29 18:44
 * @desc 取消委派的cmd
 */
public class CancelDelegateTaskCmd implements Command<Void> {
    protected  String taskId;
    protected  String initialAssignee;//原来的审批人
    @Override
    public Void execute(CommandContext commandContext) {
        SpringProcessEngineConfiguration springProcessEngineConfiguration = SpringContextHolder.getBean(SpringProcessEngineConfiguration.class);
        TaskEntityManager taskEntityManager = springProcessEngineConfiguration.getTaskServiceConfiguration().getTaskEntityManager();
        TaskEntity taskEntity = taskEntityManager.findById(taskId);
        taskEntity.setOwner(null);
        taskEntity.setAssignee(initialAssignee);
        taskEntity.setDelegationState(null);
        return null;
    }

    public CancelDelegateTaskCmd(String taskId, String initialAssignee) {
        this.taskId = taskId;
        this.initialAssignee = initialAssignee;
    }
}
