From 1d56043cb40218c8009fbf6c9834785f4b04fc5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B2=A9=E4=BB=9488?= <> Date: Thu, 14 May 2026 12:31:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=89=B9=E9=87=8F=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/RecordQueryController.php | 186 +++++++++--------- .../resources/views/record_query.blade.php | 64 +++--- 2 files changed, 137 insertions(+), 113 deletions(-) diff --git a/Laravel/app/Http/Controllers/RecordQueryController.php b/Laravel/app/Http/Controllers/RecordQueryController.php index 81e8cb0..73f2e90 100644 --- a/Laravel/app/Http/Controllers/RecordQueryController.php +++ b/Laravel/app/Http/Controllers/RecordQueryController.php @@ -8,12 +8,58 @@ class RecordQueryController extends Controller { + private function buildQuery(Request $request) + { + $startDate = $request->input('start_date'); + $endDate = $request->input('end_date'); + $recordType = $request->input('record_type'); + $examType = $request->input('exam_type'); + + if (!$startDate || !$endDate || !$recordType) { + return null; + } + + if ($recordType == 1) { + $query = DB::table('appointment_record') + ->select('name', 'id_card_num', 'created_at', DB::raw('(select org_name from medical_institution where sn = org_code) as org_name')) + ->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) + ->where('is_del', 0); + } else { + $query = DB::table('examination_records') + ->select('name', 'id_card_num', 'created_at', 'industry_type', DB::raw('(select org_name from medical_institution where id = institution_id) as org_name')) + ->whereBetween('created_at', [$startDate . ' 00:00:00', $endDate . ' 23:59:59']) + ->where('is_del', 0); + } + + if ($examType) { + $query->where('type', $examType); + } + + $query->orderBy('id'); + + return $query; + } + + private function decryptRecord($record) + { + try { + if ($record->id_card_num) { + $decryptIdCard = HSM::HsmDecrypt($record->id_card_num); + $record->id_card_num = (!empty($decryptIdCard) && ($decryptIdCard['status'] ?? false)) ? $decryptIdCard['data'] : '解密失败'; + } + } catch (\Throwable $e) { + $record->id_card_num = '解密失败'; + } + return $record; + } + public function index() { return view('record_query', [ 'startDate' => '', 'endDate' => '', - 'type' => '', + 'recordType' => '', + 'examType' => '', 'records' => null ]); } @@ -22,126 +68,88 @@ public function query(Request $request) { $startDate = $request->input('start_date'); $endDate = $request->input('end_date'); - $type = $request->input('type'); // 1预约 2登记 + $recordType = $request->input('record_type'); + $examType = $request->input('exam_type'); - if (!$startDate || !$endDate || !$type) { + if (!$startDate || !$endDate || !$recordType) { return redirect()->back()->with('error', '请填写完整查询条件'); } - if ($type == 1) { - // 查询预约表 - $records = DB::table('appointment_record') - ->select('name', 'tel', 'id_card_num', 'created_at', DB::raw('(select org_name from medical_institution where sn = org_code) as org_name')) - ->whereBetween('created_at', [$startDate, $endDate]) - ->where('is_del', 0) - ->get(); - } else { - // 查询登记表 - $records = DB::table('examination_records') - ->select('name', 'tel', 'id_card_num', 'created_at', DB::raw('(select org_name from medical_institution where id = institution_id) as org_name')) - ->whereBetween('created_at', [$startDate, $endDate]) - ->where('is_del', 0) - ->get(); + $query = $this->buildQuery($request); + if (!$query) { + return redirect()->back()->with('error', '请填写完整查询条件'); } - // 解密处理 - foreach ($records as $record) { - // 解密电话 - if ($record->tel) { - $decryptTel = HSM::HsmDecrypt($record->tel); - $record->tel = $decryptTel['status'] ? $decryptTel['data'] : '解密失败'; - } + $records = $query->paginate(50)->through(function ($record) { + return $this->decryptRecord($record); + }); - // 解密身份证号 - if ($record->id_card_num) { - $decryptIdCard = HSM::HsmDecrypt($record->id_card_num); - $record->id_card_num = $decryptIdCard['status'] ? $decryptIdCard['data'] : '解密失败'; - } - } + $records->appends([ + 'start_date' => $startDate, + 'end_date' => $endDate, + 'record_type' => $recordType, + 'exam_type' => $examType, + ]); - return view('record_query', compact('records', 'startDate', 'endDate', 'type')); + return view('record_query', compact('records', 'startDate', 'endDate', 'recordType', 'examType')); } public function export(Request $request) { + set_time_limit(0); + ini_set('memory_limit', '512M'); + $startDate = $request->input('start_date'); $endDate = $request->input('end_date'); - $type = $request->input('type'); // 1预约 2登记 + $recordType = $request->input('record_type'); + $examType = $request->input('exam_type'); - if (!$startDate || !$endDate || !$type) { + if (!$startDate || !$endDate || !$recordType) { return redirect()->back()->with('error', '请填写完整查询条件'); } - if ($type == 1) { - // 查询预约表 - $records = DB::table('appointment_record') - ->select('name', 'tel', 'id_card_num', 'created_at', DB::raw('(select org_name from medical_institution where sn = org_code) as org_name')) - ->whereBetween('created_at', [$startDate, $endDate]) - ->where('is_del', 0) - ->get(); - } else { - // 查询登记表 - $records = DB::table('examination_records') - ->select('name', 'tel', 'id_card_num', 'created_at', DB::raw('(select org_name from medical_institution where id = institution_id) as org_name')) - ->whereBetween('created_at', [$startDate, $endDate]) - ->where('is_del', 0) - ->get(); - } - - // 解密处理 - foreach ($records as $record) { - // 解密电话 - if ($record->tel) { - $decryptTel = HSM::HsmDecrypt($record->tel); - $record->tel = $decryptTel['status'] ? $decryptTel['data'] : '解密失败'; - } else { - $record->tel = '空'; - } - - // 解密身份证号 - if ($record->id_card_num) { - $decryptIdCard = HSM::HsmDecrypt($record->id_card_num); - $record->id_card_num = $decryptIdCard['status'] ? $decryptIdCard['data'] : '解密失败'; - } else { - $record->id_card_num = '空'; - } - - // 处理空值 - $record->name = $record->name ?? '空'; - $record->created_at = $record->created_at ?? '空'; - $record->org_name = $record->org_name ?? '空'; + $query = $this->buildQuery($request); + if (!$query) { + return redirect()->back()->with('error', '请填写完整查询条件'); } - // 设置CSV文件名 + $recordType = (int) $recordType; $fileName = 'records_' . date('YmdHis') . '.csv'; - // 设置响应头 $headers = [ 'Content-Type' => 'text/csv', 'Content-Disposition' => 'attachment; filename="' . $fileName . '"', ]; - // 创建CSV内容 - $callback = function() use ($records) { + $callback = function () use ($query, $recordType) { $handle = fopen('php://output', 'w'); - - // 写入BOM头,解决中文乱码问题 fwrite($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); - // 写入表头 - fputcsv($handle, ['姓名', '电话', '身份证号', '创建时间', '机构名称'], ','); - - // 写入数据 - foreach ($records as $record) { - fputcsv($handle, [ - $record->name, - $record->tel, - $record->id_card_num, - $record->created_at, - $record->org_name - ], ','); + if ($recordType == 2) { + fputcsv($handle, ['姓名', '身份证号', '体检日期', '机构名称', '行业类型'], ','); + } else { + fputcsv($handle, ['姓名', '身份证号', '体检日期', '机构名称'], ','); } + $query->chunk(500, function ($records) use ($handle, $recordType) { + foreach ($records as $record) { + $record = $this->decryptRecord($record); + + $row = [ + $record->name ?? '', + $record->id_card_num ?? '', + $record->created_at ?? '', + $record->org_name ?? '', + ]; + + if ($recordType == 2) { + $row[] = $record->industry_type ?? ''; + } + + fputcsv($handle, $row, ','); + } + }); + fclose($handle); }; diff --git a/Laravel/resources/views/record_query.blade.php b/Laravel/resources/views/record_query.blade.php index 3e0d820..dc96b56 100644 --- a/Laravel/resources/views/record_query.blade.php +++ b/Laravel/resources/views/record_query.blade.php @@ -10,7 +10,7 @@

记录查询

- + @if(session('error'))
{{ session('error') }} @@ -20,20 +20,28 @@
@csrf
-
+
- +
-
+
- + +
+
+ +
-
- - + + +
@@ -45,43 +53,51 @@ @if(isset($records))

查询结果

-

共 {{ count($records) }} 条记录

- - +

共 {{ $records->total() }} 条记录

+ @csrf - + + - +
- - + + @if($recordType == 2) + + @endif @foreach($records as $record) - - - - - + + + + + @if($recordType == 2) + + @endif @endforeach
姓名电话 身份证号创建时间体检日期 机构名称行业类型
{{ $record->name ?? '空' }}{{ $record->tel ?? '空' }}{{ $record->id_card_num ?? '空' }}{{ $record->created_at ?? '空' }}{{ $record->org_name ?? '空' }}{{ $record->name ?? '' }}{{ $record->id_card_num ?? '' }}{{ $record->created_at ?? '' }}{{ $record->org_name ?? '' }}{{ $record->industry_type ?? '' }}
+ +
+ {{ $records->links() }} +
@endif
- \ No newline at end of file +