更新批量查询解密

main
岩仔88 1 month ago
parent f8c6434555
commit 1d56043cb4

@ -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);
};

@ -10,7 +10,7 @@
<body>
<div class="container mt-5">
<h2 class="text-center mb-4">记录查询</h2>
@if(session('error'))
<div class="alert alert-danger">
{{ session('error') }}
@ -20,20 +20,28 @@
<form action="{{ route('record.query') }}" method="POST" class="mb-5">
@csrf
<div class="row g-3">
<div class="col-md-4">
<div class="col-md-3">
<label for="start_date" class="form-label">开始日期</label>
<input type="datetime-local" class="form-control" id="start_date" name="start_date" value="{{ $startDate ?? '' }}" required>
<input type="date" class="form-control" id="start_date" name="start_date" value="{{ $startDate ?? '' }}" required>
</div>
<div class="col-md-4">
<div class="col-md-3">
<label for="end_date" class="form-label">结束日期</label>
<input type="datetime-local" class="form-control" id="end_date" name="end_date" value="{{ $endDate ?? '' }}" required>
<input type="date" class="form-control" id="end_date" name="end_date" value="{{ $endDate ?? '' }}" required>
</div>
<div class="col-md-3">
<label for="record_type" class="form-label">记录类型</label>
<select class="form-select" id="record_type" name="record_type" required>
<option value="" {{ empty($recordType ?? '') ? 'selected' : '' }}>请选择类型</option>
<option value="1" {{ ($recordType ?? '') == 1 ? 'selected' : '' }}>预约</option>
<option value="2" {{ ($recordType ?? '') == 2 ? 'selected' : '' }}>登记</option>
</select>
</div>
<div class="col-md-4">
<label for="type" class="form-label">记录类型</label>
<select class="form-select" id="type" name="type" required>
<option value="" {{ empty($type ?? '') ? 'selected' : '' }}>请选择类型</option>
<option value="1" {{ ($type ?? '') == 1 ? 'selected' : '' }}>预约</option>
<option value="2" {{ ($type ?? '') == 2 ? 'selected' : '' }}>登记</option>
<div class="col-md-3">
<label for="exam_type" class="form-label">体检类型</label>
<select class="form-select" id="exam_type" name="exam_type">
<option value="" {{ empty($examType ?? '') ? 'selected' : '' }}>全部</option>
<option value="1" {{ ($examType ?? '') == 1 ? 'selected' : '' }}>健康证</option>
<option value="2" {{ ($examType ?? '') == 2 ? 'selected' : '' }}>老年人段</option>
</select>
</div>
</div>
@ -45,43 +53,51 @@
@if(isset($records))
<div class="mt-5">
<h3>查询结果</h3>
<p>共 {{ count($records) }} 条记录</p>
<!-- 导出CSV按钮 -->
<p>共 {{ $records->total() }} 条记录</p>
<form action="{{ route('record.export') }}" method="POST" style="display: inline;">
@csrf
<input type="hidden" name="start_date" value="{{ $startDate }}">
<input type="hidden" name="end_date" value="{{ $endDate }}">
<input type="hidden" name="type" value="{{ $type }}">
<input type="hidden" name="record_type" value="{{ $recordType }}">
<input type="hidden" name="exam_type" value="{{ $examType }}">
<button type="submit" class="btn btn-success mb-3">导出CSV</button>
</form>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead class="table-dark">
<tr>
<th>姓名</th>
<th>电话</th>
<th>身份证号</th>
<th>创建时间</th>
<th>体检日期</th>
<th>机构名称</th>
@if($recordType == 2)
<th>行业类型</th>
@endif
</tr>
</thead>
<tbody>
@foreach($records as $record)
<tr>
<td>{{ $record->name ?? '空' }}</td>
<td>{{ $record->tel ?? '空' }}</td>
<td>{{ $record->id_card_num ?? '空' }}</td>
<td>{{ $record->created_at ?? '空' }}</td>
<td>{{ $record->org_name ?? '空' }}</td>
<td>{{ $record->name ?? '' }}</td>
<td>{{ $record->id_card_num ?? '' }}</td>
<td>{{ $record->created_at ?? '' }}</td>
<td>{{ $record->org_name ?? '' }}</td>
@if($recordType == 2)
<td>{{ $record->industry_type ?? '' }}</td>
@endif
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="d-flex justify-content-center">
{{ $records->links() }}
</div>
</div>
@endif
</div>
</body>
</html>
</html>

Loading…
Cancel
Save