cuikaidong
2025-05-27 504333e56f249d16e71e8ac2a435cf2212040c23
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.jeecg.modules.flowable.apithird.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.jeecg.modules.flowable.apithird.business.dto.FlowMyBusinessDto;
import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
import org.jeecg.modules.flowable.apithird.business.mapper.FlowMyBusinessMapper;
import org.jeecg.modules.flowable.apithird.business.service.IFlowMyBusinessService;
import org.jeecg.modules.flowable.domain.dto.FlowTaskDto;
import org.jeecg.modules.flowable.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
/**
 * @Description: 流程业务扩展表
 * @Author: jeecg-boot
 * @Date:   2021-11-25
 * @Version: V1.0
 */
@Service
public class FlowMyBusinessServiceImpl extends ServiceImpl<FlowMyBusinessMapper, FlowMyBusiness> implements IFlowMyBusinessService {
    @Resource
    private FlowMyBusinessMapper flowMyBusinessMapper;
 
    @Autowired
    private HistoryService historyService;
 
    public HistoricActivityInstance getPreviousNode(String taskId) {
        // 获取当前任务的执行实例 ID
        String processInstanceId = historyService.createHistoricTaskInstanceQuery()
                .taskId(taskId)
                .singleResult()
                .getProcessInstanceId();
 
        // 查询历史活动实例
        List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .activityType("userTask")
                .finished()
                .orderByHistoricActivityInstanceEndTime()
                .desc()
                .list();
 
        // 取第一个结果,即上一级节点
        if (!historicActivityInstances.isEmpty()) {
            return historicActivityInstances.get(0);
        }
 
        return null;
    }
 
    @Transactional   // 降低隔离级别
    public FlowMyBusiness getByDataId(String dataId) {
        //如果保存数据前未调用必调的FlowCommonService.initActBusiness方法,就会有问题
        FlowMyBusiness business = flowMyBusinessMapper.selectByDataId(dataId);
        return business;
    }
 
 
    public FlowMyBusiness getByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<FlowMyBusiness> flowMyBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        flowMyBusinessLambdaQueryWrapper.eq(FlowMyBusiness::getProcessInstanceId,processInstanceId)
        ;
        //如果保存数据前未调用必调的FlowCommonService.initActBusiness方法,就会有问题
        FlowMyBusiness business = this.getOne(flowMyBusinessLambdaQueryWrapper);
        return business;
    }
 
    /**
     * 流程-我的已办
     * @param flowMyBusinessDto
     * @return
     */
    public List<FlowTaskDto> ListMyBusiness(FlowMyBusinessDto flowMyBusinessDto){
        return flowMyBusinessMapper.ListMyBusiness(flowMyBusinessDto);
    }
 
    /**
     * 流程总台账
     * @param flowMyBusinessDto
     * @return
     */
    public IPage<FlowMyBusinessDto> getPageList(Page page, FlowMyBusinessDto flowMyBusinessDto){
        IPage<FlowMyBusinessDto> flowMyBusinessDtoIPage =flowMyBusinessMapper.PageList(page,flowMyBusinessDto);
        flowMyBusinessDtoIPage.getRecords().forEach(item -> {
            if (!("").equals(item.getTaskId())&&item.getTaskId()!=null){
                HistoricActivityInstance historicActivityInstance = getPreviousNode(item.getTaskId());
                if (historicActivityInstance != null){
                    item.setPreNode(historicActivityInstance.getActivityName());
                }
            }
            if (item.getTodoUsers() == null){
                item.setTodoUsers("");
            }else {
                //去除[]
                item.setTodoUsers(item.getTodoUsers().replaceAll("\\[", "").replaceAll("\\]", ""));
                item.setTodoUsers(item.getTodoUsers().replaceAll("\"", ""));
            }
 
            if (item.getDoneUsers() == null){
                item.setDoneUsers("");
            }else {
                //去除[]
                item.setDoneUsers(item.getDoneUsers().replaceAll("\\[", "").replaceAll("\\]", ""));
                item.setDoneUsers(item.getDoneUsers().replaceAll("\"", ""));
            }
            //计算处理时长
            Date kssj=item.getStartTime();
            Date jssj;
            if (item.getEndTime() != null){
                jssj=item.getEndTime();
            }else {
                jssj=new Date();
            }
            item.setDuration(TimeUtil.howLong(kssj, jssj,2));
        });
        return flowMyBusinessDtoIPage;
    }
 
    @Override
    public FlowMyBusiness getFlowMyBusiness(String instanceId) {
        List<FlowMyBusiness> businessList = super.list(
                new QueryWrapper<FlowMyBusiness>().eq("process_instance_id", instanceId));
        return businessList.isEmpty() ? null : businessList.get(0);
    }
 
    @Override
    public FlowMyBusiness getFlowMyBusiness(String instanceId, String taskId) {
        List<FlowMyBusiness> businessList = super.list(
                new QueryWrapper<FlowMyBusiness>().eq("process_instance_id", instanceId).eq("task_id", taskId));
        return businessList.isEmpty() ? null : businessList.get(0);
    }
 
    @Override
    public FlowMyBusiness selectByDataId(String dataId) {
        List<FlowMyBusiness> businessList = super.list(
                new QueryWrapper<FlowMyBusiness>().eq("data_id", dataId));
        return businessList.isEmpty() ? null : businessList.get(0);
    }
 
    @Override
    public List<FlowMyBusiness> selectListByDataId(String dataId) {
        //如果保存数据前未调用必调的FlowCommonService.initActBusiness方法,就会有问题
        LambdaQueryWrapper<FlowMyBusiness> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FlowMyBusiness::getDataId, dataId);
        List<FlowMyBusiness> list = flowMyBusinessMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return list;
    }
}