UserNameLog.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 UserNameLog extends Model
  7. {
  8. protected $table = 'tb_user_name_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 getChangeDay(): int
  21. {
  22. return intval(config('change_nickname_day', 0));
  23. }
  24. /**
  25. * 이름 변경 남은 일 수
  26. */
  27. public function getDayLeft(int $uid): int
  28. {
  29. $a = $this->getChangeDay();
  30. $b = $this->getLastChangeDay($uid);
  31. $c = ($a - $b);
  32. if($a === 0) {
  33. return -1;
  34. }else if($c > 0) { // 남은 기간
  35. return $c;
  36. }else if($c < 0) { // 지난 기간
  37. return $c;
  38. }
  39. }
  40. /**
  41. * 마지막 이름 변경 일
  42. */
  43. public function getLastChangeDay(int $uid): int
  44. {
  45. return (collect(DB::select(
  46. 'SELECT TIMESTAMPDIFF(DAY, created_at, NOW()) AS diff FROM tb_user_name_log WHERE user_id = ? ORDER BY id DESC LIMIT 1'
  47. , [$uid]))->get(0)->diff ?? 0);
  48. }
  49. /**
  50. * 이름 변경이 가능한지 확인
  51. */
  52. public function isUpdateAble(int $uid): bool
  53. {
  54. $lastChangeDay = $this->getLastChangeDay($uid);
  55. if(!$lastChangeDay || $this->getChangeDay() <= $lastChangeDay) {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * 이름 변경 기록 입력
  62. */
  63. public function setLog(User $user, string $name): bool
  64. {
  65. return $this->insert([
  66. 'user_id' => $user->id,
  67. 'before' => $user->nickname,
  68. 'after' => $name,
  69. 'created_at' => now()
  70. ]);
  71. }
  72. /**
  73. * 이름 변경 기록 조회
  74. */
  75. public function data(SearchData $params): object
  76. {
  77. $query = $this->query();
  78. $query->select('users.sid', 'tb_user_name_log.*');
  79. if ($params->keyword) {
  80. switch ($params->field) {
  81. case 'tb_user_name_log.before' :
  82. case 'tb_user_name_log.after' :
  83. case 'users.name' :
  84. case 'users.email' :
  85. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  86. break;
  87. case 'tb_user_name_log.id' :
  88. case 'users.id' :
  89. case 'users.sid' :
  90. $query->where($params->field, '=', $params->keyword);
  91. break;
  92. }
  93. }
  94. $query->leftJoin('users', 'users.id', '=', 'tb_user_name_log.user_id');
  95. $query->orderByDesc('tb_user_name_log.id');
  96. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  97. $total = $this->count();
  98. $rows = $list->count();
  99. return (object)[
  100. 'total' => $total,
  101. 'rows' => $rows,
  102. 'list' => $list
  103. ];
  104. }
  105. }