更新批量查询解密

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

@ -8,12 +8,58 @@
class RecordQueryController extends Controller 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() public function index()
{ {
return view('record_query', [ return view('record_query', [
'startDate' => '', 'startDate' => '',
'endDate' => '', 'endDate' => '',
'type' => '', 'recordType' => '',
'examType' => '',
'records' => null 'records' => null
]); ]);
} }
@ -22,126 +68,88 @@ public function query(Request $request)
{ {
$startDate = $request->input('start_date'); $startDate = $request->input('start_date');
$endDate = $request->input('end_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', '请填写完整查询条件'); return redirect()->back()->with('error', '请填写完整查询条件');
} }
if ($type == 1) { $query = $this->buildQuery($request);
// 查询预约表 if (!$query) {
$records = DB::table('appointment_record') return redirect()->back()->with('error', '请填写完整查询条件');
->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();
} }
// 解密处理 $records = $query->paginate(50)->through(function ($record) {
foreach ($records as $record) { return $this->decryptRecord($record);
// 解密电话 });
if ($record->tel) {
$decryptTel = HSM::HsmDecrypt($record->tel);
$record->tel = $decryptTel['status'] ? $decryptTel['data'] : '解密失败';
}
// 解密身份证号 $records->appends([
if ($record->id_card_num) { 'start_date' => $startDate,
$decryptIdCard = HSM::HsmDecrypt($record->id_card_num); 'end_date' => $endDate,
$record->id_card_num = $decryptIdCard['status'] ? $decryptIdCard['data'] : '解密失败'; '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) public function export(Request $request)
{ {
set_time_limit(0);
ini_set('memory_limit', '512M');
$startDate = $request->input('start_date'); $startDate = $request->input('start_date');
$endDate = $request->input('end_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', '请填写完整查询条件'); return redirect()->back()->with('error', '请填写完整查询条件');
} }
if ($type == 1) { $query = $this->buildQuery($request);
// 查询预约表 if (!$query) {
$records = DB::table('appointment_record') return redirect()->back()->with('error', '请填写完整查询条件');
->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文件名 $recordType = (int) $recordType;
$fileName = 'records_' . date('YmdHis') . '.csv'; $fileName = 'records_' . date('YmdHis') . '.csv';
// 设置响应头
$headers = [ $headers = [
'Content-Type' => 'text/csv', 'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $fileName . '"', 'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
]; ];
// 创建CSV内容 $callback = function () use ($query, $recordType) {
$callback = function() use ($records) {
$handle = fopen('php://output', 'w'); $handle = fopen('php://output', 'w');
// 写入BOM头解决中文乱码问题
fwrite($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); fwrite($handle, chr(0xEF) . chr(0xBB) . chr(0xBF));
// 写入表头 if ($recordType == 2) {
fputcsv($handle, ['姓名', '电话', '身份证号', '创建时间', '机构名称'], ','); fputcsv($handle, ['姓名', '身份证号', '体检日期', '机构名称', '行业类型'], ',');
} else {
// 写入数据 fputcsv($handle, ['姓名', '身份证号', '体检日期', '机构名称'], ',');
foreach ($records as $record) {
fputcsv($handle, [
$record->name,
$record->tel,
$record->id_card_num,
$record->created_at,
$record->org_name
], ',');
} }
$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); fclose($handle);
}; };

@ -20,20 +20,28 @@
<form action="{{ route('record.query') }}" method="POST" class="mb-5"> <form action="{{ route('record.query') }}" method="POST" class="mb-5">
@csrf @csrf
<div class="row g-3"> <div class="row g-3">
<div class="col-md-4"> <div class="col-md-3">
<label for="start_date" class="form-label">开始日期</label> <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>
<div class="col-md-4"> <div class="col-md-3">
<label for="end_date" class="form-label">结束日期</label> <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>
<div class="col-md-4"> <div class="col-md-3">
<label for="type" class="form-label">记录类型</label> <label for="record_type" class="form-label">记录类型</label>
<select class="form-select" id="type" name="type" required> <select class="form-select" id="record_type" name="record_type" required>
<option value="" {{ empty($type ?? '') ? 'selected' : '' }}>请选择类型</option> <option value="" {{ empty($recordType ?? '') ? 'selected' : '' }}>请选择类型</option>
<option value="1" {{ ($type ?? '') == 1 ? 'selected' : '' }}>预约</option> <option value="1" {{ ($recordType ?? '') == 1 ? 'selected' : '' }}>预约</option>
<option value="2" {{ ($type ?? '') == 2 ? 'selected' : '' }}>登记</option> <option value="2" {{ ($recordType ?? '') == 2 ? 'selected' : '' }}>登记</option>
</select>
</div>
<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> </select>
</div> </div>
</div> </div>
@ -45,14 +53,14 @@
@if(isset($records)) @if(isset($records))
<div class="mt-5"> <div class="mt-5">
<h3>查询结果</h3> <h3>查询结果</h3>
<p>共 {{ count($records) }} 条记录</p> <p>共 {{ $records->total() }} 条记录</p>
<!-- 导出CSV按钮 -->
<form action="{{ route('record.export') }}" method="POST" style="display: inline;"> <form action="{{ route('record.export') }}" method="POST" style="display: inline;">
@csrf @csrf
<input type="hidden" name="start_date" value="{{ $startDate }}"> <input type="hidden" name="start_date" value="{{ $startDate }}">
<input type="hidden" name="end_date" value="{{ $endDate }}"> <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> <button type="submit" class="btn btn-success mb-3">导出CSV</button>
</form> </form>
@ -61,25 +69,33 @@
<thead class="table-dark"> <thead class="table-dark">
<tr> <tr>
<th>姓名</th> <th>姓名</th>
<th>电话</th>
<th>身份证号</th> <th>身份证号</th>
<th>创建时间</th> <th>体检日期</th>
<th>机构名称</th> <th>机构名称</th>
@if($recordType == 2)
<th>行业类型</th>
@endif
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@foreach($records as $record) @foreach($records as $record)
<tr> <tr>
<td>{{ $record->name ?? '空' }}</td> <td>{{ $record->name ?? '' }}</td>
<td>{{ $record->tel ?? '空' }}</td> <td>{{ $record->id_card_num ?? '' }}</td>
<td>{{ $record->id_card_num ?? '空' }}</td> <td>{{ $record->created_at ?? '' }}</td>
<td>{{ $record->created_at ?? '空' }}</td> <td>{{ $record->org_name ?? '' }}</td>
<td>{{ $record->org_name ?? '空' }}</td> @if($recordType == 2)
<td>{{ $record->industry_type ?? '' }}</td>
@endif
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="d-flex justify-content-center">
{{ $records->links() }}
</div>
</div> </div>
@endif @endif
</div> </div>

Loading…
Cancel
Save