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

import java.text.SimpleDateFormat;
import java.util.*;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.leaveapply.service.LeaveApplyService;
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.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.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workcontractinfo.service.WorkContractInfoService;
import com.jeeplus.modules.workcontractrecord.dao.WorkContractRecordDao;
import com.jeeplus.modules.workfield.entity.WorkField;
import com.jeeplus.modules.workfield.service.WorkFieldService;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workprojectnotify.util.UtilNotify;
import com.jeeplus.modules.workstaff.dao.WorkStaffAchivesDao;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.activiti.engine.ActivitiObjectNotFoundException;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
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.workovertime.entity.WorkOvertime;
import com.jeeplus.modules.workovertime.dao.WorkOvertimeDao;

/**
 * 加班申请Service
 * @author ssrh
 * @version 2018-09-04
 */
@Service
@Transactional(readOnly = true)
public class WorkOvertimeService extends CrudService<WorkOvertimeDao, WorkOvertime> {
	@Autowired
	private IdentityService identityService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private SerialNumTplService serialNumTplService;

	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private WorkFieldService workFieldService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private LeaveApplyService leaveApplyService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;
	@Autowired
	private WorkStaffAchivesDao workStaffAchivesDao;
	@Autowired
	private WorkOvertimeDao workOvertimeDao;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;

	public WorkOvertime get(String id) {
		WorkOvertime workOvertime = super.get(id);
		if(workOvertime!=null){
			//附件
			WorkClientAttachment workClientAttachment = new WorkClientAttachment();
			workClientAttachment.setAttachmentId(id);
			workClientAttachment.setAttachmentFlag("72");
			workOvertime.setWorkAttachments(workClientAttachmentDao.findList(workClientAttachment));
		}
		return workOvertime;
	}
	public WorkOvertime getByStaffIdAndTime(String staffId,String time) {
		WorkOvertime workOvertime = dao.getByStaffIdAndTime(staffId, time);
		return workOvertime;
	}

	public List<WorkOvertime> findList(WorkOvertime workOvertime) {
		return super.findList(workOvertime);
	}
	
	public Page<WorkOvertime> findPage(Page<WorkOvertime> page, WorkOvertime workOvertime) {
		workOvertime.getSqlMap().put("dsf", dataScopeFilter(workOvertime.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_OVERTIME.getValue()));
		workOvertime.setPage(page);
		page.setList(findList(workOvertime));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(WorkOvertime workOvertime) {
		super.save(workOvertime);
		updateInfo(workOvertime);
	}
	@Transactional(readOnly = false)
	public void updateInfo(WorkOvertime workOvertime) {
		//附件信息
		for (WorkClientAttachment workClientAttachment : workOvertime.getWorkAttachments()) {
			if (workClientAttachment.getId() == null) {
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
				workClientAttachment.setAttachmentId(workOvertime.getId());
				workClientAttachment.setAttachmentFlag("72");
				workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
				if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
					workClientAttachment.preInsert();
					workClientAttachmentDao.insert(workClientAttachment);
				} else {
					workClientAttachment.preUpdate();
					workClientAttachmentDao.update(workClientAttachment);
				}
			} else {
				workClientAttachmentDao.delete(workClientAttachment);
			}
		}
	}
	//时间验证
	@Transactional(readOnly = false)
	public String validateTime(Date startTime,String startType,Date endTime,String endType){
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
		WorkStaffBasicInfo workStaffBasicInfo=leaveApplyService.getBasicInfo();
		List<WorkOvertime> leaveDetails=workOvertimeDao.findListValidateTime(workStaffBasicInfo.getId());
		double start=Integer.parseInt(sdf1.format(startTime));
		double end=Integer.parseInt(sdf1.format(endTime));
		if("2".equals(startType)){
			start=start+0.5;
		}
		if("2".equals(endType)){
			end=end+0.5;
		}
		for(int i=0;i<leaveDetails.size();i++){
			WorkOvertime detail=leaveDetails.get(i);
			Date stateTime1 = detail.getStartDate();
			double start1=Integer.parseInt(sdf1.format(stateTime1));
			Date realEndTime1 = detail.getEndDate();
			double realEnd1=Integer.parseInt(sdf1.format(realEndTime1));
			String startType1 = detail.getStartType();
			String realEndType1 = detail.getEndType();
			if("2".equals(startType1)){
				start1=start1+0.5;
			}
			if("2".equals(realEndType1)){
				realEnd1=realEnd1+0.5;
			}
			if((start>=start1&&start<=realEnd1)||(end>=start1&&end<=realEnd1)
					||(start1>=start&&start1<=end)||(realEnd1>=start&&realEnd1<=end)){
				return  "加班时间存在重叠情况！";
			}
		}
		return "";
	}
	/**
	 * 提交存储
	 * @param
	 * @param variables
	 */
	@Transactional(readOnly = false)
	public String save(WorkOvertime workOvertime, Map<String, Object> variables, String processInstanceId) {
		//时间校验
		String sign1 = leaveApplyService.validateTime(workOvertime.getStartDate(),workOvertime.getStartType(),workOvertime.getEndDate(),workOvertime.getEndType());
		if(StringUtils.isNotBlank(sign1)){
			updateInfo(workOvertime);
			if("2".equals(workOvertime.getStatus())){
				workOvertime.setStatus("1");
			}
			super.save(workOvertime);
			return sign1;
		}else{
			String sign2 = this.validateTime(workOvertime.getStartDate(),workOvertime.getStartType(),workOvertime.getEndDate(),workOvertime.getEndType());
			if(StringUtils.isNotBlank(sign2)){
				updateInfo(workOvertime);
				if("2".equals(workOvertime.getStatus())){
					workOvertime.setStatus("1");
				}
				super.save(workOvertime);
				return sign2;
			}
		}
		User user = UserUtils.getUser();
		if (StringUtils.isBlank(workOvertime.getNum())) {
			workOvertime.setNum(serialNumTplService.genSerialNum(user.getCompany(), WorkOvertime.SERIAL_BIZCODE));
		}
		super.save(workOvertime);
		Office office = officeService.get(workOvertime.getOfficeId());
		String str =  "加班申请编号:"+workOvertime.getNum()+",加班原因:"+workOvertime.getReason()+",所属部门:"+office.getName();
		String title = "加班申请人:"+user.getName();
		updateInfo(workOvertime);
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		// 启动流程
		String businessKey = workOvertime.getId().toString();
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("zzsql3125f1f194clzsqasdfiiii750904", office);
		// 启动流程
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(workOvertime.getId(),
						null,
						workOvertime.getCompanyId(),
						title,
						str,
						"77",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		List<User> bmzrs = UserUtils.getByRoleActivityEnname("bmzr",2,office.getId(),"19",workOvertime.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(),"19",workOvertime.getCreateBy());
					if (enusers.size()==0){
						workOvertime.setStatus("1");
						super.save(workOvertime);
						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(),"19",workOvertime.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
			variables.put("bmzrList", bmzrs);
			if (bmzrs.size()==0 ){
				workOvertime.setStatus("1");
				super.save(workOvertime);
			}
			if (bmzrs.size()==0){
				return "流程审批人不能为空,角色部门负责人下无用户,请联系管理员！";
			}
			variables.put("bmzrcount",bmzrs.size());
			processType = "workovertime";
			users.addAll(bmzrs);
		}
		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","77");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", "审批单：" + workOvertime.getNum());//设置标题；

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		workOvertime.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		// 更新流程实例ID
		workOvertime.setProcessInstanceId(processInstance.getId());
		workOvertimeDao.updateProcessInstanceId(workOvertime);
		List<Activity> list = workActivityMenu.getActivities();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("workovertime");
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcessService.insertAuditsByType(bmzrs,processInstance.getId(),1,1);
		}
		logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{
				ActUtils.PD_REIMBURSEMENT[0], businessKey, processInstance.getId(), variables});
		return "";
	}

	@Transactional(readOnly = false)
	public void delete(WorkOvertime workOvertime) {
		super.delete(workOvertime);
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workOvertime.getId());
		List<User> users = workProjectNotifyService.readByNotifyId(notify);
		if (users!=null && users.size()!=0){
			for (User user:users){
				UserUtils.pushMeIm(user.getId());
			}
		}
	}

	/**
	 * 审核流程
	 * @param
	 */
	@Transactional(readOnly = false)
	public String auditSave(WorkOvertime workOvertime,List<User> auditUsers) {
		String userName = UserUtils.get(workOvertime.getCreateBy().getId()).getName();
		Office office = officeService.get(workOvertime.getOfficeId());
		String title = "加班申请人:"+userName;
		String str  =  "加班申请编号:"+workOvertime.getNum()+",加班原因:"+workOvertime.getReason()+",所属部门:"+office.getName();
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workOvertime.getAct().getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey)) {
			actTaskService.claim(workOvertime.getAct().getTaskId(), UserUtils.getUser().getId());
		}else{
			//时间校验
			String sign1 = leaveApplyService.validateTime(workOvertime.getStartDate(),workOvertime.getStartType(),workOvertime.getEndDate(),workOvertime.getEndType());
			if(StringUtils.isNotBlank(sign1)){
				updateInfo(workOvertime);
				super.save(workOvertime);
				return sign1;
			}else{
				String sign2 = this.validateTime(workOvertime.getStartDate(),workOvertime.getStartType(),workOvertime.getEndDate(),workOvertime.getEndType());
				if(StringUtils.isNotBlank(sign2)){
					updateInfo(workOvertime);
					super.save(workOvertime);
					return sign2;
				}
			}
			workOvertime.getAct().setFlag("yes");
			updateInfo(workOvertime);
		}
		String comment = "";
		if (workOvertime.getStatus().equals("4")){
			comment = ("yes".equals(workOvertime.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(workOvertime.getAct().getFlag())?"[同意] ":"[驳回] ")+workOvertime.getAct().getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workOvertime.setStatus(("yes".equals(workOvertime.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(workOvertime.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(workOvertime.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workovertime")) {
			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(workOvertime.getAct().getFlag())) {
						workOvertime.setStatus("4");
						workActivityProcess.setIsApproval("2");
						String returnBack = "-1";
						for (Activity activity : activities) {
							if (activity.getCount() == activityProcess.getCount()) {
								returnBack = activity.getReturnBack();
								notifyRole = activity.getName();
								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(workOvertime.getAct().getFlag())) {
						workOvertime.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("workovertime");
			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 ("bmzr".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(workOvertime.getAct().getFlag())) {
						notifyRole = "审批通过";
						workActivityProcess.setIsApproval("1");
					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("modifyApply".equals(taskDefKey)&& count.contains("0")) {
					taskCount = "0";
					notifyRole = "部门负责人审批";
					exp = "pass";
					workActivityProcess.setCount(0);
					enname = "bmzr";
					if (!"yes".equals(workOvertime.getAct().getFlag())) {
						workOvertime.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
		// 设置意见
		workOvertime.getAct().setComment(("yes".equals(workOvertime.getAct().getFlag()) ? "[同意] " : "[驳回] ") + workOvertime.getAct().getComment());
		workOvertime.preUpdate();
		// 提交流程任务
		vars.put(exp, "yes".equals(workOvertime.getAct().getFlag()) ? true : false);
		vars.put("passs", true);
		workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workOvertime.getProcessInstanceId(),taskDefKey,"modifyApply",workOvertime.getAct().getFlag(),comment, activities);
		// 提交流程任务
		actTaskService.complete(workOvertime.getAct().getTaskId(), workOvertime.getAct().getProcInsId(), workOvertime.getAct().getComment(), vars);
		boolean state = actTaskService.isProcessEnd(workOvertime.getAct().getProcInsId());
		List<User> users = new ArrayList<>();
		List<User> userList = new ArrayList<>();
		if (!state) {
			users.add(workOvertime.getCreateBy());
			if ("yes".equals(workOvertime.getAct().getFlag())) {
				workOvertime.setStatus("5");
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workOvertime.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				workProjectNotifyService
						.save(UtilNotify
								.saveNotify(workOvertime.getId(),
										workOvertime.getCreateBy(),
										workOvertime.getCompanyId(),
										title,
										str,
										"77",
										"0",
										"待通知",
										notifyRole));

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workOvertime.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(workOvertime.getStatus()) && !workOvertime.getStatus().equals("3")){
					workOvertime.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workOvertime.getId(),
											workOvertime.getCreateBy(),
											workOvertime.getCompanyId(),
											title,
											str,
											"77",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workOvertime.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workovertime")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workOvertime.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				//users.addAll(userList);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workOvertime.getId(),
								new User(),
								workOvertime.getCompanyId(),
								title,
								str,
								"77",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workOvertime.getAct().getFlag(),
						taskCount,
						workOvertime.getCreateBy(),
						workOvertime.getOfficeId(),
						"19");
				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","77");
						UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

					}
				}

			} else {
				if (!"yes".equals(workOvertime.getAct().getFlag())) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workOvertime.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workOvertime.getId(),
											workOvertime.getCreateBy(),
											workOvertime.getCompanyId(),
											title,
											str,
											"77",
											"0",
											"重新申请",
											notifyRole));
					users.add( workOvertime.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workOvertime.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList1);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workOvertime.getId(),
										new User(),
										workOvertime.getCompanyId(),
										title,
										str,
										"77",
										"0",
										"待审批",
										notifyRole);
						users.addAll(auditUsers);
						for (User user1:auditUsers){
							workProjectNotify.setId("");
							workProjectNotify.setUser(user1);
							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","77");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());

							}
						}
					}else {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workOvertime.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.addAll(userList);
						users.add(workOvertime.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workOvertime.getId(),
												workOvertime.getCreateBy(),
												workOvertime.getCompanyId(),
												title,
												str,
												"77",
												"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());
			}
		}

		workOvertimeDao.update(workOvertime);
		return "保存审核意见成功！";
	}

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

	/**
	 * 强制撤销
	 * @param
	 */
	@Transactional(readOnly = false)
	public void cancelInvalidate(WorkOvertime workOvertime) {
		String invalidateProcessInstanceId = workOvertime.getProcessInstanceId();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workOvertime.getProcessInstanceId());
			process.setIsApproval("0");
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workOvertime.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workOvertime.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()+",归档编号:"+workContractRecord.getRecordNum()+" 强制撤销!\"," +
								"\"useType\":\"sys\"}";
						ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
					}*/
					UserUtils.pushIm(u.getId(),"加班申请人:"+ user.getName()+",加班申请编号:"+workOvertime.getNum()+" 强制撤销!");

				}
			}
			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.setDelFlag("1");
						p.setIsApproval("-1");
						workActivityProcessDao.updateDelFlagAndIsApproval(p);
					}
				}
				WorkActivityProcess pro = new WorkActivityProcess();
				pro.setId("");
				pro.preInsert();
				pro.setDelFlag("0");
				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,"加班申请-强制撤销");
			workOvertime.setStatus("3");
			workOvertime.preUpdate();
			workOvertimeDao.update(workOvertime);

		}catch (ActivitiObjectNotFoundException e){
			System.err.println("撤销异常，因为这个流程已不存在!");
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}