CommentDeleted.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. use Exception;
  7. class CommentDeleted extends Model
  8. {
  9. protected $table = 'tb_comment_deleted';
  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 = null;
  16. protected $guarded = [];
  17. public function board(): Board
  18. {
  19. return $this->hasOne(Board::class)->withDefault();
  20. }
  21. public function post(): Post
  22. {
  23. return $this->hasOne(Post::class)->withDefault();
  24. }
  25. public function comment(): Comment
  26. {
  27. return $this->hasOne(Comment::class)->withDefault();
  28. }
  29. public function user(): User
  30. {
  31. return $this->hasOne(User::class)->withDefault();
  32. }
  33. /**
  34. * 댓글 조회
  35. */
  36. public function data(SearchData $params): object
  37. {
  38. $query = $this->query();
  39. $query->select(
  40. 'tb_board.name AS boardName',
  41. 'tb_comment_deleted.*',
  42. 'tb_comment.*',
  43. 'tb_post.subject',
  44. 'users.sid',
  45. 'users.name',
  46. 'users.email'
  47. );
  48. if($params->keyword) {
  49. switch ($params->field) {
  50. case 'tb_post.subject' :
  51. case 'tb_comment.content' :
  52. case 'tb_post_deleted.ip_address' :
  53. case 'users.name' :
  54. case 'users.email' :
  55. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  56. break;
  57. case 'tb_post.id' :
  58. case 'tb_comment.id' :
  59. case 'users.id' :
  60. case 'users.sid' :
  61. $query->where($params->field, '=', $params->keyword);
  62. break;
  63. }
  64. }
  65. if($params->startDate || $params->endDate) {
  66. if($params->startDate) {
  67. $query->where('tb_comment_deleted.created_at', '>=', $params->startDate . ' 00:00:00');
  68. }
  69. if($params->endDate) {
  70. $query->where('tb_comment_deleted.created_at', '<=', $params->endDate . ' 23:59:59');
  71. }
  72. }
  73. if($params->boardID) {
  74. $query->where('tb_comment.board_id', '=', $params->boardID);
  75. }
  76. $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_deleted.post_id');
  77. $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_deleted.comment_id');
  78. $query->join('tb_board', 'tb_board.id', '=', 'tb_comment.board_id');
  79. $query->leftjoin('users', 'users.id', '=', 'tb_comment_deleted.user_id');
  80. $query->orderByDesc('tb_comment_deleted.id');
  81. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  82. $total = $this->count();
  83. $rows = $list->count();
  84. return (object)[
  85. 'total' => $total,
  86. 'rows' => $rows,
  87. 'list' => $list
  88. ];
  89. }
  90. /**
  91. * 댓글 전체삭제
  92. */
  93. public function truncate(): bool
  94. {
  95. DB::beginTransaction();
  96. try
  97. {
  98. // 댓글 실제 삭제
  99. foreach ($this->select('id')->get() as $row) {
  100. if (!$this->remove($row->id)) {
  101. throw new Exception();
  102. }
  103. }
  104. DB::commit();
  105. return true;
  106. }catch(Exception) {
  107. DB::rollBack();
  108. return false;
  109. }
  110. }
  111. /**
  112. * 댓글 삭제
  113. */
  114. public function remove(int $deletedID): bool
  115. {
  116. $boardModel = new Board();
  117. $postModel = new Post();
  118. $commentModel = new Comment();
  119. $commentBlameModel = new CommentBlame();
  120. $commentHistoryModel = new CommentHistory();
  121. $commentLikeModel = new CommentLike();
  122. $commentMetaModel = new CommentMeta();
  123. $editorImageModel = new EditorImage();
  124. DB::beginTransaction();
  125. try
  126. {
  127. $deleteInfo = $this->find($deletedID);
  128. $boardID = $deleteInfo->board_id;
  129. $postID = $deleteInfo->post_id;
  130. $commentID = $deleteInfo->comment_id;
  131. $where = [
  132. ['board_id', $boardID],
  133. ['post_id', $postID],
  134. ['comment_id', $commentID]
  135. ];
  136. $commentModel->where($where)->delete(); // 댓글 삭제
  137. $commentBlameModel->where($where)->delete(); // 댓글 신고 기록 삭제
  138. $commentHistoryModel->where($where)->delete(); // 댓글 변경 이력 삭제
  139. $commentLikeModel->where($where)->delete(); // 추천/비추천 기록 삭제
  140. $commentMetaModel->deleteMeta($commentID); // 댓글 메타 정보 삭제
  141. // 게시글 이미지 삭제
  142. $editorImageModel->truncate(EDITOR_IMG_TYPE_2, ($postID . DIRECTORY_SEPARATOR . $commentID));
  143. // 게시판 댓글 수 갱신
  144. $boardModel->updateCommentRows($boardID);
  145. // 게시글 댓글 수 갱신
  146. $postModel->updateCommentRows($postID);
  147. // 댓글 삭제 완료
  148. $deleteInfo->delete();
  149. DB::commit();
  150. return true;
  151. }catch(Exception) {
  152. DB::rollBack();
  153. return false;
  154. }
  155. }
  156. /**
  157. * 댓글 복원
  158. */
  159. public function recovery(int $deletedID): bool
  160. {
  161. $commentModel = new Comment();
  162. DB::beginTransaction();
  163. try
  164. {
  165. $deleteInfo = $this->find($deletedID);
  166. // 댓글 삭제 복구
  167. $commentModel->where([
  168. ['board_id', $deleteInfo->board_id],
  169. ['post_id', $deleteInfo->post_id],
  170. ['comment_id', $deleteInfo->comment_id],
  171. ['is_delete', 1]
  172. ])->update([
  173. 'is_delete' => 0,
  174. 'deleted_at' => null
  175. ]);
  176. // 댓글 삭제 기록 제거
  177. $deleteInfo->delete();
  178. DB::commit();
  179. return true;
  180. }catch(Exception) {
  181. DB::rollBack();
  182. return false;
  183. }
  184. }
  185. /**
  186. * 댓글 삭제 등록
  187. */
  188. public function register(array $params): int
  189. {
  190. return $this->insertGetId($params);
  191. }
  192. }