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.

151 lines
5.3 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Lib\HSM;
class RecordQueryController extends Controller
{
public function index()
{
return view('record_query', [
'startDate' => '',
'endDate' => '',
'type' => '',
'records' => null
]);
}
public function query(Request $request)
{
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
$type = $request->input('type'); // 1预约 2登记
if (!$startDate || !$endDate || !$type) {
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'] : '解密失败';
}
// 解密身份证号
if ($record->id_card_num) {
$decryptIdCard = HSM::HsmDecrypt($record->id_card_num);
$record->id_card_num = $decryptIdCard['status'] ? $decryptIdCard['data'] : '解密失败';
}
}
return view('record_query', compact('records', 'startDate', 'endDate', 'type'));
}
public function export(Request $request)
{
$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
$type = $request->input('type'); // 1预约 2登记
if (!$startDate || !$endDate || !$type) {
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 ?? '空';
}
// 设置CSV文件名
$fileName = 'records_' . date('YmdHis') . '.csv';
// 设置响应头
$headers = [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
];
// 创建CSV内容
$callback = function() use ($records) {
$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
], ',');
}
fclose($handle);
};
return response()->stream($callback, 200, $headers);
}
}