/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workreceiptsrevise.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.websocket.onchat.ChatServerPool;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workactivity.dao.WorkActivityProcessDao;
import com.jeeplus.modules.workactivity.entity.Activity;
import com.jeeplus.modules.workactivity.entity.WorkActivityProcess;
import com.jeeplus.modules.workactivity.service.ActivityService;
import com.jeeplus.modules.workactivity.service.WorkActivityProcessService;
import com.jeeplus.modules.workactivitymenu.entity.WorkActivityMenu;
import com.jeeplus.modules.workactivitymenu.service.WorkActivityMenuService;
import com.jeeplus.modules.workcontractinfo.service.WorkContractInfoService;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workprojectnotify.util.UtilNotify;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.java_websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workreceiptsrevise.entity.WorkReceiptsRevise;
import com.jeeplus.modules.workreceiptsrevise.dao.WorkReceiptsReviseDao;

/**
 * 收入调整Service
 * @author lw
 * @version 2018-06-29
 */
@Service
@Transactional(readOnly = true)
public class WorkReceiptsReviseService extends CrudService<WorkReceiptsReviseDao, WorkReceiptsRevise> {
	@Autowired
	private WorkContractInfoService workContractInfoService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private SerialNumTplService serialNumTplService;
	@Autowired
	private OfficeService officeService;

	@Autowired
	private WorkReceiptsReviseDao workReceiptsReviseDao;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;
	public WorkReceiptsRevise get(String id) {
		return super.get(id);
	}
	
	public List<WorkReceiptsRevise> findList(WorkReceiptsRevise workReceiptsRevise) {
		return super.findList(workReceiptsRevise);
	}
	
	public Page<WorkReceiptsRevise> findPage(Page<WorkReceiptsRevise> page, WorkReceiptsRevise workReceiptsRevise) {
		workReceiptsRevise.getSqlMap().put("dsf", dataScopeFilter(workReceiptsRevise.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_RECEIPTSREVISE.getValue()));
		workReceiptsRevise.setPage(page);
		page.setList(findList(workReceiptsRevise));
		return page;
	}
	public Page<WorkReceiptsRevise> findPageInvalid(Page<WorkReceiptsRevise> page, WorkReceiptsRevise workReceiptsRevise) {
		workReceiptsRevise.getSqlMap().put("dsf", dataScopeFilter(workReceiptsRevise.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_RECEIPTSREVISE.getValue()));
		workReceiptsRevise.setPage(page);
		page.setList(workReceiptsReviseDao.findListInvalid(workReceiptsRevise));
		return page;
	}
	@Transactional(readOnly = false)
	public void save(WorkReceiptsRevise workReceiptsRevise) {
		super.save(workReceiptsRevise);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkReceiptsRevise workReceiptsRevise) {
		super.delete(workReceiptsRevise);
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workReceiptsRevise.getId());
		workProjectNotifyService.readByNotifyId(notify);
	}
	@Transactional(readOnly = false)
	public void deleteInvalid(WorkReceiptsRevise workReceiptsRevise) {
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workReceiptsRevise.getId());
		workProjectNotifyService.readByNotifyId(notify);
		workReceiptsRevise.setStatus("5");
		workReceiptsRevise.setInvalidStatus(null);
		workReceiptsRevise.preUpdate();
		workReceiptsReviseDao.update(workReceiptsRevise);
	}

	@Transactional(readOnly = false)
	public String save(WorkReceiptsRevise workReceiptsRevise, Map<String, Object> variables, String processInstanceId) {
		try {
			super.save(workReceiptsRevise);
			User user = UserUtils.getUser();
			if (StringUtils.isBlank(workReceiptsRevise.getOfficeNum())) {
				workReceiptsRevise.setOfficeNum(serialNumTplService.genSerialNum(UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getCompany(), workReceiptsRevise.SERIAL_BIZCODE));
				workReceiptsRevise.preUpdate();
				workReceiptsReviseDao.update(workReceiptsRevise);
			}
			Office office = officeService.get(workReceiptsRevise.getOfficeId());
			String userName = UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getName();
			String str = "";
			str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney()+",申请人："+userName+",所属部门："+office.getName();
			String title = "调整部门:"+office.getName();
			// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
			identityService.setAuthenticatedUserId(user.getId());
			// 启动流程
			String businessKey = workReceiptsRevise.getId().toString();
			WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("ggh3125f1f194c82bdereseopc75qwer", office);
			// 启动流程
			String processType = workActivityMenu.getProcessType();
			StringBuffer buffer = new StringBuffer();
			Activity activity = new Activity();
			WorkProjectNotify workProjectNotify = UtilNotify
					.saveNotify(workReceiptsRevise.getId(),
							null,
							workReceiptsRevise.getCompanyId(),
							title,
							str,
							"53",
							"0",
							"待审批",
							""
					);
			List<User> users = new ArrayList<>();
			List<User> fglds = UserUtils.getByRoleActivityEnname("fgld",2,office.getId(),"7",workReceiptsRevise.getCreateBy());
			List<User> scyfglbzrs = UserUtils.getByRoleActivityEnname("scyfglbzr",3,office.getId(),"7",workReceiptsRevise.getCreateBy());
			if (StringUtils.isNotBlank(workActivityMenu.getId())) {
				workProjectNotify.setNotifyRole("");
				workActivityMenu = workActivityMenuService.get(workActivityMenu.getId());
				List<Activity> activities = workActivityMenu.getActivities();
				for (Activity a : activities) {
					String encount = a.getEncount();
					String enlist = a.getEnlist();
					if (a.getRole()!=null && StringUtils.isNotBlank(a.getRole().getEnname())){
						List enusers = UserUtils.getByRoleActivityEnnames(a.getRole().getEnname(),office.getId(),"3",workReceiptsRevise.getCreateBy());
						if (enusers.size()==0){
							workReceiptsRevise.setStatus("1");
							this.save(workReceiptsRevise);
							return "流程审批人不能为空,角色"+a.getRole().getName()+"下无用户,请联系管理员！";
						}
						variables.put(enlist, enusers);
						variables.put(encount, enusers.size());
					}
					if (a.getDelFlag().equals("0") && a.getCount() == 1) {
						activity = a;
					}
				}
				buffer.append(activity.getRole().getEnname());
				if (activity != null && StringUtils.isNotBlank(activity.getId())) {
					//角色审批
					if (StringUtils.isNotBlank(activity.getRole().getEnname())) {
						users = UserUtils.getByRoleActivityEnnames(activity.getRole().getEnname(),office.getId(),"3",workReceiptsRevise.getCreateBy());
					}
					//人员审批
					if (StringUtils.isNotBlank(activity.getUser().getId())) {
						users.add(activity.getUser());
					}
				}
				workProjectNotify.setId("");
			} else {
				variables.put("fgldList", fglds);
				if (fglds.size()==0 ){
					workReceiptsRevise.setStatus("1");
					this.save(workReceiptsRevise);
				}
				if (fglds.size()==0){
					return "流程审批人不能为空,业务分管领导下无用户,请联系管理员！";
				}
				variables.put("fgldcount",fglds.size());
				processType = "workreceiptsrevise";
				users.addAll(fglds);
			}
			for (User u : users){
				workProjectNotify.setUser(u);
				workProjectNotify.setId("");
				workProjectNotify.setNotifyRole("调整审批人审批");
				workProjectNotifyService
						.save(workProjectNotify);
				Map<String,Object> extras = new HashMap<>();
				extras.put("type","7002");
				extras.put("id",workProjectNotify.getId());
				extras.put("procDefKey","53");
				UserUtils.pushInfoToApp(title,str,extras,u.getId());
				UserUtils.pushIm(u.getId(),str);
			}
			variables.put("type", processType);
			variables.put("busId", businessKey);
			variables.put("title", "审批单收入调整部门：" +  workReceiptsRevise.getOfficeName());//设置标题；

			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
			workReceiptsRevise.setProcessInstance(processInstance);
			if (StringUtils.isNotBlank(processInstanceId)) {
				workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
				workActivityProcessService.deleteProcessInstanceId(processInstanceId);
				workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
			}
			// 更新流程实例ID
			workReceiptsRevise.setProcessInstanceId(processInstance.getId());
			workReceiptsReviseDao.updateProcessInstanceId(workReceiptsRevise);
			List<Activity> list = workActivityMenu.getActivities();
			if (list != null && list.size() != 0) {
				workActivityProcessService.saveList(list, processInstance.getId());
			} else {
				WorkActivityProcess workActivityProcess = new WorkActivityProcess();
				workActivityProcess.setProcessKey("workreceiptsrevise");
				workActivityProcess.setCount(1);
				workActivityProcess.setProcessInstanceId(processInstance.getId());
				workActivityProcess.setIsApproval("0");
				workActivityProcessService.save(workActivityProcess);
				workActivityProcess.setCount(2);
				workActivityProcess.setId("");
				workActivityProcessService.save(workActivityProcess);
				workActivityProcessService.insertAuditsByType(fglds,processInstance.getId(),1,1);
				workActivityProcessService.insertAuditsByType(scyfglbzrs,processInstance.getId(),2,0);
			}
			logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{
					ActUtils.PD_REIMBURSEMENT[0], businessKey, processInstance.getId(), variables});
		}catch (Exception e){
			logger.info("Exception e:"+e);
			return "Exception e:"+e;
		}
		return "";
	}
	//作废申请
	@Transactional(readOnly = false)
	public String invalidSave(WorkReceiptsRevise workReceiptsRevise, Map<String, Object> variables, String processInstanceId) {
			User user = UserUtils.getUser();
			super.save(workReceiptsRevise);
			Office office = officeService.get(workReceiptsRevise.getOfficeId());
			String userName = UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getName();
			String str = "";
			str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney()+",申请人："+userName+",所属部门："+office.getName();
			String title = "调整部门:"+office.getName();
			// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
			identityService.setAuthenticatedUserId(user.getId());
			// 启动流程
			String businessKey = workReceiptsRevise.getId().toString();
			WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("ggh3125f1f194c82bd1111eopc75qwer", office);
			// 启动流程
			String processType = workActivityMenu.getProcessType();
			StringBuffer buffer = new StringBuffer();
			Activity activity = new Activity();
			WorkProjectNotify workProjectNotify = UtilNotify
					.saveNotify(workReceiptsRevise.getId(),
							null,
							workReceiptsRevise.getCompanyId(),
							title,
							str,
							"54",
							"0",
							"待审批",
							""
					);
			List<User> users = new ArrayList<>();
		List<User> fglds = UserUtils.getByRoleActivityEnname("fgld",2,office.getId(),"7",workReceiptsRevise.getCreateBy());
		List<User> scyfglbzrs = UserUtils.getByRoleActivityEnname("scyfglbzr",3,office.getId(),"7",workReceiptsRevise.getCreateBy());
		if (StringUtils.isNotBlank(workActivityMenu.getId())) {
				workProjectNotify.setNotifyRole("");
				workActivityMenu = workActivityMenuService.get(workActivityMenu.getId());
				List<Activity> activities = workActivityMenu.getActivities();
				for (Activity a : activities) {
					String encount = a.getEncount();
					String enlist = a.getEnlist();
					if (a.getRole()!=null && StringUtils.isNotBlank(a.getRole().getEnname())){
						List enusers = UserUtils.getByRoleActivityEnnames(a.getRole().getEnname(),office.getId(),"3",workReceiptsRevise.getCreateBy());
						if (enusers.size()==0){
							workReceiptsRevise.setStatus("5");
							workReceiptsRevise.setInvalidStatus(null);
							this.save(workReceiptsRevise);
							return "流程审批人不能为空,角色"+a.getRole().getName()+"下无用户,请联系管理员！";
						}
						variables.put(enlist, enusers);
						variables.put(encount, enusers.size());
					}
					if (a.getDelFlag().equals("0") && a.getCount() == 1) {
						activity = a;
					}
				}
				buffer.append(activity.getRole().getEnname());
				if (activity != null && StringUtils.isNotBlank(activity.getId())) {
					//角色审批
					if (StringUtils.isNotBlank(activity.getRole().getEnname())) {
						users = UserUtils.getByRoleActivityEnnames(activity.getRole().getEnname(),office.getId(),"3",workReceiptsRevise.getCreateBy());
					}
					//人员审批
					if (StringUtils.isNotBlank(activity.getUser().getId())) {
						users.add(activity.getUser());
					}
				}
				workProjectNotify.setId("");
			} else {
				variables.put("fgldList", fglds);
				if (fglds.size()==0 ){
					workReceiptsRevise.setStatus("5");
					workReceiptsRevise.setInvalidStatus(null);
					this.save(workReceiptsRevise);
				}
				if (fglds.size()==0){
					return "流程审批人不能为空,业务分管领导下无用户,请联系管理员！";
				}
				variables.put("fgldcount",fglds.size());
				processType = "workreceiptsreviseinvalid";
				users.addAll(fglds);
			}
			for (User u : users){
				workProjectNotify.setUser(u);
				workProjectNotify.setId("");
				workProjectNotify.setNotifyRole("调整审批人审批");
				workProjectNotifyService
						.save(workProjectNotify);
				Map<String,Object> extras = new HashMap<>();
				extras.put("type","7001");
				extras.put("id",workProjectNotify.getId());
				extras.put("procDefKey","54");
				UserUtils.pushInfoToApp(title,str,extras,u.getId());
				UserUtils.pushIm(u.getId(),str);
			}
			variables.put("type", processType);
			variables.put("busId", businessKey);
			variables.put("title", "审批单收入调整部门：" +  workReceiptsRevise.getOfficeName());//设置标题；

			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
			workReceiptsRevise.setProcessInstance(processInstance);
			if (StringUtils.isNotBlank(processInstanceId)) {
				workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
				workActivityProcessService.deleteProcessInstanceId(processInstanceId);
				workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
			}
			// 更新流程实例ID
			workReceiptsRevise.setInvalidProcessInstanceId(processInstance.getId());
			workReceiptsReviseDao.updateInvalidProcessInstanceId(workReceiptsRevise);

			//状态设置为作废中
			workReceiptsRevise.setStatus("6");
			//作废状态-审批中
			workReceiptsRevise.setInvalidStatus("2");
			workReceiptsRevise.preUpdate();
			workReceiptsReviseDao.update(workReceiptsRevise);

			List<Activity> list = workActivityMenu.getActivities();
			if (list != null && list.size() != 0) {
				workActivityProcessService.saveList(list, processInstance.getId());
			} else {
				WorkActivityProcess workActivityProcess = new WorkActivityProcess();
				workActivityProcess.setProcessKey("workreceiptsreviseinvalid");
				workActivityProcess.setCount(1);
				workActivityProcess.setProcessInstanceId(processInstance.getId());
				workActivityProcess.setIsApproval("0");
				workActivityProcessService.save(workActivityProcess);
				workActivityProcess.setCount(2);
				workActivityProcess.setId("");
				workActivityProcessService.save(workActivityProcess);
				workActivityProcessService.insertAuditsByType(fglds,processInstance.getId(),1,1);
				workActivityProcessService.insertAuditsByType(scyfglbzrs,processInstance.getId(),2,0);
			}
			logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{
					ActUtils.PD_REIMBURSEMENT[0], businessKey, processInstance.getId(), variables});
		return "";
	}
	@Transactional(readOnly = false)
	public String auditSave(WorkReceiptsRevise workReceiptsRevise,List<User> auditUsers) {
		Office office = officeService.get(workReceiptsRevise.getOfficeId());
		String userName = UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getName();
		String str = "";
		str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney()+",申请人："+userName+",所属部门："+office.getName();
		String title = "调整部门:"+office.getName();
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workReceiptsRevise.getAct().getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey)) {
			actTaskService.claim(workReceiptsRevise.getAct().getTaskId(), UserUtils.getUser().getId());
		}else {
			workReceiptsRevise.getAct().setFlag("yes");
		}
		String comment = "";
		if (workReceiptsRevise.getStatus().equals("4")){
			comment = ("yes".equals(workReceiptsRevise.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(workReceiptsRevise.getAct().getFlag())?"[同意] ":"[驳回] ")+workReceiptsRevise.getAct().getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workReceiptsRevise.setStatus(("yes".equals(workReceiptsRevise.getAct().getFlag()) ? "2" : "4"));
		Map<String, Object> vars = Maps.newHashMap();
		//业务逻辑对应的条件表达式
		String exp = "";
		String taskCount = "";
		String notifyRole = "";
		int key = 0;
		String enname = "";
		List<Activity> activitieList = activityService.getByProcessInstanceId(workReceiptsRevise.getProcessInstanceId());
		WorkActivityMenu workActivityMenu = new WorkActivityMenu();
		if (activitieList != null && activitieList.size() != 0) {
			workActivityMenu.setProcessType(activitieList.get(0).getProcessKey());
			workActivityMenu.setActivities(activitieList);
		}

		WorkActivityProcess workActivityProcess = new WorkActivityProcess();
		WorkActivityProcess selectProcess = new WorkActivityProcess();
		selectProcess.setProcessInstanceId(workReceiptsRevise.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workreceiptsrevise")) {
			key = 1;
			for (int i = 0; i < workActivityProcesses.size(); i++) {
				WorkActivityProcess activityProcess = workActivityProcesses.get(i);
				if (taskDefKey.equals(activityProcess.getActivityTask()) && !taskDefKey.equals("modifyApply")) {
					taskCount = activityProcess.getCount()+"";
					workActivityProcess = activityProcess;
					if (!workActivityProcess.getIsApproval().equals("0")) {
						workActivityProcess.setId("");
					}
					exp = "pass";
					if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
						workReceiptsRevise.setStatus("4");
						workActivityProcess.setIsApproval("2");
						String returnBack = "-1";
						for (Activity activity : activities) {
							if (activity.getCount() == activityProcess.getCount()) {
								notifyRole = activity.getName();
								returnBack = activity.getReturnBack();
								break;
							}
						}
						if (returnBack.equals("0")) {
							workActivityProcess.setId("");
						}

					} else {
						workActivityProcess.setIsApproval("1");
					}
				}else if(taskDefKey.equals("modifyApply")){
					notifyRole = "调整申请";
					taskCount = "0";
					exp = "pass";
					workActivityProcess.setId("");
					workActivityProcess.setCount(0);
					if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
						workReceiptsRevise.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("workreceiptsrevise");
			for (int i = 0; i < workActivityProcesses.size(); i++) {
				WorkActivityProcess activityProcess = workActivityProcesses.get(i);
				String count = activityProcess.getCount() + "";
				workActivityProcess = activityProcess;
				if (!workActivityProcess.getIsApproval().equals("0")) {
					workActivityProcess.setId("");
				}
				// 审核环节
				if ("fgld".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
						vars.put("scyfglbzrList", auditUsers);
						vars.put("scyfglbzrcount",auditUsers.size());
						workActivityProcessService.insertAuditsByType(auditUsers,workReceiptsRevise.getProcessInstanceId(),2,1);
						workActivityProcess.setIsApproval("1");
						notifyRole = "市场研发管理部负责人审核";
						enname = "scyfglbzr";
					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				}else if ("scyfglbzr".equals(taskDefKey) && count.contains("2")) {
					taskCount = "2";
					exp = "pass";
					if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
						notifyRole = "审批通过";
						workActivityProcess.setIsApproval("1");
					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("modifyApply".equals(taskDefKey)&& count.contains("0")) {
					notifyRole = "业务分管领导审批";
					taskCount = "0";
					exp = "pass";
					workActivityProcess.setCount(0);
					enname = "fgld";
					if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
						workReceiptsRevise.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
		// 设置意见
		workReceiptsRevise.getAct().setComment(("yes".equals(workReceiptsRevise.getAct().getFlag()) ? "[同意] " : "[驳回] ") + workReceiptsRevise.getAct().getComment());
		workReceiptsRevise.preUpdate();
		// 提交流程任务
		vars.put(exp, "yes".equals(workReceiptsRevise.getAct().getFlag()) ? true : false);
		vars.put("passs", true);
		workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workReceiptsRevise.getProcessInstanceId(),taskDefKey,"modifyApply",workReceiptsRevise.getAct().getFlag(),comment, activities);
		// 提交流程任务
		actTaskService.complete(workReceiptsRevise.getAct().getTaskId(), workReceiptsRevise.getAct().getProcInsId(), workReceiptsRevise.getAct().getComment(), vars);
		boolean state = actTaskService.isProcessEnd(workReceiptsRevise.getAct().getProcInsId());
		List<User> users = new ArrayList<>();
		List<User> userList = new ArrayList<>();
		if (!state) {
			str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney();
			title = "调整部门:"+office.getName();
			users.add(workReceiptsRevise.getCreateBy());
			if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
				//title = "您的收入调整部门:"+workReceiptsRevise.getOfficeName()+"审核：已通过!";
				workReceiptsRevise.setStatus("5");
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workReceiptsRevise.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				workProjectNotifyService
						.save(UtilNotify
								.saveNotify(workReceiptsRevise.getId(),
										workReceiptsRevise.getCreateBy(),
										workReceiptsRevise.getCompanyId(),
										title,
										str,
										"53",
										"0",
										"待通知",
										notifyRole));

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workReceiptsRevise.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(workReceiptsRevise.getStatus()) && !workReceiptsRevise.getStatus().equals("3")){
					//title = "您的收入调整部门:"+workReceiptsRevise.getOfficeName()+"审核：被驳回!";
					workReceiptsRevise.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workReceiptsRevise.getId(),
											workReceiptsRevise.getCreateBy(),
											workReceiptsRevise.getCompanyId(),
											title,
											str,
											"53",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workReceiptsRevise.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workreceiptsrevise")) {
				//title = "收入调整申请人:"+UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getName()+",收入调整部门:"+workReceiptsRevise.getOfficeName()+" 待审批!";
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workReceiptsRevise.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				//users.addAll(userList);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workReceiptsRevise.getId(),
								new User(),
								workReceiptsRevise.getCompanyId(),
								title,
								str,
								"53",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workReceiptsRevise.getAct().getFlag(),
						taskCount,
						workReceiptsRevise.getCreateBy(),
						workReceiptsRevise.getOfficeId(),
						"7");
				for (WorkProjectNotify workProjectNotify1:workProjectNotifys){
					users.add(workProjectNotify1.getUser());
					workProjectNotify1.setId("");
					workProjectNotify1.setIsNewRecord(false);
					workProjectNotifyService
							.save(workProjectNotify1);
					if (!"modifyApply".equals(taskDefKey)){
						Map<String,Object> extras = new HashMap<>();
							extras.put("type","7002");
						extras.put("id",workProjectNotify.getId());
						extras.put("procDefKey","53");
						UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

					}
				}

			} else {
				if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workReceiptsRevise.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workReceiptsRevise.getId(),
											workReceiptsRevise.getCreateBy(),
											workReceiptsRevise.getCompanyId(),
											title,
											str,
											"53",
											"0",
											"重新申请",
											notifyRole));
					users.add( workReceiptsRevise.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workReceiptsRevise.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList1);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workReceiptsRevise.getId(),
										new User(),
										workReceiptsRevise.getCompanyId(),
										title,
										str,
										"53",
										"0",
										"待审批",
										notifyRole);
						users.addAll(auditUsers);
						for (User user1:auditUsers){
							workProjectNotify.setUser(user1);
							workProjectNotify.setId("");
							workProjectNotify.setIsNewRecord(false);
							workProjectNotifyService
									.save(workProjectNotify);
							if (!"modifyApply".equals(taskDefKey)){
								Map<String,Object> extras = new HashMap<>();
								extras.put("type","7002");
								extras.put("id",workProjectNotify.getId());
								extras.put("procDefKey","53");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());

							}
						}
					}else {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workReceiptsRevise.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.addAll(userList);
						users.add(workReceiptsRevise.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workReceiptsRevise.getId(),
												workReceiptsRevise.getCreateBy(),
												workReceiptsRevise.getCompanyId(),
												title,
												str,
												"53",
												"0",
												"重新申请",
												notifyRole));
					}
				}
			}
		}
		if (users!=null && users.size()!=0) {
			for (User u : users) {
				UserUtils.pushIm(u.getId(),str);
			}
		}
		if (userList!=null && userList.size()!=0) {
			for (User u : userList) {
				UserUtils.pushMeIm(u.getId());
			}
		}
		workReceiptsReviseDao.update(workReceiptsRevise);
		return "保存审核意见成功！";
	}

	@Transactional(readOnly = false)
	public String invalidAuditSave(WorkReceiptsRevise workReceiptsRevise ,List<User> auditUsers) {
			Office office = officeService.get(workReceiptsRevise.getOfficeId());
			String userName = UserUtils.get(workReceiptsRevise.getCreateBy().getId()).getName();
			String str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney()+",申请人："+userName+",所属部门："+office.getName();
			String title = "调整部门:"+office.getName();
			// 对不同环节的业务逻辑进行操作
			String taskDefKey = workReceiptsRevise.getAct().getTaskDefKey();
			if (!"modifyApply".equals(taskDefKey)) {
				actTaskService.claim(workReceiptsRevise.getAct().getTaskId(), UserUtils.getUser().getId());
			}else {
				workReceiptsRevise.getAct().setFlag("yes");
			}
			String comment = "";
			if (workReceiptsRevise.getInvalidStatus().equals("4")){
				comment = ("yes".equals(workReceiptsRevise.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
			}else {
				comment = ("yes".equals(workReceiptsRevise.getAct().getFlag())?"[同意] ":"[驳回] ")+workReceiptsRevise.getAct().getComment();
			}
			//yes 的时候状态为审核通过 否则为未通过
			//1 审核中 2 未通过
			workReceiptsRevise.setInvalidStatus(("yes".equals(workReceiptsRevise.getAct().getFlag()) ? "2" : "4"));
			Map<String, Object> vars = Maps.newHashMap();
			//业务逻辑对应的条件表达式
			String exp = "";
			String taskCount = "";
			String notifyRole = "";
			int key = 0;
			String enname = "";
			List<Activity> activitieList = activityService.getByProcessInstanceId(workReceiptsRevise.getInvalidProcessInstanceId());
			WorkActivityMenu workActivityMenu = new WorkActivityMenu();
			if (activitieList != null && activitieList.size() != 0) {
				workActivityMenu.setProcessType(activitieList.get(0).getProcessKey());
				workActivityMenu.setActivities(activitieList);
			}

			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			WorkActivityProcess selectProcess = new WorkActivityProcess();
			selectProcess.setProcessInstanceId(workReceiptsRevise.getInvalidProcessInstanceId());
			List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
			List<Activity> activities = workActivityMenu.getActivities();
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workreceiptsreviseinvalid")) {
				key = 1;
				for (int i = 0; i < workActivityProcesses.size(); i++) {
					WorkActivityProcess activityProcess = workActivityProcesses.get(i);
					if (taskDefKey.equals(activityProcess.getActivityTask()) && !taskDefKey.equals("modifyApply")) {
						taskCount = activityProcess.getCount()+"";
						workActivityProcess = activityProcess;
						if (!workActivityProcess.getIsApproval().equals("0")) {
							workActivityProcess.setId("");
						}
						exp = "pass";
						if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
							workReceiptsRevise.setInvalidStatus("4");
							workActivityProcess.setIsApproval("2");
							String returnBack = "-1";
							for (Activity activity : activities) {
								if (activity.getCount() == activityProcess.getCount()) {
									notifyRole = activity.getName();
									returnBack = activity.getReturnBack();
									break;
								}
							}
							if (returnBack.equals("0")) {
								workActivityProcess.setId("");
							}

						} else {
							workActivityProcess.setIsApproval("1");
						}
					}else if(taskDefKey.equals("modifyApply")){
						notifyRole = "调整申请";
						taskCount = "0";
						exp = "pass";
						workActivityProcess.setId("");
						workActivityProcess.setCount(0);
						if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
							workReceiptsRevise.setInvalidStatus("3");
							workActivityProcess.setIsApproval("2");
						} else {
							workActivityProcess.setIsApproval("1");
						}
						break;
					}
				}
			} else {
				workActivityMenu.setProcessType("workreceiptsreviseinvalid");
				for (int i = 0; i < workActivityProcesses.size(); i++) {
					WorkActivityProcess activityProcess = workActivityProcesses.get(i);
					String count = activityProcess.getCount() + "";
					workActivityProcess = activityProcess;
					if (!workActivityProcess.getIsApproval().equals("0")) {
						workActivityProcess.setId("");
					}
					// 审核环节
					if ("fgld".equals(taskDefKey) && count.contains("1")) {
						taskCount = "1";
						exp = "pass";
						if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
							vars.put("scyfglbzrList", auditUsers);
							vars.put("scyfglbzrcount",auditUsers.size());
							workActivityProcessService.insertAuditsByType(auditUsers,workReceiptsRevise.getProcessInstanceId(),2,1);
							workActivityProcess.setIsApproval("1");
							notifyRole = "市场研发管理部负责人审核";
							enname = "scyfglbzr";
						} else {
							notifyRole = "调整申请";
							workActivityProcess.setIsApproval("2");
						}
						break;
					}else if ("scyfglbzr".equals(taskDefKey) && count.contains("2")) {
						taskCount = "2";
						exp = "pass";
						if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
							notifyRole = "审批通过";
							workActivityProcess.setIsApproval("1");
						} else {
							notifyRole = "调整申请";
							workActivityProcess.setIsApproval("2");
						}
						break;
					} else if ("modifyApply".equals(taskDefKey)&& count.contains("0")) {
						notifyRole = "业务分管领导审批";
						taskCount = "0";
						exp = "pass";
						workActivityProcess.setCount(0);
						enname = "fgld";
						if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
							workReceiptsRevise.setStatus("3");
						}
						break;
					} else if ("apply_end".equals(taskDefKey)) {
					}

				}
			}
			// 设置意见
			workReceiptsRevise.getAct().setComment(("yes".equals(workReceiptsRevise.getAct().getFlag()) ? "[同意] " : "[驳回] ") + workReceiptsRevise.getAct().getComment());
			workReceiptsRevise.preUpdate();
			// 提交流程任务
			vars.put(exp, "yes".equals(workReceiptsRevise.getAct().getFlag()) ? true : false);
			vars.put("passs", true);
			workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workReceiptsRevise.getInvalidProcessInstanceId(),taskDefKey,"modifyApply",workReceiptsRevise.getAct().getFlag(),comment, activities);
			// 提交流程任务
			actTaskService.complete(workReceiptsRevise.getAct().getTaskId(), workReceiptsRevise.getAct().getProcInsId(), workReceiptsRevise.getAct().getComment(), vars);
			boolean state = actTaskService.isProcessEnd(workReceiptsRevise.getAct().getProcInsId());
			List<User> users = new ArrayList<>();
			List<User> userList = new ArrayList<>();
			if (!state) {
				str =  "调整编号："+workReceiptsRevise.getOfficeNum()+",调整类型：" +DictUtils.getDictLabel(workReceiptsRevise.getReviseType(),"revise_type","")+",调整金额："+workReceiptsRevise.getReviseMoney();
				title = "调整部门:"+office.getName();
				users.add(workReceiptsRevise.getCreateBy());
				if ("yes".equals(workReceiptsRevise.getAct().getFlag())) {
					workReceiptsRevise.setInvalidStatus("5");
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workReceiptsRevise.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workReceiptsRevise.getId(),
											workReceiptsRevise.getCreateBy(),
											workReceiptsRevise.getCompanyId(),
											title,
											str,
											"54",
											"0",
											"待通知",
											notifyRole));

				} else {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workReceiptsRevise.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					if (StringUtils.isNotBlank(workReceiptsRevise.getInvalidStatus()) && !workReceiptsRevise.getInvalidStatus().equals("3")){
						workReceiptsRevise.setInvalidStatus("4");
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workReceiptsRevise.getId(),
												workReceiptsRevise.getCreateBy(),
												workReceiptsRevise.getCompanyId(),
												title,
												str,
												"54",
												"0",
												"待通知",
												notifyRole));
					}
				}
				workActivityProcessService.deleteProcessIdAuditUsers(workReceiptsRevise.getInvalidProcessInstanceId());
			} else {
				if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workreceiptsreviseinvalid")) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workReceiptsRevise.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					WorkProjectNotify workProjectNotify = UtilNotify
							.saveNotify(workReceiptsRevise.getId(),
									new User(),
									workReceiptsRevise.getCompanyId(),
									title,
									str,
									"54",
									"0",
									"待审批",
									notifyRole);
					List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
							activities,
							workProjectNotify,
							taskDefKey,
							workReceiptsRevise.getAct().getFlag(),
							taskCount,
							workReceiptsRevise.getCreateBy(),
							workReceiptsRevise.getOfficeId(),
							"7");
					for (WorkProjectNotify workProjectNotify1:workProjectNotifys){
						users.add(workProjectNotify1.getUser());
						workProjectNotify1.setId("");
						workProjectNotify1.setIsNewRecord(false);
						workProjectNotifyService
								.save(workProjectNotify1);
						if (!"modifyApply".equals(taskDefKey)){
							Map<String,Object> extras = new HashMap<>();
								extras.put("type","7001");
							extras.put("id",workProjectNotify.getId());
							extras.put("procDefKey","54");
							UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

						}
					}

				} else {
					if (!"yes".equals(workReceiptsRevise.getAct().getFlag())) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workReceiptsRevise.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList);
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workReceiptsRevise.getId(),
												workReceiptsRevise.getCreateBy(),
												workReceiptsRevise.getCompanyId(),
												title,
												str,
												"54",
												"0",
												"重新申请",
												notifyRole));
						users.add( workReceiptsRevise.getCreateBy());
					} else {
						if (StringUtils.isNotBlank(enname)) {
							WorkProjectNotify notify = new WorkProjectNotify();
							notify.setNotifyId(workReceiptsRevise.getId());
							userList = workProjectNotifyService.readByNotifyId(notify);
							//users.addAll(userList1);
							WorkProjectNotify workProjectNotify = UtilNotify
									.saveNotify(workReceiptsRevise.getId(),
											new User(),
											workReceiptsRevise.getCompanyId(),
											title,
											str,
											"54",
											"0",
											"待审批",
											notifyRole);
							users.addAll(auditUsers);
							for (User user1:auditUsers){
								workProjectNotify.setUser(user1);
								workProjectNotify.setId("");
								workProjectNotify.setIsNewRecord(false);
								workProjectNotifyService
										.save(workProjectNotify);
								if (!"modifyApply".equals(taskDefKey)){
									Map<String,Object> extras = new HashMap<>();
									extras.put("type","7001");
									extras.put("id",workProjectNotify.getId());
									extras.put("procDefKey","54");
									UserUtils.pushInfoToApp(title,str,extras,user1.getId());

								}
							}
						}else {
							WorkProjectNotify notify = new WorkProjectNotify();
							notify.setNotifyId(workReceiptsRevise.getId());
							userList = workProjectNotifyService.readByNotifyId(notify);
							users.addAll(userList);
							users.add(workReceiptsRevise.getCreateBy());
							workProjectNotifyService
									.save(UtilNotify
											.saveNotify(workReceiptsRevise.getId(),
													workReceiptsRevise.getCreateBy(),
													workReceiptsRevise.getCompanyId(),
													title,
													str,
													"54",
													"0",
													"重新申请",
													notifyRole));
						}
					}
				}
			}
		if (users!=null && users.size()!=0) {
			for (User u : users) {
				UserUtils.pushIm(u.getId(),str);
			}
		}
		if (userList!=null && userList.size()!=0) {
			for (User u : userList) {
				UserUtils.pushMeIm(u.getId());
			}
		}
			//已作废状态修改
			if("5".equals(workReceiptsRevise.getInvalidStatus())){
				workReceiptsRevise.setStatus("7");
			}
			workReceiptsReviseDao.update(workReceiptsRevise);
			return "保存审核意见成功！";
	}

	/**
	 * 获取流程详细信息
	 * @param processInstanceId
	 */
	@SuppressWarnings("unchecked")
	public WorkReceiptsRevise getByProcessInstanceId(String processInstanceId) {
		WorkReceiptsRevise workReceiptsRevise = workReceiptsReviseDao.getByProcessInstanceId(processInstanceId);
		return workReceiptsRevise;
	}

	@Transactional(readOnly = false)
	public void cancelInvalidate(WorkReceiptsRevise workReceiptsRevise) {
		String invalidateProcessInstanceId = workReceiptsRevise.getProcessInstanceId();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workReceiptsRevise.getProcessInstanceId());
			process.setIsApproval("0");
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workReceiptsRevise.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workReceiptsRevise.getId());
			List<User> userList = workProjectNotifyService.readByNotifyId(notify);
			if (userList!=null && userList.size()!=0) {
				for (User u : userList) {
					User user = UserUtils.get(u.getId());
					/*List<WebSocket> toUserConns = ChatServerPool.getWebSocketByUser(u.getId());
					for (WebSocket toUserConn : toUserConns) {
						String message = "{\"to\":\""+u.getId()+"\"," +
								"\"msg\":\"审批信息 申请人:"+ user.getName()+",收入调整部门:"+workReceiptsRevise.getOfficeName() +" 强制撤销!\"," +
								"\"useType\":\"sys\"}";
						ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
						//ChatServerPool.sendMessageToUser(toUserConn, u.getId() + "_sys_审批信息 " + "报销人:" + user.getName() + ",报销编号:" + workReimbursement.getNumber() + " 强制撤销!");//同时向本人发送消息
					}*/
					UserUtils.pushIm(u.getId(),"申请人:"+ user.getName()+",收入调整部门:"+workReceiptsRevise.getOfficeName() +" 强制撤销!");
				}
			}
			if(processList!=null && processList.size()>0){
				for (int i =0;i<processList.size();i++) {
					WorkActivityProcess p = processList.get(i);
					if(StringUtils.isNotBlank(p.getIsApproval()) && "0".equals(p.getIsApproval())){
						p.setIsApproval("-1");
						p.setDelFlag("1");
						workActivityProcessDao.updateDelFlagAndIsApproval(p);
					}
				}
				WorkActivityProcess pro = new WorkActivityProcess();
				pro.setId("");
				pro.setDelFlag("0");
				pro.preInsert();
				pro.setRemarks("[强制撤销]");
				pro.setProcessKey(processList.get(0).getProcessKey());
				pro.setIsApproval("1");
				pro.setProcessInstanceId(processList.get(0).getProcessInstanceId());
				pro.setCount(0);
				workActivityProcessDao.insert(pro);
			}

			//结束该流程,设为"撤销"状态
			actTaskService.endProcessInstance(invalidateProcessInstanceId,"收入调整-强制撤销");
			workReceiptsRevise.setStatus("3");
			workReceiptsRevise.preUpdate();
			workReceiptsReviseDao.update(workReceiptsRevise);
		}catch (ActivitiObjectNotFoundException e){
			System.err.println("撤销收入调整申请异常，因为这个流程已不存在!");
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	//作废撤回
	@Transactional(readOnly = false)
	public void cancelInvalidateInvalid(WorkReceiptsRevise workReceiptsRevise) {
		String invalidateProcessInstanceId = workReceiptsRevise.getInvalidProcessInstanceId();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workReceiptsRevise.getInvalidProcessInstanceId());
			process.setIsApproval("0");
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workReceiptsRevise.getInvalidProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workReceiptsRevise.getId());
			List<User> userList = workProjectNotifyService.readByNotifyId(notify);
			if (userList!=null && userList.size()!=0) {
				for (User u : userList) {
					User user = UserUtils.get(u.getId());
					/*List<WebSocket> toUserConns = ChatServerPool.getWebSocketByUser(u.getId());
					for (WebSocket toUserConn : toUserConns) {
						String message = "{\"to\":\""+u.getId()+"\"," +
								"\"msg\":\"审批信息 收入调整作废申请人:"+ user.getName()+",收入调整部门:"+workReceiptsRevise.getOfficeName() +" 强制撤销!\"," +
								"\"useType\":\"sys\"}";
						ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
						//ChatServerPool.sendMessageToUser(toUserConn, u.getId() + "_sys_审批信息 " + "报销人:" + user.getName() + ",报销编号:" + workReimbursement.getNumber() + " 强制撤销!");//同时向本人发送消息
					}*/
					UserUtils.pushIm(u.getId(),"收入调整作废申请人:"+ user.getName()+",收入调整部门:"+workReceiptsRevise.getOfficeName() +" 强制撤销!");
				}
			}
			if(processList!=null && processList.size()>0){
				for (int i =0;i<processList.size();i++) {
					WorkActivityProcess p = processList.get(i);
					if(StringUtils.isNotBlank(p.getIsApproval()) && "0".equals(p.getIsApproval())){
						p.setIsApproval("-1");
						p.setDelFlag("1");
						workActivityProcessDao.updateDelFlagAndIsApproval(p);
					}
				}
				WorkActivityProcess pro = new WorkActivityProcess();
				pro.setId("");
				pro.setDelFlag("0");
				pro.preInsert();
				pro.setRemarks("[强制撤销]");
				pro.setProcessKey(processList.get(0).getProcessKey());
				pro.setIsApproval("1");
				pro.setProcessInstanceId(processList.get(0).getProcessInstanceId());
				pro.setCount(0);
				workActivityProcessDao.insert(pro);
			}

			//结束该流程,设为"撤销"状态
			actTaskService.endProcessInstance(invalidateProcessInstanceId,"收入调整作废申请-强制撤销");
			workReceiptsRevise.setInvalidStatus("3");
			workReceiptsRevise.preUpdate();
			workReceiptsReviseDao.update(workReceiptsRevise);
		}catch (ActivitiObjectNotFoundException e){
			System.err.println("撤销收入调整作废申请异常，因为这个流程已不存在!");
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}