RouterUtils.java 19 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 162 163
								
								System.out.println(listYgglMainEmp);
								
								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);
164
								}
tangzhaoqian committed
165
								
166
								
167
							}else if (RELATION_TYPE_USERS.equals(listRelations.get(i).getType())) {
168

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

257 258 259 260
		return listRouter;

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

	}
	
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	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);
				}
			}
		}
303 304 305 306 307 308 309

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

312
		for_insert:
313
		for (int i = 0,m = listFlowChildren.size() ; i < m; i++) {
tangzhaoqian committed
314 315
			if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				i++;
316
				continue;
tangzhaoqian committed
317 318
			}
				
319 320
			// ClassName 区分参与审批流程人的角色 CREATOR(发起人)、AUDIT(审核人)、COPY(抄送人)
			// 各个角色的逻辑不同
321 322 323 324 325
			switch (listFlowChildren.get(i).getClassName()) {
			case CREATOR:
				SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
326
						.name(ParticipatorType.INITIATOR.getName())
327 328
						.type(ParticipatorType.INITIATOR.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
329 330 331 332 333 334 335 336 337
						.build();
				// 新增 审批执行记录
				aer.insert();
				
				// 新增 执行人
				User user = listFlowChildren.get(i).getRelation().get(0).getUsers().get(0);
				SpmkExecutor.builder()
				.approveExecuteRecordId(aer.getId())
				.empNum(Integer.parseInt(user.getId()))
338
				.operatorHeaderUrl(user.getHeadUrl())
339
				.executorName(user.getName())
tangzhaoqian committed
340
				.sts(ExecutorSts.AGREE.ordinal())
341 342 343 344 345 346 347
				.build()
				.insert();
				break;
			case AUDIT:
				SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
348
				.name(ParticipatorType.APPROVER.getName())
349 350
				.type(ParticipatorType.APPROVER.ordinal())
				.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
351 352 353 354 355 356 357 358 359 360 361
				.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()))
362
					.operatorHeaderUrl(user2.getHeadUrl())
363 364 365 366
					.executorName(user2.getName())
					.build();
					switch (user2.getExecute()) {
					case EXECUTING:
tangzhaoqian committed
367
						executor.setSts(ExecutorSts.IN_EXECUTION.ordinal());
368
						executor.insert();
369
						break for_insert;
370
					case EXECUTED:
tangzhaoqian committed
371
						executor.setSts(ExecutorSts.AGREE.ordinal());
372 373 374 375 376 377 378 379 380
						executor.insert();
						break;
					}
				}
				break;
			case COPY:
				SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
381
				.name(ParticipatorType.COPY.getName())
382 383
				.type(ParticipatorType.COPY.ordinal())
				.sts(ExecuteRecordSts.AGREE.ordinal())
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
				.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())
399
						.operatorHeaderUrl(user2.getHeadUrl())
tangzhaoqian committed
400
						.sts(ExecutorSts.AGREE.ordinal())
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
						.build();
						executor.insert();
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 审批持久化
	 * @param listFlowChildren	节点
	 * @param asId				审批汇总Id
	 * @param executorId		执行人记录Id
	 * @param opinion			意见
	 * @param sts				状态 1执行中 2通过 3拒绝
	 */
418
	public static void approving(List<FlowChildren> listFlowChildren,Integer asId,Integer executeRecordId, Integer executorId, String opinion,Integer sts,User redeployUser ) throws Exception {
419
		
420
		boolean hasNextApprover = false;
421
		
422 423 424 425 426
		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
427

428 429 430 431
			}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
432
						
433 434 435 436 437 438
					}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())
439
							.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
440 441 442 443
							.sts(sts)
							.build()
						.updateById();
						listUser.get(i_user).setExecute(EXECUTED);
tangzhaoqian committed
444 445 446
						
						// 历史审批人
						SpmkApproveSummary.builder().id(asId).historyApprover(listUser.get(i_user).getName()).build().updateById();
447
						// 0未执行 1执行中 2同意 3拒绝 4 转派
448
						if (sts == ExecutorSts.REFUSE.ordinal()) {
449
							// 更新 审批汇总 状态
450
							SpmkApproveSummary.builder().id(asId).currentApprover(CommonEnum.NULL_STR.getDesc()).endTime(new Date()).sts(sts).build().updateById();
451 452 453 454 455 456 457 458
							SpmkApproveExecuteRecord
									.builder()
									.id(executeRecordId)
									.sts(sts)
									.build()
									// 更新 审批执行记录
									.updateById();
							listFlowChildren.get(i_user).setExecute(EXECUTED);
459
							return;
460
						//转派 处理
461
						//在 原审批人 列表中 插入 一个被转派人(审批人)
462
						}else if (sts == ExecutorSts.REDEPLOY.ordinal()) {
463 464 465 466 467
							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();
468 469 470 471 472 473
						}
					}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())
474
							.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
tangzhaoqian committed
475
							.sts(ExecutorSts.IN_EXECUTION.ordinal())
476 477 478 479
							.build()
							.insert();
						hasNextApprover = true;
						listUser.get(i_user).setExecute(EXECUTING);
tangzhaoqian committed
480 481 482
						
						// 当前审批人
						SpmkApproveSummary.builder().id(asId).currentApprover(listUser.get(i_user).getName()).build().updateById();
483 484 485
						
						// 处理了 下一个审批人 则跳出循环
						break;
486 487 488
					}
				}
				
489
				// 无下一个审批人 则更新 节点状态 为 EXECUTED(已执行)
490 491 492 493
				if (!hasNextApprover) {
					SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
							.builder()
							.id(executeRecordId)
494
							.sts(ExecutorSts.AGREE.ordinal())
495 496 497 498 499 500 501 502 503 504 505 506 507
							.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)
508 509
								.name(ParticipatorType.INITIATOR.getName())
								.type(ParticipatorType.INITIATOR.ordinal())
510
								.sts(ExecuteRecordSts.AGREE.ordinal())
511 512 513 514 515 516 517 518 519 520
								.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())
521
						.operatorHeaderUrl(user.getHeadUrl())
tangzhaoqian committed
522
						.sts(ExecutorSts.AGREE.ordinal())
523 524 525 526 527 528 529 530 531
						.build()
						.insert();
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					case AUDIT:
						SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
532
						.name(ParticipatorType.APPROVER.getName())
533 534
						.type(ParticipatorType.APPROVER.ordinal())
						.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
535 536 537 538 539 540 541 542 543
						.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())
544
									.operatorHeaderUrl(listUser.get(i_user2).getHeadUrl())
tangzhaoqian committed
545
									.sts(ExecutorSts.IN_EXECUTION.ordinal())
546 547 548 549 550 551 552 553 554 555 556 557
									.build();
									executor.insert();
									
									listUser.get(i_user2).setExecute(EXECUTING);
						}
						
						listFlowChildren.get(i).setExecute(EXECUTING);
						break;
					case COPY:
						SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
558
						.name(ParticipatorType.COPY.getName())
559 560
						.type(ParticipatorType.COPY.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
561 562 563 564 565 566 567 568
						.build();
						// 新增 审批执行记录
						aer3.insert();

						List<Relation> listRelation = listFlowChildren.get(i).getRelation();
						
						for (Relation relation : listRelation) {
							List<User> listUser2 = relation.getUsers();
569 570 571 572 573 574 575 576 577 578 579 580
							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();
								}
581 582 583 584 585 586 587 588 589
							}
						}
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					}
				}
			}
		}
590
		
591
	}
592 593

}