UserRelScript.java
11 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.hotent.runtime.script;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import javax.annotation.Resource;
import cn.hutool.core.util.BooleanUtil;
import com.hotent.base.manager.CommonManager;
import com.hotent.base.util.AppUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.feign.UCFeignService;
import com.hotent.base.groovy.IUserScript;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.JsonUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.bpm.api.cmd.ActionCmd;
import com.hotent.bpm.api.constant.BpmConstants;
import com.hotent.bpm.api.context.ContextThreadUtil;
import com.hotent.bpm.api.model.identity.BpmIdentity;
import com.hotent.bpm.model.identity.DefaultBpmIdentity;
import com.hotent.uc.api.impl.model.UserFacade;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.service.IUserService;
/**
* 根据用户关系或组织关系获取人员
* @author zhangxw
*
*/
@Component
public class UserRelScript implements IUserScript {
@Resource
IUserService userService;
@Resource
UCFeignService uCFeignService;
/**
* 将用户列表转换成BpmIdentity列表
* @param list
* @return
*/
private Set<BpmIdentity> convertUserList(List<IUser> list){
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
for (IUser iUser : list) {
if(BeanUtils.isNotEmpty(iUser) && (BeanUtils.isEmpty(iUser.getStatus()) || iUser.getStatus()==1)){
DefaultBpmIdentity bpmIdentity = new DefaultBpmIdentity();
bpmIdentity.setId(iUser.getUserId());
bpmIdentity.setName(iUser.getFullname());
bpmIdentity.setType(BpmIdentity.TYPE_USER);
identitys.add(bpmIdentity);
}
}
return identitys;
}
/**
* 根据用户关系获取人员列表
* @param userId 用户ID
* @param typeCode 关系类型code
* @return
*/
private List<IUser> getSuperUserByRel(String userId, String typeCode){
List<IUser> list = new ArrayList<IUser>();
ObjectNode obj=JsonUtil.getMapper().createObjectNode();
obj.put("userId", userId);
obj.put("typeCode", typeCode);
List<ObjectNode> sysUserRels = uCFeignService.getSuperUser(obj);
if(BeanUtils.isNotEmpty(sysUserRels)){
for (ObjectNode sysUserRel : sysUserRels) {
IUser user = new UserFacade();
user.setUserId(sysUserRel.get("id").asText());
user.setFullname(sysUserRel.get("fullname").asText());
list.add(user);
}
}
return list;
}
/**
* json转iuser
* @param objs
* @return
*/
private List<IUser> usersObjConvertToIusers(List<ObjectNode> objs){
List<IUser> list = new ArrayList<IUser>();
if(BeanUtils.isNotEmpty(objs)){
for (ObjectNode obj : objs) {
UserFacade user = new UserFacade();
user.setId(obj.get("id").asText());
user.setAccount(obj.get("account").asText());
user.setFullname(obj.get("fullname").asText());
user.setUserId(obj.get("id").asText());
user.setStatus(obj.get("status").asInt());
list.add(user);
}
}
return list;
}
/**
* 获取组织负责人(包含主负责人)或主负责人
* @param orgId 组织ID
* @param isMainParam 是否主负责人
* @return
*/
public Set<BpmIdentity> getChargesByOrgId(Object orgId,Object isMainParam){
Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
if(BeanUtils.isNotEmpty(orgId)){
String obj=orgId+"";
Object[] orgIds = obj.split(",");
List<IUser> list = new ArrayList<IUser>();
for (Object id : orgIds) {
List<ObjectNode> users = uCFeignService.getChargesByOrgId(id.toString(),isMain);
if(BeanUtils.isNotEmpty(users)) {
list.addAll(usersObjConvertToIusers(users));
}
}
if(BeanUtils.isNotEmpty(list)){
identitys = convertUserList(list);
}
}
return identitys;
}
/**
* 获取上级组织负责人(包含主负责人)或主负责人
* @param orgId 组织ID
* @param isMainParam 是否主负责人
* @return
*/
public Set<BpmIdentity> getUpChargesByOrgId(Object orgId,Object isMainParam){
Boolean isMain = null;
if("true".equalsIgnoreCase(String.valueOf(isMainParam))){
isMain = true;
}else if("false".equalsIgnoreCase(String.valueOf(isMainParam))){
isMain = false;
}
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
if(BeanUtils.isNotEmpty(orgId)){
ObjectNode org = uCFeignService.getOrgByIdOrCode(orgId.toString());
if(BeanUtils.isNotEmpty(org)){
identitys = getChargesByOrgId(org.get("parentId").asText(),isMain);
}
}
return identitys;
}
/**
* 获取上一节点执行人的组织负责人
* @param isMain 是否主负责人
* @param demCode 传维度id或编码都可以
* @return
*/
public Set<BpmIdentity> getChargesByPreNode(Object isMainParam,String demCode){
Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
ObjectNode orgUser = uCFeignService.getOrgUserMaster(ContextUtil.getCurrentUserId(), demCode);
if(BeanUtils.isNotEmpty(orgUser)){
identitys = getChargesByOrgId(orgUser.get("orgId").asText(),isMain);
}
return identitys;
}
/**
* 通过上一节点执行人获取汇报线上级 人员列表(参数选填)
* @param level 级别
* @param typeCode 类型code
* @return
*/
public Set<BpmIdentity> getByRelPreNode(String typeCode){
if(StringUtil.isEmpty(typeCode)){
typeCode = null;
}
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
List<IUser> list = getSuperUserByRel(ContextUtil.getCurrentUser().getUserId(), typeCode);
identitys = convertUserList(list);
return identitys;
}
/**
* 通过发起人获取汇报线上级 人员列表(参数选填)
* @param level 级别
* @param typeCode 类型ID
* @return
*/
public Set<BpmIdentity> getByRelStartUser(String typeCode){
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
ActionCmd taskCmd = ContextThreadUtil.getActionCmd();
if(BeanUtils.isNotEmpty(taskCmd)){
String userId = (String) taskCmd.getVariables().get(BpmConstants.START_USER);
List<IUser> list = getSuperUserByRel(userId, typeCode);
identitys = convertUserList(list);
}
return identitys;
}
/**
* 通过发起人获取汇报线上级 人员列表
* @param level 级别
* @param typeId 类型ID
* @return
*/
public Set<BpmIdentity> getByRelStartUser(){
return getByRelStartUser(null);
}
/**
* 获取子表字段(人员选择器)作为节点审批人员。
* @param tableName 表明
* @param field 字段名
* @return
* @throws IOException
*/
public Set<BpmIdentity> getSubFieldUser(String tableName,String field) throws IOException{
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
ActionCmd cmd = ContextThreadUtil.getActionCmd();
String json= cmd.getBusData();
ObjectNode jsonObj=(ObjectNode) JsonUtil.toJsonNode(json);
Iterator<Entry<String, JsonNode>> it = jsonObj.fields();
List<IUser> userList = new ArrayList<IUser>();
while(it.hasNext()){
ObjectNode mainTable = (ObjectNode) it.next();
if(BeanUtils.isNotEmpty(mainTable)){
ArrayNode subTable = (ArrayNode) mainTable.get("sub_"+tableName);
for (JsonNode object : subTable) {
ObjectNode subData = (ObjectNode) object;
String fieldValue = subData.get(field).asText();
if(StringUtil.isNotEmpty(fieldValue)){
String[] ids = fieldValue.split(",");
for (String id : ids) {
IUser iUser = userService.getUserById(id);
if(BeanUtils.isNotEmpty(iUser)){
userList.add(iUser);
}
}
}
}
}
}
if(BeanUtils.isNotEmpty(userList)){
identitys.addAll(convertUserList(userList));
}
return identitys;
}
/**
* 获取子表字段(部门选择器,组织负责人)作为节点审批人员。
* @param tableName 表明
* @param field 字段名
* @return
* @throws IOException
*/
public Set<BpmIdentity> getSubFieldOrg(String tableName,String field,Object isMainParam) throws IOException{
Boolean isMain = BooleanUtil.toBooleanObject(String.valueOf(isMainParam));
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
ActionCmd cmd = ContextThreadUtil.getActionCmd();
String json= cmd.getBusData();
ObjectNode jsonObj=(ObjectNode) JsonUtil.toJsonNode(json);
Iterator<Entry<String, JsonNode>> it = jsonObj.fields();
while(it.hasNext()){
ObjectNode mainTable = (ObjectNode) it.next();
if(BeanUtils.isNotEmpty(mainTable)){
ArrayNode subTable = (ArrayNode) mainTable.get("sub_"+tableName);
for (Object object : subTable) {
ObjectNode subData = (ObjectNode) object;
String fieldValue = subData.get(field).asText();
if(StringUtil.isNotEmpty(fieldValue)){
String[] orgIds = fieldValue.split(",");
for (String orgId : orgIds) {
Set<BpmIdentity> orgIdentitys = getChargesByOrgId(orgId, isMain);
identitys.addAll(orgIdentitys);
}
}
}
}
}
return identitys;
}
/*
* 自定义业务数据SQL获取人员ID,返回人员列表(会拼接busDataId)
* @param sql SQL语句
* */
public Set<BpmIdentity> getUserFromBusSql(String sql) throws Exception{
JdbcTemplate jdbcTemplate = AppUtil.getBean(JdbcTemplate.class);
Set<BpmIdentity> identitys = new LinkedHashSet<BpmIdentity>();
sql = sql.toUpperCase();
if(sql.contains("<#ID#>")){
ActionCmd cmd = ContextThreadUtil.getActionCmd();
//String json= cmd.getBusData();
HashMap boInst = (HashMap)cmd.getTransitVars().get("bo_inst_");
//ObjectNode jsonObj=(ObjectNode) JsonUtil.toJsonNode(json);
String boKey = (String)boInst.keySet().iterator().next();
ObjectNode boData = (ObjectNode)boInst.get(boKey);
String id = boData.get("id_").asText();
//String id = jsonObj.get("id_").asText();
sql = sql.replace("<#ID#>",id);
}
List<Map<String, Object>> query = jdbcTemplate.queryForList(sql);
if(BeanUtils.isEmpty(query)){
return identitys;
}
if(query.get(0).size() != 1){
throw new RuntimeException("SQL查询列数不能超过1");
}else{
List<IUser> users = new ArrayList<>();
for (Map<String, Object> stringObjectMap : query) {
String key = stringObjectMap.keySet().iterator().next();
String id = (String) stringObjectMap.get(key);
if(isContainsUserId(users,id)){
continue;
}
IUser user = userService.getUserById(id);
if(BeanUtils.isNotEmpty(user)){
users.add(user);
}
}
if(BeanUtils.isNotEmpty(users)){
identitys.addAll(convertUserList(users));
}
return identitys;
}
}
//判断用户列表中是否含有某个用户ID
private boolean isContainsUserId(List<IUser> users,String id){
if(BeanUtils.isNotEmpty(users)){
return false;
}
for (IUser user : users) {
if(user.getUserId().equals(id)){
return true;
}
}
return false;
}
}