tudc 3 tahun lalu
induk
melakukan
b091434ea9

+ 6 - 0
flowable/pom.xml

@@ -47,6 +47,12 @@
             <artifactId>spring-boot-configuration-processor</artifactId>
             <optional>true</optional>
         </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-aop</artifactId>
+        </dependency>
+
         <!--flowable  set -->
         <dependency>
             <groupId>org.flowable</groupId>

+ 21 - 0
flowable/src/main/java/com/huyi/flowable/BaseException.java

@@ -0,0 +1,21 @@
+package com.huyi.flowable;
+
+public class BaseException extends Exception {
+    private String defaultMessage;
+
+    public BaseException(String defaultMessage) {
+        this.defaultMessage = defaultMessage;
+    }
+
+    public BaseException() {
+        this.defaultMessage = "系统异常!";
+    }
+
+    public String getDefaultMessage() {
+        return this.defaultMessage;
+    }
+
+    public void setDefaultMessage(String defaultMessage) {
+        this.defaultMessage = defaultMessage;
+    }
+}

+ 9 - 0
flowable/src/main/java/com/huyi/flowable/annotation/Log.java

@@ -0,0 +1,9 @@
+package com.huyi.flowable.annotation;
+
+import java.lang.annotation.*;
+
+@Target({ElementType.PARAMETER, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Log {
+}

+ 7 - 2
flowable/src/main/java/com/huyi/flowable/api/FlowProcess.java

@@ -67,8 +67,13 @@ public class FlowProcess extends FlowableConfig {
                                 .businessKey(businessKey)                                       // 业务表主键
                                 .start();                                                       // 启动(即创建)流程实例
         Authentication.setAuthenticatedUserId(null); // 这个方法最终使用一个ThreadLocal类型的变量进行存储,也就是与当前的线程绑定,所以流程实例启动完毕之后,需要设置为null,防止多线程的时候出问题。
-        // 增加消息通知
-        messageService.addStartFlowMessage(instance);
+        try {
+            // 增加消息通知
+            messageService.addStartFlowMessage(instance.getId());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
         return instance;
     }
 

+ 13 - 7
flowable/src/main/java/com/huyi/flowable/api/FlowTask.java

@@ -145,14 +145,17 @@ public class FlowTask extends FlowableConfig {
      * @param files                     附件信息
      * @param attachmentDescription     附件描述
      */
-    public BaseResult completeTask(String taskId, String type, String message, MultipartFile[] files, String attachmentDescription, String userId) throws IOException {
+    public BaseResult completeTask(String taskId, String type, String message, MultipartFile[] files, String attachmentDescription, String userId) {
         // 任务处理人与当前人员是否匹配
         Page<Map<String, Object>> taskList = getMyTask(userId, taskId, null, 1, 1);
         if (taskList.getTotal() == 0) {
             return new BaseResult("500", "任务与当前人员不匹配或任务已完成!", null);
         }
+        Task task;
+        ProcessInstance instance;
         try {
-            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
+            task = taskService.createTaskQuery().taskId(taskId).singleResult();
+            instance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
             // 添加意见
             if (!StringUtils.isEmpty(message)) {
                 taskService.addComment(taskId, task.getProcessInstanceId(), type, message);
@@ -166,17 +169,20 @@ public class FlowTask extends FlowableConfig {
             }
             // 完成任务
             taskService.complete(taskId);
-
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new BaseResult("500", "任务处理失败!", null);
+        }
+        try {
             // 消息通知
-            ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
             // 消息通知完成状态修改
-            messageService.completeMessage(instance);
+            messageService.completeMessage(task.getProcessInstanceId());
             // 新增消息通知
-            messageService.addStartFlowMessage(instance);
+            messageService.addStartFlowMessage(task.getProcessInstanceId());
         } catch (Exception e) {
             e.printStackTrace();
-            return new BaseResult("500", "任务处理失败!", null);
         }
+
         return new BaseResult();
     }
 

+ 39 - 0
flowable/src/main/java/com/huyi/flowable/aspect/LogAspect.java

@@ -0,0 +1,39 @@
+package com.huyi.flowable.aspect;
+
+import com.alibaba.fastjson.JSONObject;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+@Aspect
+@Component
+public class LogAspect {
+    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
+
+    @Pointcut("@annotation(com.huyi.flowable.annotation.Log)")
+    public void logPointCut() {
+    }
+
+    @Around("logPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        long beginTime = System.currentTimeMillis();
+        Object[] args = point.getArgs();
+        int i = 0;
+        Object[] var7 = args;
+        int var8 = args.length;
+
+        for(int var9 = 0; var9 < var8; ++var9) {
+            Object object = var7[var9];
+            log.info("{}第{}个入参为{}", new Object[]{point.getSignature(), i++, object});
+        }
+
+        Object result = point.proceed();
+        long time = System.currentTimeMillis() - beginTime;
+        log.info("{}结果{}----耗时----{}ms", new Object[]{point.getSignature(), JSONObject.toJSONString(result), time});
+        return result;
+    }
+}

+ 14 - 4
flowable/src/main/java/com/huyi/flowable/controller/FlowController.java

@@ -1,7 +1,9 @@
 package com.huyi.flowable.controller;
 
 import com.github.pagehelper.Page;
+import com.huyi.flowable.BaseException;
 import com.huyi.flowable.BaseResult;
+import com.huyi.flowable.annotation.Log;
 import com.huyi.flowable.api.FlowModel;
 import com.huyi.flowable.api.FlowProcess;
 import com.huyi.flowable.api.FlowTask;
@@ -101,10 +103,11 @@ public class FlowController {
      * @throws IOException      文件异常
      * @throws JDOMException    格式化异常
      */
+    @Log
     @RequestMapping(value = "uploadXml")
     @ResponseBody
     @Transactional(rollbackFor = Exception.class)
-    public Integer uploadXml(@RequestBody Map requestParam) throws IOException, JDOMException {
+    public BaseResult uploadXml(@RequestBody Map requestParam) throws JDOMException, BaseException, IOException {
         // 文件名
         String fileName = String.valueOf(requestParam.get("fileName"));
         // xml内容
@@ -118,6 +121,9 @@ public class FlowController {
         // 节点数据
         List nodeFormList = (List)requestParam.get("nodeFormList");
 
+        // 数据校验
+        XmlUtil.checkUserTask(fileContent);
+
         Map<String, Object> param = new HashMap<>();
         param.put("menuId", menuId);
         param.put("companyId", companyId);
@@ -130,13 +136,14 @@ public class FlowController {
             // 与其他流程的key重复
             if (processDefinition != null) {
                 System.out.println("流程的key重复");
-                return 0;
+                return new BaseResult("500", "流程的key重复", null);
             }
             File file = FileUtil.createFile(fileName, fileContent);
             Deployment deployment = flowModel.deployFlowXml(file.getAbsolutePath());
             boolean delete = file.delete();
             if (!delete) {
                 System.out.println("文件删除失败!");
+                return new BaseResult("500", "文件删除失败!", null);
             }
             processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
 
@@ -161,7 +168,7 @@ public class FlowController {
                 Deployment deployment = flowModel.deployFlowXml(file.getAbsolutePath());
                 boolean delete = file.delete();
                 if (!delete) {
-                    System.out.println("文件删除失败!");
+                    return new BaseResult("500", "文件删除失败!", null);
                 }
                 processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
 
@@ -188,7 +195,7 @@ public class FlowController {
                 nodeFormMapper.insertNodeForm(nodeJson);
             }
         }
-        return 1;
+        return new BaseResult("200", "上传成功!", null);
     }
 
     /**
@@ -345,6 +352,9 @@ public class FlowController {
         Map<String, Object> param = new HashMap<>();
         param.put("excutionName", excutionName);
         param.put("processDefName", processDefName);
+        System.out.println(flowTask);
+        System.out.println(loginUser);
+        System.out.println(loginUser.getSysUser());
         Page<Map<String, Object>> page = flowTask.getMyTask(String.valueOf(loginUser.getSysUser().getUserId()), param, pageNum, pageSize);
         return new BaseResult(page);
     }

+ 18 - 0
flowable/src/main/java/com/huyi/flowable/controllerAdvice/MyGlobalExceptionHandler.java

@@ -0,0 +1,18 @@
+package com.huyi.flowable.controllerAdvice;
+
+import com.huyi.flowable.BaseException;
+import com.huyi.flowable.BaseResult;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+@ControllerAdvice
+public class MyGlobalExceptionHandler {
+
+    @ExceptionHandler(BaseException.class)
+    @ResponseBody
+    public BaseResult customException(BaseException e) {
+        e.printStackTrace();
+        return new BaseResult("500", e.getDefaultMessage(), null);
+    }
+}

+ 12 - 6
flowable/src/main/java/com/huyi/flowable/message/MessageService.java

@@ -1,14 +1,13 @@
 package com.huyi.flowable.message;
 
-import com.huyi.flowable.api.FlowProcess;
 import com.huyi.flowable.mapper.MeHistoryMapper;
 import com.huyi.flowable.server.mapper.MessageMapper;
-import net.sf.json.JSONObject;
 import org.apache.commons.lang3.StringUtils;
+import org.flowable.engine.HistoryService;
 import org.flowable.engine.IdentityService;
-import org.flowable.engine.ProcessEngine;
 import org.flowable.engine.RuntimeService;
 import org.flowable.engine.TaskService;
+import org.flowable.engine.history.HistoricProcessInstance;
 import org.flowable.engine.runtime.ProcessInstance;
 import org.flowable.identitylink.api.IdentityLink;
 import org.flowable.identitylink.api.IdentityLinkInfo;
@@ -31,13 +30,18 @@ public class MessageService {
     protected MeHistoryMapper meHistoryMapper;
     @Autowired
     protected RuntimeService runtimeService;
+    @Autowired
+    protected HistoryService historyService;
 
     //TODO 不同项目不适用
-    public void addStartFlowMessage(ProcessInstance instance){
+    public void addStartFlowMessage(String instanceId){
+        ProcessInstance instance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();
         if (instance == null) {
             return;
         }
+        System.out.println("------获取参数配置--start-------");
         Map<String, Object> paras = runtimeService.getVariables(instance.getId());
+        System.out.println("------获取参数配置--end-------");
 //        Map<String, Object> paras = instance.getProcessVariables();
         System.out.println("paras======>"+paras);
         // 获取公司ID
@@ -57,7 +61,7 @@ public class MessageService {
         if ("1000000006".equals(menuId)) {
             param.put("workType", "08");
             //TODO 加标题
-            param.put("title","【融资审批】您有一笔" + map.get("zfrAmountCN") + "(" + map.get("zfrAmount") + ")的融资申请待内部审批");
+            param.put("title","【融资审批】您有一笔" + paras.get("zfrAmountCN") + "(" + paras.get("zfrAmount") + ")的融资申请待内部审批");
         // 融信审批
         } else {
 //            Map<String, Object> financeInf = messageMapper.queryFinanceInf(businessKey);
@@ -117,7 +121,9 @@ public class MessageService {
         }
     }
 
-    public void completeMessage(ProcessInstance instance){
+    public void completeMessage(String instanceId){
+        HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
+
         Map<String, Object> param = new HashMap<>();
         String businessKey = instance.getBusinessKey();
         param.put("tableId", businessKey);

+ 44 - 4
flowable/src/main/java/com/huyi/flowable/util/XmlUtil.java

@@ -1,5 +1,8 @@
 package com.huyi.flowable.util;
 
+import com.huyi.flowable.BaseException;
+import org.apache.commons.lang3.StringUtils;
+import org.jdom.Attribute;
 import org.jdom.Document;
 import org.jdom.Element;
 import org.jdom.JDOMException;
@@ -7,10 +10,8 @@ import org.jdom.input.SAXBuilder;
 import org.jdom.output.Format;
 import org.jdom.output.XMLOutputter;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
+import java.util.List;
 
 public class XmlUtil {
     public static String getNodeValue(String xml, String attribute) throws JDOMException, IOException {
@@ -49,4 +50,43 @@ public class XmlUtil {
         xmlout.output(doc, bo);
         return bo.toString();
     }
+
+    public static boolean checkUserTask(String xml) throws BaseException, IOException, JDOMException {
+        InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
+        SAXBuilder saxBuilder = new SAXBuilder();
+        Document document = saxBuilder.build(stream);
+        Element rootElement = document.getRootElement();
+        Element processElement = rootElement.getChild("process", rootElement.getNamespace());
+        List<Element> userTaskList = processElement.getChildren("userTask", rootElement.getNamespace());
+        for (Element userTask : userTaskList) {
+            List<Attribute> attributeList = userTask.getAttributes();
+
+            boolean flag = false;
+            for (Attribute attribute : attributeList) {
+                // 指定人员
+                if ("assignee".equals(attribute.getName())) {
+                    if (StringUtils.isEmpty(attribute.getValue())) {
+                        throw new BaseException("请选择指定人员!");
+                    }
+                    flag = true;
+                // 候选人员
+                } else if ("candidateUsers".equals(attribute.getName())) {
+                    if (StringUtils.isEmpty(attribute.getValue())) {
+                        throw new BaseException("请选择候选人员!");
+                    }
+                    flag = true;
+                // 候选组
+                } else if ("candidateGroups".equals(attribute.getName())) {
+                    if (StringUtils.isEmpty(attribute.getValue())) {
+                        throw new BaseException("请选择候选组!");
+                    }
+                    flag = true;
+                }
+            }
+            if (!flag) {
+                throw new BaseException("请选择人员类型!");
+            }
+        }
+        return true;
+    }
 }