package com.xinwei.process.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.annotation.Resource; import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.xinwei.process.constant.ChangeConstants;
import com.xinwei.process.constant.ChangeConstants.ServiceType;
import com.xinwei.process.entity.ApprovalResult;
import com.xinwei.process.entity.CommonBiz;
import com.xinwei.process.entity.Project;
import com.xinwei.process.entity.StateInfo;
import com.xinwei.process.service.CommitteeApprovalService;
import com.xinwei.process.service.CommonBizService;
import com.xinwei.process.service.DepartleaderApprovalService;
import com.xinwei.process.service.ProcessService;
import com.xinwei.process.service.ProjectService;
import com.xinwei.process.service.UserTaskService;
import com.xinwei.security.entity.User;
import com.xinwei.security.vo.ResultVO;
import com.xinwei.util.JsonUtil; /**
* 项目变更控制器
*
*/
@Controller
@RequestMapping("/changeproject")
public class ChangeProjectController extends BaseController {//
@Resource
private ProjectService projectService;
@Resource
private ProcessService processServiceImpl;// 流程相关服务
@Resource
private CommonBizService commonBizService;// 变更服务
@Resource
private ProjectService projectServiceImpl;// 项目相关服务
@Resource
private DepartleaderApprovalService departleaderApprovalServiceImpl;// 部门领导审批相关服务
@Resource
private CommitteeApprovalService committeeApprovalServiceImpl;// 决策委员会审批相关服务
@Resource
private UserTaskService userTaskServiceImpl;// 用户任务相关服务 @Value("${committee}")
private String committee;// 决策委员会人员ID
@Value("${departLeader}")
private String departLeader;// 部门经理ID private Logger logger = LoggerFactory
.getLogger(ChangeProjectController.class); /**
* 保存变更(包括修改后保存)
*
* @param CommonBiz
* @return
*/
@RequestMapping(value = "/createChange", method = { RequestMethod.GET,
RequestMethod.POST })
public @ResponseBody String postChangeRequest(CommonBiz commonBiz) {
ResultVO<Object> result = new ResultVO<>();
String changeCurrentState = "";
// 根据project获取项目经理ID,仅仅该项目经理能够提取变更;
Project project = projectService.selectByPrimaryKey(commonBiz
.getProjectId());
User user = getCurrentUser();
logger.debug(user.getId() + "manager:" + project.getProjectManagerId());
if (user.getId().longValue() != project.getProjectManagerId()
.longValue()) {
// 构造没有权限提出变更申请
result.setResult(ChangeConstants.ErrorCode.NOT_AUTHORITY_CHANGE);
return result.toString();
} if (!StringUtils.isBlank(changeCurrentState)
&& !changeCurrentState
.equals(ChangeConstants.State.CODE_ENDEVENT)) {
// 判断project表格中状态,如果状态不是"申请变更",不允许提交;
result.setResult(ChangeConstants.ErrorCode.STATUS_NOT_ALLOW_CHANGE);
return result.toString();
} else {
// 新增
if (isNewChange(project)) {
createNewChange(commonBiz, project);
} else {// 修改
modifyChange(commonBiz, project);
} // 响应客户端
result.setOthers("dataId", commonBiz.getDataId());
return result.toString();
}
} /**
*
* @param commonBiz
* @param project
* @return
*/
protected int createNewChange(CommonBiz commonBiz, Project project) {
// CommonBiz更新这里连得创建者,创建时间,设置状态,更改人,更改时间
User user = getCurrentUser();
commonBiz.setCreatePerson(user.getId().toString());
commonBiz.setCreateTime(Calendar.getInstance().getTime());
commonBiz.setUpdatePerson(user.getUsername());
commonBiz.setUpdateTime(Calendar.getInstance().getTime());
commonBiz
.setStatus(ChangeConstants.State.CODE_COMMITTEE_APPROVALCHANGE);
commonBiz.setServiceType(ServiceType.TYPE_XMBIANGENG);
// 保存变更
String dataId = commonBizService.save(commonBiz); Map<String, Object> variables = new HashMap<String, Object>(); // 指定项目经理
variables.put("projectManager", project.getProjectManagerId()
.toString());
// 指定决策委员会
variables.put("committee", committee);
// 指定部门经理
variables.put("departLeader", departLeader); variables.put(ChangeConstants.ActivitiContextKey.ChangeServiceID,
commonBiz.getDataId());
// 创建一个流程,并启动该流程实例(在启动流程实例时,将当前CommonBiz的ID作为businessKey),将流程实例ID保存到CommonBiz;
ProcessInstance processInstance = processServiceImpl.startProcess(
ChangeConstants.getProcessKey(project.getCategoryId()), project
.getProjectId().toString(),
commonBiz.getCreatePerson(), variables); // 设置CommonBiz对象的流程实例ID
commonBiz.setProcessInstanceId(processInstance.getId());
commonBizService.update(commonBiz); // 设置主项目表中的变更流程的状态
List<Task> currentTask = userTaskServiceImpl
.findActiveTaskByProcessInstanceId(processInstance.getId());
String state = userTaskServiceImpl
.getStateByTaskDefinitionKey(currentTask.get(0)
.getTaskDefinitionKey());
// 修改project表中: 项目更改数据ID&变更流程实例ID&变更的流程当前状态信息
projectServiceImpl.updateProjectChangeByProjectId(dataId,
processInstance.getId(), project
.makeChangeCurrentStateFromStatusInfo(state,
currentTask.get(0).getId(), currentTask.get(0)
.getName()), commonBiz.getProjectId());
return 0;
} /**
*
* @param commonBiz
* @param project
* @return
*/
protected int modifyChange(CommonBiz commonBiz, Project project) {
// CommonBiz更新这里连得创建者,设置状态,更改人,更改时间
User user = getCurrentUser();
commonBiz.setUpdatePerson(user.getUsername());
commonBiz.setUpdateTime(Calendar.getInstance().getTime()); // 更新之前设置流程变量
CommonBiz oldEntity = commonBizService.selectByPrimaryKey(commonBiz
.getDataId()); // 获取老的任务,获取结果
List<Task> currentTask = userTaskServiceImpl
.findActiveTaskByProcessInstanceId(commonBiz
.getProcessInstanceId());
// 获取task对象
Task task = currentTask.get(0); // 更新流程实例
commonBizService.updateProcessInstanceAndTaskIdByDataId(
oldEntity.getProcessInstanceId(), task.getId(),
commonBiz.getDataId()); // 设置主项目表中的CurrentChangeStatus的状态
updateProjectCurrentChangeStatus(commonBiz, project,
ProjectService.CHANGEPREVIOUSSTATE); // 获取评审结论
String stringResult = commonBiz.getResult(); ApprovalResult approvalResult = JsonUtil.fromJson(stringResult,
ApprovalResult.class); Map<String, Object> variables = new HashMap<String, Object>();
variables.put(task.getTaskDefinitionKey()
+ ChangeConstants.ActivitiContextKey.result,
approvalResult.getResult()); userTaskServiceImpl.completeTask(task.getId(), variables); // 流程结束
if ("cancel".endsWith(approvalResult.getResult())) {// endChangeProcess
// 构建StateInfo对象
StateInfo endProcessStateInfo = new StateInfo();
endProcessStateInfo.setState(ChangeConstants.State.CODE_ENDEVENT);
endProcessStateInfo.setStateName(ChangeConstants.State.ENDEVENT);
endProcessStateInfo.setTaskId("");
// 更改流程当前任务状态信息
String endStatus = JsonUtil.toJson(endProcessStateInfo);
String status = endStatus;
String changeCurrentState = endStatus; commonBizService.updateChangeStatusByDataId(status,
commonBiz.getDataId());
projectServiceImpl.updateProjectPropertyByProjectId(
ProjectService.CHANGECURRENTSTATE, changeCurrentState,
commonBiz.getProjectId());
return 0;
} else {// 重新来一圈
//更新状态
updateProjectCurrentChangeStatus(commonBiz, project,
ProjectService.CHANGECURRENTSTATE);
}
return 0;
} private void updateProjectCurrentChangeStatus(CommonBiz commonBiz,
Project project, String changeProperty) {
List<Task> currentTask;
currentTask = userTaskServiceImpl
.findActiveTaskByProcessInstanceId(commonBiz
.getProcessInstanceId()); String state = userTaskServiceImpl
.getStateByTaskDefinitionKey(currentTask.get(0)
.getTaskDefinitionKey()); commonBizService.updateChangeStatusByDataId(project
.makeChangeCurrentStateFromStatusInfo(state, currentTask.get(0)
.getId(), currentTask.get(0).getName()), commonBiz.getDataId()); projectServiceImpl.updateProjectPropertyByProjectId(changeProperty,
project.makeChangeCurrentStateFromStatusInfo(state, currentTask
.get(0).getId(), currentTask.get(0).getName()),
commonBiz.getProjectId());
} protected boolean isNewChange(Project project) {
if (StringUtils.isBlank(project.getChangeCurrentStateFromStatusInfo())
|| project.getChangeCurrentStateFromStatusInfo()
.compareToIgnoreCase(
ChangeConstants.State.CODE_STARTEVENT) == 0
|| project.getChangeCurrentStateFromStatusInfo()
.compareToIgnoreCase(
ChangeConstants.State.CODE_ENDEVENT) == 0)
return true;
return false;
} /**
* 决策委员会审批变更 B001 、部门经理审批变更 B002
*
* @param id
*/
@RequestMapping(value = "/approvalChange", method = { RequestMethod.GET,
RequestMethod.POST })
public @ResponseBody String approvalChange(CommonBiz commonBiz) {
ResultVO<Object> result = new ResultVO<>();
// 设置项目更改流程先前状态信息
Task perTask = updateChangeStatusInfo(commonBiz,
ProjectService.CHANGEPREVIOUSSTATE); // 设置部门经理审批变更流程变量
Map<String, Object> variables = new HashMap<String, Object>();
// 获取评审结论
String stringResult = commonBiz.getResult(); ApprovalResult approvalResult = JsonUtil.fromJson(stringResult,
ApprovalResult.class);
variables.put(perTask.getTaskDefinitionKey()
+ ChangeConstants.ActivitiContextKey.result,
approvalResult.getResult()); userTaskServiceImpl.completeTask(perTask.getId(), variables); // 设置项目更改流程当前状态信息
Task currTask = updateChangeStatusInfo(commonBiz,
ProjectService.CHANGECURRENTSTATE);
String perstate = "";
if (currTask != null) {
perstate = userTaskServiceImpl.getStateByTaskDefinitionKey(perTask
.getTaskDefinitionKey());
String currstate = userTaskServiceImpl
.getStateByTaskDefinitionKey(currTask
.getTaskDefinitionKey()); //决策委员会或者部门经理
logger.debug("审批变更状态:" + perstate + "-->" + currstate); } else {
logger.debug("流程结束:" + "Status:endChangeProcess");
} return result.toString();
} private Task updateChangeStatusInfo(CommonBiz commonBiz, String porperty) {
Task task = null;
try {
// 设置主项目表中的变更流程的状态
List<Task> currentTask = userTaskServiceImpl
.findActiveTaskByProcessInstanceId(commonBiz
.getProcessInstanceId());
// 获取项目对象
Project project = projectService.selectByPrimaryKey(commonBiz
.getProjectId()); String status = "";
String changeCurrentState = "";
String defKey = "";
if (currentTask.size() == 0) {
logger.debug("当前任务列表为空,无法更新状态信息!"); // 构建StateInfo对象
StateInfo endProcessStateInfo = new StateInfo();
endProcessStateInfo
.setState(ChangeConstants.State.CODE_ENDEVENT);
endProcessStateInfo
.setStateName(ChangeConstants.State.ENDEVENT);
endProcessStateInfo.setTaskId("");
// 更改流程当前任务状态信息
String endStatus = JsonUtil.toJson(endProcessStateInfo);
status = endStatus;
changeCurrentState = endStatus; } else {
// 获取task对象
task = currentTask.get(0);
defKey = userTaskServiceImpl
.getStateByTaskDefinitionKey(currentTask.get(0)
.getTaskDefinitionKey());
status = project.makeChangeCurrentStateFromStatusInfo(defKey,
currentTask.get(0).getId(), currentTask.get(0).getName());
}
commonBizService.updateChangeStatusByDataId(status,
commonBiz.getDataId());
projectServiceImpl.updateProjectPropertyByProjectId(porperty,
status, commonBiz.getProjectId());
} catch (Exception e) {
e.printStackTrace();
}
return task;
} /**
* 根据projectId查询CommonBiz对象
*
* @param projectId
* 项目ID
* @return
*/
@RequestMapping(value = "/getCommonBizByProjectId", method = {
RequestMethod.GET, RequestMethod.POST })
public @ResponseBody String getCommonBizByProjectId(Long projectId) {
ResultVO<Project> resultVO = new ResultVO<Project>();
try {
CommonBiz commonBiz = commonBizService
.getCommonBizByProjectId(projectId);
resultVO.setOthers("commonBiz", commonBiz);
} catch (Exception e) {
resultVO.setResult(resultVO.FAILURE);
e.printStackTrace();
}
return resultVO.toString();
}
}

activiti_change_ok的更多相关文章

随机推荐

  1. airflow docker

    https://github.com/puckel/docker-airflow 镜像介绍:https://hub.docker.com/r/puckel/docker-airflow/ docker ...

  2. 转:Spring系列之beanFactory与ApplicationContext

    原文地址:Spring系列之beanFactory与ApplicationContext 一.BeanFactoryBeanFactory 是 Spring 的“心脏”.它就是 Spring IoC ...

  3. LeetCode 258 Add Digits 解题报告

    题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  4. BouncyCastle 密钥转换 - Java

    转自: https://blog.csdn.net/a351945755/article/details/63707040 1. PKCS#8 转 PKCS#1 You will need Bounc ...

  5. kubernetes的Controller Manager

    1. Controller Manager简介 Controller Manager作为集群内部的管理控制中心,负责集群内的Node.Pod副本.服务端点(Endpoint).命名空间(Namespa ...

  6. java 集合(三)List接口

    package cn.sasa.demo1; import java.util.ArrayList; import java.util.LinkedList; import java.util.Lis ...

  7. 递归、嵌套for循环、map集合方式实现树形结构菜单列表查询

    有时候, 我们需要用到菜单列表,但是怎么样去实现一个菜单列表的编写呢,这是一重要的问题. 比如我们需要编写一个树形结构的菜单,那么我们可以使用JQuery的zTree插件:http://www.tre ...

  8. import Vue form 'vue’的意思

    1.import Vue form ‘vue’ 写全的话是import Vue from ‘…/nodemouls/vue/list/vue.js’: 此时在webpack.base.conf.js中 ...

  9. rsyncd的配置和使用

    服务器端配置文件说明 # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for mo ...

  10. Unity3D加密流程文档

    一键搞定源代码.资源等保护 代码被反编译破解,无可奈何? Unity3D 开发的软件程序,无论使用虚拟化,还是混淆,都无法抵抗住黑客丧心病狂的破解,轻松被反编译,令开发商无比头疼. 图片等资源保护被盗 ...