|
@@ -0,0 +1,229 @@
|
|
|
+package com.platform.service.business;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.platform.activiti.bean.ActObj;
|
|
|
+import com.platform.activiti.dao.entity.ActApplyInfo;
|
|
|
+import com.platform.activiti.dao.entity.ActAuditRecord;
|
|
|
+import com.platform.activiti.enums.ActApplyEnum;
|
|
|
+import com.platform.activiti.service.ActivitiService;
|
|
|
+import com.platform.common.constant.CommonConstants;
|
|
|
+import com.platform.common.enums.ResultCode;
|
|
|
+import com.platform.common.exception.BusinessException;
|
|
|
+import com.platform.common.model.UserInfo;
|
|
|
+import com.platform.common.util.SecurityUtils;
|
|
|
+import com.platform.dao.dto.sb.SbInfoDTO;
|
|
|
+import com.platform.dao.dto.upms.SysDeptDTO;
|
|
|
+import com.platform.dao.dto.upms.SysUserDTO;
|
|
|
+import com.platform.dao.entity.sb.SbInfo;
|
|
|
+import com.platform.dao.entity.upms.SysUser;
|
|
|
+import com.platform.dao.enums.DeptCodeEnum;
|
|
|
+import com.platform.dao.enums.DeptNatureEnum;
|
|
|
+import com.platform.dao.enums.PurchaseAuditNodeEnum;
|
|
|
+import com.platform.dao.enums.SbInfoStatusEnum;
|
|
|
+import com.platform.dao.mapper.upms.SysDeptMapper;
|
|
|
+import com.platform.dao.mapper.upms.SysUserMapper;
|
|
|
+import com.platform.dao.vo.SysUserVO;
|
|
|
+import com.platform.dao.vo.query.upms.SysDeptVO;
|
|
|
+import com.platform.service.sb.SbInfoService;
|
|
|
+import com.platform.service.upms.SysDeptService;
|
|
|
+import com.platform.service.upms.SysUserService;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.activiti.engine.*;
|
|
|
+import org.activiti.engine.history.HistoricDetail;
|
|
|
+import org.activiti.engine.history.HistoricProcessInstance;
|
|
|
+import org.activiti.engine.history.HistoricVariableUpdate;
|
|
|
+import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
|
|
|
+import org.activiti.engine.runtime.ProcessInstance;
|
|
|
+import org.activiti.engine.task.Task;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 工作流业务
|
|
|
+ * @Author chenyuehu
|
|
|
+ * @Date 2020/11/19
|
|
|
+ * @Version Copyright (c) 2019,安徽阡陌网络科技有限公司 All rights reserved.
|
|
|
+ */
|
|
|
+@AllArgsConstructor
|
|
|
+@Service("sbInfoActivitiBusinessService")
|
|
|
+@Slf4j
|
|
|
+public class SbInfoActivitiBusinessService {
|
|
|
+
|
|
|
+ private final SbInfoService sbInfoService;
|
|
|
+ private final IdentityService identityService;
|
|
|
+ private final RuntimeService runtimeService;
|
|
|
+ private final HistoryService historyService;
|
|
|
+ private final TaskService taskService;
|
|
|
+ private final SysUserService sysUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动,第一个审批人为发起人,自动审批。
|
|
|
+ * 审批设置对象:ActApplyInfo
|
|
|
+ * @param sbInfoDTO
|
|
|
+ */
|
|
|
+ public ProcessInstance start(SbInfoDTO sbInfoDTO){
|
|
|
+ // 设置发起对象,保存到流程实例中
|
|
|
+ ActApplyInfo applyInfo = new ActApplyInfo(sbInfoDTO.getId(),
|
|
|
+ ActApplyEnum.SB_INFO_SCRAP.getName(), ActApplyEnum.SB_INFO_SCRAP.getKey(),
|
|
|
+ 0,
|
|
|
+ LocalDateTime.now());
|
|
|
+ Map<String, Object> map = new HashMap<>(1);
|
|
|
+ UserInfo user = SecurityUtils.getUserInfo();
|
|
|
+ applyInfo.setUserId(user.getUserId());
|
|
|
+ applyInfo.setUserName(user.getRealName());
|
|
|
+ map.put("data", applyInfo);
|
|
|
+ map.put("userId", applyInfo.getUserId());
|
|
|
+ // 设置流程启动人信息,为了后面查询我的发起列表
|
|
|
+ identityService.setAuthenticatedUserId(user.getUserId());
|
|
|
+
|
|
|
+ ProcessInstance instance = null;
|
|
|
+ if(StringUtils.isBlank(sbInfoDTO.getProcessInstanceId())){
|
|
|
+ instance = runtimeService.startProcessInstanceByKey(ActApplyEnum.SB_INFO_SCRAP.getKey(), map);
|
|
|
+ }else{ // 再次提交审批的,之前被拒绝的
|
|
|
+ instance = runtimeService.startProcessInstanceById(sbInfoDTO.getProcessInstanceId(), map);
|
|
|
+ }
|
|
|
+ if (instance == null) {
|
|
|
+ throw new BusinessException(ResultCode.ACTIVITI_PROCESS_NOT_EXIST.getDescription());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 第一个审批人为发起人,自动审批。
|
|
|
+ // 设置审批对象,保存到task实例中,每次审核都要更新追加这个对象
|
|
|
+ Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).orderByTaskCreateTime().desc().singleResult();
|
|
|
+ task.setAssignee(user.getUserId());
|
|
|
+ taskService.saveTask(task);
|
|
|
+ List<ActAuditRecord> records = new ArrayList<>();
|
|
|
+ records.add(new ActAuditRecord(user.getUserId(), user.getRealName(), "提交",
|
|
|
+ LocalDateTime.now(), true, task.getId(), task.getName()));
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put(CommonConstants.ACT_AUDIT_RECORD, records);
|
|
|
+ data.put("result", 1);
|
|
|
+ taskService.addComment(task.getId(), sbInfoDTO.getProcessInstanceId(), "提交");
|
|
|
+ taskService.complete(task.getId(), map);
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动,第一个审批人为发起人,自动审批。
|
|
|
+ * 审批设置对象:ActApplyInfo
|
|
|
+ * @param sbInfoDTO
|
|
|
+ */
|
|
|
+ public boolean audit(SbInfoDTO sbInfoDTO){
|
|
|
+ UserInfo user = SecurityUtils.getUserInfo();
|
|
|
+ // 设置发起对象,保存到流程实例中
|
|
|
+ String taskId = sbInfoDTO.getTaskId();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ Integer result = 0;
|
|
|
+ if (sbInfoDTO.getAuditFlag()) {
|
|
|
+ result = 1;// 同意
|
|
|
+ } else {
|
|
|
+ result = 0;// 拒绝
|
|
|
+ }
|
|
|
+ map.put("result", result);
|
|
|
+ Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
|
|
|
+ Map<String, Object> variables = taskService.getVariables(taskId);
|
|
|
+ List<ActAuditRecord> records = new ArrayList<>();
|
|
|
+ Object o = variables.get(CommonConstants.ACT_AUDIT_RECORD);
|
|
|
+ if (o != null) {
|
|
|
+ records = (List<ActAuditRecord>) o;
|
|
|
+ }
|
|
|
+ boolean flag = result==0?false:true;
|
|
|
+ records.add(0, new ActAuditRecord(user.getUserId(), user.getRealName(), sbInfoDTO.getRefuseReason(),
|
|
|
+ LocalDateTime.now(), result==0?false:true, taskId, task.getName()));
|
|
|
+ map.put(CommonConstants.ACT_AUDIT_RECORD, records);
|
|
|
+ taskService.addComment(taskId, sbInfoDTO.getProcessInstanceId(), sbInfoDTO.getRefuseReason());
|
|
|
+ taskService.complete(taskId, map);
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 终止审核
|
|
|
+ * 审批设置对象:ActApplyInfo
|
|
|
+ * @param sbInfoDTO
|
|
|
+ */
|
|
|
+ public void stop(SbInfoDTO sbInfoDTO){
|
|
|
+ String processInstanceId = sbInfoDTO.getProcessInstanceId();
|
|
|
+ if (StringUtils.isNotEmpty(processInstanceId)) {
|
|
|
+ runtimeService.deleteProcessInstance(processInstanceId, "");
|
|
|
+ historyService.deleteHistoricProcessInstance(processInstanceId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转签
|
|
|
+ * 审批设置对象:ActApplyInfo
|
|
|
+ * @param sbInfoDTO
|
|
|
+ */
|
|
|
+ public void assign(SbInfoDTO sbInfoDTO){
|
|
|
+ String taskId = sbInfoDTO.getTaskId();
|
|
|
+ String userId = sbInfoDTO.getChangeUser();
|
|
|
+ if (StringUtils.isEmpty(taskId)) {
|
|
|
+ throw new BusinessException("taskId为空,请选择");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(userId)) {
|
|
|
+ throw new BusinessException("请设置转签人员");
|
|
|
+ }
|
|
|
+ SysUser sysUser = sysUserService.getModelById(userId);
|
|
|
+ if(sysUser == null){
|
|
|
+ throw new BusinessException("用户不存在,请选择其他人员");
|
|
|
+ }
|
|
|
+ taskService.setAssignee(taskId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 审核历史:都在ActRecord里面
|
|
|
+ * 审批设置对象:ActApplyInfo
|
|
|
+ * @param processInstanceId
|
|
|
+ */
|
|
|
+ public List<ActAuditRecord> history(String processInstanceId){
|
|
|
+ ProcessInstance instance = runtimeService.createProcessInstanceQuery()
|
|
|
+ .processInstanceId(processInstanceId).singleResult();
|
|
|
+ //保证运行ing
|
|
|
+ List<ActAuditRecord> records = new ArrayList<>();
|
|
|
+ if (instance != null) {
|
|
|
+ Task task = this.taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
|
|
|
+ Map<String, Object> variables = taskService.getVariables(task.getId());
|
|
|
+ Object o = variables.get(CommonConstants.ACT_AUDIT_RECORD);
|
|
|
+ if (o != null) {
|
|
|
+ /*获取历史审核信息*/
|
|
|
+ records = (List<ActAuditRecord>) o;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ List<HistoricDetail> list = historyService.createHistoricDetailQuery().
|
|
|
+ processInstanceId(processInstanceId).orderByTime().desc().list();
|
|
|
+ if (CollectionUtil.isNotEmpty(list)) {
|
|
|
+ HistoricDetail historicDetail = list.get(0);
|
|
|
+ HistoricVariableUpdate variable = (HistoricVariableUpdate) historicDetail;
|
|
|
+ String variableName = variable.getVariableName();
|
|
|
+ if (CommonConstants.ACT_AUDIT_RECORD.equals(variableName)) {
|
|
|
+ records.clear();
|
|
|
+ records.addAll((List<ActAuditRecord>) variable.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return records;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判定流程是否结束
|
|
|
+ * @param processInstanceId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean checkFinish(String processInstanceId) {
|
|
|
+ HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
|
|
|
+ if (Objects.isNull(historicProcessInstance)) {
|
|
|
+ log.info("完成1");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (Objects.isNull(historicProcessInstance.getEndTime())) {
|
|
|
+ log.info("未完成");
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ log.info("完成2");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|