ActTaskService.java 52.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 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 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 295 296 297 298 299 300 301 302 303 304 305 306 307 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 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 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/**
 * Copyright © 2015-2018 ODM All rights reserved.
 */
package com.thinkgem.jeesite.modules.act.service;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricTaskInstanceQuery;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.bpmn.diagram.ProcessDiagramGenerator;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.tracelog.ActivitiLog;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.act.dao.ActDao;
import com.thinkgem.jeesite.modules.act.entity.Act;
import com.thinkgem.jeesite.modules.act.utils.ActUtils;
import com.thinkgem.jeesite.modules.act.utils.ProcessDefCache;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.entity.setdjxl.SysDjxlOffice;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.setdjxl.SysDjxlOfficeService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 流程定义相关Service
 * @author ThinkGem
 * @version 2013-11-03
 */
@Service
@Transactional(readOnly = true)
public class ActTaskService extends BaseService {

	@Autowired
	private ActDao actDao;
	
	@Autowired
	private ProcessEngineFactoryBean processEngine;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private FormService formService;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private IdentityService identityService;
	
	@Autowired
	private OfficeService officeService;
	@Autowired
	private SysDjxlOfficeService sysDjxlOfficeService;
	
	
	
	/**
	 * 获取待办列表
	 * @param procDefKey 流程定义标识
	 * @return
	 */
	public Page<Act> getTaskTodoList(Page<Act> page, Act act){
		act.setPage(page);
		
		User user = UserUtils.getUser();
		String userName = user.getLoginName();//ObjectUtils.toString(UserUtils.getUser().getId());
		
		List<Act> result = new ArrayList<Act>();
		
		// =============== 已经签收的任务  ===============
		//按照创建时间排序
		/*
		TaskQuery todoTaskQuery = taskService.createTaskQuery().taskAssignee(userName).active()
				.includeProcessVariables().orderByTaskCreateTime().desc();
		*/
		//初审组(权证、抵押和预告科)根据相应的机构代码查询属于自己的流程业务
		Office office = officeService.get(user.getCurrentUser().getOffice().getId());
		String[] qzdjxlValueArrStr = {"1001", "1002", "1007", "201", "202", "203", "204", "206", "207", "208", "210", "301", "220",
				"302", "303", "305", "306", "307", "308", "309", "313", "401", "405", "406", "407", "408", "409", "413", "418", 
				"419", "420", "421", "429", "501", "502", "505", "506", "508", "601", "602", "604", "1003", "1004", "402", "403", "404", "416"};
		String[] dydjxlValueArrStr = {"209", "213", "214", "215", "315", "316", "317", "318", "422", "424", "426", "427", 
				"428", "430", "431", "433", "507", "509", "515", "605", "606", "608", "1003", "1004", "402", "403", "404", "416"};
		String[] ygdjxlValueArrStr = {"423", "425", "432", "510", "511", "512", "513", "607", "609", "803", "804", "806", 
				"807", "809", "810", "811", "812", "818", "819", "1003", "1004", "402", "403", "404", "416"};
		String[] lqdjxlValueArrStr = {"1005", "1006", "205", "211", "212", "304", "312", "412", "516", "517", "1003", "1004", "402", "403", "404", "416"};
		String[] zhdjxlValueArrStr = {"701", "702", "703", "704", "901", "902", "903", "904", "905", "906", "907", "908", "1003", "1004", "402", "403", "404", "416"};
		
		StringBuilder sqlTNQSBStr = new StringBuilder();
		sqlTNQSBStr.append("SELECT * FROM( ");
		sqlTNQSBStr.append("SELECT RES.*, VAR.ID_ as VAR_ID_, VAR.NAME_ as VAR_NAME_, VAR.TYPE_ as VAR_TYPE_, " +
					"VAR.REV_ as VAR_REV_, VAR.PROC_INST_ID_ as VAR_PROC_INST_ID_, VAR.EXECUTION_ID_ as VAR_EXECUTION_ID_, " +
					"VAR.TASK_ID_ as VAR_TASK_ID_, VAR.BYTEARRAY_ID_ as VAR_BYTEARRAY_ID_, VAR.DOUBLE_ as VAR_DOUBLE_, " + 
					"VAR.TEXT_ as VAR_TEXT_, VAR.TEXT2_ as VAR_TEXT2_, VAR.LONG_ as VAR_LONG " +
				"FROM ACT_RU_TASK RES left outer join ACT_RU_VARIABLE VAR " +
				"ON RES.PROC_INST_ID_ = VAR.EXECUTION_ID_ and VAR.TASK_ID_ IS NULL ");
		// 设置查询条件
		if (StringUtils.isNotBlank(act.getProcDefKey())){
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A0 on RES.PROC_INST_ID_ = A0.PROC_INST_ID_ ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){ 
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A1 on RES.PROC_INST_ID_ = A1.PROC_INST_ID_ ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){ 
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A2 on RES.PROC_INST_ID_ = A2.PROC_INST_ID_ ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A4 on RES.PROC_INST_ID_ = A4.PROC_INST_ID_ ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode()) || "S00001001002001002".equals(office.getCode()) || "S00001001002001003".equals(office.getCode()) || "S00001001002001004".equals(office.getCode()) || "S00001001002001005".equals(office.getCode())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		sqlTNQSBStr.append("WHERE 1=1 ")
				   .append("and RES.ASSIGNEE_ is not null ");
		if (act.getBeginDate() != null){
			sqlTNQSBStr.append("AND RES.CREATE_TIME_ > '"+act.getBeginDate()+"' ");
		} 
		if (act.getEndDate() != null){
			sqlTNQSBStr.append("AND RES.CREATE_TIME_ < '"+act.getEndDate()+"' ");
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQSBStr.append("and A0.TASK_ID_ is null and A0.NAME_= 'ywh' and A0.TYPE_ = 'string' and A0.TEXT_ LIKE '%"+act.getYwh()+"%' ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){
			sqlTNQSBStr.append("and A1.TASK_ID_ is null and A1.NAME_= 'sqr' and A1.TYPE_ = 'string' and A1.TEXT_ LIKE '%"+act.getSqr()+"%' ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){
			sqlTNQSBStr.append("and A2.TASK_ID_ is null and A2.NAME_= 'djlx' and A2.TYPE_ = 'string' and A2.TEXT_ = '"+act.getDjlx()+"' ");
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			String  title = DictUtils.getDictLabel(act.getTitle(),"reg_bus_djxl","");
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ = '"+title+"' ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQSBStr.append("and A4.TASK_ID_ is null and A4.NAME_= 'proid' and A4.TYPE_ = 'string' and A4.TEXT_ LIKE '%"+act.getProid()+"%' ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(qzdjxlValueArrStr)+") ");
		}else if("S00001001002001002".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(dydjxlValueArrStr)+") ");
		}else if("S00001001002001003".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(ygdjxlValueArrStr)+") ");
		}else if("S00001001002001004".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(lqdjxlValueArrStr)+") ");
		}else if("S00001001002001005".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(zhdjxlValueArrStr)+") ");
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		sqlTNQSBStr.append("and RES.SUSPENSION_STATE_ = 1 ");
		if("mysql".equals(Global.getDbName())){
			sqlTNQSBStr.append(" GROUP BY RES.ID_ order by RES.CREATE_TIME_ desc ) t1");
		}else if("oracle".equals(Global.getDbName())){
			sqlTNQSBStr.append(" AND VAR.ID_ = (select max(var1.ID_) from ACT_RU_VARIABLE var1 where var1.execution_id_ = RES.Proc_Inst_Id_) order by RES.CREATE_TIME_ desc ) ");
		}
		
		/*
		// 查询列表
		List<Task> todoList = taskService.createNativeTaskQuery().sql(sqlTNQSBStr.toString()).list();
		for (Task task : todoList) {
			Act e = new Act();
			e.setTask(task);
			//e.setVars(task.getProcessVariables()); NativeQuery - 0718
			e.setVars(taskService.getVariables(task.getId()));
//			e.setTaskVars(task.getTaskLocalVariables());
//			System.out.println(task.getId()+"  =  "+task.getProcessVariables() + "  ========== " + task.getTaskLocalVariables());
			e.setProcDef(ProcessDefCache.get(task.getProcessDefinitionId()));
//			e.setProcIns(runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult());
//			e.setProcExecUrl(ActUtils.getProcExeUrl(task.getProcessDefinitionId()));
			e.setStatus("todo");
			result.add(e);
		}*/
		
		
		String sqlStr = sqlTNQSBStr.toString();
		StringBuilder sb = new StringBuilder();
		int listCount = taskService.createNativeTaskQuery().sql(sqlStr).list().size();
		
		sb.append("当前业务流程环节打开代办任务列表,SQL查询语句:"+sqlStr).
	    append(";当前登录用户信息:登录名,"+ UserUtils.getUser().getLoginName()).
		append(";当前登录用户名,"+ UserUtils.getUser().getName()).
		append(";当前登录用户IP,"+ UserUtils.getUser().getLoginIp()).
		append(";当前登录标识,"+ UserUtils.getUser().getLoginFlag()).
		append(";当前代办任务总共条数,"+ listCount);
		logger.info("【FIND_ACT_ERR·ActtaskService·toList】"+sb.toString());
		
		page.setCount(listCount);
		
		// 查询列表
		List<Task> todoAndClaimList = taskService.createNativeTaskQuery().sql(sqlStr).listPage(page.getFirstResult(), page.getMaxResults());
		
		// 查询列表
		for (Task task : todoAndClaimList) {
			Act e = new Act();
			e.setTask(task);
			e.setVars(taskService.getVariables(task.getId()));
			e.setProcDef(ProcessDefCache.get(task.getProcessDefinitionId()));
			e.setStatus("claim");
			result.add(e);
		}
		page.setList(result);
		return page;
	}
	
	/**
	 * 获取待办列表
	 * @param procDefKey 流程定义标识
	 * @return
	 */
	public Page<Act> todoList(Page<Act> page, Act act){
		act.setPage(page);
		
		User user = UserUtils.getUser();
		String userName = user.getLoginName();//ObjectUtils.toString(UserUtils.getUser().getId());
		
		List<Act> result = new ArrayList<Act>();
		
		// =============== 已经签收的任务  ===============
		//按照创建时间排序
		/*
		TaskQuery todoTaskQuery = taskService.createTaskQuery().taskAssignee(userName).active()
				.includeProcessVariables().orderByTaskCreateTime().desc();
		*/
		//初审组(权证、抵押和预告科)根据相应的机构代码查询属于自己的流程业务
		Office office = officeService.get(user.getCurrentUser().getOffice().getId());
		String[] qzdjxlValueArrStr = {"1001", "1002", "1007", "201", "202", "203", "204", "206", "207", "208", "210", "301", "220",
				"302", "303", "305", "306", "307", "308", "309", "313", "401", "405", "406", "407", "408", "409", "413", "418", 
				"419", "420", "421", "429", "501", "502", "505", "506", "508", "601", "602", "604", "1003", "1004", "402", "403", "404", "416"};
		String[] dydjxlValueArrStr = {"209", "213", "214", "215", "315", "316", "317", "318", "422", "424", "426", "427", 
				"428", "430", "431", "433", "507", "509", "515", "605", "606", "608", "1003", "1004", "402", "403", "404", "416"};
		String[] ygdjxlValueArrStr = {"423", "425", "432", "510", "511", "512", "513", "607", "609", "803", "804", "806", 
				"807", "809", "810", "811", "812", "818", "819", "1003", "1004", "402", "403", "404", "416"};
		String[] lqdjxlValueArrStr = {"1005", "1006", "205", "211", "212", "304", "312", "412", "516", "517", "1003", "1004", "402", "403", "404", "416"};
		String[] zhdjxlValueArrStr = {"701", "702", "703", "704", "901", "902", "903", "904", "905", "906", "907", "908", "1003", "1004", "402", "403", "404", "416"};
		
		StringBuilder sqlTNQSBStr = new StringBuilder();
		sqlTNQSBStr.append("SELECT * FROM( ");
		sqlTNQSBStr.append("SELECT RES.*, VAR.ID_ as VAR_ID_, VAR.NAME_ as VAR_NAME_, VAR.TYPE_ as VAR_TYPE_, " +
					"VAR.REV_ as VAR_REV_, VAR.PROC_INST_ID_ as VAR_PROC_INST_ID_, VAR.EXECUTION_ID_ as VAR_EXECUTION_ID_, " +
					"VAR.TASK_ID_ as VAR_TASK_ID_, VAR.BYTEARRAY_ID_ as VAR_BYTEARRAY_ID_, VAR.DOUBLE_ as VAR_DOUBLE_, " + 
					"VAR.TEXT_ as VAR_TEXT_, VAR.TEXT2_ as VAR_TEXT2_, VAR.LONG_ as VAR_LONG " +
				"FROM ACT_RU_TASK RES left outer join ACT_RU_VARIABLE VAR " +
				"ON RES.PROC_INST_ID_ = VAR.EXECUTION_ID_ and VAR.TASK_ID_ IS NULL ");
		// 设置查询条件
		if (StringUtils.isNotBlank(act.getProcDefKey())){
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A0 on RES.PROC_INST_ID_ = A0.PROC_INST_ID_ ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){ 
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A1 on RES.PROC_INST_ID_ = A1.PROC_INST_ID_ ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){ 
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A2 on RES.PROC_INST_ID_ = A2.PROC_INST_ID_ ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A4 on RES.PROC_INST_ID_ = A4.PROC_INST_ID_ ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode()) || "S00001001002001002".equals(office.getCode()) || "S00001001002001003".equals(office.getCode()) || "S00001001002001004".equals(office.getCode()) || "S00001001002001005".equals(office.getCode())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			sqlTNQSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		sqlTNQSBStr.append("WHERE RES.ASSIGNEE_ = '"+userName+"' ");
		if (act.getBeginDate() != null){
			sqlTNQSBStr.append("AND RES.CREATE_TIME_ > '"+act.getBeginDate()+"' ");
		} 
		if (act.getEndDate() != null){
			sqlTNQSBStr.append("AND RES.CREATE_TIME_ < '"+act.getEndDate()+"' ");
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQSBStr.append("and A0.TASK_ID_ is null and A0.NAME_= 'ywh' and A0.TYPE_ = 'string' and A0.TEXT_ LIKE '%"+act.getYwh()+"%' ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){
			sqlTNQSBStr.append("and A1.TASK_ID_ is null and A1.NAME_= 'sqr' and A1.TYPE_ = 'string' and A1.TEXT_ LIKE '%"+act.getSqr()+"%' ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){
			sqlTNQSBStr.append("and A2.TASK_ID_ is null and A2.NAME_= 'djlx' and A2.TYPE_ = 'string' and A2.TEXT_ = '"+act.getDjlx()+"' ");
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			String  title = DictUtils.getDictLabel(act.getTitle(),"reg_bus_djxl","");
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ = '"+title+"' ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQSBStr.append("and A4.TASK_ID_ is null and A4.NAME_= 'proid' and A4.TYPE_ = 'string' and A4.TEXT_ LIKE '%"+act.getProid()+"%' ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(qzdjxlValueArrStr)+") ");
		}else if("S00001001002001002".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(dydjxlValueArrStr)+") ");
		}else if("S00001001002001003".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(ygdjxlValueArrStr)+") ");
		}else if("S00001001002001004".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(lqdjxlValueArrStr)+") ");
		}else if("S00001001002001005".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(zhdjxlValueArrStr)+") ");
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		sqlTNQSBStr.append("and RES.SUSPENSION_STATE_ = 1 ");
		if("mysql".equals(Global.getDbName())){
			sqlTNQSBStr.append(" GROUP BY RES.ID_ order by RES.CREATE_TIME_ desc ) t1");
		}else if("oracle".equals(Global.getDbName())){
			sqlTNQSBStr.append(" AND VAR.ID_ = (select max(var1.ID_) from ACT_RU_VARIABLE var1 where var1.execution_id_ = RES.Proc_Inst_Id_) order by RES.CREATE_TIME_ desc ) ");
		}
		
		/*
		// 查询列表
		List<Task> todoList = taskService.createNativeTaskQuery().sql(sqlTNQSBStr.toString()).list();
		for (Task task : todoList) {
			Act e = new Act();
			e.setTask(task);
			//e.setVars(task.getProcessVariables()); NativeQuery - 0718
			e.setVars(taskService.getVariables(task.getId()));
//			e.setTaskVars(task.getTaskLocalVariables());
//			System.out.println(task.getId()+"  =  "+task.getProcessVariables() + "  ========== " + task.getTaskLocalVariables());
			e.setProcDef(ProcessDefCache.get(task.getProcessDefinitionId()));
//			e.setProcIns(runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult());
//			e.setProcExecUrl(ActUtils.getProcExeUrl(task.getProcessDefinitionId()));
			e.setStatus("todo");
			result.add(e);
		}*/
		
		sqlTNQSBStr.append(" UNION ALL ");
		sqlTNQSBStr.append(" select * from ( ");
		// =============== 等待签收的任务  =============== 
		String officeNames = office.getName();
		StringBuilder sqlTNQClaimSBStr = new StringBuilder();
		sqlTNQClaimSBStr.append("SELECT RES.*, VAR.ID_ as VAR_ID_, VAR.NAME_ as VAR_NAME_, VAR.TYPE_ as VAR_TYPE_, " +
					"VAR.REV_ as VAR_REV_, VAR.PROC_INST_ID_ as VAR_PROC_INST_ID_, VAR.EXECUTION_ID_ as VAR_EXECUTION_ID_, " +
					"VAR.TASK_ID_ as VAR_TASK_ID_, VAR.BYTEARRAY_ID_ as VAR_BYTEARRAY_ID_, VAR.DOUBLE_ as VAR_DOUBLE_, " + 
					"VAR.TEXT_ as VAR_TEXT_, VAR.TEXT2_ as VAR_TEXT2_, VAR.LONG_ as VAR_LONG " +
				"FROM ACT_RU_TASK RES left outer join ACT_RU_VARIABLE VAR " +
				"ON RES.PROC_INST_ID_ = VAR.EXECUTION_ID_ and VAR.TASK_ID_ IS NULL "+
					"inner join   ACT_RU_IDENTITYLINK I on I.TASK_ID_ = RES.ID_ ");
		// 设置查询条件
		if (StringUtils.isNotBlank(act.getProcDefKey())){
			
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A0 on RES.PROC_INST_ID_ = A0.PROC_INST_ID_ ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A1 on RES.PROC_INST_ID_ = A1.PROC_INST_ID_ ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A2 on RES.PROC_INST_ID_ = A2.PROC_INST_ID_ ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A4 on RES.PROC_INST_ID_ = A4.PROC_INST_ID_ ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode()) || "S00001001002001002".equals(office.getCode()) || "S00001001002001003".equals(office.getCode()) || "S00001001002001004".equals(office.getCode()) || "S00001001002001005".equals(office.getCode())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
			officeNames = "初审组";
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
			officeNames = "初审组";
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
			officeNames = "核定组";
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			sqlTNQClaimSBStr.append("inner join ACT_RU_VARIABLE A3 on RES.PROC_INST_ID_ = A3.PROC_INST_ID_ ");
		}
		
		if(office.getCode().startsWith("S00001001001")){
			sqlTNQClaimSBStr.append("WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'starter' and ( I.GROUP_ID_ IN ( '"+officeNames+"' ) ) ");
		}else{
			sqlTNQClaimSBStr.append("WHERE RES.ASSIGNEE_ is null and I.TYPE_ = 'candidate' and ( I.GROUP_ID_ IN ( '"+officeNames+"' ) ) ");
		}

		if (act.getBeginDate() != null){
			sqlTNQClaimSBStr.append("AND RES.CREATE_TIME_ > '"+act.getBeginDate()+"' ");
		}
		if (act.getEndDate() != null){
			sqlTNQClaimSBStr.append("AND RES.CREATE_TIME_ < '"+act.getEndDate()+"' ");
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			sqlTNQClaimSBStr.append("and A0.TASK_ID_ is null and A0.NAME_= 'ywh' and A0.TYPE_ = 'string' and A0.TEXT_ LIKE '%"+act.getYwh()+"%' ");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){
			sqlTNQClaimSBStr.append("and A1.TASK_ID_ is null and A1.NAME_= 'sqr' and A1.TYPE_ = 'string' and A1.TEXT_ LIKE '%"+act.getSqr()+"%' ");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){
			sqlTNQClaimSBStr.append("and A2.TASK_ID_ is null and A2.NAME_= 'djlx' and A2.TYPE_ = 'string' and A2.TEXT_ = '"+act.getDjlx()+"' ");
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			String  title = DictUtils.getDictLabel(act.getTitle(),"reg_bus_djxl","");
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ = '"+title+"' ");
		}
		if(act.getProid() != null && !"".equals(act.getProid())){
			sqlTNQClaimSBStr.append("and A4.TASK_ID_ is null and A4.NAME_= 'proid' and A4.TYPE_ = 'string' and A4.TEXT_ LIKE '%"+act.getProid()+"%' ");
		}
		//初审正常查询【分为:权证、抵押和预告】
		/*
		if("S00001001002001001".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(qzdjxlValueArrStr)+") ");
		}else if("S00001001002001002".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(dydjxlValueArrStr)+") ");
		}else if("S00001001002001003".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(ygdjxlValueArrStr)+") ");
		}else if("S00001001002001004".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(lqdjxlValueArrStr)+") ");
		}else if("S00001001002001005".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueToLable(zhdjxlValueArrStr)+") ");
		}*/
		if(office.getCode().startsWith("S00001001002001") && !"S00001001002001".equals(office.getCode())){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		if(office.getCode().startsWith("S00001001002003")){
			sqlTNQClaimSBStr.append("and A3.TASK_ID_ is null and A3.NAME_= 'title' and A3.TYPE_ = 'string' and A3.TEXT_ IN ("+regBusDjxlValueFromTableToList(office.getId())+") ");
		}
		// 查询总数
		//sqlTNQClaimSBStr.append("and RES.SUSPENSION_STATE_ = 1 GROUP BY RES.ID_ order by RES.CREATE_TIME_ desc) t2");
		sqlTNQClaimSBStr.append("and RES.SUSPENSION_STATE_ = 1 ");
		if("mysql".equals(Global.getDbName())){
			sqlTNQClaimSBStr.append(" GROUP BY RES.ID_ order by RES.CREATE_TIME_ desc ) t2");
		}else if("oracle".equals(Global.getDbName())){
			sqlTNQClaimSBStr.append(" AND VAR.ID_ = (select max(var1.ID_) from ACT_RU_VARIABLE var1 where var1.execution_id_ = RES.Proc_Inst_Id_) order by RES.CREATE_TIME_ desc ) ");
		}
		String sqlStr = sqlTNQSBStr.append(sqlTNQClaimSBStr).toString();
		StringBuilder sb = new StringBuilder();
		int listCount = taskService.createNativeTaskQuery().sql(sqlStr).list().size();
		
		sb.append("当前业务流程环节打开代办任务列表,SQL查询语句:"+sqlStr).
	    append(";当前登录用户信息:登录名,"+ UserUtils.getUser().getLoginName()).
		append(";当前登录用户名,"+ UserUtils.getUser().getName()).
		append(";当前登录用户IP,"+ UserUtils.getUser().getLoginIp()).
		append(";当前登录标识,"+ UserUtils.getUser().getLoginFlag()).
		append(";当前代办任务总共条数,"+ listCount);
		logger.info("【FIND_ACT_ERR·ActtaskService·toList】"+sb.toString());
		
		page.setCount(listCount);
		
		// 查询列表
		List<Task> todoAndClaimList = taskService.createNativeTaskQuery().sql(sqlStr).listPage(page.getFirstResult(), page.getMaxResults());
		
		// 查询列表
		for (Task task : todoAndClaimList) {
			Act e = new Act();
			e.setTask(task);
			e.setVars(taskService.getVariables(task.getId()));
			e.setProcDef(ProcessDefCache.get(task.getProcessDefinitionId()));
			e.setStatus("claim");
			result.add(e);
		}
		page.setList(result);
		return page;
	}

	/**
	* @Title: regBusDjxlValueToLable 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @param @param djxlValueArrStr
	* @param @return    设定文件  eg:'001','002','003'
	* @return String    返回类型 
	* @throws
	 */
	private String regBusDjxlValueToLable(String[] djxlValueArrStr) {
		String lableStr = "";
		int count = djxlValueArrStr.length;
		if(count > 0){
			for(int i=0; i<count; i++){
				if(i == count - 1){
					lableStr += "'"+DictUtils.getDictLabel(djxlValueArrStr[i],"reg_bus_djxl","")+"'";
				}else{
					lableStr += "'"+DictUtils.getDictLabel(djxlValueArrStr[i],"reg_bus_djxl","")+"',";
				}
			}
		}
		return lableStr;
	}
	
	/****
	* @Title: regBusDjxlValueFromTableToList 
	* @Description: get some djxl from sysDjxlOffice by officeid
	* @param @param officeId
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	private String regBusDjxlValueFromTableToList(String officeId){
		String lableStr = "'000'";
		SysDjxlOffice sysDjxlOffice = new SysDjxlOffice();
		sysDjxlOffice.setOfficeid(officeId);
		List<SysDjxlOffice> findList = sysDjxlOfficeService.findList(sysDjxlOffice);
		int count = findList.size();
		if(count > 0){
			lableStr = "";
			for(int i = 0; i < count; i++){
				if(i == count - 1){
					lableStr += "'"+DictUtils.getDictLabel(findList.get(i).getValue(),"reg_bus_djxl","")+"'";
				}else{
					lableStr += "'"+DictUtils.getDictLabel(findList.get(i).getValue(),"reg_bus_djxl","")+"',";
				}
			}
		}
		return lableStr;
	}
	
	/**
	 * 获取已办任务
	 * @param page
	 * @param procDefKey 流程定义标识
	 * @return
	 */
	public Page<Act> historicList(Page<Act> page, Act act){
		act.setPage(page);
		
		String userId = UserUtils.getUser().getLoginName();//ObjectUtils.toString(UserUtils.getUser().getId());

		HistoricTaskInstanceQuery histTaskQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).finished()
				.includeProcessVariables().orderByHistoricTaskInstanceEndTime().desc();
		
		
		
		// 设置查询条件
		if (StringUtils.isNotBlank(act.getProcDefKey())){
			histTaskQuery.processDefinitionKey(act.getProcDefKey());
		}
		if (act.getBeginDate() != null){
			histTaskQuery.taskCompletedAfter(act.getBeginDate());
		}
		if (act.getEndDate() != null){
			histTaskQuery.taskCompletedBefore(act.getEndDate());
		}
		//增加流程变量查询
		if(act.getYwh() != null && !"".equals(act.getYwh())){
			histTaskQuery.processVariableValueLike("ywh", "%"+act.getYwh()+"%");
		}
		if(act.getSqr() != null && !"".equals(act.getSqr())){
			histTaskQuery.processVariableValueLike("sqr", "%"+act.getSqr()+"%");
		}
		if(act.getDjlx() != null && !"".equals(act.getDjlx())){
			histTaskQuery.processVariableValueEquals("djlx", act.getDjlx());
		}
		if(act.getTitle() != null && !"".equals(act.getTitle())){
			String  title = DictUtils.getDictLabel(act.getTitle(),"reg_bus_djxl","");
			histTaskQuery.processVariableValueEquals("title",title);
		}

		// 查询总数
		page.setCount(histTaskQuery.count()); 
		System.out.println(histTaskQuery.toString());
		// 查询列表
		List<HistoricTaskInstance> histList = histTaskQuery.listPage(page.getFirstResult(), page.getMaxResults());
		List<Act> actList = new ArrayList<Act>();
		for (HistoricTaskInstance histTask : histList) {
			Act e = new Act();
			e.setHistTask(histTask);
			e.setVars(histTask.getProcessVariables());
//			e.setTaskVars(histTask.getTaskLocalVariables());
//			System.out.println(histTask.getId()+"  =  "+histTask.getProcessVariables() + "  ========== " + histTask.getTaskLocalVariables());
			e.setProcDef(ProcessDefCache.get(histTask.getProcessDefinitionId()));
//			e.setProcIns(runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult());
//			e.setProcExecUrl(ActUtils.getProcExeUrl(task.getProcessDefinitionId()));
			e.setStatus("finish");
			actList.add(e);
			//page.getList().add(e);
		}
		page.setList(actList);
		return page;
	}
	
	
	/**
	 * 获取流转历史列表
	 * @param procInsId 流程实例
	 * @param startAct 开始活动节点名称
	 * @param endAct 结束活动节点名称
	 */
	public List<Act> histoicFlowList(String procInsId, String startAct, String endAct){
		List<Act> actList = Lists.newArrayList(); 
		
		HistoricActivityInstanceQuery query =		historyService.createHistoricActivityInstanceQuery().processInstanceId(procInsId)
				.orderByHistoricActivityInstanceStartTime().asc().orderByHistoricActivityInstanceEndTime().asc();
		 
		List<HistoricActivityInstance> list = query.list();
		boolean start = false;
		Map<String, Integer> actMap = Maps.newHashMap();
		
		for (int i=0; i<list.size(); i++){
			
			HistoricActivityInstance histIns = list.get(i);
			
			// 过滤开始节点前的节点
			if (StringUtils.isNotBlank(startAct) && startAct.equals(histIns.getActivityId())){
				start = true;
			}
			if (StringUtils.isNotBlank(startAct) && !start){
				continue;
			}
			
			// 只显示开始节点和结束节点,并且执行人不为空的任务
			if (StringUtils.isNotBlank(histIns.getAssignee())
					 || "startEvent".equals(histIns.getActivityType())
					 || "endEvent".equals(histIns.getActivityType())){
				
				// 给节点增加一个序号
				Integer actNum = actMap.get(histIns.getActivityId());
				if (actNum == null){
					actMap.put(histIns.getActivityId(), actMap.size());
				}
				
				Act e = new Act();
				e.setHistIns(histIns);
				// 获取流程发起人名称
				if ("startEvent".equals(histIns.getActivityType())){
					List<HistoricProcessInstance> il = historyService.createHistoricProcessInstanceQuery().processInstanceId(procInsId).orderByProcessInstanceStartTime().asc().list();
					if (il.size() > 0){
						if (StringUtils.isNotBlank(il.get(0).getStartUserId())){
								e.setAssignee(histIns.getAssignee());
								e.setAssigneeName(il.get(0).getStartUserId());
						}
					}
				}
				// 获取任务执行人名称
				if (StringUtils.isNotEmpty(histIns.getAssignee())){
						e.setAssignee(histIns.getAssignee());
						e.setAssigneeName(histIns.getAssignee());
				}
				// 获取意见评论内容
				if (StringUtils.isNotBlank(histIns.getTaskId())){
					List<Comment> commentList = taskService.getTaskComments(histIns.getTaskId());
					if (commentList.size()>0){
						e.setComment(commentList.get(0).getFullMessage());
					}
				}
				actList.add(e);
			}
			
			// 过滤结束节点后的节点
			if (StringUtils.isNotBlank(endAct) && endAct.equals(histIns.getActivityId())){
				boolean bl = false;
				Integer actNum = actMap.get(histIns.getActivityId());
				// 该活动节点,后续节点是否在结束节点之前,在后续节点中是否存在
				for (int j=i+1; j<list.size(); j++){
					HistoricActivityInstance hi = list.get(j);
					Integer actNumA = actMap.get(hi.getActivityId());
					if ((actNumA != null && actNumA < actNum) || StringUtils.equals(hi.getActivityId(), histIns.getActivityId())){
						bl = true;
					}
				}
				if (!bl){
					break;
				}
			}
		}
		return actList;
	}

	/**
	 * 获取流程列表
	 * @param category 流程分类
	 */
	public Page<Object[]> processList(Page<Object[]> page, String category) {
		/*
		 * 保存两个对象,一个是ProcessDefinition(流程定义),一个是Deployment(流程部署)
		 */
	    ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
	    		.latestVersion().active().orderByProcessDefinitionKey().asc();
	    
	    if (StringUtils.isNotEmpty(category)){
	    	processDefinitionQuery.processDefinitionCategory(category);
		}
	    
	    page.setCount(processDefinitionQuery.count());
	    
	    List<ProcessDefinition> processDefinitionList = processDefinitionQuery.listPage(page.getFirstResult(), page.getMaxResults());
	    for (ProcessDefinition processDefinition : processDefinitionList) {
	      String deploymentId = processDefinition.getDeploymentId();
	      Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
	      page.getList().add(new Object[]{processDefinition, deployment});
	    }
		return page;
	}
	
	/**
	 * 获取流程表单(首先获取任务节点表单KEY,如果没有则取流程开始节点表单KEY)
	 * procDefId:reg_process:12:f69c042f2f7f4b3a97981d1c4fca2f1a
	 * taskDefKey:reg_start
	 * @return
	 */
	public String getFormKey(String procDefId, String taskDefKey){
		String formKey = "";
		if (StringUtils.isNotBlank(procDefId)){
			if (StringUtils.isNotBlank(taskDefKey)){
				try{
					formKey = formService.getTaskFormKey(procDefId, taskDefKey);
				}catch (Exception e) {
					formKey = "";
				}
			}
			if (StringUtils.isBlank(formKey)){
				formKey = formService.getStartFormKey(procDefId);
			}
			if (StringUtils.isBlank(formKey)){
				formKey = "/404";
			}
		}
		logger.debug("getFormKey: {}", formKey);
		return formKey;
	}
	
	/**
	 * 获取流程实例对象
	 * @param procInsId
	 * @return
	 */
	@Transactional(readOnly = false)
	public ProcessInstance getProcIns(String procInsId) {
		return runtimeService.createProcessInstanceQuery().processInstanceId(procInsId).singleResult();
	}

	/**
	 * 启动流程
	 * @param procDefKey 流程定义KEY
	 * @param businessTable 业务表表名
	 * @param businessId	业务表编号
	 * @return 流程实例ID
	 */
	@Transactional(readOnly = false)
	public String startProcess(String procDefKey, String businessTable, String businessId) {
		return startProcess(procDefKey, businessTable, businessId, "");
	}
	
	/**
	 * 启动流程
	 * @param procDefKey 流程定义KEY
	 * @param businessTable 业务表表名
	 * @param businessId	业务表编号
	 * @param title			流程标题,显示在待办任务标题
	 * @return 流程实例ID
	 */
	@Transactional(readOnly = false)
	public String startProcess(String procDefKey, String businessTable, String businessId, String title) {
		Map<String, Object> vars = Maps.newHashMap();
		return startProcess(procDefKey, businessTable, businessId, title, vars);
	}
	
	/**
	 * 启动流程
	 * @param procDefKey 流程定义KEY
	 * @param businessTable 业务表表名
	 * @param businessId	业务表编号
	 * @param title			流程标题,显示在待办任务标题
	 * @param vars			流程变量
	 * @return 流程实例ID
	 */
	@Transactional(readOnly = false)
	public String startProcess(String procDefKey, String businessTable, String businessId, String title, Map<String, Object> vars) {
		String userId = UserUtils.getUser().getLoginName();//ObjectUtils.toString(UserUtils.getUser().getId())
		
		// 用来设置启动流程的人员ID,引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(userId);
		
		// 设置流程变量
		if (vars == null){
			vars = Maps.newHashMap();
		}
		
		// 设置流程标题
		if (StringUtils.isNotBlank(title)){
			vars.put("title", title);
		}
		
		// 启动流程
		ProcessInstance procIns = runtimeService.startProcessInstanceByKey(procDefKey, businessTable+":"+businessId, vars);
		
		// 更新业务表流程实例ID
		Act act = new Act();
		act.setBusinessTable(businessTable);// 业务表名
		act.setBusinessId(businessId);	// 业务表ID
		act.setProcInsId(procIns.getId());
		actDao.updateProcInsIdByBusinessId(act);
		return act.getProcInsId();
	}

	/**
	 * 获取任务
	 * @param taskId 任务ID
	 */
	public Task getTask(String taskId){
		return taskService.createTaskQuery().taskId(taskId).singleResult();
	}
	
	/**
	 * 删除任务
	 * @param taskId 任务ID
	 * @param deleteReason 删除原因
	 */
	public void deleteTask(String taskId, String deleteReason){
		taskService.deleteTask(taskId, deleteReason);
	}
	
	/**
	 * 签收任务
	 * @param taskId 任务ID
	 * @param userId 签收用户ID(用户登录名)
	 */
	@Transactional(readOnly = false)
	public void claim(String taskId, String userId){
		//add point for trace the claim operation issue.
		ActivitiLog.debug("call claim service. taskid: " + taskId + ", timestamp: " +  System.currentTimeMillis());
		taskService.claim(taskId, userId);
	}
	
	/**
	 * 提交任务, 并保存意见
	 * @param taskId 任务ID
	 * @param procInsId 流程实例ID,如果为空,则不保存任务提交意见
	 * @param comment 任务提交意见的内容
	 * @param vars 任务变量
	 */
	@Transactional(readOnly = false)
	public void complete(String taskId, String procInsId, String comment, Map<String, Object> vars){
		complete(taskId, procInsId, comment, "", vars);
	}
	
	/**
	 * 提交任务, 并保存意见
	 * @param taskId 任务ID
	 * @param procInsId 流程实例ID,如果为空,则不保存任务提交意见
	 * @param comment 任务提交意见的内容
	 * @param title			流程标题,显示在待办任务标题
	 * @param vars 任务变量
	 */
	@Transactional(readOnly = false)
	public void complete(String taskId, String procInsId, String comment, String title, Map<String, Object> vars){
		// 添加意见
		if (StringUtils.isNotBlank(procInsId) && StringUtils.isNotBlank(comment)){
			taskService.addComment(taskId, procInsId, comment);
		}
		
		// 设置流程变量
		if (vars == null){
			vars = Maps.newHashMap();
		}
		
		// 设置流程标题
		if (StringUtils.isNotBlank(title)){
			vars.put("title", title);
		}
		StringBuilder sb = new StringBuilder();
		sb.append("当前受理申请任务ID:"+taskId).append(";当前受理申请流程ID:"+ procInsId).
		    append(";当前登录用户信息:登录名,"+ UserUtils.getUser().getLoginName()).
			append(";当前登录用户名,"+ UserUtils.getUser().getName()).
			append(";当前登录用户IP,"+ UserUtils.getUser().getLoginIp()).
			append(";当前timestamp:"+  System.currentTimeMillis()).
			append(";当前登录标识,"+ UserUtils.getUser().getLoginFlag());
		logger.info("【FIND_ACT_ERR·ActTaskService·complete】"+sb.toString());
		//add point for trace the claim operation issue.
		ActivitiLog.debug("call complete service. taskid:"+taskId);
		// 提交任务
		taskService.complete(taskId, vars);
	}

	/**
	 * 完成第一个任务
	 * @param procInsId
	 */
	public void completeFirstTask(String procInsId){
		completeFirstTask(procInsId, null, null, null);
	}
	
	/**
	 * 完成第一个任务
	 * @param procInsId
	 * @param comment
	 * @param title
	 * @param vars
	 */
	public void completeFirstTask(String procInsId, String comment, String title, Map<String, Object> vars){
		String userId = UserUtils.getUser().getLoginName();
		Task task = taskService.createTaskQuery().taskAssignee(userId).processInstanceId(procInsId).active().singleResult();
		if (task != null){
			complete(task.getId(), procInsId, comment, title, vars);
		}
	}

//	/**
//	 * 委派任务
//	 * @param taskId 任务ID
//	 * @param userId 被委派人
//	 */
//	public void delegateTask(String taskId, String userId){
//		taskService.delegateTask(taskId, userId);
//	}
//	
//	/**
//	 * 被委派人完成任务
//	 * @param taskId 任务ID
//	 */
//	public void resolveTask(String taskId){
//		taskService.resolveTask(taskId);
//	}
//	
//	/**
//	 * 回退任务
//	 * @param taskId
//	 */
//	public void backTask(String taskId){
//		taskService.
//	}
	
	////////////////////////////////////////////////////////////////////
	
	/**
	 * 读取带跟踪的图片
	 * @param executionId	环节ID
	 * @return	封装了各种节点信息
	 */
	public InputStream tracePhoto(String processDefinitionId, String executionId) {
		// ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(executionId).singleResult();
		BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
		
		List<String> activeActivityIds = Lists.newArrayList();
		if (runtimeService.createExecutionQuery().executionId(executionId).count() > 0){
			activeActivityIds = runtimeService.getActiveActivityIds(executionId);
		}
		
		// 不使用spring请使用下面的两行代码
		// ProcessEngineImpl defaultProcessEngine = (ProcessEngineImpl)ProcessEngines.getDefaultProcessEngine();
		// Context.setProcessEngineConfiguration(defaultProcessEngine.getProcessEngineConfiguration());

		// 使用spring注入引擎请使用下面的这行代码
		Context.setProcessEngineConfiguration(processEngine.getProcessEngineConfiguration());

		return ProcessDiagramGenerator.generateDiagram(bpmnModel, "png", activeActivityIds);
	}
	
	/**
	 * 流程跟踪图信息
	 * @param processInstanceId		流程实例ID
	 * @return	封装了各种节点信息
	 */
	public List<Map<String, Object>> traceProcess(String processInstanceId) throws Exception {
		Execution execution = runtimeService.createExecutionQuery().executionId(processInstanceId).singleResult();//执行实例
		Object property = PropertyUtils.getProperty(execution, "activityId");
		String activityId = "";
		if (property != null) {
			activityId = property.toString();
		}
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId)
				.singleResult();
		ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(processInstance.getProcessDefinitionId());
		List<ActivityImpl> activitiList = processDefinition.getActivities();//获得当前任务的所有节点

		List<Map<String, Object>> activityInfos = new ArrayList<Map<String, Object>>();
		for (ActivityImpl activity : activitiList) {

			boolean currentActiviti = false;
			String id = activity.getId();

			// 当前节点
			if (id.equals(activityId)) {
				currentActiviti = true;
			}

			Map<String, Object> activityImageInfo = packageSingleActivitiInfo(activity, processInstance, currentActiviti);

			activityInfos.add(activityImageInfo);
		}

		return activityInfos;
	}

	/**
	 * 封装输出信息,包括:当前节点的X、Y坐标、变量信息、任务类型、任务描述
	 * @param activity
	 * @param processInstance
	 * @param currentActiviti
	 * @return
	 */
	private Map<String, Object> packageSingleActivitiInfo(ActivityImpl activity, ProcessInstance processInstance,
			boolean currentActiviti) throws Exception {
		Map<String, Object> vars = new HashMap<String, Object>();
		Map<String, Object> activityInfo = new HashMap<String, Object>();
		activityInfo.put("currentActiviti", currentActiviti);
		setPosition(activity, activityInfo);
		setWidthAndHeight(activity, activityInfo);

		Map<String, Object> properties = activity.getProperties();
		vars.put("节点名称", properties.get("name"));
		vars.put("任务类型", ActUtils.parseToZhType(properties.get("type").toString()));

		ActivityBehavior activityBehavior = activity.getActivityBehavior();
		logger.debug("activityBehavior={}", activityBehavior);
		if (activityBehavior instanceof UserTaskActivityBehavior) {

			Task currentTask = null;

			// 当前节点的task
			if (currentActiviti) {
				currentTask = getCurrentTaskInfo(processInstance);
			}

			// 当前任务的分配角色
			UserTaskActivityBehavior userTaskActivityBehavior = (UserTaskActivityBehavior) activityBehavior;
			TaskDefinition taskDefinition = userTaskActivityBehavior.getTaskDefinition();
			Set<Expression> candidateGroupIdExpressions = taskDefinition.getCandidateGroupIdExpressions();
			if (!candidateGroupIdExpressions.isEmpty()) {

				// 任务的处理角色
				setTaskGroup(vars, candidateGroupIdExpressions);

				// 当前处理人
				if (currentTask != null) {
					setCurrentTaskAssignee(vars, currentTask);
				}
			}
		}

		vars.put("节点说明", properties.get("documentation"));

		String description = activity.getProcessDefinition().getDescription();
		vars.put("描述", description);

		logger.debug("trace variables: {}", vars);
		activityInfo.put("vars", vars);
		return activityInfo;
	}

	/**
	 * 设置任务组
	 * @param vars
	 * @param candidateGroupIdExpressions
	 */
	private void setTaskGroup(Map<String, Object> vars, Set<Expression> candidateGroupIdExpressions) {
		String roles = "";
		for (Expression expression : candidateGroupIdExpressions) {
			String expressionText = expression.getExpressionText();
			String roleName = identityService.createGroupQuery().groupId(expressionText).singleResult().getName();
			roles += roleName;
		}
		vars.put("任务所属角色", roles);
	}

	/**
	 * 设置当前处理人信息
	 * @param vars
	 * @param currentTask
	 */
	private void setCurrentTaskAssignee(Map<String, Object> vars, Task currentTask) {
		String assignee = currentTask.getAssignee();
		if (assignee != null) {
			org.activiti.engine.identity.User assigneeUser = identityService.createUserQuery().userId(assignee).singleResult();
			String userInfo = assigneeUser.getFirstName() + " " + assigneeUser.getLastName();
			vars.put("当前处理人", userInfo);
		}
	}

	/**
	 * 获取当前节点信息
	 * @param processInstance
	 * @return
	 */
	private Task getCurrentTaskInfo(ProcessInstance processInstance) {
		Task currentTask = null;
		try {
			String activitiId = (String) PropertyUtils.getProperty(processInstance, "activityId");
			logger.debug("current activity id: {}", activitiId);

			currentTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDefinitionKey(activitiId)
					.singleResult();
			logger.debug("current task for processInstance: {}", ToStringBuilder.reflectionToString(currentTask));

		} catch (Exception e) {
			logger.error("can not get property activityId from processInstance: {}", processInstance);
		}
		return currentTask;
	}

	/**
	 * 设置宽度、高度属性
	 * @param activity
	 * @param activityInfo
	 */
	private void setWidthAndHeight(ActivityImpl activity, Map<String, Object> activityInfo) {
		activityInfo.put("width", activity.getWidth());
		activityInfo.put("height", activity.getHeight());
	}

	/**
	 * 设置坐标位置
	 * @param activity
	 * @param activityInfo
	 */
	private void setPosition(ActivityImpl activity, Map<String, Object> activityInfo) {
		activityInfo.put("x", activity.getX());
		activityInfo.put("y", activity.getY());
	}
	
	/**
	 * 自动启动流程
	 * @param procDefKey 流程定义KEY
	 * @param businessTable 业务表表名
	 * @param businessId	业务表编号
	 * @param title			流程标题,显示在待办任务标题
	 * @return 流程实例ID
	 */
	@Transactional(readOnly = false)
	public String startAutomaticProcess(String procDefKey, String businessTable, String businessId, String title) {
		Map<String, Object> vars = Maps.newHashMap();
		return startProcess(procDefKey, businessTable, businessId, title, vars);
	}
	
	/**
	 * 自动启动流程
	 * @param procDefKey 流程定义KEY
	 * @param businessTable 业务表表名
	 * @param businessId	业务表编号
	 * @param title			流程标题,显示在待办任务标题
	 * @param vars			流程变量
	 * @return 流程实例ID
	 */
	@Transactional(readOnly = false)
	public String startAutomaticProcess(String procDefKey, String businessTable, String businessId, String title, Map<String, Object> vars) {
		String userId = UserUtils.getUser().getLoginName();//ObjectUtils.toString(UserUtils.getUser().getId())
		
		// 用来设置启动流程的人员ID,引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(userId);
		// 设置流程变量
		if (vars == null){
			vars = Maps.newHashMap();
		}
		// 设置流程标题
		if (StringUtils.isNotBlank(title)){
			vars.put("title", title);
		}
		// 启动流程
		ProcessInstance procIns = runtimeService.startProcessInstanceByKey(procDefKey, businessTable+":"+businessId, vars);
		// 更新业务表流程实例ID
		Act act = new Act();
		act.setBusinessTable(businessTable);// 业务表名
		act.setBusinessId(businessId);	// 业务表ID
		act.setProcInsId(procIns.getId());
		actDao.updateProcInsIdByBusinessId(act);
		return act.getProcInsId();
	}
	
}