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

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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

10
import cn.hutool.core.bean.BeanUtil;
11 12 13
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONObject;
14
import cn.timer.api.bean.spmk.SpmkApproveExecuteRecord;
15
import cn.timer.api.bean.spmk.SpmkApproveSummary;
16
import cn.timer.api.bean.spmk.SpmkExecutor;
17 18 19
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.bean.zzgl.ZzglBmgwM;
import cn.timer.api.dto.spmk.Condition;
20 21
import cn.timer.api.dto.spmk.FlowChildren;
import cn.timer.api.dto.spmk.FromData;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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";

44 45 46
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse) {
		return NextNode(listRouter, obj, isFirse, false);
	}
47 48

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

128 129
							}
						}
130
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
131 132 133 134 135 136 137
						break;
					}
					break;
				case EXECUTING:
					switch (router.getClassName()) {
					case AUDIT:
						router.setFlow(true);
138 139 140 141 142 143 144 145 146 147
						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);
148
										executeFlog = false;
149 150 151 152
										break user;
									case EXECUTING:
										listUser.get(i).setExecute(EXECUTED);
										break;
153 154
									}
								}
155 156 157
							
							if (executeFlog) {
								router.setExecute(EXECUTED);
158
							}
159
							
160
						}
161
						NextNode(router.getChildren(),obj , isFirse, isAuditNext);
162 163 164 165 166
						break;
					}
					break;
				case EXECUTED:
					Console.log("下一个节点");
167
					NextNode(router.getChildren(),obj , isFirse, isAuditNext);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
					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 {
							for (Condition condition : listCondition) {
183 184 185 186 187 188
									for (String string : condition.getValues()) {
										// 判断条件
										if (!stringCompare(obj.get(condition.getKey(),FromData.class).getValue(), string , condition.getFormat())) {
											condition_b = false;
										}
									}
189 190 191 192 193
							}
						}
						
						routerRule.setFlow(condition_b);
						routerRule.setExecute(EXECUTED);
194
						if (routerRule.getFlow()) {
195
							Console.log("条件逻辑");
196
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
197 198 199
						}
						break;
					case EXECUTED:
200
						if (routerRule.getFlow()) {
201
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
202
						}
203 204 205 206
						break rulefor;
					}
					
				}
207 208
			
				Console.log("最后节点");
209 210
			}
		}
211

212 213 214 215
		return listRouter;

	};
	
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
	
	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;
		}

	}
	
	private static boolean stringCompare(String s1,String s2,String symbol) {
		double d1;
		double d2;
		switch (symbol) {
		case "<":
			d1 = Double.parseDouble(s1);
			d2 = Double.parseDouble(s2);
			if (NumberUtil.compare(d1,d2) == -1) return true;
			break;
		case "<=":
			d1 = Double.parseDouble(s1);
			d2 = Double.parseDouble(s2);
			if (NumberUtil.compare(d1,d2) == -1 || NumberUtil.compare(d1,d2) == 0) return true;
			break;
		case "=":
			if (s2.equals(s1)) return true;
			break;
		case ">":
			d1 = Double.parseDouble(s1);
			d2 = Double.parseDouble(s2);
			if (NumberUtil.compare(d1,d2) == 1) return true;
			break;
		case ">=":
			d1 = Double.parseDouble(s1);
			d2 = Double.parseDouble(s2);
			if (NumberUtil.compare(d1,d2) == 0 || NumberUtil.compare(d1,d2) == 1) return true;
			break;
		}
		return false;

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
	}

	
	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()) {
//					FlowChildren fc = FlowChildren.builder().build();
//					BeanUtil.copyProperties(router2, fc, "condition","children");
//					listFlowChildren.add(fc);
					getIsFlowChildren(router2.getChildren(), listFlowChildren);
				}
			}
		}
295 296 297 298 299 300 301 302 303 304

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

		for (int i = 0,m = listFlowChildren.size() ; i < m; i++) {
tangzhaoqian committed
305 306
			if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				i++;
307
				continue;
tangzhaoqian committed
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 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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
			
			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())
				.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()))
					.executorName(user2.getName())
					.build();
					switch (user2.getExecute()) {
					case EXECUTING:
						executor.setSts(1);
						executor.insert();
						break executor;
					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())
						.sts(2)
						.build();
						executor.insert();
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 审批持久化
	 * @param listFlowChildren	节点
	 * @param asId				审批汇总Id
	 * @param executorId		执行人记录Id
	 * @param opinion			意见
	 * @param sts				状态 1执行中 2通过 3拒绝
	 */
	public static void approving(List<FlowChildren> listFlowChildren,Integer asId,Integer executeRecordId, Integer executorId, String opinion,Integer sts ) {
409
		
410
		boolean hasNextApprover = false;
411
		
412 413 414 415 416
		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
417

418 419 420 421 422 423
			}else if (EXECUTING.equals(listFlowChildren.get(i).getExecute())) {
				
				// 新增 执行人
//				listUser = flowChildren.getRelation().get(0).getUsers();
				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
424
						
425 426 427 428 429 430 431 432 433 434
					}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())
							.sts(sts)
							.build()
						.updateById();
						listUser.get(i_user).setExecute(EXECUTED);
tangzhaoqian committed
435 436 437
						
						// 历史审批人
						SpmkApproveSummary.builder().id(asId).historyApprover(listUser.get(i_user).getName()).build().updateById();
438 439 440
						// 0未执行 1执行中 2同意 3拒绝
						if (sts == 3) {
							// 更新 审批汇总 状态
tangzhaoqian committed
441
							SpmkApproveSummary.builder().id(asId).endTime(new Date()).sts(sts).build().updateById();
442 443 444 445 446 447 448 449 450 451 452 453
							return;
						}
					}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())
							.sts(1)
							.build()
							.insert();
						hasNextApprover = true;
						listUser.get(i_user).setExecute(EXECUTING);
tangzhaoqian committed
454 455 456
						
						// 当前审批人
						SpmkApproveSummary.builder().id(asId).currentApprover(listUser.get(i_user).getName()).build().updateById();
457 458 459 460 461 462 463 464 465 466 467 468 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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 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 555 556 557 558
					}
				}
				
				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())
						.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();

						// 新增 执行人
//						listUser = flowChildren.getRelation().get(0).getUsers();
						for (int i_user2 = 0,n_user2 = listUser.size(); i_user2 < n_user2; i_user2++) {
							User u = listUser.get(i_user2);
							SpmkExecutor executor = SpmkExecutor.builder()
									.approveExecuteRecordId(aer2.getId())
									.empNum(Integer.parseInt(listUser.get(i_user2).getId()))
									.executorName(listUser.get(i_user2).getName())
									.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())
								.sts(2)
								.build();
								executor.insert();
							}
						}
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					}
				}
			}
		}
559
		
560
	}
561 562

}