Browse Source

完善功能

gorden 8 months ago
parent
commit
923537346a

+ 32 - 30
app/admin/controller/medical/DeptController.php

@@ -34,9 +34,10 @@ class DeptController extends Curd
     public function select(Request $request): Response
     {
         [$where, $format, $limit, $field, $order] = $this->selectInput($request);
-        if (!isset($where['dept_category'])) {
-            $where['dept_category'] = '医护科室';
-        }
+        // if (!isset($where['dept_category'])) {
+        //     $where['dept_category'] = '医护科室';
+        // }
+        $limit = 1000;
         $format = 'tree';
         $order = $request->get('order', 'desc');
         $field = $field ?? 'dept_addtimes';
@@ -61,19 +62,19 @@ class DeptController extends Curd
             $primary_key = $this->model->getKeyName();
             $model_class = get_class($this->model);
             $model = new $model_class;
-            $model->dept_category = '医护科室';
-            $model->dept_super_path = "#" . $hospitalId . "#";
+            // $model->dept_category = '医护科室';
+            // $model->dept_super_path = "/0/";
             foreach ($data as $key => $val) {
                 $model->{$key} = $val;
             }
             $model->save();
-//            if ($model->dept_pid != 0) {
-//                $superior = $this->model->getByPrimaryKey($model->dept_pid);
-//                $model->dept_path = $superior->dept_path . $model->dept_id . '/';
-//            } else {
-//                $model->dept_path = '/0/' . $model->dept_id . '/';
-//            }
-//            $model->save();
+            if ($model->dept_super_id != 0) {
+               $superior = $this->model->getByPrimaryKey($model->dept_super_id);
+               $model->dept_super_path = $superior->dept_super_path . $model->dept_id . '/';
+            } else {
+               $model->dept_super_path = '/0/' . $model->dept_id . '/';
+            }
+            $model->save();
             Db::commit();
         } catch (\Exception $e) {
             dump($e->getMessage());
@@ -97,30 +98,31 @@ class DeptController extends Curd
         Db::beginTransaction();
         try {
             $model = $this->model->find($id);
-            $oldDeptPid = $model->dept_pid;
-            $oldDeptPath = $model->dept_path;
+            $oldDeptPid = $model->dept_super_id;
+            $oldDeptPath = $model->dept_super_path;
             foreach ($data as $key => $val) {
                 $model->{$key} = $val;
             }
-//            // 上级
-//            if ($model->dept_pid != 0) {
-//                $superior = $this->model->getByPrimaryKey($model->dept_pid);
-//                $model->dept_path = $superior->dept_path . $model->dept_id . '/';
-//            }
-//
+           // 上级
+           if ($model->dept_super_id != 0) {
+               $superior = $this->model->getByPrimaryKey($model->dept_super_id);
+               $model->dept_super_path = $superior->dept_super_path.$model->dept_id . '/';
+           }
+
             $model->save();
-//            // pid变动,如果有下级,path应跟着变
-//            if ($data['dept_pid'] != $oldDeptPid) {
-//                $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'])]);
-//                    }
-//                }
-//            }
+           // pid变动,如果有下级,path应跟着变
+           if ($data['dept_super_id'] != $oldDeptPid) {
+               $subs = $this->model->getAllSubDept($oldDeptPath);
+               if ($subs) {
+                   foreach ($subs as $sub) {
+                       $this->model->where('dept_id', $sub['dept_id'])->update(['dept_super_path' => str_replace($oldDeptPath, $model->dept_super_path, $sub['dept_super_path'])]);
+                   }
+               }
+           }
             Db::commit();
         } catch (\Exception $e) {
             Db::rollBack();
+            dump($e->getMessage());
             throw new BusinessException('数据更新失败~');
         }
     }
@@ -158,7 +160,7 @@ class DeptController extends Curd
                 'name' => $item->dept_name,
                 'value' => (string)$item->dept_id,
                 'id' => $item->dept_id,
-                'pid' => $item->dept_pid,
+                'pid' => $item->dept_super_id,
                 'dept_id' => $item->dept_id,
                 'dept_pid' => $item->dept_pid,
                 'dept_name' => $item->dept_name,

+ 43 - 1
app/admin/controller/sys_manage/DeptController.php

@@ -4,10 +4,18 @@ namespace app\admin\controller\sys_manage;
 
 use app\admin\service\sys_manage\DeptService;
 use app\admin\validate\sys_manage\DeptValidate;
+use app\common\Tree;
+use app\controller\Curd;
+use app\model\SysDept;
 use support\Request;
+use support\Response;
 
-class DeptController
+class DeptController extends Curd
 {
+    public function __construct()
+    {
+        $this->model = new SysDept();
+    }
     /**
      * @Desc 部门列表
      * @Author Gorden
@@ -25,6 +33,40 @@ class DeptController
         return DeptService::deptList($page, $limit, $keywords);
     }
 
+    public function select(Request $request): Response
+    {
+        
+        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
+        $format = 'tree';
+        $order = $request->get('order', 'desc');
+        $field = $field ?? 'dept_addtimes';
+        $query = $this->doSelect($where, $field, $order);
+        return $this->doFormat($query, $format, 1000);
+    }
+
+    protected function formatTree($items): Response
+    {
+        $format_items = [];
+        foreach ($items as $item) {
+            $format_items[] = [
+                'name' => $item->dept_name,
+                'value' => (string)$item->dept_id,
+                'id' => $item->id ?? $item->dept_id,
+                'pid' => $item->pid ?? $item->dept_super_id,
+                'dept_id' => $item->dept_id,
+                'dept_name' => $item->dept_name,
+                'dept_super_id' => $item->dept_super_id,
+                'dept_status' => $item->dept_status,
+                'dept_category' => $item->dept_category,
+                'dept_code' => $item->dept_code,
+                'dept_telephone' => $item->dept_telephone,
+                'category_addtimes' => $item->category_addtimes,
+            ];
+        }
+        $tree = new Tree($format_items);
+        return json_success('success', $tree->getTree());
+    }
+
     public function selectList(Request $request)
     {
         return DeptService::selectList($request);

+ 1 - 1
app/admin/service/sys_manage/UserService.php

@@ -26,7 +26,7 @@ class UserService
         $username = $request->get('username', '');
         $loginName = $request->get('login_name', '');
 
-        $rows = SysUser::with('role')
+        $rows = SysUser::with(['role','dept'])
             ->select('join_user_role_id', 'join_user_dept_id', 'user_id', 'user_status', 'user_category', 'user_name', 'user_login_name', 'user_mobile', 'user_remark', 'user_extend_json', 'user_addtimes')
             ->when($username != '', function ($query) use ($username) {
                 $query->where('user_name', 'like', '%' . $username . '%');

+ 5 - 0
app/model/SysDept.php

@@ -23,6 +23,11 @@ class SysDept extends Model
         return $date->format('Y-m-d H:i:s');
     }
 
+    public function getByPrimaryKey($id)
+    {
+        return self::where('dept_id', $id)->first();
+    }
+    
     /**
      * @Desc 获取子部门
      * @Author Gorden

+ 6 - 0
app/model/SysUser.php

@@ -25,6 +25,12 @@ class SysUser extends Model
             ->select('role_id', 'role_name');
     }
 
+    public function dept()
+    {
+        return $this->hasOne(SysDept::class, 'dept_id', 'join_user_dept_id')
+            ->select('dept_id', 'dept_name');
+    }
+
     /* 按登录名查找用户 */
     public static function findByLoginName($name)
     {

+ 1 - 0
route/admin.php

@@ -139,6 +139,7 @@ Route::group('/admin', function () {
         /* 部门管理 */
         Route::group('/dept', function () {
             Route::get('/list', [\app\admin\controller\sys_manage\DeptController::class, 'deptList']);
+            Route::get('/select', [\app\admin\controller\sys_manage\DeptController::class, 'select']);
             Route::get('/selectList', [\app\admin\controller\sys_manage\DeptController::class, 'selectList']);
             Route::get('/info/{id:\d+}', [\app\admin\controller\sys_manage\DeptController::class, 'deptInfo']);
             Route::post('/add', [\app\admin\controller\sys_manage\DeptController::class, 'addDept']);