EmailLog.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\DTO\SearchData;
  6. class EmailLog extends Model
  7. {
  8. protected $table = 'tb_email_log';
  9. protected $primaryKey = 'id';
  10. public $keyType = 'int';
  11. public $incrementing = true;
  12. public $timestamps = true;
  13. const CREATED_AT = 'created_at';
  14. const UPDATED_AT = null;
  15. const DELETED_AT = null;
  16. protected $guarded = [];
  17. /**
  18. * 이메일 발송 이력 조회
  19. */
  20. public function data(SearchData $params): object
  21. {
  22. $query = $this->query();
  23. $query->select(
  24. 'tb_email_log.*',
  25. 'users.id',
  26. 'users.sid',
  27. 'users.name',
  28. 'users.nickname',
  29. 'users.email'
  30. );
  31. if($params->keyword) {
  32. switch ($params->field) {
  33. case 'users.name' :
  34. case 'users.email' :
  35. case 'tb_email_log.ip_address' :
  36. case 'tb_email_log.created_at' :
  37. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  38. break;
  39. case 'users.id' :
  40. case 'users.sid' :
  41. $query->where($params->field, '=', $params->keyword);
  42. break;
  43. }
  44. }
  45. if ($params->startDate || $params->endDate) {
  46. if ($params->startDate) {
  47. $query->where('tb_email_history.created_at', '>=', $params->startDate . ' 00:00:00');
  48. }
  49. if ($params->endDate) {
  50. $query->where('tb_email_history.created_at', '<=', $params->endDate . ' 23:59:59');
  51. }
  52. }
  53. $query->leftJoin('users', 'users.id', '=', 'tb_email_log.user_id');
  54. $query->orderByDesc('tb_email_log.id');
  55. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  56. $total = $this->count();
  57. $rows = $list->count();
  58. return (object)[
  59. 'total' => $total,
  60. 'rows' => $rows,
  61. 'list' => $list
  62. ];
  63. }
  64. /**
  65. * 이메일 전송 기록
  66. */
  67. public function register(array $data): mixed
  68. {
  69. return DB::transaction(function () use ($data) {
  70. DB::table($this->table)->insert($data);
  71. $keys = DB::select('SELECT LAST_INSERT_ID() AS id');
  72. $sk = $keys[0]->id;
  73. $ek = ($sk + count($data) - 1);
  74. return range($sk, $ek);
  75. });
  76. }
  77. }