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

import java.util.*;

import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.oss.OSSClientUtil;
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.entity.Act;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.casebuildinfo.dao.CaseBuildInfoDao;
import com.jeeplus.modules.casebuildinfo.service.CaseBuildInfoService;
import com.jeeplus.modules.casedesigninfo.dao.CaseDesignInfoDao;
import com.jeeplus.modules.casedesigninfo.service.CaseDesignInfoService;
import com.jeeplus.modules.caseexecuteinfo.dao.CaseExecuteInfoDao;
import com.jeeplus.modules.caseexecuteinfo.service.CaseExecuteInfoService;
import com.jeeplus.modules.casematerials.dao.CaseMaterialsDao;
import com.jeeplus.modules.casematerials.service.CaseMaterialsService;
import com.jeeplus.modules.casematerialsnorm.dao.CaseMaterialsNormDao;
import com.jeeplus.modules.casematerialsnorm.entity.CaseMaterialsNorm;
import com.jeeplus.modules.casematerialsnorm.service.CaseMaterialsNormService;
import com.jeeplus.modules.casemechanicalnorm.dao.CaseMechanicalNormDao;
import com.jeeplus.modules.casemechanicalnorm.entity.CaseMechanicalNorm;
import com.jeeplus.modules.casemechanicalnorm.service.CaseMechanicalNormService;
import com.jeeplus.modules.casenorm.dao.CaseNormDao;
import com.jeeplus.modules.casenorm.service.CaseNormService;
import com.jeeplus.modules.casepeoplenorm.dao.CasePeopleNormDao;
import com.jeeplus.modules.casepeoplenorm.entity.CasePeopleNorm;
import com.jeeplus.modules.casepeoplenorm.service.CasePeopleNormService;
import com.jeeplus.modules.caseprojectfeature.dao.CaseProjectFeatureDao;
import com.jeeplus.modules.caseprojectfeature.service.CaseProjectFeatureService;
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.test.entity.grid.Category;
import com.jeeplus.modules.test.service.grid.CategoryService;
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.workclientinfo.entity.WorkClientAttachment;
import com.jeeplus.modules.workinvoice.dao.WorkInvoiceDao;
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.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.ibatis.annotations.Case;
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.casedesigninfo.entity.CaseDesignInfo;
import com.jeeplus.modules.caseexecuteinfo.entity.CaseExecuteInfo;
import com.jeeplus.modules.casematerials.entity.CaseMaterials;
import com.jeeplus.modules.casenorm.entity.CaseNorm;
import com.jeeplus.modules.caseprojectfeature.entity.CaseProjectFeature;
import com.jeeplus.modules.casebuildinfo.entity.CaseBuildInfo;
import com.jeeplus.modules.workproject.entity.WorkProject;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.caseinfo.entity.CaseInfo;
import com.jeeplus.modules.caseinfo.dao.CaseInfoDao;

/**
 * 案例详情Service
 * @author liuw
 * @version 2018-01-24
 */
@Service
@Transactional(readOnly = true)
public class CaseInfoService extends CrudService<CaseInfoDao, CaseInfo> {
	@Autowired
	private CaseBuildInfoDao caseBuildInfoDao;
	@Autowired
	private CaseDesignInfoDao caseDesignInfoDao;
	@Autowired
	private CaseExecuteInfoDao caseExecuteInfoDao;
	@Autowired
	private WorkClientAttachmentDao workClientAttachmentDao;
	@Autowired
	private WorkattachmentService workattachmentService;
	@Autowired
	private CaseMaterialsNormDao caseMaterialsNormDao;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private CaseInfoDao caseInfoDao;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private WorkActivityMenuService workActivityMenuService;
	@Autowired
	private ActivityService activityService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CaseProjectFeatureDao caseProjectFeatureDao;
	@Autowired
	private WorkProjectNotifyService workProjectNotifyService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private WorkActivityProcessService workActivityProcessService;
	@Autowired
	private WorkActivityProcessDao workActivityProcessDao;

	@Autowired
	private CaseNormDao caseNormDao;
	@Autowired
	private CaseMaterialsDao caseMaterialsDao;
	@Autowired
	private CaseMechanicalNormDao caseMechanicalNormDao;
	@Autowired
	private CasePeopleNormDao casePeopleNormDao;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private CaseBuildInfoService caseBuildInfoService;
	@Autowired
	private CaseDesignInfoService caseDesignInfoService;
	@Autowired
	private CaseExecuteInfoService caseExecuteInfoService;
	@Autowired
	private CaseProjectFeatureService caseProjectFeatureService;
	@Autowired
	private CaseNormService caseNormService;
	@Autowired
	private CaseMaterialsService caseMaterialsService;
	@Autowired
	private CaseMaterialsNormService caseMaterialsNormService;
	@Autowired
	private CaseMechanicalNormService caseMechanicalNormService;
	@Autowired
	private CasePeopleNormService casePeopleNormService;

	public CaseInfo get(String id) {
		CaseInfo caseInfo = new CaseInfo();
		if(StringUtils.isNotBlank(id)){
			caseInfo = super.get(id);
		}
		boolean state = true;
		if(caseInfo!=null){
			WorkClientAttachment workClientAttachment = new WorkClientAttachment();
			workClientAttachment.setAttachmentId(id);
			workClientAttachment.setAttachmentFlag("66");
			List<WorkClientAttachment> list = workClientAttachmentDao.findList(workClientAttachment);
			if(list!=null&&list.size()>0){
				caseInfo.setWorkAttachments(list);
			}
			/*子表*/
			CaseBuildInfo caseBuildInfo = new CaseBuildInfo();
			CaseDesignInfo caseDesignInfo = new CaseDesignInfo();
			CaseExecuteInfo caseExecuteInfo = new CaseExecuteInfo();
			CaseProjectFeature caseProjectFeature = new CaseProjectFeature();
			CaseNorm caseNorm = new CaseNorm();
			CaseMaterials caseMaterials = new CaseMaterials();
			CaseMaterialsNorm caseMaterialsNorm = new CaseMaterialsNorm();
			CaseMechanicalNorm caseMechanicalNorm = new CaseMechanicalNorm();
			CasePeopleNorm casePeopleNorm = new CasePeopleNorm();

			caseBuildInfo.setCaseId(caseInfo.getId());
			caseDesignInfo.setCaseId(caseInfo.getId());
			caseExecuteInfo.setCaseId(caseInfo.getId());
			caseProjectFeature.setCaseId(caseInfo.getId());
			caseNorm.setCaseId(caseInfo.getId());
			caseMaterials.setCaseId(caseInfo.getId());
			caseMaterialsNorm.setCaseId(caseInfo.getId());
			caseMechanicalNorm.setCaseId(caseInfo.getId());
			casePeopleNorm.setCaseId(caseInfo.getId());

			List<CaseBuildInfo> buyDetailsList1 = caseBuildInfoService.findList(caseBuildInfo);
			List<CaseDesignInfo> buyDetailsList2 = caseDesignInfoService.findList(caseDesignInfo);
			List<CaseExecuteInfo> buyDetailsList3 = caseExecuteInfoService.findList(caseExecuteInfo);
			List<CaseProjectFeature> buyDetailsList4 = caseProjectFeatureService.findList(caseProjectFeature);
			List<CaseNorm> buyDetailsList5 = caseNormService.findList(caseNorm);
			List<CaseMaterials> buyDetailsList6 = caseMaterialsService.findList(caseMaterials);
			List<CaseMaterialsNorm> buyDetailsList7 = caseMaterialsNormService.findList(caseMaterialsNorm);
			List<CaseMechanicalNorm> buyDetailsList8 = caseMechanicalNormService.findList(caseMechanicalNorm);
			List<CasePeopleNorm> buyDetailsList9 = casePeopleNormService.findList(casePeopleNorm);


			caseInfo.setCaseBuildInfoList(buyDetailsList1);
			caseInfo.setCaseDesignInfoList(buyDetailsList2);
			caseInfo.setCaseExecuteInfoList(buyDetailsList3);
			caseInfo.setCaseProjectFeatureList(buyDetailsList4);
			caseInfo.setCaseNormList(buyDetailsList5);
			caseInfo.setCaseMaterialsList(buyDetailsList6);
			caseInfo.setCaseMaterialsNormList(buyDetailsList7);
			caseInfo.setCaseMechanicalNormList(buyDetailsList8);
			caseInfo.setCasePeopleNormList(buyDetailsList9);
			//流程结束state=false，未结束state=true
			if(StringUtils.isNotBlank(caseInfo.getProcessInstanceId())){
				state = actTaskService.isProcessEnd(caseInfo.getProcessInstanceId());
			}else{
				//如果是项目负责人，不用审核，没有流程实例
				state = false;
			}
			caseInfo.setProcessEnded(state);
		}
		return caseInfo;
	}
	
	public List<CaseInfo> findList(CaseInfo caseInfo) {
		return super.findList(caseInfo);
	}
	public List<CaseInfo> findByAllList(CaseInfo caseInfo) {
		return dao.findByAllList(caseInfo);
	}
	public List<CaseInfo> findByAllLists(CaseInfo caseInfo) {
		return dao.findByAllLists(caseInfo);
	}
	public List<CaseInfo> findCompanyList(CaseInfo caseInfo) {
		return dao.findCompanyList(caseInfo);
	}

	public Page<CaseInfo> findPage(Page<CaseInfo> page, CaseInfo caseInfo) {
		caseInfo.getSqlMap().put("dsf", dataScopeFilter(caseInfo.getCurrentUser(), "o", "u","s", MenuStatusEnum.CASE_INFO.getValue()));
		return super.findPage(page, caseInfo);
	}
	public Page<CaseInfo> findCompanyPage(Page<CaseInfo> page, CaseInfo caseInfo) {
		caseInfo.getSqlMap().put("dsf", dataScopeFilter(caseInfo.getCurrentUser(), "o", "u","s", MenuStatusEnum.CASE_INFO.getValue()));
		caseInfo.setPage(page);
		page.setList(dao.findCompanyList(caseInfo));
		return page;
	}
	public Page<CaseInfo> findBuyPage(Page<CaseInfo> page, CaseInfo caseInfo) {
		caseInfo.setPage(page);
		page.setList(dao.findBuyCase(caseInfo));
		return page;
	}
	public Page<CaseInfo> findByAllPage(Page<CaseInfo> page, CaseInfo caseInfo) {
		caseInfo.setPage(page);
		page.setList(dao.findByAllList(caseInfo));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(CaseInfo caseInfo) {
		super.save(caseInfo);
		for (CaseBuildInfo buyDetails : caseInfo.getCaseBuildInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseBuildInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseBuildInfoDao.update(buyDetails);
				}
			}else{
				caseBuildInfoDao.delete(buyDetails);
			}
		}
		for (CaseDesignInfo buyDetails : caseInfo.getCaseDesignInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseDesignInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseDesignInfoDao.update(buyDetails);
				}
			}else{
				caseDesignInfoDao.delete(buyDetails);
			}
		}
		for (CaseExecuteInfo buyDetails : caseInfo.getCaseExecuteInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseExecuteInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseExecuteInfoDao.update(buyDetails);
				}
			}else{
				caseExecuteInfoDao.delete(buyDetails);
			}
		}
		for (CaseProjectFeature buyDetails : caseInfo.getCaseProjectFeatureList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseProjectFeatureDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseProjectFeatureDao.update(buyDetails);
				}
			}else{
				caseProjectFeatureDao.delete(buyDetails);
			}
		}
		for (CaseNorm buyDetails : caseInfo.getCaseNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseNormDao.update(buyDetails);
				}
			}else{
				caseNormDao.delete(buyDetails);
			}
		}
		for (CaseMaterials buyDetails : caseInfo.getCaseMaterialsList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMaterialsDao.insert(buyDetails);
				}
			}else{
				caseMaterialsDao.delete(buyDetails);
			}
		}
		//指标子表
		for (CaseMaterialsNorm buyDetails : caseInfo.getCaseMaterialsNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMaterialsNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseMaterialsNormDao.update(buyDetails);
				}
			}else{
				caseMaterialsNormDao.delete(buyDetails);
			}
		}
		for (CaseMechanicalNorm buyDetails : caseInfo.getCaseMechanicalNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMechanicalNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseMechanicalNormDao.update(buyDetails);
				}
			}else{
				caseMechanicalNormDao.delete(buyDetails);
			}
		}
		for (CasePeopleNorm buyDetails : caseInfo.getCasePeopleNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					casePeopleNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					casePeopleNormDao.update(buyDetails);
				}
			}else{
				casePeopleNormDao.delete(buyDetails);
			}
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : caseInfo.getWorkAttachments()){
			if (workClientAttachment.getId() == null){
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())){
				workClientAttachment.setAttachmentId(caseInfo.getId());
				workClientAttachment.setAttachmentFlag("66");
				if (StringUtils.isBlank(workClientAttachment.getId())  || "null".equals(workClientAttachment.getId())&&StringUtils.isNotBlank(workClientAttachment.getUrl()) && !workClientAttachment.getUrl().equals("null")){
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					workClientAttachment.preInsert();
					workClientAttachmentDao.insert(workClientAttachment);
				}else{
					workClientAttachment.preUpdate();
					workClientAttachmentDao.update(workClientAttachment);
				}
			}else{
				workClientAttachmentDao.delete(workClientAttachment);
			}
		}
	}
	@Transactional(readOnly = false)
	public void saveTiming(CaseInfo caseInfo) {
		super.save(caseInfo);
		for (CaseBuildInfo buyDetails : caseInfo.getCaseBuildInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseBuildInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseBuildInfoDao.update(buyDetails);
				}
			}else{
				caseBuildInfoDao.delete(buyDetails);
			}
		}
		for (CaseDesignInfo buyDetails : caseInfo.getCaseDesignInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseDesignInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseDesignInfoDao.update(buyDetails);
				}
			}else{
				caseDesignInfoDao.delete(buyDetails);
			}
		}
		for (CaseExecuteInfo buyDetails : caseInfo.getCaseExecuteInfoList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseExecuteInfoDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseExecuteInfoDao.update(buyDetails);
				}
			}else{
				caseExecuteInfoDao.delete(buyDetails);
			}
		}
		for (CaseProjectFeature buyDetails : caseInfo.getCaseProjectFeatureList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseProjectFeatureDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseProjectFeatureDao.update(buyDetails);
				}
			}else{
				caseProjectFeatureDao.delete(buyDetails);
			}
		}
		for (CaseNorm buyDetails : caseInfo.getCaseNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseNormDao.update(buyDetails);
				}
			}else{
				caseNormDao.delete(buyDetails);
			}
		}
		if(caseInfo!=null && StringUtils.isNotBlank(caseInfo.getId())){
			CaseMaterials c = new CaseMaterials();
			c.setCaseId(caseInfo.getId());
			caseMaterialsService.deleteCase(c);
		}
		for (CaseMaterials buyDetails : caseInfo.getCaseMaterialsList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMaterialsDao.insert(buyDetails);
				}
			}else{
				caseMaterialsDao.delete(buyDetails);
			}
		}
		//指标子表
		for (CaseMaterialsNorm buyDetails : caseInfo.getCaseMaterialsNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMaterialsNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseMaterialsNormDao.update(buyDetails);
				}
			}else{
				caseMaterialsNormDao.delete(buyDetails);
			}
		}
		for (CaseMechanicalNorm buyDetails : caseInfo.getCaseMechanicalNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					caseMechanicalNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					caseMechanicalNormDao.update(buyDetails);
				}
			}else{
				caseMechanicalNormDao.delete(buyDetails);
			}
		}
		for (CasePeopleNorm buyDetails : caseInfo.getCasePeopleNormList()){
			if (buyDetails.getId() == null){
				continue;
			}
			if (buyDetails.DEL_FLAG_NORMAL.equals(buyDetails.getDelFlag())){
				buyDetails.setCaseId(caseInfo.getId());
				if (StringUtils.isBlank(buyDetails.getId())){
					buyDetails.preInsert();
					casePeopleNormDao.insert(buyDetails);
				}else{
					buyDetails.preUpdate();
					casePeopleNormDao.update(buyDetails);
				}
			}else{
				casePeopleNormDao.delete(buyDetails);
			}
		}
		if(caseInfo!=null && StringUtils.isNotBlank(caseInfo.getId())){
			workClientAttachmentDao.deleteCase(caseInfo.getId());
		}
		//附件信息
		for (WorkClientAttachment workClientAttachment : caseInfo.getWorkAttachments()){
			if (workClientAttachment.getId() == null){
				continue;
			}
			if (WorkClientAttachment.DEL_FLAG_NORMAL.equals(workClientAttachment.getDelFlag())){
				workClientAttachment.setAttachmentId(caseInfo.getId());
				workClientAttachment.setAttachmentFlag("66");
				if (StringUtils.isBlank(workClientAttachment.getId())  || "null".equals(workClientAttachment.getId())&&StringUtils.isNotBlank(workClientAttachment.getUrl()) && !workClientAttachment.getUrl().equals("null")){
					workClientAttachment.setAttachmentUser(UserUtils.getUser().getId());
					workClientAttachment.preInsert();
					workClientAttachmentDao.insert(workClientAttachment);
				}else{
					workClientAttachment.preUpdate();
					workClientAttachmentDao.update(workClientAttachment);
				}
			}else{
				workClientAttachmentDao.delete(workClientAttachment);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(CaseInfo caseInfo) {
		//清空通知
		WorkProjectNotify notify = new WorkProjectNotify();
		notify.setNotifyId(caseInfo.getId());
		workProjectNotifyService.readByNotifyId(notify);
		super.delete(caseInfo);
	}
	
	public Page<WorkProject> findPageByproject(Page<WorkProject> page, WorkProject project) {
		project.setPage(page);
		page.setList(dao.findListByproject(project));
		return page;
	}
	@Transactional(readOnly = false)
	public void update(CaseInfo caseInfo) {
		caseInfo.preUpdate();
		dao.update(caseInfo);
	}
	//删除附件
	//阿里云文件地址http://gangwan-app.oss-cn-hangzhou.aliyuncs.com/attachment-file/workBidingDocument/3fcf03e37b3ba0fd5c7821998e392395.doc
	//其中阿里云删除接口所需的参数 key 指的是 attachment-file/workBidingDocument/3fcf03e37b3ba0fd5c7821998e392395.doc
	@Transactional(readOnly = false)
	public void deleteFileFromAliyun(Workattachment workattachment) {
		String url = workattachment.getUrl();
		if(StringUtils.isNotBlank(url)){
			String aliyunUrl = Global.getAliyunUrl();
			String aliDownloadUrl = Global.getAliDownloadUrl();
			String cons = "";
			if (url.contains(aliyunUrl)){
				cons = aliyunUrl;
			}else if (url.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
				cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
			}else {
				cons = aliDownloadUrl;
			}
			final String[] arr = url.split(cons + "/");
			String key = arr[1];
			OSSClientUtil ossClientUtil = new OSSClientUtil();
			if(StringUtils.isNotBlank(key)){
				ossClientUtil.deleteSingleObject(key);
				workattachmentService.delete(workattachment);
			}
		}
	}

	/* * 审核审批保存
     * @param testAudit
     */
	@Transactional(readOnly = false)
	public String auditSave(CaseInfo caseInfo ,List<User> auditUsers) {
		String str = "案例申请人："+UserUtils.get(caseInfo.getCreateBy().getId()).getName()+",案例名称："+caseInfo.getName();
		String title = "案例名称:"+caseInfo.getName();
		// 对不同环节的业务逻辑进行操作
		String taskDefKey = caseInfo.getAct().getTaskDefKey();
		if (!"modifyApply".equals(taskDefKey)) {
			actTaskService.claim(caseInfo.getAct().getTaskId(), UserUtils.getUser().getId());
		}else {
			caseInfo.getAct().setFlag("yes");
		}
		String comment = "";
		if (caseInfo.getStatus().equals("4")){
			comment = ("yes".equals(caseInfo.getAct().getFlag())?"[重新申请] ":"[已撤销] ");
		}else {
			comment = ("yes".equals(caseInfo.getAct().getFlag())?"[同意] ":"[驳回] ")+caseInfo.getAct().getComment();
		}
			//yes 的时候状态为审核通过 否则为未通过
		caseInfo.setStatus(("yes".equals(caseInfo.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(caseInfo.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(caseInfo.getProcessInstanceId());
		List<WorkActivityProcess> workActivityProcesses = workActivityProcessService.findList(selectProcess);
		List<Activity> activities = workActivityMenu.getActivities();
		if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("caseInfo")) {
			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(caseInfo.getAct().getFlag())) {
						caseInfo.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("");
						}
						caseInfo.setRemarks("重新申请");
					} else {
						workActivityProcess.setIsApproval("1");
					}
				}else if(taskDefKey.equals("modifyApply")){
					notifyRole = "调整案例申请";
					taskCount = "0";
					exp = "pass";
					workActivityProcess.setId("");
					workActivityProcess.setCount(0);
					if (!"yes".equals(caseInfo.getAct().getFlag())) {
						caseInfo.setStatus("3");
						workActivityProcess.setIsApproval("2");
					} else {
						workActivityProcess.setIsApproval("1");
					}
					break;
				}
			}
		} else {
			workActivityMenu.setProcessType("caseInfo");
			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 ("audit".equals(taskDefKey) && count.contains("1")) {
					taskCount = "1";
					exp = "pass";
					if ("yes".equals(caseInfo.getAct().getFlag())) {
						notifyRole = "审批通过";
						workActivityProcess.setIsApproval("1");
					} else {
						notifyRole = "调整案例申请";
						workActivityProcess.setIsApproval("2");
						caseInfo.setRemarks("重新申请");
					}
					break;
				}  else if ("modifyApply".equals(taskDefKey)&& count.contains("0")) {
					notifyRole = "部门负责人审批";
					taskCount = "0";
					exp = "pass";
					workActivityProcess.setCount(0);
					enname = "bmzr";
					if (!"yes".equals(caseInfo.getAct().getFlag())) {
						caseInfo.setStatus("3");
					}
					break;
				} else if ("apply_end".equals(taskDefKey)) {
				}

			}
		}
			// 设置意见
			caseInfo.getAct().setComment(("yes".equals(caseInfo.getAct().getFlag()) ? "[同意] " : "[驳回] ") + caseInfo.getAct().getComment());
			caseInfo.preUpdate();
			// 提交流程任务
			vars.put(exp, "yes".equals(caseInfo.getAct().getFlag()) ? true : false);
			vars.put("passs", true);
			workActivityProcessService.updateProcess(workActivityProcess,workActivityMenu,key,taskCount,caseInfo.getProcessInstanceId(),taskDefKey,"modifyApply",caseInfo.getAct().getFlag(),comment, activities);
			// 提交流程任务
			actTaskService.complete(caseInfo.getAct().getTaskId(), caseInfo.getAct().getProcInsId(), caseInfo.getAct().getComment(), vars);
			boolean state = actTaskService.isProcessEnd(caseInfo.getAct().getProcInsId());
			List<User> users = new ArrayList<>();
			List<User> userList = new ArrayList<>();
			if (!state) {
				caseInfo.setRemarks("待通知");
				users.add(caseInfo.getCreateBy());
				if ("yes".equals(caseInfo.getAct().getFlag())) {
					caseInfo.setStatus("5");
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(caseInfo.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					workProjectNotifyService
							.save(UtilNotify
									.saveNotify(caseInfo.getId(),
											caseInfo.getCreateBy(),
											caseInfo.getCompanyId(),
											title,
											str,
											"38",
											"0",
											"待通知",
											notifyRole));
				} else {
					WorkProjectNotify notify = new WorkProjectNotify();
					notify.setNotifyId(caseInfo.getId());
					userList = workProjectNotifyService.readByNotifyId(notify);
					if (StringUtils.isNotBlank(caseInfo.getStatus()) && !caseInfo.getStatus().equals("3")){
						caseInfo.setStatus("4");
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(caseInfo.getId(),
												caseInfo.getCreateBy(),
												caseInfo.getCompanyId(),
												title,
												str,
												"38",
												"0",
												"待通知",
												notifyRole));
					}
				}
				workActivityProcessService.deleteProcessIdAuditUsers(caseInfo.getProcessInstanceId());
			} else {
				if (StringUtils.isNotBlank(workActivityMenu.getProcessType()) && !workActivityMenu.getProcessType().equals("caseInfo")) {
					if (!"yes".equals(caseInfo.getAct().getFlag())) {
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(caseInfo.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						workProjectNotifyService
								.save(UtilNotify
										.saveNotify(caseInfo.getId(),
												caseInfo.getCreateBy(),
												caseInfo.getCompanyId(),
												title,
												str,
												"38",
												"0",
												"重新申请",
												notifyRole));
						users.add( caseInfo.getCreateBy());
					}else{
						WorkProjectNotify notify = new WorkProjectNotify();
						notify.setNotifyId(caseInfo.getId());
						userList = workProjectNotifyService.readByNotifyId(notify);
						WorkProjectNotify workProjectNotify = UtilNotify
								.saveNotify(caseInfo.getId(),
										new User(),
										caseInfo.getCompanyId(),
										title,
										str,
										"38",
										"0",
										"待审批",
										notifyRole);
						List<WorkProjectNotify> workProjectNotifys = activityService.getByFlagAndTaskDefKeyList(
								activities,
								workProjectNotify,
								taskDefKey,
								caseInfo.getAct().getFlag(),
								taskCount,
								caseInfo.getCreateBy(),
								caseInfo.getOfficeId(),
								"10");
						for (WorkProjectNotify workProjectNotify1:workProjectNotifys){
							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","38");
								UserUtils.pushInfoToApp(title,str,extras,workProjectNotify1.getUser().getId());

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

								}
							}
						}else {
							WorkProjectNotify notify = new WorkProjectNotify();
							notify.setNotifyId(caseInfo.getId());
							userList = workProjectNotifyService.readByNotifyId(notify);
							users.addAll(userList);
							users.add(caseInfo.getCreateBy());
							workProjectNotifyService
									.save(UtilNotify
											.saveNotify(caseInfo.getId(),
													caseInfo.getCreateBy(),
													caseInfo.getCompanyId(),
													title,
													str,
													"38",
													"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());
				}
			}
			dao.update(caseInfo);
			return "保存审核意见成功！";
	}
	/**
	 * 启动流程
	 * @param
	 */
	@Transactional(readOnly = false)
	public String startProcess(CaseInfo caseInfo, Map<String, Object> variables,String processInstanceId) {
		User user = UserUtils.getUser();
		Office office = UserUtils.getSelectOffice();
		String str = "案例申请人："+user.getName()+",案例名称："+caseInfo.getName();
		String title = "案例名称:"+caseInfo.getName();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(user.getId());
		WorkActivityMenu workActivityMenu = workActivityMenuService.findByParentAndOffice("ggh3125f1f194c82b0000abcdc750904", officeService.get(caseInfo.getOfficeId()));
		// 启动流程
		String businessKey = caseInfo.getId().toString();
		String processType = workActivityMenu.getProcessType();
		StringBuffer buffer = new StringBuffer();
		Activity activity = new Activity();
		WorkProjectNotify workProjectNotify = UtilNotify
				.saveNotify(caseInfo.getId(),
						null,
						caseInfo.getCompanyId(),
						title,
						str,
						"38",
						"0",
						"待审批",
						""
				);
		List<User> users = new ArrayList<>();
		List<User> bmzrs = UserUtils.getByRoleActivityEnname("bmzr", 2, office.getId(), "10",caseInfo.getCreateBy());
		if (StringUtils.isNotBlank(workActivityMenu.getId())) {
			workProjectNotify.setNotifyRole("");
			workActivityMenu = workActivityMenuService.get(workActivityMenu.getId());
			List<Activity> activities = workActivityMenu.getActivities();
			for (Activity a : activities) {
				String encount = a.getEncount();
				String enlist = a.getEnlist();
				if (a.getRole()!=null && StringUtils.isNotBlank(a.getRole().getEnname())){
					List enusers = UserUtils.getByRoleActivityEnnames(a.getRole().getEnname(),office.getId(),"10",caseInfo.getCreateBy());
					if (enusers.size()==0){
						caseInfo.setStatus("1");
						this.save(caseInfo);
						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.getByRoleActivityEnname(activity.getRole().getEnname(), 2, office.getId(), "10",caseInfo.getCreateBy());
				}
				//人员审批
				if (StringUtils.isNotBlank(activity.getUser().getId())) {
					users.add(activity.getUser());
					variables.put("bmzr", activity.getUser());
				}
			}
			workProjectNotify.setId("");
		} else {
			variables.put("bmzrList", bmzrs);
			if (bmzrs.size() == 0) {
				caseInfo.setStatus("1");
				this.save(caseInfo);
				return "流程审批人不能为空,角色部门负责人下无用户,请联系管理员！";
			}
			variables.put("bmzrcount", bmzrs.size());
			processType = "caseInfo";
			users.addAll(bmzrs);
		}
		for (User u : users) {
			workProjectNotify.setUser(u);
			workProjectNotify.setId("");
			workProjectNotifyService
					.save(workProjectNotify);
			Map<String,Object> extras = new HashMap<>();
			extras.put("type","7001");
			extras.put("id",workProjectNotify.getId());
			extras.put("procDefKey","38");
			UserUtils.pushInfoToApp(title,str,extras,u.getId());
			UserUtils.pushIm(u.getId(),str);
		}
		variables.put("type", processType);
		variables.put("busId", businessKey);
		variables.put("title", "案例申请：" + caseInfo.getName());//设置标题；
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processType, businessKey, variables);
		caseInfo.setProcessInstance(processInstance);
		if (StringUtils.isNotBlank(processInstanceId)) {
			workActivityProcessService.updateProcessInstanceId(processInstance.getId(),processInstanceId);
			workActivityProcessService.deleteProcessInstanceId(processInstanceId);
			workActivityProcessService.deleteProcessIdAuditUsers(processInstanceId);
		}
		// 更新流程实例ID
		caseInfo.setProcessInstanceId(processInstance.getId());
		dao.updateProcessInstanceId(caseInfo);

		List<Activity> list = workActivityMenu.getActivities();
		if (list != null && list.size() != 0) {
			workActivityProcessService.saveList(list, processInstance.getId());
		} else {
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessKey("caseInfo");
			workActivityProcess.setCount(1);
			workActivityProcess.setProcessInstanceId(processInstance.getId());
			workActivityProcess.setIsApproval("0");
			workActivityProcess.setActivity(new Activity("ggh3125f1f194ckkkk000abcdc750904"));
			workActivityProcessService.save(workActivityProcess);
			workActivityProcessService.insertAuditsByType(bmzrs,processInstance.getId(),1,1);
		}
		return "";
	}
	/**
	 * 获取流程详细信息
	 * @param processInstanceId
	 */
	@SuppressWarnings("unchecked")
	public CaseInfo getByProcessInstanceId(String processInstanceId) {
		CaseInfo caseInfo = dao.getByProcessInstanceId(processInstanceId);
		return caseInfo;
	}

	public CaseInfo getCaseAct(CaseInfo t){
		String processInstanceId = t.getProcessInstanceId();
		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		if (historicProcessInstance != null) {
			t.setHistoricProcessInstance(historicProcessInstance);
			t.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult());
		} else {
			ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
			if (processInstance != null) {
				t.setProcessInstance(processInstance);
				t.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult());
			}
		}
		ProcessInstance processInstance = actTaskService.getProcIns(t.getProcessInstanceId());
		if (processInstance != null) {
			Task taskInfok = actTaskService.getCurrentTaskInfo(processInstance);
			Act act = new Act();
			act.setTaskId(taskInfok.getId());
			act.setTaskName(taskInfok.getName());
			act.setTaskDefKey(taskInfok.getTaskDefinitionKey());
			act.setProcDefId(taskInfok.getProcessDefinitionId());
			act.setProcInsId(taskInfok.getProcessInstanceId());
			act.setTask(taskInfok);
			act.setFlag("yes");
			t.setAct(act);
		}
		return t;
	}
	public CaseInfo getCaseAct2(CaseInfo t){
		String processInstanceId = t.getProcessInstanceId();
		HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		if (historicProcessInstance != null) {
			t.setHistoricProcessInstance(historicProcessInstance);
			t.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(historicProcessInstance.getProcessDefinitionId()).singleResult());
		} else {
			ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
			if (processInstance != null) {
				t.setProcessInstance(processInstance);
				t.setProcessDefinition(repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult());
			}
		}
		ProcessInstance processInstance = actTaskService.getProcIns(t.getProcessInstanceId());
		if (processInstance != null) {
			Task taskInfok = actTaskService.getCurrentTaskInfo(processInstance);
			Act act = new Act();
			act.setTaskId(taskInfok.getId());
			act.setTaskName(taskInfok.getName());
			act.setTaskDefKey(taskInfok.getTaskDefinitionKey());
			act.setProcDefId(taskInfok.getProcessDefinitionId());
			act.setProcInsId(taskInfok.getProcessInstanceId());
			act.setTask(taskInfok);
			act.setFlag("no");
			t.setAct(act);
		}
		return t;
	}

	@Transactional(readOnly = false)
	public void cancelInvalidate(CaseInfo caseInfo,String pid) {
		String invalidateProcessInstanceId = "";
		if(StringUtils.isNotBlank(pid)){
			invalidateProcessInstanceId = pid;
		}else{
			invalidateProcessInstanceId = caseInfo.getProcessInstanceId();
		}
		try {
			//将流程图中尚未走到的task的流程追踪信息逻辑删除，添加一条"撤销"流程追踪信息
			WorkActivityProcess process = new WorkActivityProcess();
			process.setProcessInstanceId(caseInfo.getProcessInstanceId());
			process.setIsApproval("0");
			WorkActivityProcess workActivityProcess = new WorkActivityProcess();
			workActivityProcess.setProcessInstanceId(caseInfo.getProcessInstanceId());
			List<WorkActivityProcess> processList = workActivityProcessService.findList(workActivityProcess);
			WorkProjectNotify notify = new WorkProjectNotify();
			notify.setNotifyId(caseInfo.getId());
			workProjectNotifyService.readByNotifyId(notify);
			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,"报销-强制撤销");
			caseInfo.setStatus("3");
			caseInfo.preUpdate();
			caseInfoDao.update(caseInfo);

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

	}
}