withDataPermission() // 规则引擎 ->orderBy('id') ->get(); $tree = []; foreach ($hospitals as $hospital) { $node = [ 'id' => $hospital->id, 'title' => $hospital->organ_name, 'type' => 'hospital', 'hospital_id' => $hospital->id, 'children' => [] ]; // 科室查询:自动应用规则引擎 $depts = OpmMwDepartment::where('organ_id', $hospital->id) ->withDataPermission() // 规则引擎 ->orderBy('sort_id') ->get(); foreach ($depts as $dept) { $node['children'][] = [ 'id' => $dept->id, 'title' => $dept->dept_name, 'type' => 'dept', 'hospital_id' => $hospital->id ]; } $tree[] = $node; } return json([ 'code' => 0, 'data' => $tree ]); } /** * 表格数据(已应用规则引擎) */ public function data(Request $request): Response { $type = $request->get('type'); $id = $request->get('id'); $hospitalId = $request->get('hospital_id'); // 改用模型查询 + 规则引擎 $query = OpmMwInfoDatum::withDataPermission(); // 规则引擎 // 业务过滤:医院 if ($hospitalId) { $hospital = OpmMwHospital::find($hospitalId); if ($hospital) { $query->where('organ_name', $hospital->organ_name); } } // 业务过滤:科室 if ($type === 'dept' && $id) { $dept = OpmMwDepartment::find($id); if ($dept) { $query->where('dept_name', $dept->dept_name); } } $list = $query->orderBy('id', 'desc')->paginate(20); return json([ 'code' => 0, 'count' => $list->total(), 'data' => $list->items() ]); } /** * 统计卡片(已应用规则引擎) */ public function summary(Request $request): Response { $type = $request->get('type'); $id = $request->get('id'); $hospitalId = $request->get('hospital_id'); // 改用模型查询 + 规则引擎 $query = OpmMwInfoDatum::withDataPermission(); // 规则引擎 // 业务过滤:医院 if ($hospitalId) { $hospital = OpmMwHospital::find($hospitalId); if ($hospital) { $query->where('organ_name', $hospital->organ_name); } } // 业务过滤:科室 if ($type === 'dept' && $id) { $dept = OpmMwDepartment::find($id); if ($dept) { $query->where('dept_name', $dept->dept_name); } } $data = $query ->selectRaw("waste_type as type, SUM(CAST(weight AS DECIMAL(10,2))) as total") ->groupBy('waste_type') ->get(); return json([ 'code' => 0, 'data' => $data ]); } }