/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workbidproject.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.MyBeanUtils;
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.isignature.service.ISignatureDocumentService;
import com.jeeplus.modules.serialnum.service.SerialNumTplService;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.dao.UserDao;
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.entity.Workattachment;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.WorkattachmentService;
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.workapprovalcopy.service.ApprovalCopyService;
import com.jeeplus.modules.workbidproject.dao.WorkBidProjectDao;
import com.jeeplus.modules.workbidproject.entity.WorkBidProject;
import com.jeeplus.modules.workbidproject.entity.WorkBidProjectList;
import com.jeeplus.modules.workclientinfo.dao.WorkClientAttachmentDao;
import com.jeeplus.modules.workclientinfo.dao.WorkClientLinkmanDao;
import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
import com.jeeplus.modules.workclientinfo.service.WorkClientInfoService;
import com.jeeplus.modules.workproject.dao.WorkProjectDao;
import com.jeeplus.modules.workproject.entity.WorkProject;
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.java_websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 投标项目Service
 * @author mengxy
 * @version 2017-05-27
 */
@SuppressWarnings("all")
@Service
@Transactional(readOnly = true)
public class WorkBidProjectService extends CrudService<WorkBidProjectDao, WorkBidProject> {

	@Autowired
	private WorkClientLinkmanDao workClientLinkmanDao;
	@Autowired
	private WorkBidProjectDao workBidProjectDao;
	@Autowired
	private WorkClientInfoService workClientInfoService;

	@Autowired
	private OfficeDao officeDao;

	@Autowired
	private WorkProjectDao workProjectDao;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	protected TaskService taskService;
	@Autowired
	protected HistoryService historyService;
	@Autowired
	protected RepositoryService repositoryService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ApprovalCopyService approvalCopyService;
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private ISignatureDocumentService signatureDocumentService;
	@Autowired
	private SerialNumTplService serialNumTplService;

	public WorkBidProject getWB(String id){
		WorkBidProject workBidProject = super.get(id);
		return workBidProject;
	}
	public WorkBidProject getByPId(WorkProject workProject){
		return workBidProjectDao.getByPId(workProject);
	}

	public List<WorkBidProject> findList(WorkBidProject workBidProject) {
		return super.findList(workBidProject);
	}
	//一览表
	public List<WorkBidProjectList> findLists(WorkBidProjectList workBidProjectList) {
		return workBidProjectDao.findLists(workBidProjectList);
	}
	public List<WorkBidProject> findListByGiveDate(WorkBidProject workBidProject) {
		return workBidProjectDao.findListByGiveDate(workBidProject);
	}

	public List<WorkBidProject> findListByBidDate(WorkBidProject workBidProject) {
		return workBidProjectDao.findListByBidDate(workBidProject);
	}
	//更新投标保证金交付提醒标记
	@Transactional(readOnly = false)
	public int updateGiveDateSign(WorkBidProject workBidProject){
		return workBidProjectDao.updateGiveDateSign(workBidProject);
	}

	//更新投标保证金确认提醒标记
	@Transactional(readOnly = false)
	public int updateBidDateSign(WorkBidProject workBidProject){
		return workBidProjectDao.updateBidDateSign(workBidProject);
	}
	public WorkBidProject get(String id) {
		WorkBidProject workBidProject = super.get(id);
		if(workBidProject!=null){
			//联系人
			List<WorkClientLinkman> workClientLinkmanList = Lists.newArrayList();
			WorkClientLinkman linkman =new WorkClientLinkman();
			linkman.setProjectId(id);
			linkman.setRemarks("0");
			workClientLinkmanList = workClientLinkmanDao.findListByProjectId(linkman);
			workBidProject.setWorkClientLinkmanList(workClientLinkmanList);
			//招标代理机构
			List<WorkClientLinkman> workClientLinkmanLists = Lists.newArrayList();
			WorkClientLinkman linkmans =new WorkClientLinkman();
			linkmans.setProjectId(id);
			linkmans.setRemarks("1");
			workClientLinkmanLists = workClientLinkmanDao.findListByProjectId(linkmans);
			workBidProject.setWorkClientLinkmanLists(workClientLinkmanLists);
			//附件
			WorkClientAttachment workClientAttachment = new WorkClientAttachment();
			workClientAttachment.setAttachmentId(id);
			workClientAttachment.setAttachmentFlag("95");
			workBidProject.setWorkAttachments(workClientAttachmentDao.findList(workClientAttachment));
			WorkClientAttachment workClientAttachment2 = new WorkClientAttachment();
			workClientAttachment2.setAttachmentId(id);
			workClientAttachment2.setAttachmentFlag("96");
			workBidProject.setWorkAttachmentsprice(workClientAttachmentDao.findList(workClientAttachment2));
			WorkClientAttachment workClientAttachment3 = new WorkClientAttachment();
			workClientAttachment3.setAttachmentId(id);
			workClientAttachment3.setAttachmentFlag("97");
			workBidProject.setWorkAttachmentsbid(workClientAttachmentDao.findList(workClientAttachment3));
			WorkClientAttachment workClientAttachment4 = new WorkClientAttachment();
			workClientAttachment4.setAttachmentId(id);
			workClientAttachment4.setAttachmentFlag("98");
			workBidProject.setWorkAttachmentsopenbid(workClientAttachmentDao.findList(workClientAttachment4));

		}
		return workBidProject;
	}

	public Page<WorkBidProject> findPage(Page<WorkBidProject> page, WorkBidProject workBidProject) {
//		workBidProject.getSqlMap().put("dsf", dataScopeFilter(workBidProject.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_BID_PROJECT.getValue()));
		String companyId = UserUtils.getUser().getComId();
		workBidProject.getSqlMap().put("dsf","and (a.company_id='"+companyId+"')");
		int count = dao.queryCount(workBidProject);
		page.setCount(count);
		page.setCountFlag(false);
		workBidProject.setPage(page);
		page.setList(findList(workBidProject));
		return page;
	}
	//一览表
	public Page<WorkBidProjectList> findPageList(Page<WorkBidProjectList> page, WorkBidProjectList workBidProjectList) {
		if(workBidProjectList.getBeginContractDate()==null){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format3 = new SimpleDateFormat("yyyy");
			String time1 = format3.format(new Date());
			Date startTime = null;
			try {
				startTime = format.parse(time1 + "-01-01");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			workBidProjectList.setBeginContractDate(startTime);
		}
		if(workBidProjectList.getEndContractDate()==null){
			workBidProjectList.setEndContractDate(new Date());
		}
		String companyId = UserUtils.getUser().getComId();
		workBidProjectList.getSqlMap().put("dsf","and (a.company_id='"+companyId+"')");
//		workBidProjectList.getSqlMap().put("dsf", dataScopeFilter(workBidProjectList.getCurrentUser(), "o", "u","s", MenuStatusEnum.WORK_BID_PROJECT.getValue()));
		workBidProjectList.setPage(page);
		page.setList(findLists(workBidProjectList));
		return page;
	}

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

	/**
	 * 暂存
	 * @param workReimbursement
	 */
	@Transactional(readOnly = false)
	public  void save(WorkBidProject workBidProject){
		long begin = System.currentTimeMillis();
		if (StringUtils.isBlank(workBidProject.getProjectNumber())) {
			User user = UserUtils.getUser();
			workBidProject.setProjectNumber(serialNumTplService.genSerialNum(user.getCompany(), workBidProject.SERIAL_BIZCODE));
		}
		super.save(workBidProject);
		updateWorkReimbursementInfo(workBidProject);
		long end = System.currentTimeMillis();
		logger.info("投标暂存任务结束，共耗时：[" + (end-begin)  + "]毫秒");
	}
	//处理子表及附件信息
	@Transactional(readOnly = false)
	public void updateWorkReimbursementInfo(WorkBidProject workBidProject){
		try {
			long begin = System.currentTimeMillis();
			//先解除work_project_client关联
			workClientLinkmanDao.deleteProjectClient(workBidProject);
			//联系人
			for(WorkClientLinkman workClientLinkman : workBidProject.getWorkClientLinkmanList()){
				if (workClientLinkman.DEL_FLAG_NORMAL.equals(workClientLinkman.getDelFlag())){
					if (StringUtils.isBlank(workClientLinkman.getId())&& StringUtils.isNotBlank(workClientLinkman.getName())){
						workClientLinkman.preInsert();
						workClientLinkmanDao.insert(workClientLinkman);
					}else{
						if(workClientLinkman!=null){
							WorkClientLinkman w1 = workClientLinkmanDao.get(workClientLinkman.getId());//从数据库取出记录的值
							MyBeanUtils.copyBeanNotNull2Bean(workClientLinkman, w1);//将编辑表单中的非NULL值覆盖数据库记录中的值
							workClientLinkman.preUpdate();
							workClientLinkmanDao.update(w1);
						}
					}
					WorkClientLinkman linkman = new WorkClientLinkman();
					linkman.setId(workClientLinkman.getId());
					linkman.setProjectId(workBidProject.getId());
					linkman.setRemarks("0");
					//重新插入work_project_client关联
					workClientLinkmanDao.insertProject(linkman);
				}
			}
			//招标代理机构
			for(WorkClientLinkman workClientLinkman : workBidProject.getWorkClientLinkmanLists()){
				if (workClientLinkman.DEL_FLAG_NORMAL.equals(workClientLinkman.getDelFlag())){
					if (StringUtils.isBlank(workClientLinkman.getId())&& StringUtils.isNotBlank(workClientLinkman.getOffice())){
						workClientLinkman.preInsert();
						workClientLinkmanDao.insert(workClientLinkman);
					}else{
						workClientLinkman.preUpdate();
						workClientLinkmanDao.update(workClientLinkman);
					}
					WorkClientLinkman linkman = new WorkClientLinkman();
					linkman.setId(workClientLinkman.getId());
					linkman.setProjectId(workBidProject.getId());
					linkman.setRemarks("1");
					//重新插入work_project_client关联
					workClientLinkmanDao.insertProject(linkman);
				}else{
					workClientLinkmanDao.delete(workClientLinkman);
				}
			}
			//附件
			for (WorkClientAttachment workClientAttachment : workBidProject.getWorkAttachments()) {
				if (workClientAttachment.getId() == null) {
					continue;
				}
				if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())&&"95".equals(workClientAttachment.getAttachmentFlag())) {
					workClientAttachment.setAttachmentId(workBidProject.getId());
					workClientAttachment.setAttachmentFlag("95");
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
						workClientAttachment.preInsert();
						workClientAttachmentDao.insert(workClientAttachment);
						signatureDocumentService.createIsignature(workClientAttachment.getUrl(),workClientAttachment.getAttachmentName(),workClientAttachment.getId(),workBidProject.getProjectName());
					} else {
						workClientAttachment.preUpdate();
						workClientAttachmentDao.update(workClientAttachment);
					}
				}else if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())&&"96".equals(workClientAttachment.getAttachmentFlag())) {
					workClientAttachment.setAttachmentId(workBidProject.getId());
					workClientAttachment.setAttachmentFlag("96");
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
						workClientAttachment.preInsert();
						workClientAttachmentDao.insert(workClientAttachment);
						signatureDocumentService.createIsignature(workClientAttachment.getUrl(),workClientAttachment.getAttachmentName(),workClientAttachment.getId(),workBidProject.getProjectName());
					} else {
						workClientAttachment.preUpdate();
						workClientAttachmentDao.update(workClientAttachment);
					}
				}else if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())&&"97".equals(workClientAttachment.getAttachmentFlag())) {
					workClientAttachment.setAttachmentId(workBidProject.getId());
					workClientAttachment.setAttachmentFlag("97");
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
						workClientAttachment.preInsert();
						workClientAttachmentDao.insert(workClientAttachment);
						signatureDocumentService.createIsignature(workClientAttachment.getUrl(),workClientAttachment.getAttachmentName(),workClientAttachment.getId(),workBidProject.getProjectName());
					} else {
						workClientAttachment.preUpdate();
						workClientAttachmentDao.update(workClientAttachment);
					}
				} else if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())&&"98".equals(workClientAttachment.getAttachmentFlag())) {
					workClientAttachment.setAttachmentId(workBidProject.getId());
					workClientAttachment.setAttachmentFlag("98");
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					if (StringUtils.isBlank(workClientAttachment.getId()) || "null".equals(workClientAttachment.getId())) {
						workClientAttachment.preInsert();
						workClientAttachmentDao.insert(workClientAttachment);
						signatureDocumentService.createIsignature(workClientAttachment.getUrl(),workClientAttachment.getAttachmentName(),workClientAttachment.getId(),workBidProject.getProjectName());
					} else {
						workClientAttachment.preUpdate();
						workClientAttachmentDao.update(workClientAttachment);
					}
				}else {
					workClientAttachmentDao.delete(workClientAttachment);
				}
				long end = System.currentTimeMillis();
				logger.info("投标处理子表-附件任务结束，共耗时：[" + (end-begin)  + "]毫秒");
			}
		}catch (Exception e){
			logger.info(e.getMessage());
		}
	}

	@Transactional(readOnly = false)
	public String save(WorkBidProject workBidProject,Map<String, Object> variables,String processInstanceId,String status) {
		long begin = System.currentTimeMillis();
		User user = UserUtils.getUser();
		if (StringUtils.isBlank(workBidProject.getProjectNumber())) {
			workBidProject.setProjectNumber(serialNumTplService.genSerialNum(user.getCompany(), workBidProject.SERIAL_BIZCODE));
			long end2 = System.currentTimeMillis();
			logger.info("投标送审-项目编码设置任务结束，共耗时：[" + (end2-begin)  + "]毫秒");
		}

		String oldStatus = workBidProject.getStatus();
		workBidProject.setUpdateDate(new Date());
		dao.updateForTans(workBidProject);

		if(StringUtils.isNotBlank(workBidProject.getId())){
			WorkBidProject bidProject = dao.get(workBidProject.getId());
			if(oldStatus!=null&&!oldStatus.equals(bidProject.getStatus())){
				return "数据状态已发生变化";
			}
		}
		workBidProject.setStatus(status);
		super.save(workBidProject);
		String str = "";
		String userName = UserUtils.get(workBidProject.getCreateBy().getId()).getName();
		Office office = UserUtils.getSelectOffice();
		//系统外招标单位
		if("1".equals(workBidProject.getIsExt())){
			str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getExtCompany()+",投标申请人:"+user.getName()+",所属部门:"+office.getName();
		}else {
			str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getBidCompany().getName()+",投标申请人:"+user.getName()+",所属部门:"+office.getName();
		}

		String title = "投标名称:"+workBidProject.getProjectName();
		long begin2 = System.currentTimeMillis();
		updateWorkReimbursementInfo(workBidProject);
		long end8 = System.currentTimeMillis();
		logger.info("投标送审-添加子表联系人，机构任务结束，共耗时：[" + (end8-begin2)  + "]毫秒");
		long begin9 = System.currentTimeMillis();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		// 启动流程
		String businessKey = workBidProject.getId().toString();
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("215ad3f24b1345bc800fa76a57bb45a8", office);//menuid
		// 启动流程
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(workBidProject.getId(),
						null,
						workBidProject.getCompanyId(),
						title,
						str,
						"50",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		List<User> fglds = UserUtils.getByRoleActivityEnname("fgld",2,office.getId(),"16",workBidProject.getCreateBy());
		List<User> scyfglbzrs = UserUtils.getByRoleActivityEnname("scyfglbzr",3,office.getId(),"16",workBidProject.getCreateBy());
		List<User> zjls = UserUtils.getByRoleActivityEnname("zjl",1,office.getId(),"16",workBidProject.getCreateBy());
		List<User> tbzys = UserUtils.getByRoleActivityEnname("tbzy",3,office.getId(),"16",workBidProject.getCreateBy());
		List<User> gzrs = UserUtils.getByRoleActivityEnname("gzr",1,office.getId(),"16",workBidProject.getCreateBy());
		long end3 = System.currentTimeMillis();
		logger.info("投标送审-查找人员审核任务结束，共耗时：[" + (end3-begin9)  + "]毫秒");

		if (StringUtils.isNotBlank(workActivityMenu.getId())) {
			workProjectNotify.setNotifyRole("");
			workActivityMenu = workActivityMenuService.get(workActivityMenu.getId());
			List<Activity> activities = workActivityMenu.getActivities();
			for (Activity a : activities) {
				String encount = a.getEncount();
				String enlist = a.getEnlist();
				if (a.getRole()!=null && StringUtils.isNotBlank(a.getRole().getEnname())){
					List enusers = UserUtils.getByRoleActivityEnnames(a.getRole().getEnname(),office.getId(),"2",workBidProject.getCreateBy());
					if (enusers.size()==0){
						workBidProject.setStatus("1");
						this.save(workBidProject);
						return "流程审批人不能为空,角色"+a.getRole().getName()+"下无用户,请联系管理员！";
					}
					variables.put(enlist, enusers);
					variables.put(encount, enusers.size());
				}
				if (a.getDelFlag().equals("0") && a.getCount() == 1) {
					activity = a;
				}
			}
			buffer.append(activity.getRole().getEnname());
			if (activity != null && StringUtils.isNotBlank(activity.getId())) {
				//角色审批
				if (StringUtils.isNotBlank(activity.getRole().getEnname())) {
					users = UserUtils.getByRoleActivityEnnames(activity.getRole().getEnname(),office.getId(),"2",workBidProject.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
				/*variables.put("scyfglbzrList", scyfglbzrs);
				variables.put("zjlList", zjls);
				variables.put("tbzyList", tbzys);
				variables.put("scyfglbzrsList", scyfglbzrs);
				variables.put("gzrList", gzrs);
				variables.put("scyfglbzrssList", scyfglbzrs);*/
			if (fglds == null || fglds.size()==0){
				workBidProject.setStatus("1");
				this.save(workBidProject);
			}
			if (fglds == null || fglds.size()==0){
				return "流程审批人不能为空,角色业务分管领导下无用户,请联系管理员！";
			}
				/*if (scyfglbzrs.size()==0){
					return "流程审批人不能为空,角色市场研发管理部负责人下无用户,请联系管理员！";
				}
				if (zjls.size()==0){
					return "流程审批人不能为空,角色总经理下无用户,请联系管理员！";
				}
				if (tbzys.size()==0){
					return "流程审批人不能为空,角色投标专员下无用户,请联系管理员！";
				}
				if (gzrs.size()==0){
					return "流程审批人不能为空,角色盖章人下无用户,请联系管理员！";
				}*/
				/*variables.put("scyfglbzrcount", scyfglbzrs.size());
				variables.put("zjlcount", zjls.size());
				variables.put("tbzycount", tbzys.size());
				variables.put("scyfglbzrscount", scyfglbzrs.size());
				variables.put("gzrcount", gzrs.size());
				variables.put("scyfglbzrsscount", scyfglbzrs.size());
				*/
			processType = "bidproject";
			users.addAll(fglds);
		}
		for (User u : users){
			workProjectNotify.setUser(u);
			workProjectNotify.setId("");
			workProjectNotify.setNotifyRole("业务分管领导审批");
			workProjectNotifyService
					.save(workProjectNotify);
			Map<String,Object> extras = new HashMap<>();
			extras.put("type","7002");
			extras.put("id",workProjectNotify.getId());
			extras.put("procDefKey","50");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", "审批单：" + workBidProject.getProjectNumber());//设置标题；
		variables.put("fgldcount",users.size());
		variables.put("fgldList",users);

		long begin3 = System.currentTimeMillis();
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		long end4 = System.currentTimeMillis();
		logger.info("投标送审-流程启动任务结束，共耗时：[" + (end4-begin3)  + "]毫秒");

		workBidProject.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		// 更新流程实例ID
		workBidProject.setProcessInstanceId(processInstance.getId());
		workBidProjectDao.updateProcessInstanceId(workBidProject);
		List<Activity> list = workActivityMenu.getActivities();
		long begin4 = System.currentTimeMillis();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("bidproject");
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(2);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(3);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(4);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(5);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(6);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(7);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcess.setCount(8);
			workActivityProcess.setId("");
			workActivityProcessService.save(workActivityProcess);
			workActivityProcessService.insertAuditsByType(fglds,processInstance.getId(),1,1);
			workActivityProcessService.insertAuditsByType(scyfglbzrs,processInstance.getId(),2,0);
			workActivityProcessService.insertAuditsByType(zjls,processInstance.getId(),3,0);
			workActivityProcessService.insertAuditsByType(tbzys,processInstance.getId(),4,0);
			workActivityProcessService.insertAuditsByType(scyfglbzrs,processInstance.getId(),5,0);
			workActivityProcessService.insertAuditsByType(gzrs,processInstance.getId(),6,0);
			workActivityProcessService.insertAuditsByType(scyfglbzrs,processInstance.getId(),8,0);
		}
		long end5 = System.currentTimeMillis();
		logger.info("流程追踪结束，共耗时：[" + (end5-begin4)  + "]毫秒");
		logger.debug("start process of {key={}, bkey={}, pid={}, variables={}}", new Object[]{
				ActUtils.PD_REIMBURSEMENT[0], businessKey, processInstance.getId(), variables});
		long end = System.currentTimeMillis();
		logger.info("投标送审任务结束，共耗时：[" + (end-begin)  + "]毫秒");
		return "";
	}





	@Transactional(readOnly = false)
	public void delete(WorkBidProject workBidProject) {

		//删除子表信息
		if(workBidProject!=null){
			List<WorkClientLinkman> workClientLinkmanLists = Lists.newArrayList();
			WorkClientLinkman linkmans =new WorkClientLinkman();
			linkmans.setProjectId(workBidProject.getId());
			linkmans.setRemarks("1");
			workClientLinkmanLists = workClientLinkmanDao.findListByProjectId(linkmans);
			for(WorkClientLinkman w1 :workClientLinkmanLists){
				workClientLinkmanDao.delete(w1);
			}
		}
		workClientLinkmanDao.deleteProjectClient(workBidProject);
		super.delete(workBidProject);
		//删除通知信息
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(workBidProject.getId());
		List<User> users = workProjectNotifyService.readByNotifyId(notify);
		if (users!=null && users.size()!=0){
			for (User user:users){
				UserUtils.pushMeIm(user.getId());
			}
		}
	}

	/* * 审核审批保存
	 * @param testAudit
	 */
	@Transactional(readOnly = false)
	public String auditSave(WorkBidProject workBidProject,List<User> auditUsers) {
		long begin = System.currentTimeMillis();
		String str = "";
		String userName = UserUtils.get(workBidProject.getCreateBy().getId()).getName();
		Office office = officeService.get(workBidProject.getOfficeId());
		//系统外招标单位
		if("1".equals(workBidProject.getIsExt())){
			str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getExtCompany()+",投标申请人:"+userName+",所属部门:"+office.getName();
		}else {
			str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getBidCompany().getName()+",投标申请人:"+userName+",所属部门:"+office.getName();
		}
		String title = "投标名称:"+workBidProject.getProjectName();

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = workBidProject.getAct().getTaskDefKey();
		// 投标专员，，申请人
		if("tbzy".equals(taskDefKey) || ("sqr").equals(taskDefKey)){
			// 如果是这两个人，需要提交补充材料，处理一下附件。
			updateWorkReimbursementInfo(workBidProject);
			actTaskService.claim(workBidProject.getAct().getTaskId(), UserUtils.getUser().getId());
		}else if(!"modifyApply".equals(taskDefKey)){
			actTaskService.claim(workBidProject.getAct().getTaskId(), UserUtils.getUser().getId());
		}else {
			workBidProject.getAct().setFlag("yes");
			updateWorkReimbursementInfo(workBidProject);
		}
		String comment = "";
		if (workBidProject.getStatus().equals("4")){
			comment = ("yes".equals(workBidProject.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(workBidProject.getAct().getFlag())?"[同意] ":"[驳回] ")+workBidProject.getAct().getComment();
		}
		//yes 的时候状态为审核通过 否则为未通过
		//1 审核中 2 未通过
		workBidProject.setStatus(("yes".equals(workBidProject.getAct().getFlag()) ? "2" : "4"));
		Map<String, Object> vars = Maps.newHashMap();
		//业务逻辑对应的条件表达式
		String exp = "";
		String taskCount = "";
		String notifyRole = "";
		List<User> users = new ArrayList<>();
		List<User> userList = new ArrayList<>();
		int key = 0;
		String enname = "";
		List<Activity> activitieList = activityService.getByProcessInstanceId(workBidProject.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(workBidProject.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("bidproject")) {
			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(workBidProject.getAct().getFlag())) {
						workBidProject.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(workBidProject.getAct().getFlag())) {
						workBidProject.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("bidproject");
			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 ("fgld".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),2,1);
						//通知下一位审批人
						notifyRole = "市场研发管理部负责人备案";
						workActivityProcess.setIsApproval("1");
						enname = "scyfglbzr";
						vars.put("scyfglbzrcount",auditUsers.size());
						vars.put("scyfglbzrList",auditUsers);

					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("scyfglbzr".equals(taskDefKey) && count.contains("2")) {
					taskCount = "2";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),3,1);
						notifyRole = "总经理审核";
						workActivityProcess.setIsApproval("1");
						enname = "zjl";
						vars.put("zjlcount",auditUsers.size());
						vars.put("zjlList",auditUsers);

					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("zjl".equals(taskDefKey) && count.contains("3")) {
					taskCount = "3";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),4,1);
						notifyRole = "投标专员标书编制";
						workActivityProcess.setIsApproval("1");
						enname = "tbzy";
						vars.put("tbzycount",auditUsers.size());
						vars.put("tbzyList",auditUsers);

					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("tbzy".equals(taskDefKey) && count.contains("4")) {
					taskCount = "4";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),5,1);
						notifyRole = "市场研发管理部负责人审批";
						workActivityProcess.setIsApproval("1");
						enname = "scyfglbzr";
						vars.put("scyfglbzrscount",auditUsers.size());
						vars.put("scyfglbzrsList",auditUsers);
					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("scyfglbzrs".equals(taskDefKey) && count.contains("5")) {
					taskCount = "5";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),6,1);
						notifyRole = "盖章人盖章";
						workActivityProcess.setIsApproval("1");
						enname = "gzr";
						vars.put("gzrcount",auditUsers.size());
						vars.put("gzrList",auditUsers);

					} else {
						enname = "不为空";
						workActivityProcess.setIsApproval("2");
						notifyRole = "投标专员调整申请";
						//title = "请投标申请人："+UserUtils.get(workBidProject.getCreateBy().getId()).getName()+",投标名称："+workBidProject.getProjectName()+"的标书重新上传";
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workBidProject.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workBidProject.getId(),
										new User(),
										workBidProject.getCompanyId(),
										title,
										str,
										"50",
										"0",
										"待审批",
										notifyRole);
						List<User> tbzys = UserUtils.getByRoleActivityEnname("tbzy",3,workBidProject.getOfficeId(),"16",workBidProject.getCreateBy());
						users.addAll(tbzys);
						for (User user1:tbzys){
							workProjectNotify.setUser(user1);
							workProjectNotify.setId("");
							workProjectNotify.setIsNewRecord(false);
							workProjectNotifyService
									.save(workProjectNotify);
							if (!"modifyApply".equals(taskDefKey)){
								Map<String,Object> extras = new HashMap<>();
								if ("tbzy".equals(taskDefKey) || "gzr".equals(taskDefKey) ){
									extras.put("type","7001");
								}else if("scyfglbzrss".equals(taskDefKey)){
									extras.put("type","7003");
								}else{
									extras.put("type","7002");
								}
								extras.put("id",workProjectNotify.getId());
								extras.put("procDefKey","50");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());
							}
						}
					}
					break;
				} else if ("gzr".equals(taskDefKey) && count.contains("6")) {
					taskCount = "6";
					exp = "pass";
					notifyRole = "申请人开标";
					workActivityProcess.setIsApproval("1");
					enname = "sqr";
                    vars.put("sqrcount",auditUsers.size());
                    vars.put("sqrList",auditUsers);

					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workBidProject.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					WorkProjectNotify workProjectNotify = UtilNotify
							.saveNotify(workBidProject.getId(),
									workBidProject.getCreateBy(),
									workBidProject.getCompanyId(),
									title,
									str,
									"50",
									"0",
									"待审批",
									notifyRole);
					workProjectNotify.setId("");
					workProjectNotify.setIsNewRecord(false);
					workProjectNotifyService
							.save(workProjectNotify);
					users.add(workBidProject.getCreateBy());
					break;
				} else if ("sqr".equals(taskDefKey) && count.contains("7")) {
					taskCount = "7";
					exp = "pass";
					if ("yes".equals(workBidProject.getAct().getFlag())) {
						workActivityProcessService.insertAuditsByType(auditUsers,workBidProject.getProcessInstanceId(),8,1);
						notifyRole = "市场研发管理部负责人";
						workActivityProcess.setIsApproval("1");
						enname = "scyfglbzrss";
                        vars.put("scyfglbzrsscount",auditUsers.size());
                        vars.put("scyfglbzrssList",auditUsers);

					} else {
						notifyRole = "调整申请";
						workActivityProcess.setIsApproval("2");
					}
					break;
				} else if ("scyfglbzrss".equals(taskDefKey) && count.contains("8")) {
					taskCount = "8";
					exp = "pass";
					if ("yes".equals(workBidProject.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 = "fgld";
                    vars.put("fgldcount",auditUsers.size());
                    vars.put("fgldList",auditUsers);

					if (!"yes".equals(workBidProject.getAct().getFlag())) {
						workBidProject.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
		// 设置意见
		workBidProject.getAct().setComment(("yes".equals(workBidProject.getAct().getFlag()) ? "[同意] " : "[驳回] ") + workBidProject.getAct().getComment());
		workBidProject.preUpdate();
		// 提交流程任务
		vars.put(exp, "yes".equals(workBidProject.getAct().getFlag()) ? true : false);
		workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,workBidProject.getProcessInstanceId(),taskDefKey,"modifyApply",workBidProject.getAct().getFlag(),comment, activities);
		// 提交流程任务
		actTaskService.complete(workBidProject.getAct().getTaskId(), workBidProject.getAct().getProcInsId(), workBidProject.getAct().getComment(), vars);
		boolean state = actTaskService.isProcessEnd(workBidProject.getAct().getProcInsId());
		if (!state) {
			title = "投标名称:"+workBidProject.getProjectName();
			//系统外招标单位
			if("1".equals(workBidProject.getIsExt())){
				str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getExtCompany();
			}else {
				str = "投标编号:"+workBidProject.getProjectNumber()+",招标单位:"+workBidProject.getBidCompany().getName();
			}
//            str = "yes".equals(workReimbursement.getAct().getFlag())?"您的报销单审核：已通过":"您的报销单审核：未通过";
			users.add(workBidProject.getCreateBy());
			if ("yes".equals(workBidProject.getAct().getFlag())) {
				workBidProject.setStatus("5");
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workBidProject.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				workProjectNotifyService
						.save(UtilNotify
								.saveNotify(workBidProject.getId(),
										workBidProject.getCreateBy(),
										workBidProject.getCompanyId(),
										title,
										str,
										"50",
										"0",
										"待通知",
										notifyRole));

			} else {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workBidProject.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				if (StringUtils.isNotBlank(workBidProject.getStatus()) && !workBidProject.getStatus().equals("3")){
					workBidProject.setStatus("4");
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(workBidProject.getId(),
											workBidProject.getCreateBy(),
											workBidProject.getCompanyId(),
											title,
											str,
											"50",
											"0",
											"待通知",
											notifyRole));
				}
			}
			workActivityProcessService.deleteProcessIdAuditUsers(workBidProject.getProcessInstanceId());
		} else {
			if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("bidproject")) {
				WorkProjectNotify notify = new WorkProjectNotify();
				notify.setNotifyId(workBidProject.getId());
				userList = workProjectNotifyService.readByNotifyId(notify);
				//users.addAll(userList);
				WorkProjectNotify workProjectNotify = UtilNotify
						.saveNotify(workBidProject.getId(),
								new User(),
								workBidProject.getCompanyId(),
								title,
								str,
								"50",
								"0",
								"待审批",
								notifyRole);
				List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
						activities,
						workProjectNotify,
						taskDefKey,
						workBidProject.getAct().getFlag(),
						taskCount,
						workBidProject.getCreateBy(),
						workBidProject.getOfficeId(),
						"16");
				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 ("tbzy".equals(taskDefKey) || "gzr".equals(taskDefKey) ){
							extras.put("type","7001");
						}else if("scyfglbzrss".equals(taskDefKey)){
							extras.put("type","7003");
						}else{
							extras.put("type","7002");
						}
						if ("scyfglbzr".equals(taskDefKey)||"scyfglbzrss".equals(taskDefKey)){
							extras.put("appShow","1");
						}
						extras.put("id",workProjectNotify.getId());
						extras.put("procDefKey","50");
						UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());
					}
				}

			} else {
				if (!"yes".equals(workBidProject.getAct().getFlag())&&!enname.contains("不为空")) {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(workBidProject.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					//users.addAll(userList);
					workProjectNotifyService.save(UtilNotify.saveNotify(
							workBidProject.getId(),
							workBidProject.getCreateBy(),
							workBidProject.getCompanyId(),
							title,
							str,
							"50",
							"0",
							"重新申请",
							notifyRole));
					users.add( workBidProject.getCreateBy());
				} else {
					if (StringUtils.isNotBlank(enname)&&!enname.contains("不为空")&&!enname.equals("sqr")){
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workBidProject.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						//users.addAll(userList1);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(workBidProject.getId(),
										new User(),
										workBidProject.getCompanyId(),
										title,
										str,
										"50",
										"0",
										"待审批",
										notifyRole);
						users.addAll(auditUsers);
						for (User user1:auditUsers){
							workProjectNotify.setUser(user1);
							workProjectNotify.setId("");
							workProjectNotify.setIsNewRecord(false);
							workProjectNotifyService
									.save(workProjectNotify);
							if (!"modifyApply".equals(taskDefKey)){
								Map<String,Object> extras = new HashMap<>();
								if ("tbzy".equals(taskDefKey) || "gzr".equals(taskDefKey) ){
									extras.put("type","7001");
								}else if("scyfglbzrss".equals(taskDefKey)){
									extras.put("type","7003");
								}else{
									extras.put("type","7002");
								}
								extras.put("id",workProjectNotify.getId());
								extras.put("procDefKey","50");
								UserUtils.pushInfoToApp(title,str,extras,user1.getId());
							}
						}
					}else if(StringUtils.isBlank(enname)){
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(workBidProject.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						users.addAll(userList);
						users.add(workBidProject.getCreateBy());
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(workBidProject.getId(),
												workBidProject.getCreateBy(),
												workBidProject.getCompanyId(),
												title,
												str,
												"50",
												"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());
			}
		}
		workBidProjectDao.update(workBidProject);
		long end = System.currentTimeMillis();
		logger.info("投标审核任务结束，共耗时：[" + (end-begin)  + "]毫秒");
		return "保存审核意见成功！";
	}

	@Transactional(readOnly = false)
	public void cancelInvalidate(WorkBidProject workBidProject) {
		String invalidateProcessInstanceId = workBidProject.getProcessInstanceId();
		long begin = System.currentTimeMillis();
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(workBidProject.getProcessInstanceId());
			process.setIsApproval("0");
			//List<WorkActivityProcess> processList1 = workActivityProcessService.findByProcInsId(process);
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(workBidProject.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(workBidProject.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()+",投标编号:"+workBidProject.getProjectNumber() +" 强制撤销!\"," +
								"\"useType\":\"sys\"}";
						ChatServerPool.sendMessageToUser(toUserConn, message);//同时向本人发送消息
						//ChatServerPool.sendMessageToUser(toUserConn, u.getId() + "_sys_审批信息 " + "报销人:" + user.getName() + ",报销编号:" + workReimbursement.getNumber() + " 强制撤销!");//同时向本人发送消息
					}*/
					UserUtils.pushIm(u.getId(),"投标申请人:"+ user.getName()+",投标编号:"+workBidProject.getProjectNumber() +" 强制撤销!");

				}
			}
			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,"投标申请-强制撤销");
			workBidProject.setStatus("3");
			workBidProject.preUpdate();
			workBidProjectDao.update(workBidProject);

		}catch (ActivitiObjectNotFoundException e){
			System.err.println("撤销投标申请异常，因为这个流程已不存在!");
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}finally {
			long end = System.currentTimeMillis();
			logger.info("投标撤销任务结束，共耗时：[" + (end-begin)  + "]毫秒");
		}

	}
	@Transactional(readOnly = false)
	public void update(WorkBidProject workBidProject) {
		workBidProject.preUpdate();
		dao.update(workBidProject);
	}



	/*--------------------------------------暂弃----------------------------------------*/


	public Page<WorkClientInfo> findPageBybidCompany(Page<WorkClientInfo> page, WorkClientInfo bidCompany) {
		Page<WorkClientInfo> p = workClientInfoService.findPage(page,bidCompany);
		return p;
	}

	public Page<Office> findPageByimplementingDept(Page<Office> page, Office implementingDept) {
		implementingDept.setPage(page);
		page.setList(dao.findListByimplementingDept(implementingDept));
		return page;
	}
	public Page<User> findPageByprojectMaster(Page<User> page, User projectMaster) {
		projectMaster.setPage(page);
		page.setList(dao.findListByprojectMaster(projectMaster));
		return page;
	}

	public Page<WorkClientInfo> findPageBybiddingCompany(Page<WorkClientInfo> page, WorkClientInfo biddingCompany) {
		biddingCompany.setPage(page);
		page.setList(dao.findListBybidCompany(biddingCompany));
		return page;
	}
	/**
	 * 启动流程
	 * @param
	 */
	@Transactional(readOnly = false)
	public void startProcess(WorkBidProject workBidProject, Map<String, Object> variables) {
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(workBidProject.getCurrentUser().getId());
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("215ad3f24b1345bc800fa76a57bb45a8",officeService.get(workBidProject.getOfficeId()));
		// 启动流程
		String businessKey = workBidProject.getId().toString();
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = 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;
				}
			}
			buffer.append(activity.getRole().getEnname());
		}else {
			processType= "prequalification";

			Role role = UserUtils.getSelectRole().get(0);
			buffer = DateUtils.getByEnnme(role.getEnname());
			buffer.append("bmzr");
			variables.put("bmzr", buffer.toString());
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", workBidProject.getRemarks());//设置标题；

		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		workBidProject.setProcessInstance(processInstance);
		// 更新流程实例ID
		workBidProject.setProcessInstanceId(processInstance.getId());
		dao.updateProcessInstanceId(workBidProject);
		List<Activity> list= workActivityMenu.getActivities();
		if (list!=null && list.size()!=0){
			workActivityProcessService.saveList(list,processInstance.getId());
		}else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("prequalification");
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcess.setActivity(new Activity("4e62d343e9444a55b5544839eedcaf4z"));
			workActivityProcessService.save(workActivityProcess);
		}
		WorkProjectNotify workProjectNotify = new WorkProjectNotify();
		workProjectNotify.setTitle("投标管理-资格预审审批申请");
		workProjectNotify.setNotifyId(workBidProject.getId());
		workProjectNotify.setNotifyRole(buffer.toString());
		workProjectNotify.setCompanyId(workBidProject.getCompanyId());
		workProjectNotify.setContent(UserUtils.get(workBidProject.getCreateBy().getId()).getName()+"发起投标管理-资格预审,请审批!");
		workProjectNotify.setType("1");
		workProjectNotify.setStatus("0");
		workProjectNotify.setRemarks("待审批");
		if(workActivityMenu!=null&&StringUtils.isNotBlank(workActivityMenu.getId())&&StringUtils.isNotBlank(activity.getId())){
			//角色审批
			if(StringUtils.isNotBlank(activity.getRole().getEnname())){
				workProjectNotify.setNotifyRole(buffer.toString());
			}
			//人员审批
			if(StringUtils.isNotBlank(activity.getUser().getId())){
				workProjectNotify.setUser(new User(activity.getUser().getId()));
			}
		}else {
			workProjectNotify.setNotifyRole(buffer.toString());
		}
		workProjectNotifyService.save(workProjectNotify);
	}
}