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

import java.util.*;

import com.google.common.collect.Maps;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.projectrecord.dao.WorkProjectUserDao;
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.workprojectnotify.entity.WorkProjectNotify;
import com.jeeplus.modules.workprojectnotify.service.WorkProjectNotifyService;
import com.jeeplus.modules.workprojectnotify.util.UtilNotify;
import com.jeeplus.modules.worksinglewage.dao.WorkSingleWageRecordDao;
import com.jeeplus.modules.worksinglewage.entity.WorkSingleWageRecord;
import com.jeeplus.modules.workstaff.dao.WorkStaffAchivesDao;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import org.activiti.engine.*;
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.worksinglewage.entity.WorkSingleWage;
import com.jeeplus.modules.worksinglewage.dao.WorkSingleWageDao;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 单项工资Service
 * @author 杨帆
 * @version 2018-09-26
 */
@Service
@Transactional(readOnly = true)
public class WorkSingleWageService extends CrudService<WorkSingleWageDao, WorkSingleWage> {
	@Autowired
	private WorkSingleWageRecordDao workSingleWageRecordDao;
	@Autowired
	private WorkSingleWageDao workSingleWageDao;
	@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 OfficeService officeService;
	@Autowired
	private WorkStaffAchivesDao workStaffAchivesDao;

	public WorkSingleWage get(String id) {
		WorkSingleWage workSingleWage = super.get(id);
		WorkSingleWageRecord workSingleWageRecord = new WorkSingleWageRecord();
		workSingleWageRecord.setWorkSingleWage(workSingleWage);
		List<WorkSingleWageRecord> workAttendanceRuleRecordList = workSingleWageRecordDao.findUsers(workSingleWageRecord);
		List<WorkSingleWageRecord> workAttendanceRuleRecordOfficeList = workSingleWageRecordDao.findOffices(workSingleWageRecord);
		if (workAttendanceRuleRecordList!=null &&workAttendanceRuleRecordList.size()!=0){
			List<User> users = new ArrayList<>();
			for(WorkSingleWageRecord wageRecord:workAttendanceRuleRecordList){
				users.add(wageRecord.getUser());
			}
			workSingleWage.setUserList(users);
		}
		if (workAttendanceRuleRecordOfficeList!=null &&workAttendanceRuleRecordOfficeList.size()!=0){
			List<Office> offices = new ArrayList<>();
			for(WorkSingleWageRecord wageRecord:workAttendanceRuleRecordOfficeList){
				Office office = new Office(wageRecord.getOfficeId());
				office.setName(wageRecord.getOfficeName());
				offices.add(office);
			}
			workSingleWage.setOfficeList(offices);
		}
		return workSingleWage;
	}
	
	public List<WorkSingleWage> findList(WorkSingleWage workSingleWage) {
		return super.findList(workSingleWage);
	}
	
	public Page<WorkSingleWage> findPage(Page<WorkSingleWage> page, WorkSingleWage workSingleWage) {
		if (StringUtils.isBlank(workSingleWage.getBranchOffice())){
			workSingleWage.setBranchOffice(UserUtils.getSelectBranchOffice());
		}
		page.setList(findList(workSingleWage));
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(WorkSingleWage workSingleWage) {
		if (StringUtils.isBlank(workSingleWage.getCompanyId())){
			workSingleWage.setCompanyId(UserUtils.getSelectCompany().getId());
		}
		if (StringUtils.isBlank(workSingleWage.getBranchOffice())){
			workSingleWage.setBranchOffice(UserUtils.getSelectBranchOffice());
		}
		if (workSingleWage.getOffice()==null || StringUtils.isBlank(workSingleWage.getOffice().getId())){
			workSingleWage.setOffice(UserUtils.getSelectOffice());
		}
		List<User> users = workSingleWage.getUserList();
		List<Office> offices = workSingleWage.getOfficeList();
		workSingleWage.setWorkSingleWageRecordIds(users);
		workSingleWage.setWorkSingleWageRecordOffices(offices);
		WorkSingleWageRecord workSingleWageRecord = new WorkSingleWageRecord();
		workSingleWageRecord.setWorkSingleWage(workSingleWage);
		Set<String> set = new HashSet<>();
		// 更新发送接受人记录
		workSingleWageRecordDao.deleteByworkSingleWageId(workSingleWageRecord);
		if (workSingleWage.getWorkSingleWageRecordList()!=null && workSingleWage.getWorkSingleWageRecordList().size() > 0){
			for (WorkSingleWageRecord wageRecord : workSingleWage.getWorkSingleWageRecordList()){
				set.add(wageRecord.getUser().getId());
			}
			workSingleWageRecordDao.insertAll(workSingleWage.getWorkSingleWageRecordList());
		}
		if (workSingleWage.getWorkSingleWageRecordOfficeList()!=null && workSingleWage.getWorkSingleWageRecordOfficeList().size() > 0){
			workSingleWageRecordDao.insertOfficeAll(workSingleWage.getWorkSingleWageRecordOfficeList());
			for (WorkSingleWageRecord wageRecord : workSingleWage.getWorkSingleWageRecordList()){
				String officeId = wageRecord.getOfficeId();
				WorkStaffBasicInfo workStaffBasicInfo = new WorkStaffBasicInfo();
				workStaffBasicInfo.setOffice(new Office(officeId));
				List<WorkStaffBasicInfo> workStaffBasicInfos = workStaffAchivesDao.getByOffice(workStaffBasicInfo);
				if (workStaffBasicInfos!=null && workStaffBasicInfos.size()!=0){
					for (WorkStaffBasicInfo staffBasicInfo:workStaffBasicInfos){
						set.add(staffBasicInfo.getUserId());
					}
				}
			}
		}
		workSingleWage.setGrantCount(set.size());
		super.save(workSingleWage);

	}

	/**
	 * 提交存储
	 * @param workSingleWage
	 * @param variables
	 */
	@Transactional(readOnly = false)
	public String save(WorkSingleWage workSingleWage, Map<String, Object> variables, String processInstanceId) {
		User user = UserUtils.getUser();
		this.save(workSingleWage);
		String str =  "发放名称:"+workSingleWage.getName()+",发放月份:"+workSingleWage.getMonth()+",发放金额:"+workSingleWage.getMoney()+",发放人数:"+workSingleWage.getGrantCount()+",分公司人数:"+workSingleWage.getBranchCount();
		Office office = officeService.get(workSingleWage.getOffice().getId());
		String title = "发放名称:"+workSingleWage.getName();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		// 启动流程
		String businessKey = workSingleWage.getId().toString();
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("dbxql3125f1f194c8lz88a9miiii75090499", office);
		// 启动流程
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(workSingleWage.getId(),
						null,
						workSingleWage.getCompanyId(),
						title,
						str,
						"85",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		List<User> bmzrs = UserUtils.getByRoleActivityEnname("bmzr",2,office.getId(),"28",workSingleWage.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(),"28",workSingleWage.getCreateBy());
					if (enusers.size()==0){
						workSingleWage.setStatus("1");
						super.save(workSingleWage);
						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(),"28",workSingleWage.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
			variables.put("bmzrList", bmzrs);
			if (bmzrs.size()==0 ){
				workSingleWage.setStatus("1");
				super.save(workSingleWage);
			}
			if (bmzrs.size()==0){
				return "流程审批人不能为空,角色部门负责人下无用户,请联系管理员！";
			}
			variables.put("bmzrcount",bmzrs.size());
			processType = "workwage";
			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","7001");
			extras.put("id",workProjectNotify.getId());
			extras.put("procDefKey","85");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", title);//设置标题；

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		workSingleWage.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		// 更新流程实例ID
		workSingleWage.setProcessInstanceId(processInstance.getId());
		workSingleWageDao.updateProcessInstanceId(workSingleWage);
		List<Activity> list = workActivityMenu.getActivities();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("workwage");
			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[]{
				"workwage", businessKey, processInstance.getId(), variables});
		return "";
	}

	/**
	 * 审核流程
	 * @param workSingleWage
	 */
	@Transactional(readOnly = false)
	public String auditSave(WorkSingleWage workSingleWage,List<User> auditUsers) {
		String userName = UserUtils.get(workSingleWage.getCreateBy().getId()).getName();
		String str =  "发放名称:"+workSingleWage.getName()+",发放月份:"+workSingleWage.getMonth()+",发放金额:"+workSingleWage.getMoney()+",发放人数:"+workSingleWage.getGrantCount()+",分公司人数:"+workSingleWage.getBranchCount();
		Office office = officeService.get(workSingleWage.getOffice().getId());
		String title = "发放名称:"+workSingleWage.getName();
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workSingleWage.getAct().getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey) && !taskDefKey.contains("audit") ) {
			actTaskService.claim(workSingleWage.getAct().getTaskId(), UserUtils.getUser().getId());
		}else{
			workSingleWage.getAct().setFlag("yes");
			save(workSingleWage);
		}
		String comment = "";
		if (workSingleWage.getStatus().equals("4")){
			comment = ("yes".equals(workSingleWage.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(workSingleWage.getAct().getFlag())?"[同意] ":"[驳回] ")+workSingleWage.getAct().getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workSingleWage.setStatus(("yes".equals(workSingleWage.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(workSingleWage.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(workSingleWage.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workwage")) {
			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(workSingleWage.getAct().getFlag())) {
						workSingleWage.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(workSingleWage.getAct().getFlag())) {
						workSingleWage.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("workwage");
			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(workSingleWage.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(workSingleWage.getAct().getFlag())) {
						workSingleWage.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

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

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workSingleWage.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(workSingleWage.getStatus()) && !workSingleWage.getStatus().equals("3")){
					workSingleWage.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workSingleWage.getId(),
											workSingleWage.getCreateBy(),
											workSingleWage.getCompanyId(),
											title,
											str,
											"85",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workSingleWage.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workwage")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workSingleWage.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workSingleWage.getId(),
								new User(),
								workSingleWage.getCompanyId(),
								title,
								str,
								"85",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workSingleWage.getAct().getFlag(),
						taskCount,
						workSingleWage.getCreateBy(),
						workSingleWage.getOffice().getId(),
						"28");
				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<>();
						if ("cw".equals(taskDefKey) || "cn".equals(taskDefKey) ){
							extras.put("type","7001");
						}else {
							extras.put("type","7002");
						}
						extras.put("id",workProjectNotify.getId());
						extras.put("procDefKey","85");
						UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

					}
				}

			} else {
				if (!"yes".equals(workSingleWage.getAct().getFlag())) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workSingleWage.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workSingleWage.getId(),
											workSingleWage.getCreateBy(),
											workSingleWage.getCompanyId(),
											title,
											str,
											"85",
											"0",
											"重新申请",
											notifyRole));
					users.add( workSingleWage.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workSingleWage.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList1);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workSingleWage.getId(),
										new User(),
										workSingleWage.getCompanyId(),
										title,
										str,
										"85",
										"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","7001");
								extras.put("id",workProjectNotify.getId());
								extras.put("procDefKey","85");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());

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

		workSingleWageDao.update(workSingleWage);
		return "保存审核意见成功！";
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkSingleWage workSingleWage) {
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workSingleWage.getId());
		List<User> users = workProjectNotifyService.readByNotifyId(notify);
		if (users!=null && users.size()!=0){
			for (User user:users){
				UserUtils.pushMeIm(user.getId());
			}
		}
		workSingleWageRecordDao.deleteByWageId(workSingleWage.getId());
		super.delete(workSingleWage);
	}
	/**
	 * 读取单个实体流程
	 * @return
	 */
	@RequestMapping(value = "getProcessOne")
	public String getProcessOne(WorkSingleWage workSingleWage,Model model) {
		model.addAttribute("processInstanceId", workSingleWage.getProcessInstanceId());
		return "modules/workworkwage/workworkwageTrack";
	}
	/**
	 * 单笔工资项作废：强制撤销
	 * @param workSingleWage
	 */
	@Transactional(readOnly = false)
	public void cancelInvalidate(WorkSingleWage workSingleWage) {
		String invalidateProcessInstanceId = workSingleWage.getProcessInstanceId();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workSingleWage.getProcessInstanceId());
			process.setIsApproval("0");
			//List<WorkActivityProcess> processList1 = workActivityProcessService.findByProcInsId(process);
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workSingleWage.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workSingleWage.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.setIsApproval("1");
				pro.setProcessKey(processList.get(0).getProcessKey());
				pro.setProcessInstanceId(processList.get(0).getProcessInstanceId());
				pro.setCount(0);
				workActivityProcessDao.insert(pro);
			}

			//结束该流程,设为"撤销"状态
			actTaskService.endProcessInstance(invalidateProcessInstanceId,"单笔工资项-强制撤销");
			workSingleWage.setStatus("3");
			workSingleWage.preUpdate();
			dao.update(workSingleWage);
		}catch (ActivitiObjectNotFoundException e){
			System.err.println("撤销单笔工资项作废异常，因为这个流程已不存在!");
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

	}


}