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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.common.websocket.onchat.ChatServerPool;
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.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.workcatalogueitem.entity.WorkCatalogueItem;
import com.jeeplus.modules.workcatalogueitem.service.WorkCatalogueItemService;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workitem.service.WorkItemService;
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.workreimbursement.dao.ReimbursementVATTaxDao;
import com.jeeplus.modules.workreimbursement.dao.WorkAccountDao;
import com.jeeplus.modules.workreimbursement.dao.WorkReimbursementDao;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
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.modules.workitem.entity.WorkItem;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.workscrap.entity.WorkScrap;
import com.jeeplus.modules.workscrap.dao.WorkScrapDao;

/**
 * 报废管理Service
 * @author lw
 * @version 2018-07-16
 */
@Service
@Transactional(readOnly = true)
public class WorkScrapService extends CrudService<WorkScrapDao, WorkScrap> {
	@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 SerialNumTplService serialNumTplService;
	@Autowired
	private WorkScrapDao workScrapDao;
	@Autowired
	private WorkItemService workItemService;
	@Autowired
	private WorkCatalogueItemService workCatalogueItemService;
	public WorkScrap get(String id) {
		WorkScrap workScrap = super.get(id);
		//补充固定资产详细信息
		if(workScrap!=null){
			if(workScrap.getItem()!=null){
				String itemId = workScrap.getItem().getId();
				if(StringUtils.isNotBlank(itemId)){
					WorkItem workItem = workItemService.get(itemId);
					if(workItem!=null){
						if(workItem.getWorkCatalogueItem()!=null){
							String cItemId = workItem.getWorkCatalogueItem().getId();
							if(StringUtils.isNotBlank(cItemId)){
								WorkCatalogueItem workCatalogueItem = workCatalogueItemService.get(cItemId);
								workItem.setWorkCatalogueItem(workCatalogueItem);
								workScrap.setItem(workItem);
							}
						}
					}
				}
			}
		}
		return workScrap;
	}

	public List<WorkScrap> findList(WorkScrap workScrap) {
		return super.findList(workScrap);
	}

	public Page<WorkScrap> findPage(Page<WorkScrap> page, WorkScrap workScrap) {
		workScrap.getSqlMap().put("dsf", dataScopeFilter(workScrap.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_SCRAP.getValue()));
		workScrap.setPage(page);
		page.setList(findList(workScrap));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(WorkScrap workScrap) {
		super.save(workScrap);
	}

	/**
	 * 提交存储
	 * @param workScrap
	 * @param variables
	 */
	@Transactional(readOnly = false)
	public String save(WorkScrap workScrap, Map<String, Object> variables, String processInstanceId) {
		User user = UserUtils.getUser();
		if (StringUtils.isBlank(workScrap.getNumber())) {
			workScrap.setNumber(serialNumTplService.genSerialNum(user.getCompany(), workScrap.SERIAL_BIZCODE));
		}
		super.save(workScrap);
		String str = "";
		if(workScrap.getItem()!=null && workScrap.getItem().getWorkCatalogueItem()!=null){
			str = "报废编码："+workScrap.getNumber()+",报废日期:"+ DateUtils.formatDate(workScrap.getScrapDate())+",物品名称:"+workScrap.getItem().getItemName();
		}else{
			str = "报废编码："+workScrap.getNumber()+",报废日期:"+ DateUtils.formatDate(workScrap.getScrapDate());
		}
		String title = "物品报废申请人："+user.getName();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		// 启动流程
		String businessKey = workScrap.getId().toString();
		Office office = UserUtils.getSelectOffice();
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("ggh3125f1f194c82bfglcggldc750904", office);
		// 启动流程
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(workScrap.getId(),
						null,
						workScrap.getCompanyId(),
						title,
						str,
						"63",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		List<User> xzbzrs = UserUtils.getByRoleActivityEnname("xzbzr",3,office.getId(),"15",workScrap.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(),"15",workScrap.getCreateBy());
					if (enusers.size()==0){
						workScrap.setStatus("1");
						super.save(workScrap);
						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(),"15",workScrap.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
			variables.put("xzbzrList", xzbzrs);
			if (xzbzrs.size()==0 ){
				workScrap.setStatus("1");
				super.save(workScrap);
			}
			if (xzbzrs.size()==0){
				return "流程审批人不能为空,角色行政部负责人下无用户,请联系管理员！";
			}
			variables.put("xzbzrcount",xzbzrs.size());
			processType = "workscrap";
			users.addAll(xzbzrs);
		}
		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","63");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", "审批单：" + workScrap.getNumber());//设置标题；

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		workScrap.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		// 更新流程实例ID
		workScrap.setProcessInstanceId(processInstance.getId());
		workScrapDao.updateProcessInstanceId(workScrap);
		List<Activity> list = workActivityMenu.getActivities();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("workscrap");
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcessService.insertAuditsByType(xzbzrs,processInstance.getId(),1,1);
		}
		logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{
				ActUtils.PD_REIMBURSEMENT[0], businessKey, processInstance.getId(), variables});
		return "";
	}
	@Transactional(readOnly = false)
	public void delete(WorkScrap workScrap) {
		super.delete(workScrap);
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workScrap.getId());
		workProjectNotifyService.readByNotifyId(notify);
	}

	public Page<WorkItem> findPageByitem(Page<WorkItem> page, WorkItem item) {
		item.setPage(page);
		page.setList(dao.findListByitem(item));
		return page;
	}

	/**
	 * 审核流程
	 * @param
	 */
	@Transactional(readOnly = false)
	public String auditSave(WorkScrap workScrap,List<User> auditUsers) {
		String userName = UserUtils.get(workScrap.getCreateBy().getId()).getName();
		String str = "";
		if(workScrap.getItem()!=null && workScrap.getItem().getWorkCatalogueItem()!=null){
			str = "报废编码："+workScrap.getNumber()+",报废日期:"+ DateUtils.formatDate(workScrap.getScrapDate())+",物品名称:"+workScrap.getItem().getWorkCatalogueItem().getName();
		}else{
			str = "报废编码："+workScrap.getNumber()+",报废日期:"+ DateUtils.formatDate(workScrap.getScrapDate());
		}
		String title = "物品报废申请人:"+userName;
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workScrap.getAct().getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey)) {
			actTaskService.claim(workScrap.getAct().getTaskId(), UserUtils.getUser().getId());
		}else {
			workScrap.getAct().setFlag("yes");
		}
		String comment = "";
		if (workScrap.getStatus().equals("4")){
			comment = ("yes".equals(workScrap.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(workScrap.getAct().getFlag())?"[同意] ":"[驳回] ")+workScrap.getAct().getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workScrap.setStatus(("yes".equals(workScrap.getAct().getFlag()) ? "2" : "4"));
		//String buffer = UserUtils.getRoleActivityEnname(UserUtils.getSelectCompany().getId(),workScrap.getCreateBy());
		Map<String, Object> vars = Maps.newHashMap();
		//业务逻辑对应的条件表达式
		String exp = "";
		String taskCount = "";
		String notifyRole = "";
		int key = 0;
		String enname = "";
		List<Activity> activitieList = activityService.getByProcessInstanceId(workScrap.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(workScrap.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workscrap")) {
			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(workScrap.getAct().getFlag())) {
						workScrap.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(workScrap.getAct().getFlag())) {
						workScrap.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("workscrap");
			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 ("xzbzr".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(workScrap.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 = "xzbzr";
					if (!"yes".equals(workScrap.getAct().getFlag())) {
						workScrap.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
		// 设置意见
		workScrap.getAct().setComment(("yes".equals(workScrap.getAct().getFlag()) ? "[同意] " : "[驳回] ") + workScrap.getAct().getComment());
		workScrap.preUpdate();
		// 提交流程任务
		vars.put(exp, "yes".equals(workScrap.getAct().getFlag()) ? true : false);
		vars.put("passs", true);
		workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workScrap.getProcessInstanceId(),taskDefKey,"modifyApply",workScrap.getAct().getFlag(),comment, activities);
		// 提交流程任务
		actTaskService.complete(workScrap.getAct().getTaskId(), workScrap.getAct().getProcInsId(), workScrap.getAct().getComment(), vars);
		boolean state = actTaskService.isProcessEnd(workScrap.getAct().getProcInsId());
		List<User> users = new ArrayList<>();
		List<User> userList = new ArrayList<>();
		if (!state) {
			if(workScrap.getItem()!=null && workScrap.getItem().getWorkCatalogueItem()!=null){
				title =  "物品名称:"+workScrap.getItem().getWorkCatalogueItem().getName();
			}
			if(workScrap.getItem()!=null){
				str = "报废编码："+workScrap.getNumber()+",固定资产编号:"+workScrap.getItem().getNumber()+",报废日期:"+ DateUtils.formatDate(workScrap.getScrapDate());
			}
			users.add(workScrap.getCreateBy());
			if ("yes".equals(workScrap.getAct().getFlag())) {
				workScrap.setStatus("5");
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workScrap.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				workProjectNotifyService
						.save(UtilNotify
								.saveNotify(workScrap.getId(),
										workScrap.getCreateBy(),
										workScrap.getCompanyId(),
										title,
										str,
										"63",
										"0",
										"待通知",
										notifyRole));

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workScrap.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(workScrap.getStatus()) && !workScrap.getStatus().equals("3")){
					workScrap.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workScrap.getId(),
											workScrap.getCreateBy(),
											workScrap.getCompanyId(),
											title,
											str,
											"63",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workScrap.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("workscrap")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workScrap.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				//users.addAll(userList);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workScrap.getId(),
								new User(),
								workScrap.getCompanyId(),
								title,
								str,
								"63",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workScrap.getAct().getFlag(),
						taskCount,
						workScrap.getCreateBy(),
						workScrap.getOfficeId(),
						"15");
				for (WorkProjectNotify workProjectNotify1:workProjectNotifys){
					users.add(workProjectNotify1.getUser());
					workProjectNotify1.setId("");
					workProjectNotify1.setIsNewRecord(false);
					workProjectNotifyService
							.save(workProjectNotify1);
					if (!"modifyApply".equals(taskDefKey)){
						Map<String,Object> extras = new HashMap<>();
						extras.put("type","7001");
						extras.put("id",workProjectNotify.getId());
						extras.put("procDefKey","63");
						UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

					}
				}
				workActivityProcessService.deleteProcessIdAuditUsers(workScrap.getProcessInstanceId());
			} else {
				if (!"yes".equals(workScrap.getAct().getFlag())) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workScrap.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workScrap.getId(),
											workScrap.getCreateBy(),
											workScrap.getCompanyId(),
											title,
											str,
											"63",
											"0",
											"重新申请",
											notifyRole));
					users.add( workScrap.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workScrap.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workScrap.getId(),
										new User(),
										workScrap.getCompanyId(),
										title,
										str,
										"63",
										"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","63");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());
							}
						}
					}else {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workScrap.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.addAll(userList);
						users.add(workScrap.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workScrap.getId(),
												workScrap.getCreateBy(),
												workScrap.getCompanyId(),
												title,
												str,
												"63",
												"0",
												"重新申请",
												notifyRole));
					}
				}
			}
		}
		if (StringUtils.isNotBlank(title) && users!=null && users.size()!=0) {
			for (User u : users) {
				UserUtils.pushIm(u.getId(),title);
			}
		}
		if (StringUtils.isNotBlank(title) && userList!=null && userList.size()!=0) {
			for (User u : userList) {
				UserUtils.pushMeIm(u.getId());
			}
		}

		workScrapDao.update(workScrap);
		if("5".equals(workScrap.getStatus())){
			if(workScrap.getItem()!=null){
				String iId = workScrap.getItem().getId();
				if(StringUtils.isNotBlank(iId)){
					WorkItem workItem = workItemService.get(iId);
					//报废处理
					workItem.setState("4");
					workItemService.save(workItem);
				}
			}
		}
		return "保存审核意见成功！";
	}

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

	/**
	 * 强制撤销
	 * @param
	 */
	@Transactional(readOnly = false)
	public void cancelInvalidate(WorkScrap workScrap) {
		String invalidateProcessInstanceId = workScrap.getProcessInstanceId();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workScrap.getProcessInstanceId());
			process.setIsApproval("0");
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workScrap.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workScrap.getId());
			List<User> userList = workProjectNotifyService.readByNotifyId(notify);
			if (userList!=null && userList.size()!=0) {
				for (User u : userList) {
					User user = UserUtils.get(u.getId());
					/*List<WebSocket> toUserConns = ChatServerPool.getWebSocketByUser(u.getId());
					for (WebSocket toUserConn : toUserConns) {
						String message = "{\"to\":\""+u.getId()+"\"," +
								"\"msg\":\"审批信息 物品报废申请人:"+ user.getName()+",报废编号:"+workScrap.getNumber()+" 强制撤销!\"," +
								"\"useType\":\"sys\"}";
						ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
					}*/
					UserUtils.pushIm(u.getId(),"物品报废申请人:"+ user.getName()+",报废编号:"+workScrap.getNumber()+" 强制撤销!");
				}
			}
			if(processList!=null && processList.size()>0){
				for (int i =0;i<processList.size();i++) {
					WorkActivityProcess p = processList.get(i);
					if(StringUtils.isNotBlank(p.getIsApproval()) && "0".equals(p.getIsApproval())){
						p.setDelFlag("1");
						p.setIsApproval("-1");
						workActivityProcessDao.updateDelFlagAndIsApproval(p);
					}
				}
				WorkActivityProcess pro = new WorkActivityProcess();
				pro.setId("");
				pro.preInsert();
				pro.setDelFlag("0");
				pro.setRemarks("[强制撤销]");
				pro.setProcessKey(processList.get(0).getProcessKey());
				pro.setIsApproval("1");
				pro.setProcessInstanceId(processList.get(0).getProcessInstanceId());
				pro.setCount(0);
				workActivityProcessDao.insert(pro);
			}

			//结束该流程,设为"撤销"状态
			actTaskService.endProcessInstance(invalidateProcessInstanceId,"报废申请-强制撤销");
			workScrap.setStatus("3");
			workScrap.preUpdate();
			workScrapDao.update(workScrap);

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