RouterUtils.java 17.6 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 22
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.bean.zzgl.ZzglBmgwM;
import cn.timer.api.dto.spmk.Condition;
23 24
import cn.timer.api.dto.spmk.FlowChildren;
import cn.timer.api.dto.spmk.FromData;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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 {
	
	private final static String UNEXECUTED = "0";
	private final static String EXECUTING = "1";
	private final static String EXECUTED = "2";
	private final static String CREATOR = "creator";
	private final static String RULES = "rules";
	private final static String AUDIT = "audit";
	private final static String COPY = "copy";
	private final static String RELATION_TYPE_DEPARTMENT = "department";
	private final static String RELATION_TYPE_USERS = "users";

47
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse) throws NumberFormatException, ConvertException, Exception {
48 49
		return NextNode(listRouter, obj, isFirse, false);
	}
50 51

	// 执行下一个节点
52
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse, boolean isAuditNext) throws NumberFormatException, ConvertException, Exception {
53 54 55 56 57 58
		
		Router router;
		if (listRouter != null && listRouter.size() != 0) {
			// 非条件节点
			if (listRouter.size() == 1) {
				router = listRouter.get(0);
59 60 61 62
				if (router.getExecute() == null) {
					System.out.println("默认UNEXECUTED");
					router.setExecute(UNEXECUTED);
				}
63 64 65 66 67 68 69 70
				// 0未执行 1执行中 2已执行
				switch (router.getExecute()) {
				case UNEXECUTED:
					switch (router.getClassName()) {
					case CREATOR:
						Console.log("发起人逻辑");
						router.setExecute(EXECUTED);
						router.setFlow(true);
tangzhaoqian committed
71 72 73 74 75
						
						List<User> users = new ArrayList<User>();
						User userFirst =  User.builder()
								.name(obj.get("initiator",FromData.class).getValue())
								.id(obj.get("id",FromData.class).getValue())
76
								.headUrl(obj.get("headUrl",FromData.class).getValue())
tangzhaoqian committed
77 78 79 80 81 82 83 84 85
								.execute("0")
								.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());
86 87
						
						NextNode(router.getChildren(),obj, isFirse, isAuditNext);
88 89 90 91 92 93 94 95 96 97 98 99 100
						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);
101 102
									// 首次发起申请时,写入 审批人名称 至 obj 中
									if (isFirse) {
103
										obj.set("current_approver", listUser.get(i).getName());
104
									}
105
									
106
									break user;
107 108 109
//								case EXECUTING:
//									listUser.get(i).setExecute(EXECUTED);
//									break;
110 111
								}
							}
112
						isAuditNext = true;
113
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
114 115
						break;
					case COPY:
116 117 118
						if (!isAuditNext) {
							router.setExecute(EXECUTED);
						}
119 120 121 122 123
						Console.log("抄送人逻辑");
						router.setFlow(true);
						List<Relation> listRelations = router.getRelation();
						for (int i = 0; i < listRelations.size(); i++) {
							// 装配 部门人员
124 125 126 127 128
							if (RELATION_TYPE_DEPARTMENT.equals(listRelations.get(i).getType()) && listRelations.get(i).getDepartmentId() != null) {
								
								Console.log("listRelations-----------"+ listRelations);
								Console.log("obj-----------"+ obj);
								List<YgglMainEmp> listYgglMainEmp = selectOtherlistent(Integer.parseInt(obj.get("orgCode",FromData.class).getValue().trim()), Integer.valueOf(listRelations.get(i).getDepartmentId().trim()));
tangzhaoqian committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
								
								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);
143
								}
tangzhaoqian committed
144
								
145
								
146
							}else if (RELATION_TYPE_USERS.equals(listRelations.get(i).getType())) {
147

148 149
							}
						}
150
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
151 152 153 154 155 156 157
						break;
					}
					break;
				case EXECUTING:
					switch (router.getClassName()) {
					case AUDIT:
						router.setFlow(true);
158 159 160 161 162 163 164 165 166 167
						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);
168
										executeFlog = false;
169 170 171 172
										break user;
									case EXECUTING:
										listUser.get(i).setExecute(EXECUTED);
										break;
173 174
									}
								}
175 176 177
							
							if (executeFlog) {
								router.setExecute(EXECUTED);
178
							}
179
							
180
						}
181
						NextNode(router.getChildren(),obj , isFirse, isAuditNext);
182 183 184 185 186
						break;
					}
					break;
				case EXECUTED:
					Console.log("下一个节点");
187
					NextNode(router.getChildren(),obj , isFirse, isAuditNext);
188 189 190 191 192 193 194 195 196 197 198 199 200 201
					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 {
202
							Compare compare;
203
							for (Condition condition : listCondition) {
204
									for (String string : condition.getValues()) {
205 206 207 208
										
										// 简单工厂模式 - 判断条件
										compare = CompareFactory.createCompare(obj.get(condition.getKey(),FromData.class).getValue(), string, condition.getFormat());
										if (!compare.GetResutl()) {
209 210
											condition_b = false;
										}
211
										
212
									}
213 214 215 216 217
							}
						}
						
						routerRule.setFlow(condition_b);
						routerRule.setExecute(EXECUTED);
218
						if (routerRule.getFlow()) {
219
							Console.log("条件逻辑");
220
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
221 222 223
						}
						break;
					case EXECUTED:
224
						if (routerRule.getFlow()) {
225
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
226
						}
227 228 229 230
						break rulefor;
					}
					
				}
231 232
			
				Console.log("最后节点");
233 234
			}
		}
235

236 237 238 239
		return listRouter;

	};
	
240
	
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	
	public static List<YgglMainEmp> selectOtherlistent(Integer orgCode, Integer id){
		ArrayList<Integer> list = new ArrayList<Integer>();
		QueryWrapper<ZzglBmgwM> queryWrapper = new QueryWrapper<ZzglBmgwM>();
		queryWrapper.eq("org_code", orgCode);
		List<ZzglBmgwM> zzglBmgwMs = ZzglBmgwM.builder().build().selectList(queryWrapper);
		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;
		}

	}
	
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	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);
				}
			}
		}
283 284 285 286 287 288 289

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

292
		for_insert:
293
		for (int i = 0,m = listFlowChildren.size() ; i < m; i++) {
tangzhaoqian committed
294 295
			if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				i++;
296
				continue;
tangzhaoqian committed
297 298
			}
				
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
			
			switch (listFlowChildren.get(i).getClassName()) {
			case CREATOR:
				SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
						.name("发起申请")
						.type(0)
						.sts(2)
						.build();
				// 新增 审批执行记录
				aer.insert();
				
				// 新增 执行人
				User user = listFlowChildren.get(i).getRelation().get(0).getUsers().get(0);
				SpmkExecutor.builder()
				.approveExecuteRecordId(aer.getId())
				.empNum(Integer.parseInt(user.getId()))
317
				.operatorHeaderUrl(user.getHeadUrl())
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
				.executorName(user.getName())
				.sts(2)
				.build()
				.insert();
				break;
			case AUDIT:
				SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
				.name("审批人")
				.type(2)
				.sts(1)
				.build();
				// 新增 审批执行记录
				aer2.insert();

				// 新增 执行人
				List<User> listUser = listFlowChildren.get(i).getRelation().get(0).getUsers();
				
				executor: 
				for (User user2 : listUser) {
					SpmkExecutor executor = SpmkExecutor.builder()
					.approveExecuteRecordId(aer2.getId())
					.empNum(Integer.parseInt(user2.getId()))
342
					.operatorHeaderUrl(user2.getHeadUrl())
343 344 345 346 347 348
					.executorName(user2.getName())
					.build();
					switch (user2.getExecute()) {
					case EXECUTING:
						executor.setSts(1);
						executor.insert();
349
						break for_insert;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
					case EXECUTED:
						executor.setSts(2);
						executor.insert();
						break;
					}
				}
				break;
			case COPY:
				SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
				.name("抄送人")
				.type(1)
				.sts(2)
				.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())
379
						.operatorHeaderUrl(user2.getHeadUrl())
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
						.sts(2)
						.build();
						executor.insert();
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 审批持久化
	 * @param listFlowChildren	节点
	 * @param asId				审批汇总Id
	 * @param executorId		执行人记录Id
	 * @param opinion			意见
	 * @param sts				状态 1执行中 2通过 3拒绝
	 */
398
	public static void approving(List<FlowChildren> listFlowChildren,Integer asId,Integer executeRecordId, Integer executorId, String opinion,Integer sts,User redeployUser ) throws Exception {
399
		
400
		boolean hasNextApprover = false;
401
		
402 403 404 405 406
		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
407

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

}