| 
					
				 | 
			
			
				@@ -0,0 +1,155 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+<?php 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+namespace app\admin\controller\medical; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use _PHPStan_cc8d35ffb\Nette\Neon\Exception; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use app\admin\validate\medical\DeptValidate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use app\common\Tree; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use app\controller\Curd; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use app\model\MedicalDept; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use support\Db; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use support\Request; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use support\Response; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+class DeptController extends Curd 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function __construct() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->model = new MedicalDept(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->validate = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->validateClass = new DeptValidate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Desc 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Author Gorden 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Date 2024/3/1 14:38 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param Request $request 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return Response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws \support\exception\BusinessException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function select(Request $request): Response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        [$where, $format, $limit, $field, $order] = $this->selectInput($request); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $format = 'tree'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $order = $request->get('order', 'desc'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $field = $field ?? 'dept_addTime'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $where['dept_is_del'] = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $query = $this->doSelect($where, $field, $order); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return $this->doFormat($query, $format, $limit); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Desc 执行写入数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Author Gorden 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Date 2024/3/1 15:42 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param array $data 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return mixed|null 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws Exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    protected function doInsert(array $data) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Db::beginTransaction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $primary_key = $this->model->getKeyName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $model_class = get_class($this->model); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $model = new $model_class; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            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(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (\Exception $e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollBack(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new Exception('数据写入失败'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return $primary_key ? $model->$primary_key : null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Desc 执行更新 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Author Gorden 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Date 2024/3/1 15:51 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param $id 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param $data 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return void 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    protected function doUpdate($id, $data) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Db::beginTransaction(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $model = $this->model->find($id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $oldDeptPid = $model->dept_pid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $oldDeptPath = $model->dept_path; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            foreach ($data as $key => $val) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $model->{$key} = $val; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 上级 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $superior = $this->model->getByPrimaryKey($model->dept_pid); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $model->dept_path = $superior->dept_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'])]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (\Exception $e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollBack(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return json_fail('数据更新失败~'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Desc 软删除 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Author Gorden 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @Date 2024/3/1 14:39 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param Request $request 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return Response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws \support\exception\BusinessException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function delete(Request $request): Response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $ids = $this->deleteInput($request); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $this->doSoftDelete($ids, ['dept_is_del' => 1]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return json_success('success'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    protected function formatTree($items): Response 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $format_items = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        foreach ($items as $item) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $format_items[] = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'name' => $item->dept_name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'value' => (string)$item->dept_id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'id' => $item->dept_id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'pid' => $item->dept_pid, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_id' => $item->dept_id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_pid' => $item->dept_pid, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_name' => $item->dept_name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_type' => $item->dept_type, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_sort' => $item->dept_sort, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_addTime' => $item->dept_addTime, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'dept_updateTime' => $item->dept_updateTime 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $tree = new Tree($format_items); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return json_success('success', $tree->getTree()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |