giftcon_dev/app/Repositories/Admin/Log/MemberAccountLogRepository.php

98 lines
3.2 KiB
PHP

<?php
namespace App\Repositories\Admin\Log;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
final class MemberAccountLogRepository
{
private const TABLE = 'mem_account_log';
public function paginate(array $filters, int $perPage = 30): LengthAwarePaginator
{
$q = DB::table(self::TABLE . ' as l')
->select([
'l.seq',
'l.mem_no',
'l.request_data',
'l.result_data',
'l.request_time',
'l.result_time',
]);
// 기간(요청시간 기준)
if (!empty($filters['date_from'])) {
$q->where('l.request_time', '>=', $filters['date_from'] . ' 00:00:00');
}
if (!empty($filters['date_to'])) {
$q->where('l.request_time', '<=', $filters['date_to'] . ' 23:59:59');
}
// mem_no
if (($filters['mem_no'] ?? null) !== null) {
$q->where('l.mem_no', (int)$filters['mem_no']);
}
// bank_code (요청 JSON)
$bank = trim((string)($filters['bank_code'] ?? ''));
if ($bank !== '') {
$q->whereRaw(
"JSON_UNQUOTE(JSON_EXTRACT(l.request_data, '$.bank_code')) = ?",
[$bank]
);
}
// status (결과 JSON)
$status = trim((string)($filters['status'] ?? ''));
if ($status !== '') {
if ($status === 'ok') {
$q->whereRaw("JSON_EXTRACT(l.result_data, '$.status') = 200");
} elseif ($status === 'fail') {
$q->whereRaw("JSON_EXTRACT(l.result_data, '$.status') <> 200");
} elseif (ctype_digit($status)) {
$q->whereRaw("JSON_EXTRACT(l.result_data, '$.status') = ?", [(int)$status]);
}
}
// account (요청 JSON) - 부분검색
$account = trim((string)($filters['account'] ?? ''));
if ($account !== '') {
$like = '%' . $this->escapeLike($account) . '%';
$q->whereRaw(
"JSON_UNQUOTE(JSON_EXTRACT(l.request_data, '$.account')) LIKE ?",
[$like]
);
}
// name (요청 JSON: mam_accountname)
$name = trim((string)($filters['name'] ?? ''));
if ($name !== '') {
$like = '%' . $this->escapeLike($name) . '%';
$q->whereRaw(
"JSON_UNQUOTE(JSON_EXTRACT(l.request_data, '$.mam_accountname')) LIKE ?",
[$like]
);
}
// q: request/result 전체 LIKE (운영에서 필요할 때만)
$kw = trim((string)($filters['q'] ?? ''));
if ($kw !== '') {
$like = '%' . $this->escapeLike($kw) . '%';
$q->where(function ($qq) use ($like) {
$qq->where('l.request_data', 'like', $like)
->orWhere('l.result_data', 'like', $like);
});
}
$q->orderByDesc('l.request_time')->orderByDesc('l.seq');
return $q->paginate($perPage)->withQueryString();
}
private function escapeLike(string $s): string
{
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $s);
}
}