CommentBlame.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class CommentBlame extends Model
  6. {
  7. protected $table = 'tb_comment_blame';
  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 = null;
  14. protected $guarded = [];
  15. public function board()
  16. {
  17. return $this->belongsTo(Board::class)->withDefault();
  18. }
  19. public function post()
  20. {
  21. return $this->belongsTo(Post::class)->withDefault();
  22. }
  23. public function user()
  24. {
  25. return $this->belongsTo(User::class)->withDefault();
  26. }
  27. /**
  28. * 댓글 신고 목록
  29. */
  30. public function data(SearchData $params): object
  31. {
  32. $query = $this->query();
  33. $query->select(
  34. 'tb_board.name AS boardName',
  35. 'tb_board.code',
  36. 'tb_post.subject',
  37. 'tb_comment_blame.*',
  38. 'user_1.sid AS sid_1',
  39. 'user_1.name AS name_1',
  40. 'user_2.sid AS sid_2',
  41. 'user_2.name AS name_2'
  42. );
  43. if($params->keyword) {
  44. switch ($params->field) {
  45. case 'tb_comment_blame.ip_address' :
  46. case 'user_1.name' :
  47. case 'user_1.email' :
  48. case 'user_2.name' :
  49. case 'user_2.email' :
  50. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  51. break;
  52. case 'tb_comment_blame.id' :
  53. case 'tb_comment_blame.post_id' :
  54. case 'user_1.id' :
  55. case 'user_1.sid' :
  56. case 'user_2.id' :
  57. case 'user_2.sid' :
  58. $query->where($params->field, '=', $params->keyword);
  59. break;
  60. }
  61. }
  62. if($params->startDate || $params->endDate) {
  63. if($params->startDate) {
  64. $query->where('tb_comment_blame.created_at', '>=', $params->startDate . ' 00:00:00');
  65. }
  66. if($params->endDate) {
  67. $query->where('tb_comment_blame.created_at', '<=', $params->endDate . ' 23:59:59');
  68. }
  69. }
  70. if($params->boardID) {
  71. $query->where('tb_comment_blame.board_id', '=', $params->boardID);
  72. }
  73. $query->join('tb_board', 'tb_board.id', '=', 'tb_comment_blame.board_id'); // 게시판
  74. $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_blame.post_id'); // 게시글
  75. $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_blame.comment_id'); // 댓글
  76. $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_comment.user_id'); // 작성자
  77. $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_comment_blame.user_id'); // 신고자
  78. $query->orderByDesc('tb_comment_blame.id');
  79. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  80. $total = $this->count();
  81. $rows = $list->count();
  82. return (object)[
  83. 'total' => $total,
  84. 'rows' => $rows,
  85. 'list' => $list
  86. ];
  87. }
  88. /**
  89. * 댓글 신고 접수
  90. */
  91. public function register(array $params): int
  92. {
  93. return $this->insertGetId($params);
  94. }
  95. /**
  96. * 댓글 신고 결과 변경
  97. */
  98. public function commentBlameResultUpdate(int $commentBlameID, int $status, string $memo): int
  99. {
  100. return $this->find($commentBlameID)->update([
  101. 'status' => $status,
  102. 'memo' => $memo
  103. ]);
  104. }
  105. /**
  106. * 댓글 신고 존재 유무
  107. */
  108. public function isAlready(Comment $comment, int $userID): bool
  109. {
  110. return $this->where([
  111. ['post_id', $comment->post_id],
  112. ['comment_id', $comment->id],
  113. ['user_id', $userID]
  114. ])->exists();
  115. }
  116. }