package com.th.demo.service.impl.process;

import com.th.demo.mapping.system.SysUserMapper;

import com.th.demo.service.process.ProcessService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("ProcessService")
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public String startWithProcessId(String ProcessId, String userId, Map<String, Object> map) {
        identityService.setAuthenticatedUserId(userId);
        ProcessInstance pi = runtimeService.startProcessInstanceById(ProcessId, map);
        return pi.getId();
    }

    @Override
    public String startWithProcessKey(String ProcessKey, String userId, Map<String, Object> map) {
        identityService.setAuthenticatedUserId(userId);
        ProcessInstance pi = runtimeService.startProcessInstanceByKey(ProcessKey, map);
        return pi.getId();
    }

    @Override
    public String getProcessStatus(String processInstanceId) {
        String result = "";
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if (pi == null) {
            result = "1";
        } else {
            result = "0";
        }
        return result;
    }

}
