@ -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 || !$t ype) {
if (!$startDate || !$endDate || !$recordT ype) {
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', 't ype'));
return view('record_query', compact('records', 'startDate', 'endDate', 'recordType', 'examT ype'));
}
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 || !$t ype) {
if (!$startDate || !$endDate || !$recordT ype) {
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);
};