刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package jnpf.workflow.flowable.service;
 
import java.time.ZoneId;
 
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;
 
import cn.hutool.core.collection.CollectionUtil;
import jnpf.workflow.common.exception.BizException;
import jnpf.workflow.common.exception.ResultCode;
import jnpf.workflow.common.model.fo.InstanceDeleteFo;
import jnpf.workflow.common.model.fo.InstanceStartFo;
import jnpf.workflow.common.model.vo.HistoricInstanceVo;
import jnpf.workflow.common.model.vo.InstanceVo;
import jnpf.workflow.common.service.IInstanceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class InstanceServiceImpl implements IInstanceService {
 
    private final RepositoryService repositoryService;
    private final RuntimeService runtimeService;
    private final HistoryService historyService;
 
    @Override
    public InstanceVo startById(InstanceStartFo fo) {
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(fo.getDeploymentId()).singleResult();
        if (null == definition) {
            throw new BizException(ResultCode.DEFINITION_NOT_EXIST);
        }
        InstanceVo vo = new InstanceVo();
        ProcessInstance instance = CollectionUtil.isNotEmpty(fo.getVariables())
                ? runtimeService.startProcessInstanceById(definition.getId(), fo.getVariables())
                : runtimeService.startProcessInstanceById(definition.getId());
        if (null != instance) {
            vo.setInstanceId(instance.getId());
        }
        return vo;
    }
 
    @Override
    public HistoricInstanceVo getHistoricProcessInstance(String processInstanceId) {
        HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(processInstanceId).singleResult();
        if (null == historicInstance) {
            throw new BizException(ResultCode.INSTANCE_NOT_EXIST);
        }
        HistoricInstanceVo vo = new HistoricInstanceVo();
        vo.setInstanceId(historicInstance.getId());
        vo.setStartTime(historicInstance.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        vo.setEndTime(historicInstance.getEndTime() == null ? null
                : historicInstance.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
        vo.setDurationInMillis(historicInstance.getDurationInMillis());
        vo.setDeleteReason(historicInstance.getDeleteReason());
        return vo;
    }
 
    @Override
    public boolean deleteInstance(InstanceDeleteFo fo) {
        try {
            runtimeService.deleteProcessInstance(fo.getInstanceId(), fo.getDeleteReason());
            return true;
        } catch (Exception e) {
            log.error(ResultCode.DELETE_FAILURE.getMsg(), e);
            return false;
        }
    }
}