kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.highdatas.mdm.service.act.impl;
 
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class IdentityServiceImpl extends BaseServiceImpl implements com.highdatas.mdm.service.act.IdentityService{
    @Autowired
    IdentityService identityService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    RedisClient redisClient;
 
    /**
     *
     * @description:  启动流程
     * @param businessId 业务id
     * @param key 流程key
     * @return: 实例id
     *
     */
    @Override
    public String startProcess(String businessId, String key) {
        return startProcess(businessId, key, null, null);
    }
    /**
     *
     * @description:  启动流程
     * @param businessId 业务id
     * @param key 流程key
     * @param variables 额外参数
     * @return: 实例id
     *
     */
    @Override
    public String startProcess(String businessId, String key, String workflowStall, HashMap<String, Object> variables) {
        TUser user = getOnlineUser();
        if (user == null) {
            error(" user is null");
            return "";
        }
        
        ProcessInstance processInstance = null;
        try {
            // add workflowstall
 
            if (StringUtils.isEmpty(workflowStall)) {
                workflowStall = "1";
            }
 
            if (variables == null) {
                variables = new HashMap<>();
            }
 
            int workflowStallInt = Integer.parseInt(workflowStall);
            variables.put("workflowstall", workflowStallInt);
            
            identityService.setAuthenticatedUserId(user.getUserId());
            Date startDate = new Date();
            //get max version processDefinition
            List<Model> modelList = repositoryService.createModelQuery().modelKey(key).orderByModelVersion().desc().list();
            Date endDate = new Date();
            log.info("process load model:" + (endDate.getTime() - startDate.getTime()) +"ms");
            ProcessDefinition processDefinition = null;
            
            for (Model model : modelList) {
                if (processDefinition == null && model.getDeploymentId() != null) {
                    String deploymentId = model.getDeploymentId();
                    processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
                    break;
                }
            }
            startDate = new Date();
            log.info("process load processDef:" + (startDate.getTime() - endDate.getTime()) +"ms");
            processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), businessId, variables);
 
            endDate = new Date();
            log.info("process start process :" + (endDate.getTime() - startDate.getTime()) +"ms");
 
            String processInstanceId = processInstance.getId();
            //TODO db
//            NamedSQL updateActinstUserSql = NamedSQL.getInstance("updateActinstUser");
//            updateActinstUserSql.setParam("userid", user.getId());
//            updateActinstUserSql.setParam("id", processInstanceId);
//            SQLRunner.execSQL(updateActinstUserSql);
            
            return processInstanceId;
           
        }
        finally {
            identityService.setAuthenticatedUserId(null);
        }
    }
 
    /**
     *
     * @description:  添加工作流用户
     * @param id 用户id
     * @return: 添加结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result addUser(String id) {
        User preUser = identityService.createUserQuery().userId(id).singleResult();
        if (preUser != null) {
            return  Result.error(new CodeMsg(6002, "已有相同id的用户存在"));
        }
        try {
            User user = identityService.newUser(id);
            identityService.saveUser(user);
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(new CodeMsg(6002, e.getMessage()));
        }
 
    }
 
    /**
     *
     * @description:  添加工作流角色
     * @param id 角色id
     * @return: 添加结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result addRole(String id) {
        Group preGroup = identityService.createGroupQuery().groupId(id).singleResult();
        if (preGroup != null) {
            return  Result.error(new CodeMsg(6002, "已有相同id的角色存在"));
        }
        try {
            Group group = identityService.newGroup(id);
            identityService.saveGroup(group);
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(new CodeMsg(6002, e.getMessage()));
        }
    }
 
    /**
     *
     * @description:  添加工作流用户,角色关联
     * @param roleId 觉得id
     * @param userId 用户id
     * @return: 添加结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result addUserRole(String roleId, String userId) {
        try {
            User user = identityService.createUserQuery().userId(userId).singleResult();
            if (user == null) {
                return Result.error(new CodeMsg(3001, "当前用户不存在,请先创建用户"));
            }
 
            Map<String, String> memberShip = tableInfoMapper.selectActMemberShip(userId, roleId);
            if (memberShip != null) {
                return Result.success(null);
            }
            identityService.createMembership(userId,roleId);
            return Result.success(null);
        }
        catch (Exception e) {
            e.printStackTrace();
            return Result.error(new CodeMsg(3002, e.getMessage()));
        }
    }
    /**
     *
     * @description:  删除工作流用户
     * @param id 用户id
     * @return: 删除结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result deleteUser(String id) {
        try {
            User user = identityService.createUserQuery().userId(id).singleResult();
            if (user == null) {
                return Result.error(new CodeMsg(3001, "当前用户不存在"));
            }
            List<Map<String, Object>> maps = tableInfoMapper.selectActMemberShipByUser(id);
            for (Map<String, Object> map : maps) {
                Object group_id_ = map.get("GROUP_ID_");
                Object user_id_ = map.get("USER_ID_");
                if (user_id_ == null || group_id_ == null) {
                    continue;
                }
                identityService.deleteMembership(user_id_.toString(), group_id_.toString());
            }
            redisClient.delByCharacter(id);
            identityService.deleteUser(id);
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(new CodeMsg(3002, e.getMessage()));
        }
 
    }
    /**
     *
     * @description:  删除工作流角色
     * @param id 角色id
     * @return: 删除结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result deleteRole(String id) {
        try {
            Group group = identityService.createGroupQuery().groupId(id).singleResult();
            if (group == null) {
                return Result.error(new CodeMsg(3001, "当前角色不存在"));
            }
            List<Map<String, Object>> maps = tableInfoMapper.selectActMemberShipByRole(id);
            for (Map<String, Object> map : maps) {
                Object group_id_ = map.get("GROUP_ID_");
                Object user_id_ = map.get("USER_ID_");
                if (user_id_ == null || group_id_ == null) {
                    continue;
                }
                identityService.deleteMembership(user_id_.toString(), group_id_.toString());
                redisClient.delByCharacter(user_id_.toString());
            }
            identityService.deleteGroup(id);
            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(new CodeMsg(3002, e.getMessage()));
        }
    }
 
    /**
     *
     * @description:  删除工作流用户 角色关联
     * @param userId 用户id
     * @param roleId 角色id
     * @return: 删除结果
     *
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Result deleteUserRole(String roleId, String userId) {
        try {
 
            Map<String, String> memberShip = tableInfoMapper.selectActMemberShip(userId, roleId);
            if (memberShip == null) {
                return Result.success(null);
            }
            redisClient.delByCharacter(userId);
            identityService.deleteMembership(userId,roleId);
            return Result.success(null);
        }
        catch (Exception e) {
            e.printStackTrace();
            return Result.error(new CodeMsg(3002, e.getMessage()));
        }
    }
    /**
     *
     * @description:  通过user获取关联的角色
     * @param userId 用户id
     * @return: 角色id列表
     *
     */
    @Override
    public List<String> getRoleByUser(String userId) {
        List<Group> list = identityService.createGroupQuery().groupMember(userId).list();
        if (list == null || list.isEmpty()) {
            return null;
        }
        return  list.stream().map(group -> group.getId()).collect(Collectors.toList());
    }
 
 
}