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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.DateUtils;
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.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
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.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.workfullmanage.dao.WorkFullManageDao;
import com.jeeplus.modules.workfullmanage.entity.WorkFullManage;
import com.jeeplus.modules.workfullmanage.service.WorkFullManageService;
import com.jeeplus.modules.workfullmeetingminutes.dao.WorkFullMeetingminutesDao;
import com.jeeplus.modules.workfullmeetingminutes.entity.WorkFullMeetingminutes;
import com.jeeplus.modules.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 会议纪要Service
 * @author fgy
 * @version 2017-11-02
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true)
public class WorkFullMeetingminutesService extends CrudService<WorkFullMeetingminutesDao, WorkFullMeetingminutes> {

	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private WorkFullManageService workFullManageService ;
	@Autowired
	private WorkFullMeetingminutesDao workFullMeetingminutesDao;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private WorkFullManageDao workFullManageDao;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private ActivityService activityService;

	private Lock lock = new ReentrantLock();

	public WorkFullMeetingminutes get(String id) {
		WorkFullMeetingminutes workFullMeetingminutes = super.get(id);
		if(workFullMeetingminutes != null){
			WorkFullManage workFullManage = workFullManageService.get(workFullMeetingminutes.getWorkFullManageId());
			if(workFullManage != null){
				workFullMeetingminutes.setWorkFullManage(workFullManage);
				workFullMeetingminutes.setWorkFullManageId(workFullManage.getId());
			}
			List<User> membersList = new ArrayList<>();
			StringBuilder names = new StringBuilder();
			String meetingMembers = workFullMeetingminutes.getMeetingMembers();
			if(StringUtils.isNotBlank(meetingMembers)){
				String[] idArray = meetingMembers.split(",");
				if(idArray != null && idArray.length>0){
					for(int i = 0 ;i<idArray.length;i++){
						if(StringUtils.isNotBlank(idArray[i])){
							String idStr = idArray[i];
							User user = UserUtils.get(idStr);
							names.append(",").append(user.getName());
							membersList.add(user);
						}
					}
				}
			}
			String meetingMembersNames = names.toString();
			if(meetingMembersNames.length()>1){
				workFullMeetingminutes.setMeetingMembersNames(names.toString().substring(1));
			}
			workFullMeetingminutes.setMembersList(membersList);

			String meetingRecorderId = workFullMeetingminutes.getMeetingRecorderId();
			if(StringUtils.isNotBlank(meetingRecorderId)){
				User recorder = UserUtils.get(meetingRecorderId);
				workFullMeetingminutes.setMeetingRecorder(recorder);
			}
			String meetingOffice = workFullMeetingminutes.getMeetingOffice();
			Office o = officeService.get(meetingOffice);
			workFullMeetingminutes.setOffice(o);

			//流程结束state=false，未结束state=true
			boolean state = true;
			if(StringUtils.isNotBlank(workFullMeetingminutes.getProcessInstanceId())){
				state = actTaskService.isProcessEnd(workFullMeetingminutes.getProcessInstanceId());
			}else{
				//如果是项目负责人，不用审核，没有流程实例
				state = false;
			}
			workFullMeetingminutes.setProcessEnded(state);
		}
		return workFullMeetingminutes ;
	}

	public List<WorkFullMeetingminutes> findList(WorkFullMeetingminutes workFullMeetingminutes) {
		return super.findList(workFullMeetingminutes);
	}

	public Page<WorkFullMeetingminutes> findPage(Page<WorkFullMeetingminutes> page, WorkFullMeetingminutes workFullMeetingminutes) {
		workFullMeetingminutes.setPage(page);
		page.setList(findListBelongtoWorkfullmanage(workFullMeetingminutes));
		return page;
	}

	//一个项目有多个会议纪要记录
	private List<WorkFullMeetingminutes> findListBelongtoWorkfullmanage(WorkFullMeetingminutes workFullMeetingminutes) {
		if(StringUtils.isBlank(workFullMeetingminutes.getWorkFullManageId())){
			return null ;
		}
		List<WorkFullMeetingminutes> meetingList = workFullMeetingminutesDao.findList(workFullMeetingminutes);
		if(meetingList != null && meetingList.size()>0){
			for(int i=0 ; i< meetingList.size();i++){
				WorkFullMeetingminutes meeting = meetingList.get(i);

				//流程结束state=false，未结束state=true
				boolean state = true;
				if(StringUtils.isNotBlank(meeting.getProcessInstanceId())){
					state = actTaskService.isProcessEnd(meeting.getProcessInstanceId());
				}else{
					//如果是项目负责人，不用审核，没有流程实例
					state = false;
				}
				meeting.setProcessEnded(state);

				String meetingRecorderId = meeting.getMeetingRecorderId();
				if(StringUtils.isNotBlank(meetingRecorderId)){
					meeting.setMeetingRecorder(UserUtils.get(meetingRecorderId));
				}
				StringBuilder builder = new StringBuilder();
				String memberIds = meeting.getMeetingMembers();
				if(StringUtils.isNotBlank(memberIds)){
					String[] idArray = memberIds.split(",");
					if(idArray != null && idArray.length>0){
						for( int j =0 ;j<idArray.length;j++){
							if(StringUtils.isNotBlank(idArray[j])){
								User u = UserUtils.get(idArray[j]);
								if(u!=null){
									String name = u.getName();
									builder.append(",").append(name);
								}
							}
						}
					}
				}
				if(builder.toString().length()>1){
					meeting.setMeetingMembersNames(builder.toString().substring(1));
				}
				String meetingOffice = meeting.getMeetingOffice();
				Office o = officeService.get(meetingOffice);
				meeting.setOffice(o);

			}
		}
		return meetingList;
	}

	@Transactional(readOnly = false)
	public WorkFullMeetingminutes save(WorkFullMeetingminutes workFullMeetingminutes,String workFullManageId,String submit) {
		//(0暂存，1审核中，2已通过，3未通过、4已撤销)

		//提交、暂存、重新申请，执行保存，撤销不执行保存
		if("submit".equals(submit) || "toStore".equals(submit)|| "reapply".equals(submit)){
			if(StringUtils.isBlank(workFullMeetingminutes.getId())){
				workFullMeetingminutes.setStatus("0");
			}
			workFullMeetingminutes.setWorkFullManageId(workFullManageId);
			workFullMeetingminutes.setCompanyId(UserUtils.getSelectCompany().getId());
			workFullMeetingminutes.setOfficeId(UserUtils.getSelectOffice().getId());
			super.save(workFullMeetingminutes);
		}

		//新添加数据：先保存，后启动流程；暂存数据：只保存，不启动流程；
		//重新申请：先保存数据，后执行审核；撤销：不保存，但是执行审核
		if("submit".equals(submit)){
			Map<String,Object> variables = new HashMap<String,Object>();
			startProcess(workFullMeetingminutes,variables);
		}else if("reapply".equals(submit) || "repeal".equals(submit)){
			auditSave(workFullMeetingminutes);
		}
		return workFullMeetingminutes;
	}
	@Transactional(readOnly = false)
	private void startProcess(WorkFullMeetingminutes workFullMeetingminutes, Map<String, Object> variables) {
		System.err.print("================================");
		//用来设置启动流程的人员的id，引擎会自动把人员的id保存到activiti:initiator中
		identityService.setAuthenticatedUserId(workFullMeetingminutes.getCurrentUser().getId());
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("a2d1b3bbff70432890acf20a18b05838",officeService.get(workFullMeetingminutes.getOfficeId()));
		//启动流程
		String businessKey = workFullMeetingminutes.getId().toString();
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer1 = new StringBuffer();
		Activity activity = new Activity();
		if(StringUtils.isNotBlank(workActivityMenu.getId())){
			workActivityMenu = workActivityMenuService.get(workActivityMenu.getId());
			List<Activity> activities = workActivityMenu.getActivities();
			for (Activity a:activities){
				if (a.getDelFlag().equals("0") && a.getCount() == 1){
					activity = a;
					break;
				}
			}
			buffer1.append(activity.getRole().getEnname());
			variables.put("type", workActivityMenu.getProcessType());
		}else{
			processType= ActUtils.PD_WORKFULLMEETINGMINUTES[0];
			variables.put("type",processType);
			Role role = UserUtils.getSelectRole().get(0);
			buffer1 = DateUtils.getByEnnme(role.getEnname());
			buffer1.append("bmzr");
			variables.put("bmzr",buffer1.toString());//部门负责人
		}

		variables.put("businessId",businessKey);
		variables.put("title",workFullMeetingminutes.getRemarks());

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType,businessKey,variables);
		workFullMeetingminutes.setProcessInstance(processInstance);
		workFullMeetingminutes.setProcessInstanceId(processInstance.getId());
		workFullMeetingminutesDao.updateProcessInstanceId(workFullMeetingminutes);

		workFullMeetingminutes.setStatus("1");
		updateStatus(workFullMeetingminutes);

		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");
			workActivityProcess.setActivity(new Activity("9ab1eecb19d4451896c72414423f40e2"));
			workActivityProcessService.save(workActivityProcess);
		}

		//发起通知给审批人
		WorkProjectNotify workProjectNotify = new WorkProjectNotify();
		workProjectNotify.setTitle("您有新的会议纪要待审批");
		workProjectNotify.setNotifyId(workFullMeetingminutes.getId());
		workProjectNotify.setCompanyId(workFullMeetingminutes.getCompanyId());
		workProjectNotify.setContent("审批任务-会议纪要");
		workProjectNotify.setRemarks("待审批");
		workProjectNotify.setType("8");
		workProjectNotify.setStatus("0");
		workProjectNotify.setNotifyRole("");
		workProjectNotify.setUser(new User(""));

		if(workActivityMenu!=null&&StringUtils.isNotBlank(workActivityMenu.getId())&&StringUtils.isNotBlank(activity.getId())){
			//角色审批
			if(StringUtils.isNotBlank(activity.getRole().getEnname())){
				workProjectNotify.setNotifyRole(buffer1.toString());
			}
			//人员审批
			if(StringUtils.isNotBlank(activity.getUser().getId())){
				workProjectNotify.setUser(new User(activity.getUser().getId()));
			}
		}else{
			workProjectNotify.setNotifyRole(buffer1.toString());
		}
		workProjectNotifyService.save(workProjectNotify);
	}

	@Transactional(readOnly = false)
	private void updateStatus(WorkFullMeetingminutes workFullMeetingminutes) {
		workFullMeetingminutesDao.updateStatus(workFullMeetingminutes);
	}

	@Transactional(readOnly = false)
	public void delete(WorkFullMeetingminutes workFullMeetingminutes) {
		super.delete(workFullMeetingminutes);
		//逻辑删除相关的通知记录
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workFullMeetingminutes.getId());
		workProjectNotifyService.modifyDelflag(notify);
	}


	public Page<WorkFullManage> findListOfWorkFullManage(Page<WorkFullManage> page, WorkFullManage workFullManage) {
		if (StringUtils.isNotBlank(workFullManage.getTotalFullIds())){
			workFullManage.setTotalFullIds(","+workFullManage.getTotalFullIds()+",");
		}
		workFullManage.getSqlMap().put("dsf", dataScopeFilterOR(workFullManage.getCurrentUser(), "o", "u","s", MenuStatusEnum.END.getValue()));
		workFullManage.setPage(page);
		List<WorkFullManage> list = Lists.newArrayList();
		workFullManage.setIsTotal("1");
		List<WorkFullManage> totals = getListOfWorkFullManage(workFullManage);
		List<WorkFullManage> workFullManages = new ArrayList<>();
		for (WorkFullManage fullManage:totals){
			WorkFullManage wfm = new WorkFullManage();
			wfm.setTotalFullId(fullManage.getId());
			List<WorkFullManage> workEngineeringProjectLists = getListOfWorkFullManage(wfm);
			if (workEngineeringProjectLists!=null && workEngineeringProjectLists.size()!=0){
				workFullManages.addAll(workEngineeringProjectLists);
			}
		}
		workFullManages.addAll(totals);
		WorkFullManageService.sortList(list,workFullManages,"0",true);
		//WorkFullManageService.sortList(list,getListOfWorkFullManage(workFullManage),"0",true);
		page.setList(list);
		return page ;
	}

	private List<WorkFullManage> getListOfWorkFullManage(WorkFullManage workFullManage) {
		List<WorkFullManage> workFullManageList = workFullMeetingminutesDao.getListOfWorkFullManage(workFullManage);
		if (workFullManageList != null && workFullManageList.size()>0){
			for(WorkFullManage w : workFullManageList){
				String hostMasterIds = w.getHostMaster();
				StringBuilder hostMasterName = new StringBuilder();
				String[] idArray = hostMasterIds.split(",");
				if(idArray != null && idArray.length>0){
					for(int i =0 ;i<idArray.length;i++){
						if(StringUtils.isNotBlank(idArray[i])){
							User u = UserUtils.get(idArray[i]);
							hostMasterName.append("，").append(u.getName());
						}
					}
				}
				if(hostMasterName.length()>1){
					w.setHostMasterName(hostMasterName.substring(1));
				}
			}
		}
		return workFullManageList ;
	}
	//获取流程信息
	public WorkFullMeetingminutes getByProcessInstanceId(String processInstanceId) {
		return workFullMeetingminutesDao.getByProcessInstanceId(processInstanceId);
	}

	//执行审核操作(0暂存，1审核中，2通过，3未通过，4已撤销)
	@Transactional(readOnly = false)
    public String auditSave(WorkFullMeetingminutes workFullMeetingminutes) {
		try {
			String userId = UserUtils.getUser().getId();
			actTaskService.claim(workFullMeetingminutes.getAct().getTaskId(), userId);
			// 设置意见
			String comment = "";
			if(workFullMeetingminutes.getStatus().equals("3")){//如果是被驳回状态
				comment = ("yes".equals(workFullMeetingminutes.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
			}else{
				comment = ("yes".equals(workFullMeetingminutes.getAct().getFlag())?"[同意] ":"[驳回] ")+workFullMeetingminutes.getAct().getComment();
			}
			workFullMeetingminutes.getAct().setComment(comment);
			workFullMeetingminutes.setStatus("1");

			System.err.println("comment======"+workFullMeetingminutes.getAct().getComment());
			WorkProjectNotify workProjectNotify = new WorkProjectNotify();
			workProjectNotify.setNotifyId(workFullMeetingminutes.getId());
			workProjectNotifyService.readByNotifyId(workProjectNotify);

			// 对不同环节的业务逻辑进行操作
			String taskDefKey = workFullMeetingminutes.getAct().getTaskDefKey();
			System.err.println("taskDefKey======"+taskDefKey);
			//业务逻辑对应的条件表达式
			String exp = "";
			//WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("a2d1b3bbff70432890acf20a18b05838",officeService.get(workFullMeetingminutes.getOfficeId()));
			List<Activity> activitieList = activityService.getByProcessInstanceId(workFullMeetingminutes.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 selectedProcess = new WorkActivityProcess();
			selectedProcess.setProcessInstanceId(workFullMeetingminutes.getProcessInstanceId());
			List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectedProcess);

			//是自定义流程
			boolean isCustomProcess = StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals(ActUtils.PD_WORKFULLMEETINGMINUTES[0]);

			String taskCount = "";
			int key = 0 ;

			List<Activity> activities = workActivityMenu.getActivities();
			if (isCustomProcess) {
				key = 1;
				for (int i =0;i<workActivityProcesses.size();i++){
					WorkActivityProcess activityProcess = workActivityProcesses.get(i);
					String count = taskDefKey.replace("task","").replace("audit","");

					if(taskDefKey.contains("audit") && (activityProcess.getCount()+"").contains(count)){
						taskCount = count;
						exp = "pass";
						workActivityProcess.setId("");
						workActivityProcess.setCount(0);
						if (!"yes".equals(workFullMeetingminutes.getAct().getFlag())) {
							workFullMeetingminutes.setStatus("4");
							workActivityProcess.setIsApproval("2");
						}else {
							workFullMeetingminutes.setStatus("1");
							workActivityProcess.setIsApproval("1");
							workProjectNotify.setTitle("会议纪要：重新申请");
							workProjectNotify.setRemarks("重新申请");
						}
					}
					else if ((activityProcess.getCount()+"").contains(count)){
						taskCount = count ;
						workActivityProcess = activityProcess;
						workActivityProcess.setRemarks(comment);

						if (!workActivityProcess.getIsApproval().equals("0")){
							workActivityProcess.setId("");
						}
						exp = "pass";
						if (!"yes".equals(workFullMeetingminutes.getAct().getFlag())) {
							workFullMeetingminutes.setStatus("3");
							workActivityProcess.setIsApproval("2");
							workProjectNotify.setTitle("您的会议纪要审请被驳回!");
							workProjectNotify.setNotifyRole("");
							workProjectNotify.setUser(workFullMeetingminutes.getCreateBy());
							String returnBack = "-1";
							for (Activity activity:activities){
								if(activity.getCount()==Integer.parseInt(count)){
									returnBack = activity.getReturnBack();
									break;
								}
							}
							if (returnBack.equals("0")) {
								workActivityProcess.setId("");
							}
						}else {
							workActivityProcess.setIsApproval("1");
							workProjectNotify.setTitle("会议纪要：待审批");
						}
					}
				}
			}else{
				workActivityMenu.setId(workActivityProcesses.get(0).getId());
				workActivityMenu.setProcessType(ActUtils.PD_WORKFULLMEETINGMINUTES[0]);
				for (int i = 0; i < workActivityProcesses.size(); i++) {
					WorkActivityProcess activityProcess = workActivityProcesses.get(i);
					String count = activityProcess.getCount() + "";

					if(! activityProcess.getIsApproval().equals("0")){
						workActivityProcess.setId("");
					}
					taskCount = "1" ; // 默认流程只有一个审批环节

					if ("audit".equals(taskDefKey) && count.equals("1")){
						workActivityProcess.setCount(1); //默认流程里部门负责人是第一个环节
						exp = "pass";
						if("yes".equals(workFullMeetingminutes.getAct().getFlag())){
							workActivityProcess.setIsApproval("1");
							workFullMeetingminutes.setStatus("2");
							workProjectNotify.setTitle("您的会议纪要申请已通过");
							workProjectNotify.setRemarks("待通知");
							workProjectNotify.setNotifyRole("");
							workProjectNotify.setUser(workFullMeetingminutes.getCreateBy());
						}else{
							workActivityProcess.setIsApproval("2");

							workFullMeetingminutes.setStatus("3");
							workProjectNotify.setRemarks("重新申请");
							workProjectNotify.setTitle("会议纪要申请被驳回!");
							workProjectNotify.setNotifyRole("");
							workProjectNotify.setUser(workFullMeetingminutes.getCreateBy());
						}
					}else if("reapply".equals(taskDefKey) && count.equals("0")){
						workActivityProcess.setId("");
						workActivityProcess.setCount(0);
						exp="pass";
						if("yes".equals(workFullMeetingminutes.getAct().getFlag())){//重新申请
							workActivityProcess.setIsApproval("1");
							workProjectNotify.setTitle("会议纪要重新申请!");
							Role role = UserUtils.getSelectRole().get(0);
							StringBuffer buffer = DateUtils.getByEnnme(role.getEnname());
							buffer.append("bmzr");
							workProjectNotify.setNotifyRole(buffer.toString());//给部门负责人再次发送通知
							workProjectNotify.setUser(new User(""));//如果是重新申请，就不给创建人发通知，只给部门负责人发通知
							workProjectNotify.setRemarks("待审批");
							workFullMeetingminutes.setStatus("1");
						}else{
							workActivityProcess.setIsApproval("2");
							workFullMeetingminutes.setStatus("4");//撤销
						}
						break;
					}
					// 未知环节
				}
			}

			//更新自定义的流程追踪数据
			String defaultReapply = "reapply"; //默认流程中的重新申请任务标识
			workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workFullMeetingminutes.getProcessInstanceId(),taskDefKey,defaultReapply,workFullMeetingminutes.getAct().getFlag(),comment, activities);


			// 设置意见
			workFullMeetingminutes.getAct().setComment(comment);
			workFullMeetingminutes.preUpdate();


			// 提交流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put(exp, "yes".equals(workFullMeetingminutes.getAct().getFlag()) ?true : false );
			System.err.println("vars======"+vars.toString());
			// 提交流程任务
			actTaskService.complete(workFullMeetingminutes.getAct().getTaskId(), workFullMeetingminutes.getAct().getProcInsId(), workFullMeetingminutes.getAct().getComment(), vars);
			boolean state = actTaskService.isProcessEnd(workFullMeetingminutes.getAct().getProcInsId());
			if (!state){

				if(isCustomProcess){
					if("yes".equals(workFullMeetingminutes.getAct().getFlag())){
						workFullMeetingminutes.setStatus("2");
					}else{
						if(! "3".equals(workFullMeetingminutes.getStatus()))
							workFullMeetingminutes.setStatus("4");
					}
				}

				if("2".equals(workFullMeetingminutes.getStatus())){
					workProjectNotify.setTitle("您的会议纪要申请已通过");
				}
				if("3".equals(workFullMeetingminutes.getStatus())){
					workProjectNotify.setTitle("您的会议纪要申请被驳回");
				}
				workProjectNotify.setRemarks("待通知");
				workProjectNotify.setNotifyRole("");
				workProjectNotify.setUser(workFullMeetingminutes.getCreateBy());
			}else{
				if (isCustomProcess) {
					String count = taskDefKey.replace("audit","").replace("task","");
					Activity activity = new Activity();

					if (taskDefKey.contains("audit")) {
						if (activities.size() == 1 || count.equals("1")){
							activity = activities.get(0);
						}else if (activities.size()==Integer.parseInt(count)){
							activity = activities.get(activities.size()-1);
							String returnBack = activity.getReturnBack();
							if (returnBack.equals("1")){
								activity = activities.get(Integer.parseInt(count)-2);
							}else if (returnBack.equals("2")){
								activity = activities.get(0);
							}
						}else {
							activity = activities.get(Integer.parseInt(count)-1);
							String returnBack = activity.getReturnBack();
							if (returnBack.equals("1")){
								activity = activities.get(Integer.parseInt(count)-2);
							}else if (returnBack.equals("2")){
								activity = activities.get(0);
							}
						}
					}

					else if(!"yes".equals(workFullMeetingminutes.getAct().getFlag())){
						workProjectNotify.setRemarks("重新申请");
						activity = new Activity();
					}
					else {
						activity = activities.get(Integer.parseInt(count));
						workProjectNotify.setRemarks("待审批");
					}
					if(StringUtils.isNotBlank(activity.getId())){
						workProjectNotify.setUser(activity.getUser());
						workProjectNotify.setNotifyRole(activity.getRole().getEnname());
					}
				}

				else{

				}
			}

			workProjectNotify.setCompanyId(workFullMeetingminutes.getCompanyId());
			workProjectNotify.setContent(workFullMeetingminutes.getAct().getComment());
			workProjectNotify.setType("8");
			workProjectNotify.setStatus("0");
			//创建人如果做了撤销操作，无需发送通知
			if (!workFullMeetingminutes.getStatus().equals("4")) {
				workProjectNotifyService.save(workProjectNotify);
			}
			//更新审核状态
			workFullMeetingminutesDao.updateStatus(workFullMeetingminutes);

			System.err.println("会议纪要审批步骤正常");
			return "审批执行成功";

			//如果是撤销操作，更改与此相关的所有通知的已读状态，不再显示
			//如果是同意或驳回操作：将前面发给审批人的所有通知变更为已读；
			//如果是重新申请操作：将前面发给申请人的所有通知变更为已读；
			//如果是同意操作，发送审批通过的通知给申请人；申请人在阅读完成后，变更已读状态；
//			WorkProjectNotify notify = new WorkProjectNotify();
//			notify.setNotifyId(workFullMeetingminutes.getId());
//			if("4".equals(workFullMeetingminutes.getStatus())){
//				workProjectNotifyService.readByNotifyId(notify);
//			}
//			else if("2".equals(workFullMeetingminutes.getStatus()) ||"3".equals(workFullMeetingminutes.getStatus())){
//				String roleEnname = DateUtils.getByEnnme(UserUtils.getSelectRole().getEnname())+"bmzr";
//				notify.setNotifyRole(roleEnname);
//				workProjectNotifyService.readByNotifyIdAndNotifyRole(notify);
//			}else if("1".equals(workFullMeetingminutes.getStatus())){
//				notify.setUser(workFullMeetingminutes.getCreateBy());
//				workProjectNotifyService.readByNotifyIdAndNotifyUser(notify);
//			}

		}catch (ActivitiObjectNotFoundException e) {
			e.printStackTrace();
			System.err.println("会议纪要--重复审批");
			logger.error("ActivitiObjectNotFoundException -- " + e);
			return "流程已审批，不能重新审批！";
		}catch (Exception e) {
			e.printStackTrace();
			System.err.println("会议纪要审批步骤异常");
			logger.error("Exception e:"+e);
			return "审核执行失败！！";
		}
	}

}