Files
dsserver/plugin/admin/app/controller/TestController.php
T
zimoyin 673c83109f init
2026-04-03 15:34:04 +08:00

139 lines
3.7 KiB
PHP

<?php
namespace plugin\admin\app\controller;
use plugin\admin\app\model\OpmMwDepartment;
use plugin\admin\app\model\OpmMwHospital;
use plugin\admin\app\model\OpmMwInfoDatum; // 引入新模型
use support\Request;
use support\Response;
class TestController extends Crud
{
public function index(): Response
{
return raw_view('test/index');
}
/**
* 树结构(已应用规则引擎)
*/
public function tree(): Response
{
// 医院查询:自动应用规则引擎
$hospitals = OpmMwHospital::where('is_true', 1)
->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
]);
}
}