浏览代码

工作流

cqp 9 月之前
父节点
当前提交
1650875a1d
共有 2 个文件被更改,包括 83 次插入6 次删除
  1. 0 1
      app/Http/Controllers/Api/WorkFlowController.php
  2. 83 5
      app/Service/WorkFlowService.php

+ 0 - 1
app/Http/Controllers/Api/WorkFlowController.php

@@ -61,7 +61,6 @@ class WorkFlowController extends BaseController
         list($status,$data) = $service->create($request->all(),$user);
 
         if($status){
-            if($status == -1) return $this->json_return(202,$data);
             return $this->json_return(200,'',$data);
         }else{
             return $this->json_return(201,$data);

+ 83 - 5
app/Service/WorkFlowService.php

@@ -107,8 +107,6 @@ class WorkFlowService extends Service
             $clear_array = $this->organizationData($array);
             //树结构
             $tree = $this->makeTree(0, $clear_array);
-            //通过节点id 找到当下流程
-//            $aa = $this->createIdToNodeMap($tree);
 
             list($status, $msg) = $this->settleWorkFlowData($data,$array);
             if(! $status) return [false, $msg];
@@ -493,7 +491,7 @@ class WorkFlowService extends Service
     }
 
     //触发工作流并生成
-    public function create($data, $user){
+    public function create($data, $user){$this->runWorkFlow($data['order_number'], 23);dd(1);
         list($status, $msg) = $this->createRule($data, $user);
         if(! $status) return [false, $msg];
 
@@ -695,7 +693,7 @@ class WorkFlowService extends Service
         }
     }
 
-    //运行工作流
+    //运行工作流   0 发起人 1审批 2抄送 3条件 4路由
     public function runWorkFlow($order_number = "", $workFlowGeneratedId = 0){
         $workFlowGenerated = WorkFlowGenerated::where('del_time', 0)
             ->when(! empty($order_number), function ($query) use ($order_number) {
@@ -708,7 +706,87 @@ class WorkFlowService extends Service
         if(empty($workFlowGenerated)) return [false, '工作流不存在或已被删除'];
         $workFlowGenerated = $workFlowGenerated->toArray();
 
-//        if($workFlowGenerated['node_id'])
+        //当前所处的节点ID
+        $node_id = $workFlowGenerated['node_id'];
+        //节点关系树状图
+        $workFlowTree = json_decode($workFlowGenerated['my_param'], true);
+        //获取剩余节点的树状图
+        if($node_id > 1) $workFlowTree = $this->getDescendants($workFlowTree, $node_id);
+        //赋值临时变量
+        $tmp = $workFlowTree;
+
+        //路由、审批、抄送节点
+        $ly = $sp = $cs = [];
+        //下个节点
+        $this->traverseTree($tmp, $ly, $sp, $cs);
+    }
+
+    //获取某个节点下所有子集
+    //$son = $this->getDescendants($workFlowTree, $node_id);
+    function getDescendants(array $tree, int $targetId): array {
+        $descendants = [];
+
+        // Helper function to perform a depth-first search in the tree.
+        $dfs = function ($node) use (&$dfs, &$descendants, $targetId) {
+            if ($node['id'] == $targetId) {
+                // If this is the target node, start collecting its children.
+                foreach ($node['children'] ?? [] as $childId => $child) {
+                    $descendants[$childId] = $child;
+                    $dfs($child); // Recursively collect all descendants of this child.
+                }
+            } else {
+                // Otherwise, continue searching in the children.
+                foreach ($node['children'] ?? [] as $child) {
+                    $dfs($child);
+                }
+            }
+        };
+
+        // Start the search from the root node.
+        foreach ($tree as $node) {
+            $dfs($node);
+            if (!empty($descendants)) {
+                break; // Stop searching once we've found the target and collected its descendants.
+            }
+        }
+
+        return $descendants;
+    }
+
+    function traverseTree($data, &$ly = [], &$sp = [], &$cs = []) {
+        // Helper function to perform the actual traversal.
+        $traverseHelper = function($node) use (&$ly, &$sp, &$cs, &$traverseHelper) {
+            if ($node['type'] == 4) { //路由 路由下会带条件
+                $ly[] = $node;
+                return false; // Stop further recursion when type is 4.
+            }
+
+            if ($node['type'] == 2) {
+                $cs[] = $node['id']; // 抄送节点id
+            }
+
+            if ($node['type'] == 1) {
+                $sp[] = $node['id']; // 审批人节点id
+            }
+
+            if (!empty($node['children'])) {
+                foreach ($node['children'] as $childNode) {
+                    if ($traverseHelper($childNode) === false) {
+                        break; // Break if a child node has type 4.
+                    }
+                }
+            }
+
+            return $node['type'] != 4;
+        };
+
+        // Find the root node and start traversing from there.
+        foreach ($data as $nodeId => $node) {
+            $traverseHelper($node);
+            break; // Only one root node should exist.
+        }
+
+        return [$sp, $cs, $ly];
     }
 
     //调用审批