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

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

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.act.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.contractclient.entity.WorkContractClient;
import com.jeeplus.modules.isignature.service.ISignatureDocumentService;
import com.jeeplus.modules.leaveapply.entity.LeaveApply;
import com.jeeplus.modules.leaveapply.entity.LeaveDetail;
import com.jeeplus.modules.leavetype.entity.LeaveType;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.dao.UserDao;
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.service.SystemService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.dao.UserofficeDao;
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.workattendance.entity.WorkAttendanceInfo;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceRule;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workcontractinfo.dao.WorkContractInfoDao;
import com.jeeplus.modules.workcontractinfo.entity.WorkContractInfo;
import com.jeeplus.modules.workcontractinfo.service.WorkContractInfoService;
import com.jeeplus.modules.workofficechange.dao.WorkOfficeChangeUserDao;
import com.jeeplus.modules.workofficechange.entity.WorkOfficeChange;
import com.jeeplus.modules.workofficechange.service.WorkOfficeChangeUserService;
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.WorkStaffBasicInfoDao;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.activiti.engine.HistoryService;
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.workcontractsignature.entity.WorkContractSignature;
import com.jeeplus.modules.workcontractsignature.dao.WorkContractSignatureDao;

/**
 * 合同签章Service
 * @author ssrh
 * @version 2018-10-26
 */
@Service
@Transactional(readOnly = true)
public class WorkContractSignatureService extends CrudService<WorkContractSignatureDao, WorkContractSignature> {
	@Autowired
	private UserDao userDao;
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;
	@Autowired
	private SerialNumTplService serialNumTplService;
	@Autowired
	protected WorkOfficeChangeUserDao workOfficeChangeUserDao;
	@Autowired
	protected WorkStaffBasicInfoDao workStaffBasicInfoDao;
	@Autowired
	protected WorkOfficeChangeUserService workOfficeChangeUserService;
	@Autowired
	protected SystemService systemService;
	@Autowired
	protected UserofficeDao userofficeDao;
	@Autowired
	protected RoleDao roleDao;
	@Autowired
	protected WorkContractInfoDao workContractInfoDao;
	@Autowired
	protected OfficeService officeService;
	@Autowired
	protected WorkContractInfoService workContractInfoService;
	@Autowired
	protected WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private ISignatureDocumentService signatureDocumentService;

	public WorkContractSignature get(String id) {
		WorkContractSignature workContractSignature=super.get(id);
		if(workContractSignature!=null){
			WorkContractInfo workContractInfo=workContractInfoService.get(workContractSignature.getWorkContractInfo().getId());
			workContractSignature.setWorkContractInfo(workContractInfo);
			workContractSignature.setWorkAttachments(workContractInfo.getWorkAttachments());
		}
		return workContractSignature;
	}
	
	public List<WorkContractSignature> findList(WorkContractSignature workContractSignature) {
		return super.findList(workContractSignature);
	}
	
	public Page<WorkContractSignature> findPage(Page<WorkContractSignature> page, WorkContractSignature workContractSignature) {
//		workContractSignature.getSqlMap().put("dsf", dataScopeFilter(workContractSignature.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_CONTRACT_INFO.getValue()));
		String companyId = UserUtils.getUser().getComId();
		workContractSignature.getSqlMap().put("dsf","and (a.company_id='"+companyId+"')");
		workContractSignature.setPage(page);
		page.setList(this.findList( workContractSignature));
		return page;
	}
	@Transactional(readOnly = false)
	public String saveChange(WorkContractSignature workContractSignature) {
		this.save(workContractSignature);
		String processInstanceId=workContractSignature.getProcessInstanceId();
		WorkContractInfo workContractInfo=workContractInfoDao.get(workContractSignature.getWorkContractInfo().getId());
		workContractInfo.setContractState("11");
		workContractInfoService.save(workContractInfo);
		try{
			Map<String, Object> variables = new HashMap<String, Object>();
			identityService.setAuthenticatedUserId(workContractSignature.getCurrentUser().getId());
			User createUser = UserUtils.getUser();
			String contentStr = "合同编号："+ workContractInfo.getContractNum()
					+"，所属部门："+ workContractInfo.getChargeCompanyName()
					+"，主委托方："+ workContractInfo.getClient().getName()
					+"，签章类型："+ workContractSignature.getType();
			String titleStr = "合同名称："+ workContractInfo.getName();
			String businessKey = workContractSignature.getId();
			Office office = officeService.get(workContractInfo.getChargeCompany());
			WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("1663125f1f194c82bdea93555c7544204", office);
			// 启动流程
			String processType = workActivityMenu.getProcessType();
			StringBuffer buffer = new StringBuffer();
			Activity activity = new Activity();
			WorkProjectNotify workProjectNotify = UtilNotify
					.saveNotify(workContractSignature.getId(),
							null,
							workContractSignature.getCompanyId(),
							titleStr,
							contentStr,
							"91",
							"0",
							"待审批",
							""
					);

			List<User> users = new ArrayList<>();
			List<User>  bmzrs = UserUtils.getByRoleActivityEnname("bmzr",2,office.getId(),"2",workContractSignature.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(),"2",workContractSignature.getCreateBy());
						if (enusers.size()==0){
							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(),"2",workContractSignature.getCreateBy());
					}
					//人员审批
					if (StringUtils.isNotBlank(activity.getUser().getId())) {
						users.add(activity.getUser());
					}
				}
				workProjectNotify.setId("");
			} else {
				if (bmzrs.size()==0 ){
					return "流程审批人不能为空,请联系管理员！";
				}
				processType = "contractSign";
				variables.put("bmzrList", bmzrs);
				variables.put("bmzrCount",bmzrs.size());
				users.addAll(bmzrs);
			}
			List<String> userIds = new ArrayList<>(users.size());
			for (User u : users){
				userIds.add(u.getId());
				workProjectNotify.setUser(u);
				workProjectNotify.setId("");
				workProjectNotify.setNotifyRole("盖章人审核");
				workProjectNotifyService.save(workProjectNotify);
				Map<String,Object> extras = new HashMap<>();
				extras.put("type","7001");
				extras.put("procDefKey","91");
				extras.put("id",workProjectNotify.getId());
				UserUtils.pushInfoToApp(titleStr, contentStr,extras,u.getId());
			}
			UserUtils.pushIm(userIds,contentStr);

			variables.put("busId", businessKey);
			variables.put("type", processType);
			variables.put("title", "审批单：" +"合同名称："+workContractInfo.getName());//设置标题；

			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
			if (org.apache.commons.lang3.StringUtils.isNotBlank(processInstanceId)) {
				workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
				workActivityProcessService.deleteProcessInstanceId(processInstanceId);
				workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
			}
			List<Activity> list = workActivityMenu.getActivities();
			if (list != null && list.size() != 0) {
				workActivityProcessService.saveList(list, processInstance.getId());
			} else {
				WorkActivityProcess workActivityProcess = new WorkActivityProcess();
				workActivityProcess.setProcessKey(processType);
				workActivityProcess.setCount(1);
				workActivityProcess.setProcessInstanceId(processInstance.getId());
				workActivityProcess.setIsApproval("0");
				workActivityProcessService.insert(workActivityProcess);
				workActivityProcessService.insertAuditsByType(bmzrs,processInstance.getId(),1,1);
			}
			workContractSignature.setProcessInstanceId(processInstance.getId());
			workContractSignature.setStatus("2");
			dao.updateProcessInstanceId(workContractSignature);
		}catch (Exception e){
			logger.error("启动审批流程异常：",e);
			return "启动审批流程异常:"+e.getMessage();
		}
		return"";
	}
	@Transactional(readOnly = false)
	public void save(WorkContractSignature workContractSignature) {
		super.save(workContractSignature);
		//附件信息
		updateWorkContractInfo(workContractSignature);

	}
	//附件信息
	@Transactional(readOnly = false)
	public void updateWorkContractInfo(WorkContractSignature workContractSignature) {
		if(workContractSignature.getWorkAttachments()!=null&&workContractSignature.getWorkAttachments().size()>0){
			for (WorkClientAttachment workClientAttachment : workContractSignature.getWorkAttachments()) {
				if (workClientAttachment.getId() == null) {
					continue;
				}
				if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())) {
					workClientAttachment.setAttachmentId(workContractSignature.getWorkContractInfo().getId());
					workClientAttachment.setAttachmentFlag("44");
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
						workClientAttachment.preInsert();
						workClientAttachmentDao.insert(workClientAttachment);
						signatureDocumentService.createIsignature(workClientAttachment.getUrl(),workClientAttachment.getAttachmentName(),workClientAttachment.getId(),workContractSignature.getWorkContractInfo().getName());
					} else {
						workClientAttachment.preUpdate();
						workClientAttachmentDao.update(workClientAttachment);
					}
				} else {
					workClientAttachmentDao.delete(workClientAttachment);
				}
			}
		}

	}
	@Transactional(readOnly = false)
	public void delete(WorkContractSignature workContractSignature) {
		WorkContractInfo workContractInfo=workContractInfoDao.get(workContractSignature.getWorkContractInfo().getId());
		workContractInfo.setContractState("5");
		workContractInfoService.save(workContractInfo);
		super.delete(workContractSignature);
	}

	@Transactional(readOnly = false)
	public void cancelProcess(String id) throws Exception {
		WorkContractSignature workContractSignature = this.get(id);
		WorkActivityProcess process = new WorkActivityProcess();
		process.setProcessInstanceId(workContractSignature.getProcessInstanceId());
		process.setIsApproval("0");
		WorkActivityProcess workActivityProcess = new WorkActivityProcess();
		workActivityProcess.setProcessInstanceId(workContractSignature.getProcessInstanceId());
		List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workContractSignature.getId());
		List<User> userList = workProjectNotifyService.readByNotifyId(notify);
		if (userList!=null && userList.size()!=0) {
			for (User u : userList) {
				User user = UserUtils.get(u.getId());
				UserUtils.pushIm(u.getId(),"合同名称:"+ workContractSignature.getWorkContractInfo().getName() +" 强制撤销!");
			}
		}
		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);

			//结束该流程,设为"撤销"状态月
			WorkContractSignature contractSignature = new WorkContractSignature();
			contractSignature.setId(id);
			contractSignature.setStatus("3");
			contractSignature.setProcessInstanceId(workContractSignature.getProcessInstanceId());
			contractSignature.preUpdate();
			dao.updateProcessInstanceId(contractSignature);
			actTaskService.endProcessInstance(contractSignature.getProcessInstanceId(), "合同签章申请-撤回");
		}
	}
	@Transactional(readOnly = false)
	public String auditSave(WorkContractSignature workContractSignature, List<User> auditUsers) {
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workContractSignature.getAct().getTaskDefKey();
		String flag = workContractSignature.getAct().getFlag();
		Act act = workContractSignature.getAct();
		WorkContractInfo workContractInfo=workContractInfoDao.get(workContractSignature.getWorkContractInfo().getId());
		if (!"modifyApply".equals(taskDefKey) && !taskDefKey.contains("audit")) {
			actTaskService.claim(workContractSignature.getAct().getTaskId(), UserUtils.getUser().getId());
		} else {
			workContractSignature.getAct().setFlag("yes");
			updateWorkContractInfo(workContractSignature);
		}
		String comment = "";
		String notifyTitle = "合同名称:"+workContractInfo.getName();
		String str =  "合同编号:"+workContractInfo.getContractNum()+"主委托方:"+workContractInfo.getClient().getName();

		if (StringUtils.isBlank(act.getComment())){
			act.setComment(flag.equals("yes")?"同意":"驳回");
		}
		if (workContractSignature.getStatus().equals("4")){
			comment = ("yes".equals(flag)?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(flag)?"[同意] ":"[驳回] ")+act.getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workContractSignature.setStatus(("yes".equals(flag) ? "2" : "4"));
		Map<String, Object> vars = Maps.newHashMap();
		//业务逻辑对应的条件表达式
		String exp = "";
		String taskCount = "";
		String notifyRole = "";
		int key = 0;
		String enname = "";
		List<Activity> activitieList = activityService.getByProcessInstanceId(workContractSignature.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(workContractSignature.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("contractSign")) {
			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(workContractSignature.getAct().getFlag())) {
						workContractSignature.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(workContractSignature.getAct().getFlag())) {
						workContractSignature.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("contractSign");
			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(workContractSignature.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 = "bmzr";
					if (!"yes".equals(workContractSignature.getAct().getFlag())) {
						workContractSignature.setStatus("3");
					}
					break;
				}

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

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workContractSignature.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (!"3".equals(workContractSignature.getStatus())) {
					workContractSignature.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workContractSignature.getId(),
											workContractSignature.getCreateBy(),
											workContractSignature.getCompanyId(),
											notifyTitle,
											str,
											"91",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workContractSignature.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("contractSign")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workContractSignature.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				//users.addAll(userList);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workContractSignature.getId(),
								new User(),
								workContractSignature.getCompanyId(),
								notifyTitle,
								str,
								"91",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workContractSignature.getAct().getFlag(),
						taskCount,
						workContractSignature.getCreateBy(),
						workContractSignature.getOfficeId(),
						"91");
				for (WorkProjectNotify workProjectNotify1 : workProjectNotifys) {
					users.add(workProjectNotify1.getUser());
					workProjectNotify1.setId("");
					workProjectNotify1.setIsNewRecord(false);
					workProjectNotifyService
							.save(workProjectNotify1);
				}

			} else {
				if (!"yes".equals(workContractSignature.getAct().getFlag())) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workContractSignature.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workContractSignature.getId(),
											workContractSignature.getCreateBy(),
											workContractSignature.getCompanyId(),
											notifyTitle,
											str,
											"91",
											"0",
											"重新申请",
											notifyRole));
					users.add(workContractSignature.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workContractSignature.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList1);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workContractSignature.getId(),
										new User(),
										workContractSignature.getCompanyId(),
										notifyTitle,
										str,
										"91",
										"0",
										"待审批",
										notifyRole);
						users.addAll(auditUsers);
						for (User user1 : auditUsers) {
							workProjectNotify.setUser(user1);
							workProjectNotify.setId("");
							workProjectNotify.setIsNewRecord(false);
							workProjectNotifyService.save(workProjectNotify);
							Map<String, Object> extras = new HashMap<>();
							extras.put("type", "7002");
							extras.put("procDefKey", "91");
							extras.put("id", workProjectNotify.getId());
							UserUtils.pushInfoToApp(notifyTitle, str, extras, user1.getId());
						}
					} else {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workContractSignature.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.addAll(userList);
						users.add(workContractSignature.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workContractSignature.getId(),
												workContractSignature.getCreateBy(),
												workContractSignature.getCompanyId(),
												notifyTitle,
												str,
												"91",
												"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());
			}
		}
		dao.update(workContractSignature);
		return "保存审核意见成功！";
	}
	
}