瀏覽代碼

解决修改部门上级后,导致下级path错乱

gorden 1 年之前
父節點
當前提交
650b375db3
共有 3 個文件被更改,包括 34 次插入9 次删除
  1. 3 2
      app/admin/controller/medical/DeptController.php
  2. 18 7
      app/admin/service/sys_manage/DeptService.php
  3. 13 0
      app/model/SysDept.php

+ 3 - 2
app/admin/controller/medical/DeptController.php

@@ -8,6 +8,7 @@ use app\common\Tree;
 use app\controller\Curd;
 use app\model\MedicalDept;
 use support\Db;
+use support\exception\BusinessException;
 use support\Request;
 use support\Response;
 
@@ -103,14 +104,14 @@ class DeptController extends Curd
                 $subs = $this->model->getAllSubDept($oldDeptPath);
                 if ($subs) {
                     foreach ($subs as $sub) {
-                        $this->model->where('dept_id',$sub['dept_id'])->update(['dept_path'=>str_replace($oldDeptPath, $model->dept_path, $sub['dept_path'])]);
+                        $this->model->where('dept_id', $sub['dept_id'])->update(['dept_path' => str_replace($oldDeptPath, $model->dept_path, $sub['dept_path'])]);
                     }
                 }
             }
             Db::commit();
         } catch (\Exception $e) {
             Db::rollBack();
-            return json_fail('数据更新失败~');
+            throw new BusinessException('数据更新失败~');
         }
     }
 

+ 18 - 7
app/admin/service/sys_manage/DeptService.php

@@ -73,9 +73,9 @@ class DeptService
                 'dept_city' => $params['dept_city'],
                 'dept_name' => $params['dept_name'],
                 'dept_telephone' => $params['dept_telephone'],
-                'dept_position' => $params['dept_position'],
-                'dept_address' => $params['dept_address'],
-                'dept_remark' => $params['dept_remark'],
+                'dept_position' => $params['dept_position'] ?? '',
+                'dept_address' => $params['dept_address'] ?? '',
+                'dept_remark' => $params['dept_remark'] ?? '',
                 'dept_extend_json' => !empty($params['dept_extend_json']) ? $params['dept_extend_json'] : '{}',
                 'dept_addtimes' => time()
             ];
@@ -123,7 +123,7 @@ class DeptService
         if ($dept->dept_super_id != $params['dept_super_id']) {
             $deptSuperPath = SysDept::where('dept_id', $params['dept_super_id'])->value('dept_super_path');
         }
-
+        Db::beginTransaction();
         try {
             $data = [
                 'dept_status' => $params['dept_status'],
@@ -132,9 +132,9 @@ class DeptService
                 'dept_city' => $params['dept_city'],
                 'dept_name' => $params['dept_name'],
                 'dept_telephone' => $params['dept_telephone'],
-                'dept_position' => $params['dept_position'],
-                'dept_address' => $params['dept_address'],
-                'dept_remark' => $params['dept_remark'],
+                'dept_position' => $params['dept_position'] ?? '',
+                'dept_address' => $params['dept_address'] ?? '',
+                'dept_remark' => $params['dept_remark'] ?? '',
                 'dept_extend_json' => !empty($params['dept_extend_json']) ? $params['dept_extend_json'] : '{}',
             ];
             // 上级变动,更新
@@ -146,7 +146,18 @@ class DeptService
             if (!SysDept::where('dept_id', $id)->update($data)) {
                 throw new \Exception('修改部门失败');
             }
+
+            if ($dept->dept_super_id != $params['dept_super_id']) {
+                $subs = SysDept::getAllSubDept($dept->dept_super_path);
+                if ($subs) {
+                    foreach ($subs as $sub) {
+                        SysDept::where('dept_id', $sub['dept_id'])->update(['dept_super_path' => str_replace($dept->dept_super_path, $data['dept_super_path'], $sub['dept_super_path'])]);
+                    }
+                }
+            }
+            Db::commit();
         } catch (\Exception $e) {
+            Db::rollBack();
             return json_fail('修改部门失败');
         }
 

+ 13 - 0
app/model/SysDept.php

@@ -16,4 +16,17 @@ class SysDept extends Model
     const CREATED_AT = 'dept_addtimes';
 
     const UPDATED_AT = null;
+
+    /**
+     * @Desc 获取子部门
+     * @Author Gorden
+     * @Date 2024/3/7 14:03
+     *
+     * @param $path
+     * @return array
+     */
+    public static function getAllSubDept($path)
+    {
+        return self::where('dept_super_path', 'like', $path . '%')->get()->toArray();
+    }
 }