|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\API\Admin;
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
use App\Services\ExclusionValidationService;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* 互斥校验测试控制器
|
|
|
*/
|
|
|
class ExclusionTestController extends Controller
|
|
|
{
|
|
|
/**
|
|
|
* 测试互斥校验
|
|
|
*/
|
|
|
public function test(Request $request)
|
|
|
{
|
|
|
$data = $request->validate([
|
|
|
'patient_id' => 'required|integer|min:1',
|
|
|
'new_item_codes' => 'nullable|array',
|
|
|
'new_item_codes.*' => 'required|string',
|
|
|
'target_date' => 'nullable|date_format:Y-m-d',
|
|
|
]);
|
|
|
|
|
|
$patientId = $data['patient_id'];
|
|
|
$newItemCodes = $data['new_item_codes'] ?? [];
|
|
|
$targetDate = $data['target_date'] ?? null;
|
|
|
|
|
|
// 创建互斥校验服务
|
|
|
$service = new ExclusionValidationService();
|
|
|
|
|
|
// 执行校验
|
|
|
$result = $service->validate($patientId, $newItemCodes, $targetDate);
|
|
|
|
|
|
// 获取调试信息
|
|
|
$debug = $this->getDebugInfo($patientId, $newItemCodes);
|
|
|
|
|
|
return response()->json([
|
|
|
'success' => true,
|
|
|
'data' => [
|
|
|
'valid' => $result['valid'],
|
|
|
'conflicts' => $result['conflicts'],
|
|
|
'error' => $result['error'] ?? null,
|
|
|
'debug' => $debug,
|
|
|
]
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取调试信息
|
|
|
*/
|
|
|
private function getDebugInfo(int $patientId, array $newItemCodes): array
|
|
|
{
|
|
|
// 获取患者已预约项目
|
|
|
$bookedItems = DB::table('exam_appointment as ea')
|
|
|
->join('exam_application as eapp', 'ea.exam_application_id', '=', 'eapp.id')
|
|
|
->leftJoin('exam_item as ei', 'eapp.examination_item_code', '=', 'ei.code')
|
|
|
->where('ea.patient_id', $patientId)
|
|
|
->whereIn('ea.status', [1, 2])
|
|
|
->whereIn('eapp.status', [1, 2])
|
|
|
->select(
|
|
|
'ei.id as item_id',
|
|
|
'ei.code as item_code',
|
|
|
'ei.name as item_name',
|
|
|
DB::raw('DATE(ea.appointment_time) as appointment_date'),
|
|
|
'ea.status as appointment_status',
|
|
|
'eapp.status as application_status'
|
|
|
)
|
|
|
->get()
|
|
|
->toArray();
|
|
|
|
|
|
// 获取新项目信息
|
|
|
$newItems = empty($newItemCodes) ? [] : DB::table('exam_item')
|
|
|
->whereIn('code', $newItemCodes)
|
|
|
->where('deleted', 0)
|
|
|
->select('id', 'code', 'name')
|
|
|
->get()
|
|
|
->toArray();
|
|
|
|
|
|
// 获取涉及的互斥规则
|
|
|
$allItemIds = array_merge(
|
|
|
array_column($bookedItems, 'item_id'),
|
|
|
array_column($newItems, 'id')
|
|
|
);
|
|
|
$allItemIds = array_unique($allItemIds);
|
|
|
|
|
|
$rules = DB::table('exam_item_exclusion as eie')
|
|
|
->whereIn('eie.source_item_id', $allItemIds)
|
|
|
->where('eie.status', 1)
|
|
|
->where('eie.deleted', 0)
|
|
|
->leftJoin('exam_item as ei', 'eie.source_item_id', '=', 'ei.id')
|
|
|
->leftJoin('exam_group as eg', function ($join) {
|
|
|
$join->on('eie.target_id', '=', 'eg.id')
|
|
|
->where('eie.target_type', 'GROUP');
|
|
|
})
|
|
|
->select(
|
|
|
'eie.id',
|
|
|
'eie.source_item_id',
|
|
|
'ei.code as source_item_code',
|
|
|
'ei.name as source_item_name',
|
|
|
'eie.target_type',
|
|
|
'eie.target_id',
|
|
|
'eie.exclusion_type',
|
|
|
'eie.exclusion_value',
|
|
|
DB::raw('IF(eie.target_type = "GROUP", eg.name, "ITEM:" + eie.target_id) as target_name')
|
|
|
)
|
|
|
->get()
|
|
|
->toArray();
|
|
|
|
|
|
// 对于GROUP类型,补充组成员信息
|
|
|
foreach ($rules as &$rule) {
|
|
|
if ($rule->target_type === 'GROUP') {
|
|
|
$rule->group_members = DB::table('exam_group_item')
|
|
|
->where('group_id', $rule->target_id)
|
|
|
->leftJoin('exam_item as ei', 'exam_group_item.item_id', '=', 'ei.id')
|
|
|
->select('ei.id', 'ei.code', 'ei.name')
|
|
|
->get()
|
|
|
->toArray();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
'patient_id' => $patientId,
|
|
|
'booked_items' => $bookedItems,
|
|
|
'new_items' => $newItems,
|
|
|
'exclusion_rules' => $rules,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取可用患者列表(方便测试)
|
|
|
*/
|
|
|
public function getPatients()
|
|
|
{
|
|
|
$patients = DB::table('exam_appointment as ea')
|
|
|
->join('exam_application as eapp', 'ea.exam_application_id', '=', 'eapp.id')
|
|
|
->select(
|
|
|
'ea.patient_id',
|
|
|
'ea.patient_name',
|
|
|
DB::raw('COUNT(*) as appointment_count')
|
|
|
)
|
|
|
->groupBy('ea.patient_id', 'ea.patient_name')
|
|
|
->orderBy('ea.patient_id')
|
|
|
->limit(20)
|
|
|
->get();
|
|
|
|
|
|
return response()->json([
|
|
|
'success' => true,
|
|
|
'data' => [
|
|
|
'patients' => $patients
|
|
|
]
|
|
|
]);
|
|
|
}
|
|
|
}
|