UserDormantNotify.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class UserDormantNotify extends Model
  6. {
  7. protected $table = 'tb_user_dormant_notify';
  8. protected $primaryKey = 'id';
  9. public $keyType = 'int';
  10. public $incrementing = true;
  11. public $timestamps = true;
  12. const CREATED_AT = 'created_at';
  13. const UPDATED_AT = 'updated_at';
  14. protected $guarded = [];
  15. protected $dates = [
  16. 'register_at',
  17. 'last_login_at',
  18. 'dormant_at',
  19. 'notify_at'
  20. ];
  21. /**
  22. * 알림 발송 내역 조회
  23. */
  24. public function data(SearchData $params): object
  25. {
  26. $query = $this->query();
  27. $query->select('tb_user_dormant_notify.*');
  28. if ($params->keyword) {
  29. switch ($params->field) {
  30. case 'tb_user_dormant_notify.id':
  31. case 'tb_user_dormant_notify.user_id':
  32. case 'tb_user_dormant_notify.sid':
  33. $query->where($params->field, '=', $params->keyword);
  34. break;
  35. case 'tb_user_dormant_notify.name':
  36. case 'tb_user_dormant_notify.nickname':
  37. case 'tb_user_dormant_notify.email':
  38. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  39. break;
  40. case 'tb_user_dormant_notify.last_login_at': // 마지막 로그인 일시
  41. case 'tb_user_dormant_notify.dormant_at': // 휴면예정 일시
  42. case 'tb_user_dormant_notify.created_at': // 알림 발송일시
  43. $query->whereDate($params->field, $params->keyword);
  44. break;
  45. }
  46. }
  47. if($params->startDate || $params->endDate) {
  48. if($params->startDate) {
  49. $query->where('register_at', '>=', $params->startDate . ' 00:00:00');
  50. }
  51. if($params->endDate) {
  52. $query->where('register_at', '<=', $params->endDate . ' 23:59:59');
  53. }
  54. }
  55. $query->orderByDesc('tb_user_dormant_notify.id');
  56. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  57. $total = $this->count(); // 전체 수
  58. $rows = $list->count(); // 검색 수
  59. return (object)[
  60. 'total' => $total,
  61. 'rows' => $rows,
  62. 'list' => $list
  63. ];
  64. }
  65. /**
  66. * 휴면(예정) 알림 발송
  67. */
  68. public function register(User $user): bool
  69. {
  70. // 이메일 전송
  71. (new EmailLib)->send(SEND_MAIL_FORM_TYPE_11, $user);
  72. // 전송 내역 기록
  73. return $this->insert([
  74. 'user_id' => $user->id,
  75. 'sid' => $user->sid,
  76. 'email' => $user->email,
  77. 'name' => $user->name,
  78. 'nickname' => $user->nickname,
  79. 'last_login_at' => $user->last_login_at,
  80. 'dormant_at' => $user->last_login_at->addYear(1),
  81. 'created_at' => now()
  82. ]);
  83. }
  84. }