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

84 lines
2.4 KiB
PHP

<?php
namespace App\Repositories\Admin\Log;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
final class MemberJoinLogRepository
{
private const TABLE = 'mem_join_log';
public function paginate(array $filters, int $perPage = 30): LengthAwarePaginator
{
$q = DB::table(self::TABLE . ' as l')
->select([
'l.seq',
'l.gubun',
'l.mem_no',
'l.cell_corp',
'l.cell_phone',
'l.email',
'l.ip4',
'l.ip4_c',
'l.error_code',
'l.dt_reg',
]);
// 기간(dt_reg)
if (!empty($filters['date_from'])) {
$q->where('l.dt_reg', '>=', $filters['date_from'] . ' 00:00:00');
}
if (!empty($filters['date_to'])) {
$q->where('l.dt_reg', '<=', $filters['date_to'] . ' 23:59:59');
}
// gubun
$gubun = trim((string)($filters['gubun'] ?? ''));
if ($gubun !== '') $q->where('l.gubun', $gubun);
// error_code (2자리)
$err = trim((string)($filters['error_code'] ?? ''));
if ($err !== '') $q->where('l.error_code', $err);
// mem_no
if (($filters['mem_no'] ?? null) !== null) {
$q->where('l.mem_no', (int)$filters['mem_no']);
}
// phone exact (encrypt 결과)
if (!empty($filters['phone_enc'])) {
$q->where('l.cell_phone', (string)$filters['phone_enc']);
}
// email (부분검색)
$email = trim((string)($filters['email'] ?? ''));
if ($email !== '') {
$like = '%' . $this->escapeLike($email) . '%';
$q->where('l.email', 'like', $like);
}
// ip4 prefix
$ip4 = trim((string)($filters['ip4'] ?? ''));
if ($ip4 !== '') {
$q->where('l.ip4', 'like', $this->escapeLike($ip4) . '%');
}
// ip4_c prefix
$ip4c = trim((string)($filters['ip4_c'] ?? ''));
if ($ip4c !== '') {
$q->where('l.ip4_c', 'like', $this->escapeLike($ip4c) . '%');
}
// 최신순
$q->orderByDesc('l.dt_reg')->orderByDesc('l.seq');
return $q->paginate($perPage)->withQueryString();
}
private function escapeLike(string $s): string
{
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $s);
}
}