giftcon_dev/app/Repositories/Admin/Sms/AdminSmsTemplateRepository.php

67 lines
2.0 KiB
PHP

<?php
namespace App\Repositories\Admin\Sms;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
final class AdminSmsTemplateRepository
{
public function paginate(array $filters, int $perPage = 30): LengthAwarePaginator
{
$q = DB::table('admin_sms_templates')->orderByDesc('id');
if (isset($filters['active']) && $filters['active'] !== '') {
$q->where('is_active', (int)$filters['active']);
}
if (!empty($filters['q'])) {
$kw = '%' . (string)$filters['q'] . '%';
$q->where(function ($w) use ($kw) {
$w->where('code', 'like', $kw)
->orWhere('title', 'like', $kw)
->orWhere('body', 'like', $kw);
});
}
return $q->paginate($perPage)->withQueryString();
}
public function find(int $id): ?object
{
return DB::table('admin_sms_templates')->where('id', $id)->first();
}
public function insert(array $data): int
{
$now = now();
return (int) DB::table('admin_sms_templates')->insertGetId([
'code' => (string)($data['code'] ?? ''),
'title' => (string)($data['title'] ?? ''),
'body' => (string)($data['body'] ?? ''),
'description' => $data['description'] ?? null,
'is_active' => (int)($data['is_active'] ?? 1),
'created_by' => $data['created_by'] ?? null,
'created_at' => $now,
'updated_at' => $now,
]);
}
public function update(int $id, array $data): int
{
$data['updated_at'] = now();
return (int) DB::table('admin_sms_templates')->where('id', $id)->update($data);
}
public function listActive(int $limit = 200): array
{
return DB::table('admin_sms_templates')
->select(['id','code','title','body'])
->where('is_active', 1)
->orderBy('title')
->limit($limit)
->get()
->all();
}
}