You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers\API\Admin;
use App\Http\Controllers\Controller;
use App\Services\ItemRuleValidationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* 项目规则校验测试控制器
*/
class ItemRuleTestController extends Controller
{
/**
* 测试项目规则校验
*/
public function test(Request $request)
{
$data = $request->validate([
'item_codes' => 'required|array',
'item_codes.*' => 'required|string',
'patient_id' => 'required|integer|min:1',
'plan_id' => 'required|integer|min:1',
]);
$itemCodes = $data['item_codes'];
$patientId = $data['patient_id'];
$planId = $data['plan_id'];
// 创建项目规则校验服务
$service = new ItemRuleValidationService();
// 执行校验
$result = $service->validate($itemCodes, $patientId, $planId);
// 获取调试信息
$debug = $this->getDebugInfo($itemCodes, $patientId, $planId);
return response()->json([
'success' => true,
'data' => [
'valid' => $result['valid'],
'conflicts' => $result['conflicts'],
'patient_info' => $result['patient_info'] ?? null,
'appointment_time' => $result['appointment_time'] ?? null,
'debug' => $debug,
]
]);
}
/**
* 获取调试信息
*/
private function getDebugInfo(array $itemCodes, int $patientId, int $planId): array
{
// 获取患者信息
$patient = DB::table('exam_application')
->where('patient_id', $patientId)
->orderBy('id', 'desc')
->select('patient_age', 'patient_gender')
->first();
// 获取项目信息
$items = DB::table('exam_item')
->whereIn('code', $itemCodes)
->where('deleted', 0)
->select('id', 'code', 'name')
->get()
->toArray();
// 获取项目绑定的规则
$itemIds = array_column($items, 'id');
$rules = DB::table('exam_item_rule')
->whereIn('target_id', $itemIds)
->where('target_type', 'ITEM')
->where('status', 1)
->where('deleted', 0)
->select('target_id', 'rule_type', 'rule_value')
->get()
->toArray();
// 获取号源信息
$plan = DB::table('plan as p')
->join('plan_batch as pb', 'p.batch_id', '=', 'pb.id')
->where('p.id', $planId)
->select('pb.plan_date', 'p.start_time', 'p.end_time', 'pb.weekname')
->first();
return [
'patient' => $patient,
'items' => $items,
'rules' => $rules,
'plan' => $plan,
];
}
}