RouterUtils.java 18.9 KB
Newer Older
1
package cn.timer.api.utils.router;
2 3

import java.util.ArrayList;
tangzhaoqian committed
4
import java.util.Date;
5 6
import java.util.List;

7 8
import org.apache.commons.collections4.ListUtils;

9 10 11
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

12
import cn.hutool.core.bean.BeanUtil;
13
import cn.hutool.core.collection.CollectionUtil;
14
import cn.hutool.core.convert.ConvertException;
15 16
import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
17
import cn.timer.api.bean.spmk.SpmkApproveExecuteRecord;
18
import cn.timer.api.bean.spmk.SpmkApproveSummary;
19
import cn.timer.api.bean.spmk.SpmkExecutor;
20 21
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.bean.zzgl.ZzglBmgwM;
22
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ExecuteRecordSts;
tangzhaoqian committed
23
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ExecutorSts;
24
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ParticipatorType;
25
import cn.timer.api.config.enums.CommonEnum;
26
import cn.timer.api.dto.spmk.Condition;
27 28
import cn.timer.api.dto.spmk.FlowChildren;
import cn.timer.api.dto.spmk.FromData;
29 30 31 32 33 34 35 36 37 38 39
import cn.timer.api.dto.spmk.Relation;
import cn.timer.api.dto.spmk.Router;
import cn.timer.api.dto.spmk.User;

/**
 * 流程节点工具类
 * 
 * @author Administrator
 *
 */
public class RouterUtils {
40 41 42
	/**
	 * 0 未执行
	 */
43
	private final static String UNEXECUTED = "0";
44 45 46
	/**
	 * 1 执行中
	 */
47
	private final static String EXECUTING = "1";
48 49 50
	/**
	 * 2 已执行
	 */
51
	private final static String EXECUTED = "2";
52 53 54
	/**
	 * creator 抄送人
	 */
55
	private final static String CREATOR = "creator";
56 57 58
	/**
	 * audit 审批人
	 */
59
	private final static String AUDIT = "audit";
60 61 62
	/**
	 * copy 抄送人
	 */
63
	private final static String COPY = "copy";
64 65 66
	/**
	 * department 部门类型
	 */
67
	private final static String RELATION_TYPE_DEPARTMENT = "department";
68 69 70
	/**
	 * users 用户类型
	 */
71 72
	private final static String RELATION_TYPE_USERS = "users";

73
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse) throws NumberFormatException, ConvertException, Exception {
74 75
		return NextNode(listRouter, obj, isFirse, false);
	}
76 77

	// 执行下一个节点
78
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse, boolean isAuditNext) throws NumberFormatException, ConvertException, Exception {
79 80 81 82 83 84
		
		Router router;
		if (listRouter != null && listRouter.size() != 0) {
			// 非条件节点
			if (listRouter.size() == 1) {
				router = listRouter.get(0);
85 86 87 88
				if (router.getExecute() == null) {
					System.out.println("默认UNEXECUTED");
					router.setExecute(UNEXECUTED);
				}
89 90 91 92 93 94 95 96
				// 0未执行 1执行中 2已执行
				switch (router.getExecute()) {
				case UNEXECUTED:
					switch (router.getClassName()) {
					case CREATOR:
						Console.log("发起人逻辑");
						router.setExecute(EXECUTED);
						router.setFlow(true);
tangzhaoqian committed
97 98 99 100 101
						
						List<User> users = new ArrayList<User>();
						User userFirst =  User.builder()
								.name(obj.get("initiator",FromData.class).getValue())
								.id(obj.get("id",FromData.class).getValue())
102
								.headUrl(obj.get("headUrl",FromData.class).getValue())
103
								.execute(UNEXECUTED)
tangzhaoqian committed
104 105 106 107 108 109 110 111
								.build();
						users.add(userFirst);
						List<Relation> relations = new ArrayList<Relation>();
						Relation relation = Relation.builder().type("user").users(users).build();
						relations.add(relation);
						
						router.setRelation(relations);
//						router.getRelation().get(0).setName(obj.get("initiator",FromData.class).getValue());
112 113
						
						NextNode(router.getChildren(),obj, isFirse, isAuditNext);
114 115 116 117 118 119 120 121 122 123 124 125 126
						break;
					case AUDIT:
						Console.log("审批人逻辑");
						router.setExecute(EXECUTING);
						router.setFlow(true);
						
						List<User> listUser = router.getRelation().get(0).getUsers();
						user:
							for (int i = 0; i < listUser.size(); i++) {
								String execute = listUser.get(i).getExecute();
								switch (execute) {
								case UNEXECUTED:
									listUser.get(i).setExecute(EXECUTING);
127 128
									// 首次发起申请时,写入 审批人名称 至 obj 中
									if (isFirse) {
129
										obj.set("current_approver", listUser.get(i).getName());
130
									}
131
									
132 133 134
									break user;
								}
							}
135
						isAuditNext = true;
136
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
137 138
						break;
					case COPY:
139 140 141
						if (!isAuditNext) {
							router.setExecute(EXECUTED);
						}
142 143 144 145 146
						Console.log("抄送人逻辑");
						router.setFlow(true);
						List<Relation> listRelations = router.getRelation();
						for (int i = 0; i < listRelations.size(); i++) {
							// 装配 部门人员
147 148 149
							if (RELATION_TYPE_DEPARTMENT.equals(listRelations.get(i).getType()) && listRelations.get(i).getDepartmentId() != null) {
								
								List<YgglMainEmp> listYgglMainEmp = selectOtherlistent(Integer.parseInt(obj.get("orgCode",FromData.class).getValue().trim()), Integer.valueOf(listRelations.get(i).getDepartmentId().trim()));
tangzhaoqian committed
150 151 152 153 154 155 156 157 158 159 160 161
								
								if (listYgglMainEmp != null && listYgglMainEmp.size() > 0) {
									List<User> listUsers = new ArrayList<User>();
									for (YgglMainEmp emp : listYgglMainEmp) {
										User user = new User();
										user.setName(emp.getName());
										user.setId(String.valueOf(emp.getEmpNum()));
										listUsers.add(user);
									}
									listRelations.get(i).setUsers(listUsers);
								}else {
									listRelations.get(i).setUsers(null);
162
								}
tangzhaoqian committed
163
								
164
								
165
							}else if (RELATION_TYPE_USERS.equals(listRelations.get(i).getType())) {
166

167 168
							}
						}
169
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
170 171 172 173 174 175 176
						break;
					}
					break;
				case EXECUTING:
					switch (router.getClassName()) {
					case AUDIT:
						router.setFlow(true);
177 178 179 180 181 182 183 184 185 186
						if (!isFirse) {
							Console.log("下一个审批人逻辑");
							boolean executeFlog = true;
							List<User> listUser = router.getRelation().get(0).getUsers();
							user:
								for (int i = 0; i < listUser.size(); i++) {
									String execute = listUser.get(i).getExecute();
									switch (execute) {
									case UNEXECUTED:
										listUser.get(i).setExecute(EXECUTING);
187
										executeFlog = false;
188 189 190 191
										break user;
									case EXECUTING:
										listUser.get(i).setExecute(EXECUTED);
										break;
192 193
									}
								}
194 195 196
							
							if (executeFlog) {
								router.setExecute(EXECUTED);
197
							}
198
							
199
						}
200
						NextNode(router.getChildren(),obj , isFirse, isAuditNext);
201 202 203 204 205
						break;
					}
					break;
				case EXECUTED:
					Console.log("下一个节点");
206
					NextNode(router.getChildren(),obj , isFirse, isAuditNext);
207 208 209 210 211 212 213 214 215 216 217 218 219 220
					break;
				}
				
			} else {
				// 条件节点
				rulefor:
				for (Router routerRule : listRouter) {
					switch (routerRule.getExecute()) {
					case UNEXECUTED:
						boolean condition_b = true;
						List<Condition> listCondition = routerRule.getCondition();
						if (listCondition == null || listCondition.size() == 0) {
							routerRule.setFlow(true);
						}else {
221
							Compare compare;
222
							for (Condition condition : listCondition) {
223
									for (String string : condition.getValues()) {
224 225 226 227
										
										// 简单工厂模式 - 判断条件
										compare = CompareFactory.createCompare(obj.get(condition.getKey(),FromData.class).getValue(), string, condition.getFormat());
										if (!compare.GetResutl()) {
228 229
											condition_b = false;
										}
230
										
231
									}
232 233 234 235 236
							}
						}
						
						routerRule.setFlow(condition_b);
						routerRule.setExecute(EXECUTED);
237
						if (routerRule.getFlow()) {
238
							Console.log("条件逻辑");
239
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
240 241 242
						}
						break;
					case EXECUTED:
243
						if (routerRule.getFlow()) {
244
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
245
						}
246 247 248 249
						break rulefor;
					}
					
				}
250 251
			
				Console.log("最后节点");
252 253
			}
		}
254

255 256 257 258
		return listRouter;

	};
	
259
	
260 261 262
	
	public static List<YgglMainEmp> selectOtherlistent(Integer orgCode, Integer id){
		ArrayList<Integer> list = new ArrayList<Integer>();
263 264
		List<ZzglBmgwM> zzglBmgwMs = ZzglBmgwM.builder().build().selectList(new QueryWrapper<ZzglBmgwM>().lambda()
				.eq(ZzglBmgwM::getOrgCode, orgCode));
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		list.add(id);
		ZzglBmgwM.getDepts(list, id, zzglBmgwMs);

		if (list == null || list.size() == 0) {
			return null;
		} else {
			LambdaQueryWrapper<YgglMainEmp> ygglMainEmpsLambdaQueryWrapper = new LambdaQueryWrapper<>();
			ygglMainEmpsLambdaQueryWrapper
					.select(YgglMainEmp::getId, YgglMainEmp::getEmpNum, YgglMainEmp::getName, YgglMainEmp::getPhone,
							YgglMainEmp::getBmgwId)
					.eq(YgglMainEmp::getOrgCode, orgCode).and(i -> i.in(YgglMainEmp::getBmgwId, list.toArray()));
			List<YgglMainEmp> ygglMainEmps = YgglMainEmp.builder().build().selectList(ygglMainEmpsLambdaQueryWrapper);
			return ygglMainEmps;
		}

	}
	
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	public static void getIsFlowChildren(List<Router> listRouter, List<FlowChildren> listFlowChildren) {
		// TODO Auto-generated method stub
		Router router;
		if (listRouter != null && listRouter.size() == 1) {
			router = listRouter.get(0);
			if (router.getFlow()) {
				FlowChildren fc = FlowChildren.builder().build();
				BeanUtil.copyProperties(router, fc, "condition","children");
				listFlowChildren.add(fc);
				getIsFlowChildren(router.getChildren(), listFlowChildren);
			}
			
		}else if (listRouter.size() > 1) {
			for (Router router2 : listRouter) {
				if (router2.getFlow()) {
					getIsFlowChildren(router2.getChildren(), listFlowChildren);
				}
			}
		}
301 302 303 304 305 306 307

	};
	
	
	
	
	// 审批执行记录 持久化
308
	public static void insertogExecuteRecord(List<FlowChildren> listFlowChildren,Integer asId) throws Exception{
309

310
		for_insert:
311
		for (int i = 0,m = listFlowChildren.size() ; i < m; i++) {
tangzhaoqian committed
312 313
			if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				i++;
314
				continue;
tangzhaoqian committed
315 316
			}
				
317 318
			// ClassName 区分参与审批流程人的角色 CREATOR(发起人)、AUDIT(审核人)、COPY(抄送人)
			// 各个角色的逻辑不同
319 320 321 322 323
			switch (listFlowChildren.get(i).getClassName()) {
			case CREATOR:
				SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
324
						.name(ParticipatorType.INITIATOR.getName())
325 326
						.type(ParticipatorType.INITIATOR.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
327 328 329 330 331 332 333 334 335
						.build();
				// 新增 审批执行记录
				aer.insert();
				
				// 新增 执行人
				User user = listFlowChildren.get(i).getRelation().get(0).getUsers().get(0);
				SpmkExecutor.builder()
				.approveExecuteRecordId(aer.getId())
				.empNum(Integer.parseInt(user.getId()))
336
				.operatorHeaderUrl(user.getHeadUrl())
337
				.executorName(user.getName())
tangzhaoqian committed
338
				.sts(ExecutorSts.AGREE.ordinal())
339 340 341 342 343 344 345
				.build()
				.insert();
				break;
			case AUDIT:
				SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
346
				.name(ParticipatorType.APPROVER.getName())
347 348
				.type(ParticipatorType.APPROVER.ordinal())
				.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
349 350 351 352 353 354 355 356 357 358 359
				.build();
				// 新增 审批执行记录
				aer2.insert();

				// 新增 执行人
				List<User> listUser = listFlowChildren.get(i).getRelation().get(0).getUsers();
				
				for (User user2 : listUser) {
					SpmkExecutor executor = SpmkExecutor.builder()
					.approveExecuteRecordId(aer2.getId())
					.empNum(Integer.parseInt(user2.getId()))
360
					.operatorHeaderUrl(user2.getHeadUrl())
361 362 363 364
					.executorName(user2.getName())
					.build();
					switch (user2.getExecute()) {
					case EXECUTING:
tangzhaoqian committed
365
						executor.setSts(ExecutorSts.IN_EXECUTION.ordinal());
366
						executor.insert();
367
						break for_insert;
368
					case EXECUTED:
tangzhaoqian committed
369
						executor.setSts(ExecutorSts.AGREE.ordinal());
370 371 372 373 374 375 376 377 378
						executor.insert();
						break;
					}
				}
				break;
			case COPY:
				SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
379
				.name(ParticipatorType.COPY.getName())
380 381
				.type(ParticipatorType.COPY.ordinal())
				.sts(ExecuteRecordSts.AGREE.ordinal())
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
				.build();
				// 新增 审批执行记录
				aer3.insert();

				List<Relation> listRelation = listFlowChildren.get(i).getRelation();
				
				for (Relation relation : listRelation) {
					List<User> listUser2 = relation.getUsers();
					
					// 新增 执行人
					for (User user2 : listUser2) {
						SpmkExecutor executor = SpmkExecutor.builder()
						.approveExecuteRecordId(aer3.getId())
						.empNum(Integer.parseInt(user2.getId()))
						.executorName(user2.getName())
397
						.operatorHeaderUrl(user2.getHeadUrl())
tangzhaoqian committed
398
						.sts(ExecutorSts.AGREE.ordinal())
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
						.build();
						executor.insert();
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 审批持久化
	 * @param listFlowChildren	节点
	 * @param asId				审批汇总Id
	 * @param executorId		执行人记录Id
	 * @param opinion			意见
	 * @param sts				状态 1执行中 2通过 3拒绝
	 */
416
	public static void approving(List<FlowChildren> listFlowChildren,Integer asId,Integer executeRecordId, Integer executorId, String opinion,Integer sts,User redeployUser ) throws Exception {
417
		
418
		boolean hasNextApprover = false;
419
		
420 421 422 423 424
		for (int i = 0,n = listFlowChildren.size(); i < n; i++) {

			// 新增 执行人
			List<User> listUser = listFlowChildren.get(i).getRelation().get(0).getUsers();
			if (EXECUTED.equals(listFlowChildren.get(i).getExecute())) {
tangzhaoqian committed
425

426 427 428 429
			}else if (EXECUTING.equals(listFlowChildren.get(i).getExecute())) {
				
				for (int i_user = 0, n_user = listUser.size(); i_user < n_user; i_user++) {
					if (EXECUTED.equals(listUser.get(i_user).getExecute())) {
tangzhaoqian committed
430
						
431 432 433 434 435 436
					}else if (EXECUTING.equals(listUser.get(i_user).getExecute())) {
						SpmkExecutor.builder()
							.id(executorId)
							.opinion(opinion)
							.empNum(Integer.parseInt(listUser.get(i_user).getId()))
							.executorName(listUser.get(i_user).getName())
437
							.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
438 439 440 441
							.sts(sts)
							.build()
						.updateById();
						listUser.get(i_user).setExecute(EXECUTED);
tangzhaoqian committed
442 443 444
						
						// 历史审批人
						SpmkApproveSummary.builder().id(asId).historyApprover(listUser.get(i_user).getName()).build().updateById();
445
						// 0未执行 1执行中 2同意 3拒绝 4 转派
446
						if (sts == ExecutorSts.REFUSE.ordinal()) {
447
							// 更新 审批汇总 状态
448
							SpmkApproveSummary.builder().id(asId).currentApprover(CommonEnum.NULL_STR.getDesc()).endTime(new Date()).sts(sts).build().updateById();
449 450 451 452 453 454 455 456
							SpmkApproveExecuteRecord
									.builder()
									.id(executeRecordId)
									.sts(sts)
									.build()
									// 更新 审批执行记录
									.updateById();
							listFlowChildren.get(i_user).setExecute(EXECUTED);
457
							return;
458
						//转派 处理
459
						//在 原审批人 列表中 插入 一个被转派人(审批人)
460
						}else if (sts == ExecutorSts.REDEPLOY.ordinal()) {
461 462 463 464 465
							List<User> users1 = CollectionUtil.sub(listUser, 0, i_user+1);
							users1.add(redeployUser);
							List<User> users2 = CollectionUtil.sub(listUser, i_user+1, listUser.size());
							listUser = ListUtils.union(users1, users2);
							n_user = listUser.size();
466 467 468 469 470 471
						}
					}else if (UNEXECUTED.equals(listUser.get(i_user).getExecute())) {
						SpmkExecutor.builder()
							.approveExecuteRecordId(executeRecordId)
							.empNum(Integer.parseInt(listUser.get(i_user).getId()))
							.executorName(listUser.get(i_user).getName())
472
							.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
tangzhaoqian committed
473
							.sts(ExecutorSts.IN_EXECUTION.ordinal())
474 475 476 477
							.build()
							.insert();
						hasNextApprover = true;
						listUser.get(i_user).setExecute(EXECUTING);
tangzhaoqian committed
478 479 480
						
						// 当前审批人
						SpmkApproveSummary.builder().id(asId).currentApprover(listUser.get(i_user).getName()).build().updateById();
481 482 483
						
						// 处理了 下一个审批人 则跳出循环
						break;
484 485 486
					}
				}
				
487
				// 无下一个审批人 则更新 节点状态 为 EXECUTED(已执行)
488 489 490 491
				if (!hasNextApprover) {
					SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
							.builder()
							.id(executeRecordId)
492
							.sts(ExecutorSts.AGREE.ordinal())
493 494 495 496 497 498 499 500 501 502 503 504 505
							.build();
							// 更新 审批执行记录
							aer.updateById();
					listFlowChildren.get(i).setExecute(EXECUTED);
				}
				
			}else if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				if (!hasNextApprover) {
					switch (listFlowChildren.get(i).getClassName()) {
					case CREATOR:
						SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
								.builder()
								.approveSummaryId(asId)
506 507
								.name(ParticipatorType.INITIATOR.getName())
								.type(ParticipatorType.INITIATOR.ordinal())
508
								.sts(ExecuteRecordSts.AGREE.ordinal())
509 510 511 512 513 514 515 516 517 518
								.build();
						// 新增 审批执行记录
						aer.insert();
						
						// 新增 执行人
						User user = listFlowChildren.get(i).getRelation().get(0).getUsers().get(0);
						SpmkExecutor.builder()
						.approveExecuteRecordId(aer.getId())
						.empNum(Integer.parseInt(user.getId()))
						.executorName(user.getName())
519
						.operatorHeaderUrl(user.getHeadUrl())
tangzhaoqian committed
520
						.sts(ExecutorSts.AGREE.ordinal())
521 522 523 524 525 526 527 528 529
						.build()
						.insert();
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					case AUDIT:
						SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
530
						.name(ParticipatorType.APPROVER.getName())
531 532
						.type(ParticipatorType.APPROVER.ordinal())
						.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
533 534 535 536 537 538 539 540 541
						.build();
						// 新增 审批执行记录
						aer2.insert();

						for (int i_user2 = 0,n_user2 = listUser.size(); i_user2 < n_user2; i_user2++) {
							SpmkExecutor executor = SpmkExecutor.builder()
									.approveExecuteRecordId(aer2.getId())
									.empNum(Integer.parseInt(listUser.get(i_user2).getId()))
									.executorName(listUser.get(i_user2).getName())
542
									.operatorHeaderUrl(listUser.get(i_user2).getHeadUrl())
tangzhaoqian committed
543
									.sts(ExecutorSts.IN_EXECUTION.ordinal())
544 545 546 547 548 549 550 551 552 553 554 555
									.build();
									executor.insert();
									
									listUser.get(i_user2).setExecute(EXECUTING);
						}
						
						listFlowChildren.get(i).setExecute(EXECUTING);
						break;
					case COPY:
						SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
556
						.name(ParticipatorType.COPY.getName())
557 558
						.type(ParticipatorType.COPY.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
559 560 561 562 563 564 565 566
						.build();
						// 新增 审批执行记录
						aer3.insert();

						List<Relation> listRelation = listFlowChildren.get(i).getRelation();
						
						for (Relation relation : listRelation) {
							List<User> listUser2 = relation.getUsers();
567 568 569 570 571 572 573 574 575 576 577 578
							if (listUser2 != null) {
								// 新增 执行人
								for (User user2 : listUser2) {
									SpmkExecutor executor = SpmkExecutor.builder()
									.approveExecuteRecordId(aer3.getId())
									.empNum(Integer.parseInt(user2.getId()))
									.executorName(user2.getName())
									.operatorHeaderUrl(user2.getHeadUrl())
									.sts(ExecutorSts.AGREE.ordinal())
									.build();
									executor.insert();
								}
579 580 581 582 583 584 585 586 587
							}
						}
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					}
				}
			}
		}
588
		
589
	}
590 591

}