UserDormant.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Models;
  3. use Dflydev\DotAccessData\Data;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\DTO\SearchData;
  7. class UserDormant extends Model
  8. {
  9. protected $table = 'tb_user_dormant';
  10. protected $primaryKey = 'id';
  11. public $keyType = 'int';
  12. public $incrementing = true;
  13. public $timestamps = true;
  14. const CREATED_AT = 'created_at';
  15. const UPDATED_AT = 'updated_at';
  16. protected $guarded = [];
  17. protected $hidden = [
  18. 'password',
  19. 'remember_token',
  20. ];
  21. protected $casts = [
  22. 'last_login_at' => 'datetime', // 마지막 로그인 일시
  23. 'email_verified_at' => 'datetime', // 이메일 인증일시
  24. 'auth_certified_at' => 'datetime', // 본인 인증일시
  25. 'password_updated_at' => 'datetime' // 비밀번호 변경일시
  26. ];
  27. protected $dates = [
  28. 'last_login_at',
  29. 'created_at',
  30. 'updated_at',
  31. 'deleted_at',
  32. 'email_verified_at',
  33. 'auth_certified_at',
  34. 'password_updated_at',
  35. 'dormant_at'
  36. ];
  37. /**
  38. * 등록된 휴면회원 조회
  39. */
  40. public function data(SearchData $params): object
  41. {
  42. $query = $this->query();
  43. $query->select('tb_user_dormant.*');
  44. if ($params->keyword) {
  45. switch ($params->field) {
  46. case 'tb_user_dormant.id':
  47. case 'tb_user_dormant.sid':
  48. $query->where($params->field, '=', $params->keyword);
  49. break;
  50. case 'tb_user_dormant.name':
  51. case 'tb_user_dormant.nickname':
  52. case 'tb_user_dormant.email':
  53. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  54. break;
  55. case 'tb_user_dormant.created_at':
  56. case 'tb_user_dormant.last_login_at':
  57. $query->whereDate($params->field, $params->keyword);
  58. break;
  59. }
  60. }
  61. $query->orderByDesc('tb_user_dormant.id');
  62. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  63. $total = $this->count();
  64. $rows = $list->count();
  65. return (object)[
  66. 'total' => $total,
  67. 'rows' => $rows,
  68. 'list' => $list
  69. ];
  70. }
  71. /**
  72. * 휴면 처리
  73. */
  74. public function dormancy(int $userID): bool
  75. {
  76. return DB::transaction(function () use ($userID)
  77. {
  78. $user = (new User)->findOrNew($userID);
  79. if (!$user->exists) {
  80. return false;
  81. }
  82. $userColumns = $this->_userColumns();
  83. foreach($userColumns as $column => $value)
  84. {
  85. if(array_key_exists($column, $user->getAttributes())) {
  86. // 휴면 회원 정보 설정
  87. $this->{$column} = $user->{$column};
  88. if($column == 'id') {
  89. continue;
  90. }
  91. // 기존 회원 정보를 null 처리
  92. $user->{$column} = $value;
  93. }
  94. }
  95. // 휴면 등록일시
  96. $this->dormant_at = now();
  97. $user->save();
  98. return $this->save();
  99. });
  100. }
  101. /**
  102. * 휴면 해제
  103. */
  104. public function recover(int $userID): bool
  105. {
  106. return DB::transaction(function () use ($userID)
  107. {
  108. $dormant = $this->findOrNew($userID);
  109. if (!$dormant->exists) {
  110. return false;
  111. }
  112. $user = (new User)->findOrNew($userID);
  113. if (!$user->exists) {
  114. return false;
  115. }
  116. $userColumns = $this->_userColumns();
  117. foreach($userColumns as $column => $value)
  118. {
  119. if(array_key_exists($column, $dormant->getAttributes())) {
  120. // 회원 정보 설정
  121. $user->{$column} = $dormant->{$column};
  122. }
  123. }
  124. // 휴면계정 삭제
  125. $dormant->forceDelete();
  126. return $user->save();
  127. });
  128. }
  129. /**
  130. * 회원 공통 속성
  131. */
  132. private function _userColumns(): array
  133. {
  134. return [
  135. 'id' => null,
  136. 'user_group_id' => null,
  137. 'user_grade_id' => null,
  138. 'sid' => '',
  139. 'email' => '',
  140. 'name' => null,
  141. 'nickname' => null,
  142. 'password' => '',
  143. 'remember_token' => null,
  144. 'today_message' => null,
  145. 'group' => 0,
  146. 'grade' => 0,
  147. 'level' => 0,
  148. 'point' => 0,
  149. 'exp' => 0,
  150. 'phone' => null,
  151. 'birthday' => null,
  152. 'gender' => 0,
  153. 'icon' => null,
  154. 'thumb' => null,
  155. 'zip_code' => null,
  156. 'address_1' => null,
  157. 'address_2' => null,
  158. 'address_3' => null,
  159. 'address_4' => null,
  160. 'receive_email' => 0,
  161. 'receive_sms' => 0,
  162. 'receive_note' => 0,
  163. 'register_ip' => null,
  164. 'last_login_at' => null,
  165. 'last_login_ip' => null,
  166. 'is_open_profile' => 0,
  167. 'is_email_cert' => 0,
  168. 'is_auth_cert' => 0,
  169. 'is_adult_cert' => 0,
  170. 'is_denied' => 0,
  171. 'is_admin' => 0,
  172. 'is_withdraw' => 0,
  173. 'about_me' => null,
  174. 'memo' => null,
  175. 'following' => 0,
  176. 'followed' => 0,
  177. 'created_at' => null,
  178. 'updated_at' => null,
  179. 'deleted_at' => null,
  180. 'email_verified_at' => null,
  181. 'auth_certified_at' => null,
  182. 'password_updated_at' => null
  183. ];
  184. }
  185. }