PostBlame.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class PostBlame extends Model
  6. {
  7. protected $table = 'tb_post_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_post_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_post_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_post_blame.id' :
  53. case 'tb_post_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_post_blame.created_at', '>=', $params->startDate . ' 00:00:00');
  65. }
  66. if($params->endDate) {
  67. $query->where('tb_post_blame.created_at', '<=', $params->endDate . ' 23:59:59');
  68. }
  69. }
  70. if($params->boardID) {
  71. $query->where('tb_post_blame.board_id', '=', $params->boardID);
  72. }
  73. $query->join('tb_board', 'tb_board.id', '=', 'tb_post_blame.board_id'); // 게시판
  74. $query->join('tb_post', 'tb_post.id', '=', 'tb_post_blame.post_id'); // 게시글
  75. $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_post.user_id'); // 작성자
  76. $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_post_blame.user_id'); // 신고자
  77. $query->orderByDesc('tb_post_blame.id');
  78. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  79. $total = $this->count();
  80. $rows = $list->count();
  81. return (object)[
  82. 'total' => $total,
  83. 'rows' => $rows,
  84. 'list' => $list
  85. ];
  86. }
  87. /**
  88. * 게시글 신고 등록
  89. */
  90. public function register(array $params): int
  91. {
  92. return $this->insertGetId($params);
  93. }
  94. /**
  95. * 게시글 신고 결과 변경
  96. */
  97. public function postBlameResultUpdate(int $postBlameID, int $status, string $memo): int
  98. {
  99. return $this->find($postBlameID)->update([
  100. 'status' => $status,
  101. 'memo' => $memo
  102. ]);
  103. }
  104. /**
  105. * 댓글 신고 존재 유무
  106. */
  107. public function isAlready(Post $post, int $userID): bool
  108. {
  109. return $this->where([
  110. ['board_id', $post->board_id],
  111. ['post_id', $post->id],
  112. ['user_id', $userID],
  113. ])->exists();
  114. }
  115. }