Bladeren bron

企业初始化流程图接口

dudm 3 jaren geleden
bovenliggende
commit
8e8bea878c
1 gewijzigde bestanden met toevoegingen van 257 en 1 verwijderingen
  1. 257 1
      flowable/src/main/java/com/huyi/flowable/controller/ApiController.java

+ 257 - 1
flowable/src/main/java/com/huyi/flowable/controller/ApiController.java

@@ -1,21 +1,48 @@
 package com.huyi.flowable.controller;
 
+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.entity.CusMeHistory;
+import com.huyi.flowable.entity.CusMeconfig;
+import com.huyi.flowable.mapper.MeHistoryMapper;
+import com.huyi.flowable.mapper.MenuMapper;
+import com.huyi.flowable.server.mapper.ConfigMapper;
+import com.huyi.flowable.util.FileUtil;
+import com.huyi.flowable.util.HttpClient;
+import com.huyi.flowable.util.JsonUtil;
+import com.huyi.flowable.util.XmlUtil;
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
 import org.apache.commons.lang3.StringUtils;
 import org.flowable.engine.IdentityService;
+import org.flowable.engine.RepositoryService;
+import org.flowable.engine.repository.Deployment;
+import org.flowable.engine.repository.ProcessDefinition;
 import org.flowable.idm.api.Group;
 import org.flowable.idm.api.User;
 import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;
 import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
+import org.jdom.JDOMException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
-import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.*;
+import java.util.*;
 
 /**
  * @Description     集成接口,用户,组织结构等
@@ -27,6 +54,25 @@ public class ApiController {
     @Autowired
     IdentityService identityService;
 
+    @Autowired
+    HttpClient httpClient;
+
+    @Autowired
+    private MenuMapper menuMapper;
+
+    @Autowired
+    private RepositoryService repositoryService;
+
+    @Autowired
+    private MeHistoryMapper meHistoryMapper;
+
+    @Autowired
+    private FlowModel flowModel;
+
+    @Autowired
+    ConfigMapper configMapper;
+
+
     /**
      * 新增用户
      * @param id        用户id
@@ -189,4 +235,214 @@ public class ApiController {
         identityService.deleteGroup(groupId);
         return baseResult;
     }
+
+    /**
+     * 初始化企业流程图
+     * @param companyId
+     * @param personId
+     * @return
+     */
+    @Log
+    @Transactional(rollbackFor=Exception.class)
+    @RequestMapping(value = "initializeFlow")
+    @ResponseBody
+    public BaseResult initializeFlow (@RequestParam("companyId") String companyId, @RequestParam("personId") String personId, HttpServletRequest request) throws JDOMException, BaseException, IOException {
+        BaseResult baseResult = new BaseResult();
+
+        // 请求获取参数
+        String menuListStr = httpClient.doPost(HttpClient.GET_MENU, null, request);
+        menuListStr = menuListStr.replace(":null", ":\"\"");
+        Map dataMap = (Map) JSONObject.fromObject(menuListStr);
+        JSONObject  obc = JSONObject.fromObject(dataMap.get("data"));
+        JSONArray menuListJsonArray = (JSONArray)obc.get("records");
+        //获取审批菜单列表
+        List menuList = JsonUtil.JsonArrayToArrayList(menuListJsonArray);
+        //先查询流程表的数据是否存在
+        if(menuList.size() > 0) {
+            List<Map<String, String>> newList = menuList;
+            for (int i = 0; i < newList.size(); i++) {
+                //获取编号
+                String  papId = newList.get(i).get("papId");
+                //获取审批名称
+                String  papBusinessName = newList.get(i).get("papBusinessName");
+                //新增审批配置
+                CusMeconfig config = new CusMeconfig();
+                //编号getMenuList
+                config.setMenuId(papId);
+                //公司
+                config.setCompanyId(companyId);
+                //是否审批
+                config.setModel("1");
+                //获取表名
+                String  papTableName = newList.get(i).get("papTableName");
+                config.setTableName(papTableName);
+                //获取类型
+                String  papType = newList.get(i).get("papType");
+                config.setType(papType);
+                //获取链接
+                String  papUrl = newList.get(i).get("papUrl");
+                //获取回调地址
+                String papTokenUrl = newList.get(i).get("papTokenUrl");
+                config.setCallBackUrl(papTokenUrl);
+                //视图
+                if("0".equals(papType)){
+                    config.setFormJson(papUrl);
+                }//自定义链接
+                else if("1".equals(papType)){
+                    config.setCustomUrl(papUrl);
+                }
+                menuMapper.insertMenuModel(config);
+                //获取操作文件夹路径
+                String xml_template_url = configMapper.querySysConfig("xml_template_url").get("value");
+                //源文件地址
+                String filePath = xml_template_url.concat("template.bpmn20.xml");
+                //目标文件地址
+                String newFilePath = xml_template_url.concat(papBusinessName + ".bpmn20.xml");
+                // 1、创建 File 对象,映射 XML 文件
+                File file = new File(filePath);
+                //流程id
+                String flowKey = UUID.randomUUID().toString();
+                ProcessDefinition processDefinition;
+                processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey(flowKey).singleResult();
+                // 与其他流程的key重复
+                if (processDefinition != null) {
+                    System.out.println("流程的key重复");
+                    return new BaseResult("500", "流程的key重复", null);
+                }
+                //修改文件配置
+                updateXML(file,flowKey,personId);
+                InputStream stream = new FileInputStream(file);
+                String fileContent = FileUtil.inStreamToString(stream);
+                // 数据校验
+                XmlUtil.checkUserTask(fileContent);
+                //复制文件
+                copyFile(filePath, newFilePath);
+                File copayfile = new File(newFilePath);
+                Deployment deployment = flowModel.deployFlowXml(copayfile.getAbsolutePath());
+                boolean delete = copayfile.delete();
+                if (!delete) {
+                    System.out.println("文件删除失败!");
+                    return new BaseResult("500", "文件删除失败!", null);
+                }
+                processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
+
+                // 修改菜单配置和新增历史数据
+                updateMenuProcdefIdAndInsertHistory(papId, companyId, processDefinition.getId());
+            }
+        }
+        return baseResult;
+    }
+
+
+    private void updateMenuProcdefIdAndInsertHistory(String menuId, String companyId, String prodefId) {
+        Map<String, Object> param = new HashMap<>();
+        param.put("menuId", menuId);
+        param.put("companyId", companyId);
+        List<Map<String, Object>> menuList = menuMapper.selectMenu(param);
+        if (menuList.size() == 0) {
+            return;
+        }
+        Map<String, Object> menuMap = menuList.get(0);
+
+        // 修改cus_me_config的PROCDEF_ID
+        CusMeconfig meconfig = new CusMeconfig();
+        meconfig.setMenuId(menuId);
+        meconfig.setCompanyId(companyId);
+        meconfig.setProcdefId(prodefId);
+        menuMapper.updateMenuModel(meconfig);
+
+        CusMeHistory m = new CusMeHistory();
+        m.setCompanyId(companyId);
+        m.setMenuId(menuId);
+        m.setProcdefId(prodefId);
+        m.setCreateTime(new Date());
+        m.setTableName(String.valueOf(menuMap.get("tableName")));
+        m.setType(String.valueOf(menuMap.get("type")));
+        m.setFormJson(String.valueOf(menuMap.get("formJson")));
+        m.setCustomUrl(String.valueOf(menuMap.get("customUrl")));
+        meHistoryMapper.insertMeHistory(m);
+    }
+
+    /**
+     * 复制文件
+     * @param srcPathStr        源文件地址
+     * @param desPathStr        目标文件地址
+     * @return
+     */
+    private static void copyFile(String srcPathStr, String desPathStr)
+    {
+        try
+        {
+            //创建输入流对象
+            FileInputStream fis = new FileInputStream(srcPathStr);
+            //创建输出流对象
+            FileOutputStream fos = new FileOutputStream(desPathStr);
+            //创建搬运工具
+            byte datas[] = new byte[1024*8];
+            //创建长度
+            int len = 0;
+            //循环读取数据
+            while((len = fis.read(datas))!=-1)
+            {
+                fos.write(datas,0,len);
+            }
+            //释放资源
+            fos.close();
+            //释放资源
+            fis.close();
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 修改XML文件内容
+     * @param file        源文件
+     * @return
+     */
+    public static void updateXML(File file, String id, String personId) {
+        try {
+            // 2、创建 DocumentBuilderFactory 对象,用来创建 DocumentBuilder 对象
+            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+            // 3、创建 DocumentBuilder 对象,用来将 XML 文件 转化为 Document 对象
+            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
+            // 4、创建 Document 对象,解析 XML 文件
+            Document document = documentBuilder.parse(file);
+
+            // 修改第一个
+            // 5、获取 process 结点
+            Element processElement = (Element) document.getElementsByTagName("process").item(0);
+            Element processId = (Element) processElement.getElementsByTagName("id").item(0);
+            processId.setTextContent(id);
+
+            // 修改第二个
+            // 5、获取 bpmndi 结点
+            Element bpmndiElement = (Element) document.getElementsByTagName("bpmndi:BPMNPlane").item(0);
+            Element bpmndiId = (Element) bpmndiElement.getElementsByTagName("id").item(0);
+            bpmndiId.setTextContent(id);
+
+            // 修改第三个
+            // 5、获取 userTask 结点
+            Element userTaskElement = (Element) document.getElementsByTagName("userTask").item(0);
+            Element userTaskAssignee = (Element) userTaskElement.getElementsByTagName("flowable:assignee").item(0);
+            userTaskAssignee.setTextContent(personId);
+
+            // 9、创建 TransformerFactory 对象
+            TransformerFactory transformerFactory = TransformerFactory.newInstance();
+            // 10、创建 Transformer 对象
+            Transformer transformer = transformerFactory.newTransformer();
+            // 11、创建 DOMSource 对象
+            DOMSource domSource = new DOMSource(document);
+            // 12、创建 StreamResult 对象
+            StreamResult reStreamResult = new StreamResult(file);
+            transformer.transform(domSource, reStreamResult);
+            // 输出测试结果
+            StreamResult consoleResult = new StreamResult(System.out);
+            transformer.transform(domSource, consoleResult);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 }