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

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

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

10 11
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
tangzhaoqian committed
12
import com.mysql.cj.x.protobuf.MysqlxPrepare.Execute;
13

14
import cn.hutool.core.bean.BeanUtil;
tangzhaoqian committed
15
import cn.hutool.core.collection.CollUtil;
16
import cn.hutool.core.collection.CollectionUtil;
tangzhaoqian committed
17 18
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
19
import cn.hutool.core.convert.ConvertException;
20 21
import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
22
import cn.timer.api.bean.spmk.SpmkApproveExecuteRecord;
23
import cn.timer.api.bean.spmk.SpmkApproveSummary;
24
import cn.timer.api.bean.spmk.SpmkExecutor;
25 26
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.bean.zzgl.ZzglBmgwM;
27
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ExecuteRecordSts;
tangzhaoqian committed
28
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ExecutorSts;
29
import cn.timer.api.config.enuminterface.SpmkEnumInterface.ParticipatorType;
30
import cn.timer.api.config.enums.CommonEnum;
31
import cn.timer.api.dto.spmk.Condition;
32 33
import cn.timer.api.dto.spmk.FlowChildren;
import cn.timer.api.dto.spmk.FromData;
34 35 36 37 38 39 40 41 42 43 44
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 {
45 46 47
	/**
	 * 0 未执行
	 */
48
	private final static String UNEXECUTED = "0";
49 50 51
	/**
	 * 1 执行中
	 */
52
	private final static String EXECUTING = "1";
53 54 55
	/**
	 * 2 已执行
	 */
56
	private final static String EXECUTED = "2";
57 58 59
	/**
	 * creator 抄送人
	 */
60
	private final static String CREATOR = "creator";
61 62 63
	/**
	 * audit 审批人
	 */
64
	private final static String AUDIT = "audit";
65 66 67
	/**
	 * copy 抄送人
	 */
68
	private final static String COPY = "copy";
69 70 71
	/**
	 * department 部门类型
	 */
72
	private final static String RELATION_TYPE_DEPARTMENT = "department";
73
	/**
tangzhaoqian committed
74 75 76 77
	 * department 部门类型
	 */
	private final static String RELATION_TYPE_EXECUTIVE = "executive";
	/**
78 79
	 * users 用户类型
	 */
80 81
	private final static String RELATION_TYPE_USERS = "users";

82
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse) throws NumberFormatException, ConvertException, Exception {
83 84
		return NextNode(listRouter, obj, isFirse, false);
	}
85 86

	// 执行下一个节点
87
	public static List<Router> NextNode(List<Router> listRouter,JSONObject obj, boolean isFirse, boolean isAuditNext) throws NumberFormatException, ConvertException, Exception {
88 89
		
		Router router;
90
		if (CollectionUtil.isNotEmpty(listRouter)) {
91 92 93
			// 非条件节点
			if (listRouter.size() == 1) {
				router = listRouter.get(0);
94
				if (router.getExecute() == null) {
ilal committed
95
//					System.out.println("默认UNEXECUTED");
96 97
					router.setExecute(UNEXECUTED);
				}
98 99 100 101 102 103 104 105
				// 0未执行 1执行中 2已执行
				switch (router.getExecute()) {
				case UNEXECUTED:
					switch (router.getClassName()) {
					case CREATOR:
						Console.log("发起人逻辑");
						router.setExecute(EXECUTED);
						router.setFlow(true);
tangzhaoqian committed
106 107 108
						
						List<User> users = new ArrayList<User>();
						User userFirst =  User.builder()
tangzhaoqian committed
109 110 111
								.name(obj.getStr("initiator"))
								.id(obj.getStr("id"))
								.headUrl(obj.getStr("headUrl"))
112
								.execute(UNEXECUTED)
tangzhaoqian committed
113 114 115 116 117 118 119
								.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);
tangzhaoqian committed
120
//						router.getRelation().get(0).setName(obj.getStr("initiator"));
121 122
						
						NextNode(router.getChildren(),obj, isFirse, isAuditNext);
123 124 125
						break;
					case AUDIT:
						Console.log("审批人逻辑");
126
						
127
						router.setFlow(true);
tangzhaoqian committed
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
						Relation r = CollectionUtil.isNotEmpty(router.getRelation()) ? router.getRelation().get(0) : null;
//						if (r != null && RELATION_TYPE_EXECUTIVE.equals(r.getType())) {
//							Integer leaderId = selectLeaderEmpNumById(
//										obj.getInt("orgCode"), 
//										obj.getInt("id"), 
//										r.getUpward());
//							
//							if (leaderId != null) {
//								YgglMainEmp emp = YgglMainEmp.builder().build();
//								emp = emp.selectOne(new QueryWrapper<YgglMainEmp>().lambda()
//										.select(YgglMainEmp::getName, YgglMainEmp::getEmpNum, YgglMainEmp::getHeadUrl)
//										.eq(YgglMainEmp::getEmpNum, leaderId)
//										.eq(YgglMainEmp::getOrgCode, obj.getInt("orgCode")));
//								
//								if (emp != null) {
//									User user = User.builder()
//											.name(emp.getName())
//											.id(Convert.toStr(emp.getEmpNum()))
//											.headUrl(emp.getHeadUrl())
//											.execute(EXECUTING)
//											.build();
//									
//									router.getRelation().get(0).setUsers(ListUtil.toList(user));
//								}
//							}
//						}
154 155 156 157 158
						
						if (!isAuditNext) {
							
							router.setExecute(EXECUTING);
							
lal committed
159 160 161
							if(router.getRelation().size() > 0) {
	//							if (r != null && RELATION_TYPE_USERS.equals(r.getType())) {
										List<User> listUser = router.getRelation().get(0).getUsers();
ilal committed
162 163 164 165
										if(listUser != null) {
											user:
												for (int i = 0; i < listUser.size(); i++) {
													String execute = listUser.get(i).getExecute();
ilal committed
166 167 168
													if(execute == null) {
														execute = "0";
													}
ilal committed
169 170 171 172 173 174 175 176 177 178 179
													switch (execute) {
													case UNEXECUTED:
														listUser.get(i).setExecute(EXECUTING);
														// 首次发起申请时,写入 审批人名称 至 obj 中
														if (isFirse && obj.getStr("current_approver") == null ) {
															obj.set("current_approver", listUser.get(i).getName());
														}
														isAuditNext = true;
														break user;
													}
													
lal committed
180
												}
ilal committed
181 182
										}else {
											throw new Exception("发起审批有误");
183
										}
ilal committed
184
										
lal committed
185 186
	//							}
							}else {
ilal committed
187
								throw new Exception("审批流程配置有误,请联系管理员");
lal committed
188
							}
tangzhaoqian committed
189 190
						}
						
191
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
192 193
						break;
					case COPY:
194 195 196
						if (!isAuditNext) {
							router.setExecute(EXECUTED);
						}
197 198 199 200 201
						Console.log("抄送人逻辑");
						router.setFlow(true);
						List<Relation> listRelations = router.getRelation();
						for (int i = 0; i < listRelations.size(); i++) {
							// 装配 部门人员
202 203
							if (RELATION_TYPE_DEPARTMENT.equals(listRelations.get(i).getType()) && listRelations.get(i).getDepartmentId() != null) {
								
lal committed
204 205 206 207 208
								String asd = listRelations.get(i).getDepartmentId();
								asd = asd.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\\"", "");
								
//								List<YgglMainEmp> listYgglMainEmp = selectOtherlistent(Integer.parseInt(obj.get("orgCode",FromData.class).getValue().trim()), Integer.valueOf(listRelations.get(i).getDepartmentId().trim()));
								List<YgglMainEmp> listYgglMainEmp = selectOtherlistent(Integer.parseInt(obj.get("orgCode").toString()), Integer.valueOf(asd));
tangzhaoqian committed
209 210 211 212 213 214 215 216 217 218 219 220
								
								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);
221
								}
tangzhaoqian committed
222
								
223
								
224
							}else if (RELATION_TYPE_USERS.equals(listRelations.get(i).getType())) {
225

226 227
							}
						}
228
						NextNode(router.getChildren(), obj , isFirse, isAuditNext);
229 230 231 232 233 234 235
						break;
					}
					break;
				case EXECUTING:
					switch (router.getClassName()) {
					case AUDIT:
						router.setFlow(true);
236 237 238 239 240 241 242 243 244 245
						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);
246
										executeFlog = false;
247 248 249 250
										break user;
									case EXECUTING:
										listUser.get(i).setExecute(EXECUTED);
										break;
251 252
									}
								}
253 254 255
							
							if (executeFlog) {
								router.setExecute(EXECUTED);
256
							}
257
							
258
						}
259
						NextNode(router.getChildren(),obj , isFirse, isAuditNext);
260 261 262 263 264
						break;
					}
					break;
				case EXECUTED:
					Console.log("下一个节点");
265
					NextNode(router.getChildren(),obj , isFirse, isAuditNext);
266 267 268 269 270 271 272 273 274 275 276 277 278 279
					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 {
280
							Compare compare;
281
							for (Condition condition : listCondition) {
yuquan.zhu committed
282
									boolean condition_value = false;
283
									for (String string : condition.getValues()) {
ilal committed
284 285 286
										if(obj.get(condition.getKey(),FromData.class).getValue() != null) {
											// 简单工厂模式 - 判断条件
											compare = CompareFactory.createCompare(obj.get(condition.getKey(),FromData.class).getValue(), string, condition.getFormat());
yuquan.zhu committed
287 288
											if (compare.GetResutl()) {
												condition_value = true;
ilal committed
289 290 291
											}
										}else {
											throw new Exception("审批流程设置有误");
292
										}
yuquan.zhu committed
293 294 295 296 297
									}
									if(condition_value) {
										condition_b = true;
									}else {
										condition_b = false;
298
									}
299 300 301 302 303
							}
						}
						
						routerRule.setFlow(condition_b);
						routerRule.setExecute(EXECUTED);
304
						if (routerRule.getFlow()) {
305
							Console.log("条件逻辑");
306
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
307 308 309
						}
						break;
					case EXECUTED:
310
						if (routerRule.getFlow()) {
311
							NextNode(routerRule.getChildren(), obj , isFirse, isAuditNext);
312
						}
313 314 315 316
						break rulefor;
					}
					
				}
317 318
			
				Console.log("最后节点");
319 320
			}
		}
321

322 323 324 325
		return listRouter;

	};
	
tangzhaoqian committed
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
	public static Integer selectLeaderEmpNumById(Integer orgCode, Integer id, Integer leave) {
		YgglMainEmp mainEmp = YgglMainEmp.builder().build().selectOne(new QueryWrapper<YgglMainEmp>().lambda()
				.select(YgglMainEmp::getBmgwId)
				.eq(YgglMainEmp::getEmpNum, id)
				.eq(YgglMainEmp::getOrgCode, orgCode));
		Integer gWId = mainEmp != null ? mainEmp.getBmgwId() : null;
		
		// 企业所有部门岗位
		List<ZzglBmgwM> listBM = ZzglBmgwM.builder().build()
				.selectList(new QueryWrapper<ZzglBmgwM>().lambda().eq(ZzglBmgwM::getOrgCode, orgCode));
		
		// 岗位
		ZzglBmgwM gW = CollUtil.getFirst(listBM.stream().filter(bM -> bM.getId().equals(gWId)).collect(Collectors.toList()));
		
		// 岗位id
		Integer baseBmgwId = gW != null ? gW.getUpId() : null;
		
		// 指定 主管id
		Integer leaderId = null;
		Integer bmgwLeaderId;
		for (int i = 0,n = leave + 1; i < n; i++) {
			ZzglBmgwM bmgwM = getLeaderEmp(listBM, baseBmgwId);
			bmgwLeaderId = bmgwM != null ? bmgwM.getLeader() : null;
			if (i == n-1) {
				leaderId = bmgwLeaderId;
			}
		}
		
		return leaderId;
	}
356
	
tangzhaoqian committed
357 358 359 360 361
	public static ZzglBmgwM getLeaderEmp(List<ZzglBmgwM> listBM, Integer baseBmgwId) {
		ZzglBmgwM zzglBmgwM =  baseBmgwId != null ? 
				CollUtil.getFirst(listBM.stream().filter(bM -> bM.getId().equals(baseBmgwId)).collect(Collectors.toList())) : null;
		return zzglBmgwM;
	}
362 363 364
	
	public static List<YgglMainEmp> selectOtherlistent(Integer orgCode, Integer id){
		ArrayList<Integer> list = new ArrayList<Integer>();
365 366
		List<ZzglBmgwM> zzglBmgwMs = ZzglBmgwM.builder().build().selectList(new QueryWrapper<ZzglBmgwM>().lambda()
				.eq(ZzglBmgwM::getOrgCode, orgCode));
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
		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;
		}

	}
	
384 385 386 387 388
	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);
tangzhaoqian committed
389
			
ilal committed
390 391
			if(router.getRelation() != null) {
				for (Relation relation : router.getRelation()) {
yuquan.zhu committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
					if (relation != null) {
						if(CollectionUtil.isNotEmpty(relation.getUsers())){
							if (router.getFlow()) {
								FlowChildren fc = FlowChildren.builder().build();
								BeanUtil.copyProperties(router, fc, "condition","children");
								listFlowChildren.add(fc);
								getIsFlowChildren(router.getChildren(), listFlowChildren);
							}
						}else{
							//无审批人,查询是否跳过或者失败! 0不处理 1指定成员 2自动通过
							if("executive".equals(relation.getType()) && relation.getEmpty() == 2){
								getIsFlowChildren(router.getChildren(), listFlowChildren);
							}else if("executive".equals(relation.getType()) && relation.getEmpty() == 0){
								FlowChildren fc = FlowChildren.builder().build();
								BeanUtil.copyProperties(router, fc, "condition","children");
								listFlowChildren.add(fc);
								getIsFlowChildren(router.getChildren(), listFlowChildren);
							}else {
								//未开发
							}
ilal committed
412 413 414
						}
					}	
				}
415
			}
yuquan.zhu committed
416
//			Relation relation = CollectionUtil.getFirst(router.getRelation());
417
		}else if (listRouter != null && listRouter.size() > 1) {
418 419 420 421 422 423
			for (Router router2 : listRouter) {
				if (router2.getFlow()) {
					getIsFlowChildren(router2.getChildren(), listFlowChildren);
				}
			}
		}
424 425 426 427 428 429 430

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

433
		for_insert:
434
		for (int i = 0,m = listFlowChildren.size() ; i < m; i++) {
tangzhaoqian committed
435 436
			if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
				i++;
437
				continue;
tangzhaoqian committed
438 439
			}
				
440 441
			// ClassName 区分参与审批流程人的角色 CREATOR(发起人)、AUDIT(审核人)、COPY(抄送人)
			// 各个角色的逻辑不同
442 443 444 445 446
			switch (listFlowChildren.get(i).getClassName()) {
			case CREATOR:
				SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
447
						.name(ParticipatorType.INITIATOR.getName())
448 449
						.type(ParticipatorType.INITIATOR.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
450 451 452 453 454 455 456 457 458
						.build();
				// 新增 审批执行记录
				aer.insert();
				
				// 新增 执行人
				User user = listFlowChildren.get(i).getRelation().get(0).getUsers().get(0);
				SpmkExecutor.builder()
				.approveExecuteRecordId(aer.getId())
				.empNum(Integer.parseInt(user.getId()))
459
				.operatorHeaderUrl(user.getHeadUrl())
460
				.executorName(user.getName())
tangzhaoqian committed
461
				.sts(ExecutorSts.AGREE.ordinal())
462 463 464 465
				.build()
				.insert();
				break;
			case AUDIT:
466

467 468 469

				// 新增 执行人
				List<User> listUser = listFlowChildren.get(i).getRelation().get(0).getUsers();
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

				Relation relation1 =listFlowChildren.get(i).getRelation().get(0);
				if("1".equals(relation1.getApprovalOrder())){
					for (User user2 : listUser) {
						SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
								.builder()
								.approveSummaryId(asId)
								.name(ParticipatorType.APPROVER.getName())
								.type(ParticipatorType.APPROVER.ordinal())
								.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
								.build();
						// 新增 审批执行记录
						aer2.insert();
						SpmkExecutor executor = SpmkExecutor.builder()
								.approveExecuteRecordId(aer2.getId())
								.empNum(Integer.parseInt(user2.getId()))
								.operatorHeaderUrl(user2.getHeadUrl())
								.executorName(user2.getName())
								.build();
						user2.setExecute("1");
						switch (user2.getExecute()) {
							case EXECUTING:
								executor.setSts(ExecutorSts.IN_EXECUTION.ordinal());
								executor.insert();
								break;
							case EXECUTED:
								executor.setSts(ExecutorSts.AGREE.ordinal());
								executor.insert();
								break;
						}
					}
				}else{
					SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
							.builder()
							.approveSummaryId(asId)
							.name(ParticipatorType.APPROVER.getName())
							.type(ParticipatorType.APPROVER.ordinal())
							.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
							.build();
					// 新增 审批执行记录
					aer2.insert();
					for (User user2 : listUser) {
						SpmkExecutor executor = SpmkExecutor.builder()
								.approveExecuteRecordId(aer2.getId())
								.empNum(Integer.parseInt(user2.getId()))
								.operatorHeaderUrl(user2.getHeadUrl())
								.executorName(user2.getName())
								.build();
						switch (user2.getExecute()) {
							case EXECUTING:
								executor.setSts(ExecutorSts.IN_EXECUTION.ordinal());
								executor.insert();
								break for_insert;
							case EXECUTED:
								executor.setSts(ExecutorSts.AGREE.ordinal());
								executor.insert();
								break;
						}
528 529
					}
				}
530

531 532 533 534 535
				break;
			case COPY:
				SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
				.builder()
				.approveSummaryId(asId)
536
				.name(ParticipatorType.COPY.getName())
537 538
				.type(ParticipatorType.COPY.ordinal())
				.sts(ExecuteRecordSts.AGREE.ordinal())
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
				.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())
554
						.operatorHeaderUrl(user2.getHeadUrl())
tangzhaoqian committed
555
						.sts(ExecutorSts.AGREE.ordinal())
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
						.build();
						executor.insert();
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 审批持久化
	 * @param listFlowChildren	节点
	 * @param asId				审批汇总Id
	 * @param executorId		执行人记录Id
	 * @param opinion			意见
	 * @param sts				状态 1执行中 2通过 3拒绝
	 */
573
	public static void approving(List<FlowChildren> listFlowChildren,Integer asId,Integer executeRecordId, Integer executorId, String opinion,Integer sts,User redeployUser,String signatureImg,Integer empNum ) throws Exception {
574
		
575
		boolean hasNextApprover = false;
576 577

		int es=0;
578
		
579 580 581
		for (int i = 0,n = listFlowChildren.size(); i < n; i++) {

			// 新增 执行人
tangzhaoqian committed
582
			List<User> listUser = CollUtil.toList();
龙于生 committed
583
			boolean aobl = false;
tangzhaoqian committed
584 585 586
			List<Relation> listR = listFlowChildren.get(i).getRelation();
			if (CollectionUtil.isNotEmpty(listR)) {
				listUser = listR.get(0).getUsers();
587
				if("1".equals(listR.get(0).getApprovalOrder())){
龙于生 committed
588
					aobl = true;
589 590 591 592 593 594 595 596
					for (User user:listR.get(0).getUsers()
						 ) {
						if ("1".equals(user.getExecute())){
							es++;
						}
					}

				}
tangzhaoqian committed
597
			}
598

tangzhaoqian committed
599 600
			
			if (EXECUTING.equals(listFlowChildren.get(i).getExecute())) {
601 602 603
				
				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
604
						
605
					}else if (EXECUTING.equals(listUser.get(i_user).getExecute())) {
606 607 608 609 610 611 612 613 614 615 616 617 618 619

						if (Integer.parseInt(listUser.get(i_user).getId()) == empNum){
							SpmkExecutor.builder()
									.id(executorId)
									.opinion(opinion)
									.empNum(Integer.parseInt(listUser.get(i_user).getId()))
									.executorName(listUser.get(i_user).getName())
									.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
									.sts(sts)
									.signatureImg(signatureImg)
									.build()
									.updateById();
							listUser.get(i_user).setExecute(EXECUTED);

龙于生 committed
620 621 622 623 624

							// 历史审批人
							SpmkApproveSummary.builder()
									.id(asId)
									.historyApprover(listUser.get(i_user).getName())
625 626
									.build()
									.updateById();
龙于生 committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

							//拒绝处理
							// 0未执行 1执行中 2同意 3拒绝 4 转派
							if (sts == ExecutorSts.REFUSE.ordinal()) {
								// 更新 审批汇总 状态
								SpmkApproveSummary.builder().id(asId).currentApprover(CommonEnum.NULL_STR.getDesc()).endTime(new Date()).sts(sts).build().updateById();
								SpmkApproveExecuteRecord
										.builder()
										.id(executeRecordId)
										.sts(sts)
										.build()
										// 更新 审批执行记录
										.updateById();
								listFlowChildren.get(i_user).setExecute(EXECUTED);
								return;
								//转派 处理
								//在 原审批人 列表中 插入 一个被转派人(审批人)
							}else if (sts == ExecutorSts.REDEPLOY.ordinal()) {
								List<User> users1 = CollectionUtil.sub(listUser, 0, i_user+1);
								redeployUser.setExecute(UNEXECUTED);
								users1.add(redeployUser);
								List<User> users2 = CollectionUtil.sub(listUser, i_user+1, listUser.size());
ilal committed
649 650
//							System.out.println(users1);
//							System.out.println(users2);
龙于生 committed
651 652
								listUser = ListUtils.union(users1, users2);
								n_user = listUser.size();
ilal committed
653
//							System.out.println(listUser);
龙于生 committed
654
							}
655
						}
龙于生 committed
656 657 658 659

						


660 661 662 663 664
					}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())
665
							.operatorHeaderUrl(listUser.get(i_user).getHeadUrl())
tangzhaoqian committed
666
							.sts(ExecutorSts.IN_EXECUTION.ordinal())
667 668 669 670
							.build()
							.insert();
						hasNextApprover = true;
						listUser.get(i_user).setExecute(EXECUTING);
龙于生 committed
671 672


tangzhaoqian committed
673 674
						// 当前审批人
						SpmkApproveSummary.builder().id(asId).currentApprover(listUser.get(i_user).getName()).build().updateById();
675 676 677
						
						// 处理了 下一个审批人 则跳出循环
						break;
678 679 680
					}
				}
				
681
				// 无下一个审批人 则更新 节点状态 为 EXECUTED(已执行)
682 683 684 685
				if (!hasNextApprover) {
					SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
							.builder()
							.id(executeRecordId)
686
							.sts(ExecutorSts.AGREE.ordinal())
687 688 689
							.build();
							// 更新 审批执行记录
							aer.updateById();
690 691 692 693 694

					if(es<=1){
						listFlowChildren.get(i).setExecute(EXECUTED);
					}

695 696 697
				}
				
			}else if (UNEXECUTED.equals(listFlowChildren.get(i).getExecute())) {
698 699

				if (!hasNextApprover && es<=1) {
700 701 702 703 704
					switch (listFlowChildren.get(i).getClassName()) {
					case CREATOR:
						SpmkApproveExecuteRecord aer = SpmkApproveExecuteRecord
								.builder()
								.approveSummaryId(asId)
705 706
								.name(ParticipatorType.INITIATOR.getName())
								.type(ParticipatorType.INITIATOR.ordinal())
707
								.sts(ExecuteRecordSts.AGREE.ordinal())
708 709 710 711 712 713 714 715 716 717
								.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())
718
						.operatorHeaderUrl(user.getHeadUrl())
tangzhaoqian committed
719
						.sts(ExecutorSts.AGREE.ordinal())
720 721 722 723 724 725
						.build()
						.insert();
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					case AUDIT:
龙于生 committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
						if(aobl){
							String currentApprover = "";
							for (int i_user2 = 0,n_user2 = listUser.size(); i_user2 < n_user2; i_user2++) {

								SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
										.builder()
										.approveSummaryId(asId)
										.name(ParticipatorType.APPROVER.getName())
										.type(ParticipatorType.APPROVER.ordinal())
										.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
										.build();
								// 新增 审批执行记录
								aer2.insert();

								SpmkExecutor executor = SpmkExecutor.builder()
										.approveExecuteRecordId(aer2.getId())
										.empNum(Integer.parseInt(listUser.get(i_user2).getId()))
										.executorName(listUser.get(i_user2).getName())
										.operatorHeaderUrl(listUser.get(i_user2).getHeadUrl())
										.sts(ExecutorSts.IN_EXECUTION.ordinal())
										.build();
								executor.insert();
748

龙于生 committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
								listUser.get(i_user2).setExecute(EXECUTING);
								hasNextApprover = true;

								currentApprover=currentApprover!=""?currentApprover+","+listUser.get(i_user2).getName():listUser.get(i_user2).getName();
								// 当前审批人
								SpmkApproveSummary.builder().id(asId).currentApprover(currentApprover).build().updateById();

							}

							listFlowChildren.get(i).setExecute(EXECUTING);
						}else{
							SpmkApproveExecuteRecord aer2 = SpmkApproveExecuteRecord
									.builder()
									.approveSummaryId(asId)
									.name(ParticipatorType.APPROVER.getName())
									.type(ParticipatorType.APPROVER.ordinal())
									.sts(ExecuteRecordSts.IN_EXECUTION.ordinal())
766
									.build();
龙于生 committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
							// 新增 审批执行记录
							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())
										.operatorHeaderUrl(listUser.get(i_user2).getHeadUrl())
										.sts(ExecutorSts.IN_EXECUTION.ordinal())
										.build();
								executor.insert();

								listUser.get(i_user2).setExecute(EXECUTING);
								hasNextApprover = true;
								// 当前审批人
								SpmkApproveSummary.builder().id(asId).currentApprover(listUser.get(i_user2).getName()).build().updateById();
								// 处理了 下一个审批人 则跳出循环
								break;
							}

							listFlowChildren.get(i).setExecute(EXECUTING);
789
						}
龙于生 committed
790

791 792 793 794 795
						break;
					case COPY:
						SpmkApproveExecuteRecord aer3 = SpmkApproveExecuteRecord
						.builder()
						.approveSummaryId(asId)
796
						.name(ParticipatorType.COPY.getName())
797 798
						.type(ParticipatorType.COPY.ordinal())
						.sts(ExecuteRecordSts.AGREE.ordinal())
799 800 801 802 803 804 805 806
						.build();
						// 新增 审批执行记录
						aer3.insert();

						List<Relation> listRelation = listFlowChildren.get(i).getRelation();
						
						for (Relation relation : listRelation) {
							List<User> listUser2 = relation.getUsers();
807 808 809 810 811 812 813 814 815 816 817 818
							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();
								}
819 820 821 822 823 824 825 826 827
							}
						}
						
						listFlowChildren.get(i).setExecute(EXECUTED);
						break;
					}
				}
			}
		}
828
		
829
	}
830

yuquan.zhu committed
831 832 833 834 835
	public static List<Router> clearRouters(List<Router> routers){

		return routers;
	}

836
}