From 34981c30a78e8bbd7791131059a9210f9928b62c Mon Sep 17 00:00:00 2001
From: 刘光辉 <347230014@qq.com>
Date: 星期四, 17 九月 2026 09:24:09 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master' into master

---
 jnpf-flowable/jnpf-flowable-entity/src/main/java/jnpf/flowable/model/xml/FlowXmlUtil.java |  256 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 256 insertions(+), 0 deletions(-)

diff --git a/jnpf-flowable/jnpf-flowable-entity/src/main/java/jnpf/flowable/model/xml/FlowXmlUtil.java b/jnpf-flowable/jnpf-flowable-entity/src/main/java/jnpf/flowable/model/xml/FlowXmlUtil.java
new file mode 100644
index 0000000..6b57d24
--- /dev/null
+++ b/jnpf-flowable/jnpf-flowable-entity/src/main/java/jnpf/flowable/model/xml/FlowXmlUtil.java
@@ -0,0 +1,256 @@
+package jnpf.flowable.model.xml;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.XML;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.google.common.collect.ImmutableList;
+import jakarta.xml.bind.JAXBContext;
+import jakarta.xml.bind.Marshaller;
+import jnpf.flowable.enums.NodeEnum;
+import jnpf.flowable.model.xml.diagram.*;
+import jnpf.flowable.model.xml.process.*;
+import jnpf.flowable.model.xml.process.Process;
+import jnpf.model.ai.AiFlowModel;
+import jnpf.util.JsonUtil;
+import jnpf.util.StringUtil;
+
+import java.io.StringWriter;
+import java.net.URLEncoder;
+import java.util.*;
+import java.util.stream.Collectors;
+
+
+public class FlowXmlUtil {
+
+    private FlowXmlUtil() {
+    }
+
+
+    public static XmlModel model(String xml, String oldNodeCode, boolean isStart) {
+        String newNodeCode = getNodeCode("Activity_");
+        String newLineCode = getNodeCode("Flow_");
+        xml = StringUtil.isNotEmpty(oldNodeCode) ? xml.replaceAll(oldNodeCode, newNodeCode) : xml;
+        JSONObject jsonObject = XML.toJSONObject(xml);
+        XmlModel xmlModel = JsonUtil.getJsonToBean(jsonObject, XmlModel.class);
+        xmlModel.setNewNodeCode(newNodeCode);
+        xmlModel.setNewLineCode(newLineCode);
+        if (isStart) {
+            return xmlModel;
+        }
+
+        Definitions definitions = xmlModel.getDefinitions();
+
+        Process process = definitions.getProcess();
+
+        // 鑺傜偣閰嶇疆
+        List<Task> userTask = process.getUserTask();
+        Task nodeConfig = userTask.stream().filter(e -> Objects.equals(e.getId(), newNodeCode)).findFirst().orElse(null);
+        // 杩涚嚎閰嶇疆
+        List<Sequence> sequenceFlow = process.getSequenceFlow();
+        Sequence incomLine = sequenceFlow.stream().filter(e -> Objects.equals(e.getTargetRef(), newNodeCode)).findFirst().orElse(null);
+
+        if (nodeConfig == null || incomLine == null) {
+            return xmlModel;
+        }
+
+        // 杩樺師褰撳墠鑺傜偣閰嶇疆
+        Task nodeConfigOld = JsonUtil.getJsonToBean(nodeConfig, Task.class);
+        Sequence incomLineOld = JsonUtil.getJsonToBean(incomLine, Sequence.class);
+        nodeConfigOld.setId(oldNodeCode);
+        nodeConfigOld.setOutgoing(ImmutableList.of(newLineCode));
+        incomLineOld.setTargetRef(oldNodeCode);
+
+        // 鏇存柊鏂拌妭鐐归厤缃�
+        nodeConfig.setIncoming(ImmutableList.of(newLineCode));
+        incomLine.setId(newLineCode);
+        incomLine.setSourceRef(oldNodeCode);
+        Condition conditionExpression = incomLine.getConditionExpression() != null ? incomLine.getConditionExpression() : new Condition();
+        conditionExpression.setContent("${" + newLineCode + "}");
+        incomLine.setConditionExpression(conditionExpression);
+
+        userTask.add(nodeConfigOld);
+        sequenceFlow.add(incomLineOld);
+
+        Diagram bpmnDiagram = definitions.getBpmnDiagram();
+        bpmnDiagram(bpmnDiagram, oldNodeCode, newLineCode);
+
+        return xmlModel;
+    }
+
+    private static void bpmnDiagram(Diagram bpmnDiagram, String oldNodeCode, String newLineCode) {
+        Plane bpmnPlane = bpmnDiagram.getBpmnPlane();
+        List<Shape> bpmnShape = bpmnPlane.getBpmnShape();
+        Shape shape = new Shape();
+        shape.setId(oldNodeCode + "_di");
+        shape.setBpmnElement(oldNodeCode);
+        bpmnShape.add(shape);
+
+        List<Edge> bpmnEdge = bpmnPlane.getBpmnEdge();
+        Edge edge = new Edge();
+        edge.setId(newLineCode + "_di");
+        edge.setBpmnElement(newLineCode);
+        List<Waypoint> waypointList = ImmutableList.of(new Waypoint());
+        edge.setWaypoint(waypointList);
+        bpmnEdge.add(edge);
+    }
+
+    public static String xml(XmlModel xmlModel) {
+        String xml = "";
+        try {
+            Definitions definitions = xmlModel != null ? xmlModel.getDefinitions() : new Definitions();
+            JAXBContext context = JAXBContext.newInstance(Definitions.class);
+            Marshaller marshaller = context.createMarshaller();
+            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // 鏍煎紡鍖栬緭鍑猴紝浣縓ML鏇存槗璇�
+            StringWriter writer = new StringWriter();
+            marshaller.marshal(definitions, writer); // 灏嗗璞¤浆鎹负XML骞跺啓鍏tringWriter
+            xml = URLEncoder.encode(writer.toString(), StringPool.UTF_8).replace("+", "%20");
+        } catch (Exception e) {
+            e.getMessage();
+        }
+        return xml;
+    }
+
+    private static String getNodeCode(String prefix) {
+        return prefix + RandomUtil.randomString(7);
+    }
+
+    public static String getWorkFlowAi(List<AiFlowModel> aiFlowModel) {
+        if (CollUtil.isEmpty(aiFlowModel)) {
+            return "";
+        }
+        AiFlowModel startModel = aiFlowModel.stream().filter(e -> NodeEnum.START.getType().equals(e.getType())).findFirst().orElse(null);
+        if (startModel == null) {
+            return "";
+        }
+        List<AiFlowModel> endList = aiFlowModel.stream().filter(e -> NodeEnum.END.getType().equals(e.getType())).collect(Collectors.toList());
+        List<String> endCodeList = endList.stream().map(AiFlowModel::getEnCode).collect(Collectors.toList());
+        for (AiFlowModel model : aiFlowModel) {
+            List<String> nextId = model.getNextId() != null ? model.getNextId() : new ArrayList<>();
+            Set<String> nextList = new HashSet<>();
+            for (String code : nextId) {
+                AiFlowModel aiModel = aiFlowModel.stream().filter(e -> e.getEnCode().equals(code)).findFirst().orElse(null);
+                if (endCodeList.contains(code) || aiModel == null){
+                    nextList.add(NodeEnum.END.getType());
+                }else {
+                    nextList.add(code);
+                }
+            }
+            if (nextList.isEmpty()){
+                nextList.add(NodeEnum.END.getType());
+            }
+            model.setNextId(new ArrayList<>(nextList));
+        }
+
+        aiFlowModel.removeAll(endList);
+
+        AiFlowModel end = new AiFlowModel();
+        end.setType(NodeEnum.END.getType());
+        end.setEnCode(NodeEnum.END.getType());
+        end.setName("娴佺▼缁撴潫");
+        aiFlowModel.add(end);
+
+        Definitions definitions = new Definitions();
+        List<String> idList = new ArrayList<>();
+        for (AiFlowModel model : aiFlowModel) {
+            model.setId(getNodeCode("Activity_"));
+        }
+        flowXml(definitions, null, startModel, aiFlowModel, idList);
+        XmlModel xmlModel = new XmlModel();
+        xmlModel.setDefinitions(definitions);
+        return xml(xmlModel);
+    }
+
+    private static void flowXml(Definitions definitions, String incoming, AiFlowModel model, List<AiFlowModel> aiFlowModels, List<String> idList) {
+        if (model == null) {
+            return;
+        }
+
+        if (idList.contains(model.getId())) {
+            return;
+        }
+
+        Process process = definitions.getProcess();
+        Diagram bpmnDiagram = definitions.getBpmnDiagram();
+        Plane bpmnPlane = bpmnDiagram.getBpmnPlane();
+
+        idList.add(model.getId());
+
+        Map<String, AiFlowModel> flowCodeMap = new HashMap<>();
+        List<String> nextId = model.getNextId() != null ? model.getNextId() : new ArrayList<>();
+        for (String id : nextId) {
+            List<AiFlowModel> nextModelList = aiFlowModels.stream().filter(e -> Objects.equals(id, e.getEnCode())).collect(Collectors.toList());
+            for (AiFlowModel nextModel : nextModelList) {
+                //绾跨殑璧板悜
+                String flowCode = getNodeCode("Flow_");
+                List<Sequence> sequenceFlow = process.getSequenceFlow() != null ? process.getSequenceFlow() : new ArrayList<>();
+                Sequence sequence = new Sequence();
+                sequence.setId(flowCode);
+                flowCodeMap.put(flowCode, nextModel);
+                sequence.setSourceRef(model.getId());
+                sequence.setTargetRef(nextModel.getId());
+                sequenceFlow.add(sequence);
+                process.setSequenceFlow(sequenceFlow);
+
+                //绾跨殑鐢诲竷
+                List<Edge> bpmnEdge = bpmnPlane.getBpmnEdge() != null ? bpmnPlane.getBpmnEdge() : new ArrayList<>();
+                Edge edge = new Edge();
+                edge.setWaypoint(ImmutableList.of(new Waypoint()));
+                edge.setId(flowCode + "_di");
+                edge.setBpmnElement(flowCode);
+                bpmnEdge.add(edge);
+                bpmnPlane.setBpmnEdge(bpmnEdge);
+            }
+        }
+
+        //寮�濮嬨�佺粨鏉�
+        Event event = new Event();
+        event.setId(model.getId());
+        event.setOutgoing(new ArrayList<>(flowCodeMap.keySet()));
+        if (StringUtil.isNotEmpty(incoming)) {
+            event.setIncoming(ImmutableList.of(incoming));
+        }
+
+        //鐢ㄦ埛鑺傜偣
+        Task task = new Task();
+        task.setId(model.getId());
+        task.setOutgoing(new ArrayList<>(flowCodeMap.keySet()));
+        if (StringUtil.isNotEmpty(incoming)) {
+            task.setIncoming(ImmutableList.of(incoming));
+        }
+
+        boolean isStart = NodeEnum.START.getType().equals(model.getType());
+        boolean isEnd = NodeEnum.END.getType().equals(model.getType());
+
+        Shape shape = new Shape();
+        shape.setId(model.getId() + "_di");
+        shape.setBpmnElement(model.getId());
+
+        if (isStart || isEnd) {
+            if (isStart) {
+                process.setStartEvent(event);
+            } else {
+                process.setEndEvent(event);
+            }
+            Bounds bounds = new Bounds();
+            bounds.setWidth("90");
+            bounds.setHeight("32");
+            shape.setBounds(bounds);
+        } else {
+            List<Task> userTask = process.getUserTask() != null ? process.getUserTask() : new ArrayList<>();
+            userTask.add(task);
+            process.setUserTask(userTask);
+        }
+        List<Shape> shapes = bpmnPlane.getBpmnShape() != null ? bpmnPlane.getBpmnShape() : new ArrayList<>();
+        shapes.add(shape);
+        bpmnPlane.setBpmnShape(shapes);
+        for (Map.Entry<String, AiFlowModel> stringAiFlowModelEntry : flowCodeMap.entrySet()) {
+            String key = stringAiFlowModelEntry.getKey();
+            AiFlowModel nextModel = flowCodeMap.get(key);
+            flowXml(definitions, key, nextModel, aiFlowModels, idList);
+        }
+    }
+
+
+}

--
Gitblit v1.8.0