kimi
2020-05-27 2893347bf72477c4d108e8589a0f61e3e97a990c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.highdatas.mdm.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.highdatas.mdm.entity.Flows;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.MaintainDetail;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.pojo.*;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.service.act.*;
import com.highdatas.mdm.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author kimi
 * @description
 * @date 2019-12-17 10:48
 */
 
@Service
public class ActivitiServiceImpl implements ActivitiService{
    @Autowired
    IdentityService identityService;
    @Autowired
    IMaintainService maintainService;
 
    @Autowired
    IMaintainDetailService maintainDetailService;
 
    @Autowired
    TaskService taskService;
 
    @Autowired
    IFlowsService flowsService;
 
 
    @Autowired
    HistoryService historyService;
 
    @Autowired
    RepositoryService repositoryService;
 
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    IMaintainFieldService maintainFieldService;
 
    private TUser user;
    /**
     *
     * @description:  启动流程
     * @param  key 流程key
     * @param  session 请求session
     * @param  maintainId 版本id
     * @param  type 业务类型
     * @return: flows 流程实例
     *
     */
    @Override
    public Flows start(String key, HttpSession session, String maintainId, ActivitiBusinessType type) {
        return start(key, session, maintainId, type, null);
    }
 
    /**
     *
     * @description:  启动流程
     * @param  key 流程key
     * @param  session 请求session
     * @param  maintainId 版本id
     * @param  type 业务类型
     * @param  params 参数类型
     * @return: flows 流程实例
     *
     */
    @Override
    public Flows start(String key, HttpSession session, String maintainId, ActivitiBusinessType type, Map<String, Object> params) {
 
        identityService.setSession( session);
        Flows flows = new Flows();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        flows.setId(id);
        flows.setBusinessId(maintainId);
        flows.setBusinessType(type);
        flows.setStatus(ActivitiStatus.working);
        HashMap<String, Object> variableMap = new HashMap<>();
        if (params != null) {
            variableMap.putAll(params);
        }
        variableMap.put("reason", "申请审批");
 
        String workflowId = identityService.startProcess(id, key, null, variableMap);
        flows.setWorkflowId(workflowId);
        flows.setCreateTime(new Date());
        TUser user = null;
        if (session != null) {
            user = (TUser) session.getAttribute("user");
        }else  {
            user = this.user;
        }
        if (user == null) {
            return null;
        }
 
        String user_id = user.getUserId();
        flows.setUserId(user_id);
        boolean inserted = flowsService.insert(flows);
        if (inserted) {
            return flows;
        } else {
            return null;
        }
    }
    /**
     *
     * @description:  获取待审批列表
     * @param  tableName 表名
     * @param  session 请求session
     * @param  pageNo 页数
     * @param  pageSize 每页条数
     * @return: result 待处理流程list
     *
     */
    @Override
    public Result todoTask(HttpSession session, String tableName, Integer pageNo, Integer pageSize) {
        taskService.setSession(session);
        List<String> todoTask = taskService.getTodoTask();
        if (todoTask.size() == 0) {
            return Result.success(CodeMsg.SUCCESS);
        }
 
        Wrapper<Flows> flowsWrapper = new EntityWrapper<Flows>().in("workflow_id", todoTask).ne("business_type", ActivitiBusinessType.exists);
        flowsWrapper.orderBy("create_time desc");
        List<Flows> records = flowsService.selectList(flowsWrapper);
 
        List<Flows> result = new ArrayList<>();
 
        if (!StringUtils.isEmpty(tableName)) {
 
            List<Flows> maintainList = records.stream()
                    .filter(flows -> flows.getBusinessType().equals(ActivitiBusinessType.maintain))
                    .filter(flows -> (!flows.getStatus().equals(ActivitiStatus.open) || !flows.getStatus().equals(ActivitiStatus.close)))
                    .filter(flows -> maintainService.selectById(flows.getBusinessId()) != null)
                    .filter(flows -> maintainService.selectById(flows.getBusinessId()).getTableName().equalsIgnoreCase(tableName)).collect(Collectors.toList());
            List<Flows> collect = records.stream()
                    .filter(flows -> flows.getBusinessType().equals(ActivitiBusinessType.field))
                    .filter(flows -> (!flows.getStatus().equals(ActivitiStatus.open) || !flows.getStatus().equals(ActivitiStatus.close)))
                    .filter(flows -> maintainFieldService.selectById(flows.getBusinessId()) != null)
                    .filter(flows -> maintainFieldService.selectById(flows.getBusinessId()).getTableName().equalsIgnoreCase(tableName)).collect(Collectors.toList());
            records = new ArrayList<>();
 
            records.addAll(maintainList);
            records.addAll(collect);
        }
        Page page = new Page(records.size());
        page.setPageNo(pageNo);
        if (pageSize != null) {
            page.setPageSize(pageSize);
        }
        records = records.stream().skip(page.getBeginRecordNo_1()).limit(pageSize).collect(Collectors.toList());
 
        for (Flows flow : records) {
            String userId = flow.getUserId();
            TUser user = DbUtils.getUserById(userId);
            if (user == null) {
                continue;
            }
            flow.setUserId(user.getUserName());
 
            String businessId = flow.getBusinessId();
            int recordCount = maintainDetailService.selectCount(new EntityWrapper<MaintainDetail>().eq("parent_id", businessId));
 
            if (!StringUtils.isEmpty(tableName)) {
                Maintain maintain = maintainService.selectById(businessId);
                if (maintain == null) {
                    continue;
                }
                if (!maintain.getTableName().equalsIgnoreCase(tableName)) {
                    continue;
                }
                flow.setRecordCount(recordCount);
            }
            result.add(flow);
        }
 
        com.baomidou.mybatisplus.plugins.Page<Flows> flowPages = new com.baomidou.mybatisplus.plugins.Page<>();
        flowPages.setRecords(records);
        flowPages.setSize(pageSize);
        flowPages.setCurrent(pageNo);
        flowPages.setTotal(page.getRecordCount());
        return Result.success(flowPages);
    }
 
    /**
     *
     * @description:  保存用户信息到流程服务里
     * @param  user 用户信息
     * @return: void
     *
     */
 
    @Override
    public void setUser(TUser user) {
        this.user = user;
        identityService.setUser(user);
        taskService.setUser(user);
        historyService.setUser(user);
        runtimeService.setUser(user);
        repositoryService.setUser(user);
    }
    /**
     *
     * @description:  获取用户信息到流程服务里
     * @return: void
     *
     */
    @Override
    public TUser getUser() {
        return this.user;
    }
}