cqp пре 3 недеља
родитељ
комит
90dacb1e06
1 измењених фајлова са 112 додато и 97 уклоњено
  1. 112 97
      app/Service/WorkFlowService.php

+ 112 - 97
app/Service/WorkFlowService.php

@@ -324,58 +324,31 @@ class WorkFlowService extends Service
         return [true, $pageData];
     }
 
-    public function approval($data, $user){
-        list($status, $msg, $result) = $this->approve($data, $user);
-        if(! $status) return [false, $msg];
-
-        if($result == 1 || $result == 2){
-            $instance_id = $msg;
-            $this->businessSettle($result, $instance_id);
-        }
-
-        return [true, ''];
-    }
+    public function approval($data, $user)
+    {
+        DB::beginTransaction(); // 开启最外层事务
+        try {
+            // 1. 执行审批逻辑
+            list($status, $msg, $result) = $this->approve($data, $user);
 
-    public function businessSettle($result, $instance_id){
-        $w = WorkFlowInstances::where('id', $instance_id)->first();
-        if(empty($w)) return;
-        $w = $w->toArray();
+            if (!$status) {
+                DB::rollBack();
+                return [false, $msg];
+            }
 
+            // 2. 如果流程终结(通过或驳回),执行业务结算
+            if ($result == 1 || $result == 2) {
+                $instance_id = $msg;
 
-        $draft = Draft::where('del_time',0)
-            ->where('document_type', $w['document_type'])
-            ->where('document_id', $w['document_id'])
-            ->where('top_depart_id', $w['top_depart_id'])
-            ->latest()
-            ->first();
-        if($result == 1){ // 审核最终通过
-            if(! empty($draft)){
-                $draft_array = $draft->toArray();
-                if($draft_array['opt_type'] == 1){
-                    //调用编辑接口 覆盖数据
-                    if($draft_array['document_type'] == 'item'){
-                        list($status, $msg) = (new ItemService())->itemEditSave($draft_array['content'], $draft_array['opt_user']);
-                    }elseif ($draft_array['document_type'] == 'item_node'){
-                        list($status, $msg) = (new ItemService())->itemNodeEditSave($draft_array['content'], $draft_array['opt_user']);
-                    }elseif ($draft_array['document_type'] == 'item_node_mission'){
-                        list($status, $msg) = (new ItemService())->itemNodeMissionEditSave($draft_array['content'], $draft_array['opt_user']);
-                    }
-                }else{
-                    //单纯更新某个字段
-                }
+                $this->businessSettle($result, $instance_id);
             }
-//            if(! $status) {
-//                //todo 回滚---;
-//            }
-        }
 
-        //通过驳回都要把源业务数据的审批中标识更新掉 目前只有审核中 和 无
-        DB::table($w['document_type'])->where('id', $w['document_id'])->update(['approval_state' => 0]);
+            DB::commit();
 
-        //草稿更新
-        if(! empty($draft)){
-            $draft->del_time = time();
-            $draft->save();
+            return [true, ''];
+        } catch (\Exception $e) {
+            DB::rollBack();
+            return [false, "审批失败: " . $e->getMessage()];
         }
     }
 
@@ -405,72 +378,64 @@ class WorkFlowService extends Service
         $myIndex = collect($assignees)->search(fn($item) => $item['id'] == $user['id']);
         if ($myIndex === false) return [false, '您不在审批人名单中', 0];
 
-        try {
-            DB::beginTransaction();
-
-            $time = time();
-            $assignees[$myIndex]['mark'] = $remark;
-            $assignees[$myIndex]['pass_time'] = $time;
+        $time = time();
+        $assignees[$myIndex]['mark'] = $remark;
+        $assignees[$myIndex]['pass_time'] = $time;
 
-            // --- 情况 A:驳回 ---
-            if ($status == 3) {
-                $currentNode->status = 3;
-                $currentNode->handled_time = $time;
-                $assignees[$myIndex]['is_pass'] = 2;
-                $currentNode->assignees = $assignees;
-                $currentNode->save();
+        // --- 情况 A:驳回 ---
+        if ($status == 3) {
+            $currentNode->status = 3;
+            $currentNode->handled_time = $time;
+            $assignees[$myIndex]['is_pass'] = 2;
+            $currentNode->assignees = $assignees;
+            $currentNode->save();
 
-                WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_four]);
+            WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_four]);
 
-                DB::commit();
-                return [true, $currentNode->instance_id, 2];
-            }
+            DB::commit();
+            return [true, $currentNode->instance_id, 2];
+        }
 
-            // --- 情况 B:通过 ---
-            $assignees[$myIndex]['is_pass'] = 1;
-            $currentNode->assignees = $assignees;
+        // --- 情况 B:通过 ---
+        $assignees[$myIndex]['is_pass'] = 1;
+        $currentNode->assignees = $assignees;
 
-            $shouldMoveToNext = false;
-            if ($currentNode->approval_type == 2) {
+        $shouldMoveToNext = false;
+        if ($currentNode->approval_type == 2) {
+            $shouldMoveToNext = true;
+        } else {
+            $unPassedCount = collect($assignees)->where('is_pass', '!=', 1)->count();
+            if ($unPassedCount == 0) {
                 $shouldMoveToNext = true;
-            } else {
-                $unPassedCount = collect($assignees)->where('is_pass', '!=', 1)->count();
-                if ($unPassedCount == 0) {
-                    $shouldMoveToNext = true;
-                }
             }
+        }
 
-            $finalResult = 0; // 默认:还在审批中
+        $finalResult = 0; // 默认:还在审批中
 
-            if ($shouldMoveToNext) {
-                $currentNode->status = 2;
-                $currentNode->handled_time = $time;
-                $currentNode->save();
+        if ($shouldMoveToNext) {
+            $currentNode->status = 2;
+            $currentNode->handled_time = $time;
+            $currentNode->save();
 
-                if (!empty($currentNode->next_node_key)) {
-                    $hasActiveNext = $this->activateNextNode($currentNode->instance_id, $currentNode->next_node_key);
-                    if (!$hasActiveNext) {
-                        // 如果逻辑上虽然有key但没找到下个节点,也视为终结
-                        WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_three]);
-                        $finalResult = 1;
-                    }
-                } else {
-                    // 没有下个节点,流程最终终结
+            if (!empty($currentNode->next_node_key)) {
+                $hasActiveNext = $this->activateNextNode($currentNode->instance_id, $currentNode->next_node_key);
+                if (!$hasActiveNext) {
+                    // 如果逻辑上虽然有key但没找到下个节点,也视为终结
                     WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_three]);
-                    $finalResult = 1; // 返回 1,表示业务层面可以执行“生效”动作
+                    $finalResult = 1;
                 }
             } else {
-                // 会签中,还有人没审
-                $currentNode->save();
-                $finalResult = 0;
+                // 没有下个节点,流程最终终结
+                WorkFlowInstances::where('id', $currentNode->instance_id)->update(['status' => WorkFlowInstances::status_three]);
+                $finalResult = 1; // 返回 1,表示业务层面可以执行“生效”动作
             }
-
-            DB::commit();
-            return [true, $currentNode->instance_id, $finalResult];
-        } catch (\Exception $e) {
-            DB::rollBack();
-            return [false, $e->getMessage(), 0];
+        } else {
+            // 会签中,还有人没审
+            $currentNode->save();
+            $finalResult = 0;
         }
+
+        return [true, $currentNode->instance_id, $finalResult];
     }
 
     private function activateNextNode($instanceId, $nextNodeKey)
@@ -487,4 +452,54 @@ class WorkFlowService extends Service
         }
         return false;
     }
+
+    /**
+     * 业务结算逻辑
+     */
+    public function businessSettle($result, $instance_id)
+    {
+        $w = WorkFlowInstances::where('id', $instance_id)->first();
+        if (empty($w)) throw new \Exception("未找到流程实例");
+
+        $draft = Draft::where('del_time', 0)
+            ->where('document_type', $w->document_type)
+            ->where('document_id', $w->document_id)
+            ->where('top_depart_id', $w->top_depart_id)
+            ->latest()
+            ->first();
+
+        // 审核最终通过
+        if ($result == 1) {
+            if (!empty($draft)) {
+                if ($draft->opt_type == 1) {
+                    // 执行覆盖逻辑
+                    $status = false;
+                    $resMsg = '';
+
+                    // 动态调用
+                    if ($draft->document_type == 'item') {
+                        list($status, $resMsg) = (new ItemService())->itemEditSave($draft->content, $draft->opt_user);
+                    } elseif ($draft->document_type == 'item_node') {
+                        list($status, $resMsg) = (new ItemService())->itemNodeEditSave($draft->content, $draft->opt_user);
+                    } elseif ($draft->document_type == 'item_node_mission') {
+                        list($status, $resMsg) = (new ItemService())->itemNodeMissionEditSave($draft->content, $draft->opt_user);
+                    }
+
+                    // 重要:如果业务 Service 返回失败,抛出异常以触发回滚
+                    if (!$status) {
+                        throw new \Exception("业务数据更新失败: " . $resMsg);
+                    }
+                }
+            }
+        }
+
+        // 无论通过还是驳回,重置业务表状态
+        DB::table($w->document_type)->where('id', $w->document_id)->update(['approval_state' => 0]);
+
+        // 清理草稿
+        if (! empty($draft)) {
+            $draft->del_time = time();
+            $draft->save();
+        }
+    }
 }