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

import java.util.*;

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.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportData;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.entity.Workattachment;
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.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workprojectnotify.util.UtilNotify;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
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.projectcontentinfo.entity.ProjectReportRecord;
import com.jeeplus.modules.projectcontentinfo.dao.ProjectReportRecordDao;

/**
 * 报告归档归档Service
 * @author 杨帆
 * @version 2018-06-22
 */
@Service
@Transactional(readOnly = true)
public class ProjectReportRecordService extends CrudService<ProjectReportRecordDao, ProjectReportRecord> {
	@Autowired
	private WorkattachmentDao workattachmentDao;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	protected TaskService taskService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;
	@Autowired
	private ProjectReportDataService projectReportDataService;
	public ProjectReportRecord get(String id) {
		ProjectReportRecord projectReportRecord= super.get(id);
		if (projectReportRecord!=null && StringUtils.isNotBlank(projectReportRecord.getId())){
			Workattachment workattachment = new Workattachment();
			workattachment.setAttachmentId(id);
			workattachment.setAttachmentFlag("99");
			projectReportRecord.setWorkAttachments(workattachmentDao.findList(workattachment));
		}
		if (projectReportRecord.getSignCostOne()!=null && StringUtils.isNotBlank(projectReportRecord.getSignCostOne().getId())){
			User signCostOne = UserUtils.get(projectReportRecord.getSignCostOne().getId());
			projectReportRecord.setSignCostOne(signCostOne);
		}
		if (projectReportRecord.getSignCostTwo()!=null && StringUtils.isNotBlank(projectReportRecord.getSignCostTwo().getId())){
			User signCostTwo = UserUtils.get(projectReportRecord.getSignCostTwo().getId());
			projectReportRecord.setSignCostTwo(signCostTwo);
		}
		return super.get(id);
	}
	public int getNumber(String reportId) {
		String number = dao.getNumber(reportId);
		if (StringUtils.isNotBlank(number)){
			return Integer.parseInt(number)+1;
		}else {
			return 1;
		}
	}

	public List<ProjectReportRecord> findList(ProjectReportRecord projectReportRecord) {
		return super.findList(projectReportRecord);
	}
	
	public Page<ProjectReportRecord> findPage(Page<ProjectReportRecord> page, ProjectReportRecord projectReportRecord) {
		projectReportRecord.getSqlMap().put("dsf", dataScopeFilter(projectReportRecord.getCurrentUser(), "o", "u","s", MenuStatusEnum.PROJECT_REPORTRECORD.getValue()));
		return super.findPage(page, projectReportRecord);
	}
	
	@Transactional(readOnly = false)
	public void save(ProjectReportRecord projectReportRecord) {
		ProjectReportData projectReportData =projectReportRecord.getReport();
		projectReportData.setFileStatus("2");
		projectReportDataService.updateFileStatus(projectReportData);
		if (projectReportRecord.getIsNewRecord()){
			projectReportRecord.preInsert();
			projectReportRecord.setCreateDate(new Date());
			dao.insert(projectReportRecord);
		}else{
			projectReportRecord.preUpdate();
			dao.update(projectReportRecord);
		}
		saveWorkattachments(projectReportRecord);
	}
	@Transactional(readOnly = false)
	public void insert(ProjectReportRecord projectReportRecord) {
		dao.insert(projectReportRecord);
	}
	@Transactional(readOnly = false)
	public void saveWorkattachments(ProjectReportRecord projectReportRecord) {
		List<Workattachment> workattachments = projectReportRecord.getWorkAttachments();
		if (workattachments!=null && workattachments.size()!=0){
			for (Workattachment workattachment:workattachments){
				if (workattachment.getId() == null) {
					continue;
				}
				if (workattachment.DEL_FLAG_NORMAL.equals(workattachment.getDelFlag())) {
					workattachment.setAttachmentId(projectReportRecord.getId());
					workattachment.setAttachmentFlag("99");
					workattachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workattachment.getId()) || "null".equals(workattachment.getId())) {
						workattachment.preInsert();
						workattachmentDao.insert(workattachment);
					} else {
						workattachment.preUpdate();
						workattachmentDao.update(workattachment);
					}
				} else {
					workattachmentDao.delete(workattachment);
				}
			}
		}

	}

	/**
	 * 提交存储
	 * @param projectReportRecord
	 * @param variables
	 */
	@Transactional(readOnly = false)
	public String save(ProjectReportRecord projectReportRecord, Map<String, Object> variables, String processInstanceId) {

		User user = UserUtils.getUser();
		Boolean insert = false;
		if (StringUtils.isBlank(projectReportRecord.getId())){
			projectReportRecord.setCompanyId(user.getComId());
			projectReportRecord.setOfficeId(user.getOffice().getId());
			projectReportRecord.preInsert();
			insert = true;
		}
		saveWorkattachments(projectReportRecord);
		String title =  "报告名称："+projectReportRecord.getReport().getName();
		String str = "归档申请编号:"+projectReportRecord.getRecordNum()+ ",主委托方:"+projectReportRecord.getClientName()+",申请人:"+user.getName()+",所属部门:"+UserUtils.getSelectOffice().getName();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		// 启动流程
		String businessKey = projectReportRecord.getId().toString();
		Office office = UserUtils.getSelectOffice();
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("5864872f45b84acd893010e66a3db2c8", office);
		// 启动流程
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(projectReportRecord.getId(),
						null,
						projectReportRecord.getCompanyId(),
						title,
						str,
						"51",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		if (insert) {
			this.insert(projectReportRecord);
		}else {
			this.save(projectReportRecord);
		}
		List<User> bggdglys = UserUtils.getByRoleActivityEnname("bggdgly",3,office.getId(),"10",projectReportRecord.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(),"10",projectReportRecord.getCreateBy());
					if (enusers.size()==0){
						projectReportRecord.setStatus("1");
						this.save(projectReportRecord);
						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(),"10",projectReportRecord.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
			variables.put("bggdglyList", bggdglys);
			if (bggdglys.size()==0){
				projectReportRecord.setStatus("1");
				this.save(projectReportRecord);
				return "流程审批人不能为空,报告归档管理员下无用户,请联系管理员！";
			}
			variables.put("bggdglycount",bggdglys.size());
			processType = "reportRecord";
			users.addAll(bggdglys);
		}
		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","51");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);

				/*List<WebSocket> toUserConns = ChatServerPool.getWebSocketByUser(u.getId());
				for (WebSocket toUserConn:toUserConns) {
					String message = "{\"to\":\""+u.getId()+"\"," +
							"\"msg\":\"审批信息 \"," +
							"\"useType\":\"sys\"}";
					ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
				}*/
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", "报告归档编号：" + projectReportRecord.getRecordNum());//设置标题；

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);

		projectReportRecord.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		ProjectReportData projectReportData =projectReportRecord.getReport();
		projectReportData.setFileStatus("2");
		projectReportDataService.updateFileStatus(projectReportData);
		// 更新流程实例ID
		projectReportRecord.setProcessInstanceId(processInstance.getId());
		dao.updateProcessInstanceId(projectReportRecord);
		List<Activity> list = workActivityMenu.getActivities();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessKey(processType);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcessService.insert(workActivityProcess);
			workActivityProcessService.insertAuditsByType(bggdglys,processInstance.getId(),1,1);
		}
		return "true";
	}

	/**
	 * 报告：强制撤销
	 * @param projectReportRecord
	 */
	@Transactional(readOnly = false)
	public void 	cancelInvalidate(ProjectReportRecord projectReportRecord){
		String invalidateProcessInstanceId = projectReportRecord.getProcessInstanceId();
		//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
		WorkActivityProcess process = new WorkActivityProcess();
		process.setProcessInstanceId(projectReportRecord.getProcessInstanceId());
		process.setIsApproval("0");
		WorkActivityProcess workActivityProcess = new WorkActivityProcess();
		workActivityProcess.setProcessInstanceId(projectReportRecord.getProcessInstanceId());
		List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(projectReportRecord.getId());
		List<User> userList = workProjectNotifyService.readByNotifyId(notify);
		if (userList!=null && userList.size()!=0) {
			for (User u : userList) {
				User user = UserUtils.get(u.getId());
				UserUtils.pushMeIm(user.getId());
			}
		}
		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.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);
		}

		//结束该流程,设为"撤销"状态
		try {
			actTaskService.endProcessInstance(invalidateProcessInstanceId,"报告归档-强制撤销");
		} catch (Exception e) {
			e.printStackTrace();
		}
		projectReportRecord.setStatus("3");
		projectReportRecord.preUpdate();
		save(projectReportRecord);

	}

	/**
	 * 审核流程
	 * @param reportRecord
	 */
	@Transactional(readOnly = false)
	public String auditSave(ProjectReportRecord reportRecord,List<User> auditUsers) {
		String flag = reportRecord.getAct().getFlag();
		ProjectReportRecord projectReportRecord = null;
		// 对不同环节的业务逻辑进行操作
		Act act = reportRecord.getAct();
		String taskDefKey = act.getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey) && !taskDefKey.contains("audit")  ) {
			actTaskService.claim(act.getTaskId(), UserUtils.getUser().getId());
			projectReportRecord = get(reportRecord.getId());
		}else {
			projectReportRecord = reportRecord;
			saveWorkattachments(reportRecord);
			flag = "yes";
		}
		User user= UserUtils.get(projectReportRecord.getCreateBy().getId());
		//String str = "报告归档发起人："+user.getName()+",报告归档编号:"+projectReportRecord.getRecordNum()+",报告名称："+projectReportRecord.getReport().getName();
		String title =  "报告名称："+projectReportRecord.getReport().getName();
		String str = "归档申请编号:"+projectReportRecord.getRecordNum()+ ",主委托方:"+projectReportRecord.getClientName()+",申请人:"+user.getName()+",所属部门:"+UserUtils.getSelectOffice().getName();

		if (StringUtils.isBlank(act.getComment())){
			act.setComment(flag.equals("yes")?"同意":"驳回");
		}
		String comment = "";
		if (projectReportRecord.getStatus().equals("4")){
			comment = ("yes".equals(flag)?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(flag)?"[同意] ":"[驳回] ")+act.getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		projectReportRecord.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(projectReportRecord.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(projectReportRecord.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("reportRecord")) {
			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(flag)) {
						projectReportRecord.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")){
					taskCount = "0";
					notifyRole = "调整报告";
					exp = "pass";
					workActivityProcess.setId("");
					workActivityProcess.setCount(0);
					if (!"yes".equals(flag)) {
						projectReportRecord.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("reportRecord");
			for (int i = 0; i < workActivityProcesses.size(); i++) {
				WorkActivityProcess activityProcess = workActivityProcesses.get(i);
				workActivityProcess = activityProcess;
				String count = activityProcess.getCount() + "";
				if (!workActivityProcess.getIsApproval().equals("0")) {
					workActivityProcess.setId("");
				}
				// 审核环节
				if ("bggdgly".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(flag)) {
						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 = "bggdgly";
					if (!"yes".equals(flag)) {
						projectReportRecord.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
		// 设置意见
		act.setComment(("yes".equals(flag) ? "[同意] " : "[驳回] ") + act.getComment());
		projectReportRecord.preUpdate();
		// 提交流程任务
		vars.put(exp, "yes".equals(flag) ? true : false);
		vars.put("passs", true);
		workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,projectReportRecord.getProcessInstanceId(),taskDefKey,"modifyApply",flag,comment, activities);
		// 提交流程任务
		actTaskService.complete(act.getTaskId(), act.getProcInsId(), act.getComment(), vars);
		boolean state = actTaskService.isProcessEnd(act.getProcInsId());
		List<User> users = new ArrayList<>();
		List<User> userList = new ArrayList<>();
		if (!state) {
//            str = "yes".equals(flag)?"您的报销单审核：已通过":"您的报销单审核：未通过";
			users.add(projectReportRecord.getCreateBy());
			if ("yes".equals(flag)) {
				ProjectReportData projectReportData =projectReportRecord.getReport();
				projectReportData.setFileStatus("3");
				projectReportDataService.updateFileStatus(projectReportData);
				projectReportRecord.setStatus("5");
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(projectReportRecord.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				String str2 = "归档申请编号:"+projectReportRecord.getRecordNum()+ ",主委托方:"+projectReportRecord.getClientName();
				workProjectNotifyService
						.save(UtilNotify
								.saveNotify(projectReportRecord.getId(),
										projectReportRecord.getCreateBy(),
										projectReportRecord.getCompanyId(),
										title,
										str2,
										"51",
										"0",
										"待通知",
										notifyRole));

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(projectReportRecord.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(projectReportRecord.getStatus()) && !projectReportRecord.getStatus().equals("3")){
					projectReportRecord.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(projectReportRecord.getId(),
											projectReportRecord.getCreateBy(),
											projectReportRecord.getCompanyId(),
											title,
											str,
											"51",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(projectReportRecord.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("reportRecord")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(projectReportRecord.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(projectReportRecord.getId(),
								new User(),
								projectReportRecord.getCompanyId(),
								title,
								str,
								"51",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						flag,
						taskCount,
						projectReportRecord.getCreateBy(),
						user.getOffice().getId(),
						"10");
				for (WorkProjectNotify workProjectNotify1:workProjectNotifys){
					users.add(workProjectNotify1.getUser());
					workProjectNotify1.setId("");
					workProjectNotify1.setIsNewRecord(false);
					workProjectNotifyService
							.save(workProjectNotify1);
				}

			} else {
				if (!"yes".equals(flag)) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(projectReportRecord.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(projectReportRecord.getId(),
											projectReportRecord.getCreateBy(),
											projectReportRecord.getCompanyId(),
											title,
											str,
											"51",
											"0",
											"重新申请",
											notifyRole));
					users.add( projectReportRecord.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(projectReportRecord.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(projectReportRecord.getId(),
										new User(),
										projectReportRecord.getCompanyId(),
										title,
										str,
										"51",
										"0",
										"待审批",
										notifyRole);
						users.addAll(auditUsers);
						for (User user1 : users) {
							workProjectNotify.setUser(user1);
							workProjectNotify.setId("");
							workProjectNotify.setIsNewRecord(false);
							workProjectNotifyService
									.save(workProjectNotify);
						}
					}else {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(projectReportRecord.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.add(projectReportRecord.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(projectReportRecord.getId(),
												projectReportRecord.getCreateBy(),
												projectReportRecord.getCompanyId(),
												title,
												str,
												"51",
												"0",
												"重新申请",
												notifyRole));
					}
				}
			}
		}
		if (users!=null && users.size()!=0) {
			for (User u : users) {
				UserUtils.pushIm(u.getId(),title);
			}
		}
		if (users!=null && userList.size()!=0) {
			for (User u : userList) {
				UserUtils.pushMeIm(u.getId());
			}
		}
		dao.update(projectReportRecord);
		return "保存审核意见成功！";
	}


	@Transactional(readOnly = false)
	public void delete(ProjectReportRecord reportRecord) {
		ProjectReportRecord projectReportRecord = get(reportRecord.getId());
		if (projectReportRecord.getWorkAttachments() != null && projectReportRecord.getWorkAttachments().size() != 0) {
			for (Workattachment workattachment : projectReportRecord.getWorkAttachments()) {
				workattachmentDao.delete(workattachment);
			}
		}
		if (StringUtils.isNotBlank(projectReportRecord.getProcessInstanceId())){
			workActivityProcessService.deleteProcessInstanceId(projectReportRecord.getProcessInstanceId());
		}
		ProjectReportData reportData = projectReportDataService.get(projectReportRecord.getReport().getId());
		if ("2".equals(reportData.getFileStatus())){
			reportData.setFileStatus("1");
			projectReportDataService.updateFileStatus(reportData);
		}
		super.delete(projectReportRecord);
	}

	public Act getByAct(String processInstanceId){
		Act act = new Act();
		ProcessInstance processInstance = actTaskService.getProcIns(processInstanceId);
		if (processInstance!=null) {
			List<Task> taskList = actTaskService.getCurrentTaskList(processInstance);
			if(taskList!=null && taskList.size()>1){
				for (Task taskInfok:taskList) {
					if (taskInfok.getAssignee().equals(UserUtils.getUser().getId())) {
						act.setTaskId(taskInfok.getId());
						act.setTaskDefKey(taskInfok.getTaskDefinitionKey());
						act.setTaskName(taskInfok.getName());
						act.setProcInsId(taskInfok.getProcessInstanceId());
						act.setProcDefId(taskInfok.getProcessDefinitionId());
						act.setTask(taskInfok);
					}
				}
			}else {
				Task task = actTaskService.getCurrentTaskInfo(processInstance);
				act.setTaskId(task.getId());
				act.setTaskDefKey(task.getTaskDefinitionKey());
				act.setTaskName(task.getName());
				act.setProcInsId(task.getProcessInstanceId());
				act.setProcDefId(task.getProcessDefinitionId());
				act.setTask(task);
			}
		}
		return  act;
	}
	
	
	
}