This commit is contained in:
zimoyin
2026-04-03 15:34:04 +08:00
parent 1a84e92384
commit 673c83109f
5 changed files with 894 additions and 247 deletions
+99 -32
View File
@@ -4,69 +4,136 @@ namespace plugin\admin\app\controller;
use plugin\admin\app\model\OpmMwDepartment;
use plugin\admin\app\model\OpmMwHospital;
use app\utils\Logger;
use plugin\admin\app\model\OpmMwInfoDatum; // 引入新模型
use support\Request;
use support\Response;
use Throwable;
class TestController extends Crud
{
/**
* 浏览
* @return Response
* @throws Throwable
*/
public function index(): Response
{
return raw_view('test/index');
}
/**
* 获取医院科室树形结构
* @return Response
* 树结构(已应用规则引擎)
*/
public function tree(): Response
{
// 1. 查询所有医院
$hospitals = OpmMwHospital::where('is_true', 1) // 只查有效医院
->withDataPermission()
// 医院查询:自动应用规则引擎
$hospitals = OpmMwHospital::where('is_true', 1)
->withDataPermission() // 规则引擎
->orderBy('id')
->get();
// 2. 组装树结构
$treeData = [];
$tree = [];
foreach ($hospitals as $hospital) {
// 医院节点
$hospitalNode = [
$node = [
'id' => $hospital->id,
'title' => $hospital->organ_name, // 医院名称
'title' => $hospital->organ_name,
'type' => 'hospital',
'hospital_id' => $hospital->id,
'children' => []
];
// 3. 查询该医院下的所有科室
$departments = OpmMwDepartment::where('organ_id', $hospital->id)
->withDataPermission()
// 科室查询:自动应用规则引擎
$depts = OpmMwDepartment::where('organ_id', $hospital->id)
->withDataPermission() // 规则引擎
->orderBy('sort_id')
->get();
// 4. 组装科室节点
foreach ($departments as $dept) {
$hospitalNode['children'][] = [
foreach ($depts as $dept) {
$node['children'][] = [
'id' => $dept->id,
'title' => $dept->dept_name // 科室名称
'title' => $dept->dept_name,
'type' => 'dept',
'hospital_id' => $hospital->id
];
}
$treeData[] = $hospitalNode;
$tree[] = $node;
}
// 5. 返回 Layui 树需要的格式
return json([
'code' => 0,
'msg' => 'success',
'data' => $treeData
'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
]);
}
}