Post.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use CyrildeWit\EloquentViewable\InteractsWithViews;
  6. use CyrildeWit\EloquentViewable\Contracts\Viewable;
  7. use App\Models\DTO\SearchData;
  8. class Post extends Model implements Viewable
  9. {
  10. use InteractsWithViews;
  11. protected $table = 'tb_post';
  12. protected $primaryKey = 'id';
  13. public $keyType = 'int';
  14. public $incrementing = true;
  15. public $timestamps = true;
  16. const CREATED_AT = 'created_at';
  17. const UPDATED_AT = 'updated_at';
  18. protected $guarded = [];
  19. public function board()
  20. {
  21. return $this->belongsTo(Board::class)->withDefault();
  22. }
  23. public function boardCategory()
  24. {
  25. return $this->belongsTo(BoardCategory::class)->withDefault();
  26. }
  27. public function user()
  28. {
  29. return $this->belongsTo(User::class)->withDefault();
  30. }
  31. public function postBlame()
  32. {
  33. return $this->hasMany(PostBlame::class);
  34. }
  35. public function postBookmark()
  36. {
  37. return $this->hasMany(PostBookmark::class);
  38. }
  39. public function postDeleted()
  40. {
  41. return $this->hasMany(PostDeleted::class);
  42. }
  43. public function postFile()
  44. {
  45. return $this->hasMany(PostFile::class);
  46. }
  47. public function postFileDownLog()
  48. {
  49. return $this->hasMany(PostFileDownLog::class);
  50. }
  51. public function postHistory()
  52. {
  53. return $this->hasMany(PostHistory::class);
  54. }
  55. public function postLike()
  56. {
  57. return $this->hasMany(PostLike::class);
  58. }
  59. public function postLink()
  60. {
  61. return $this->hasMany(PostLink::class);
  62. }
  63. public function postLinkClickLog()
  64. {
  65. return $this->hasMany(PostLinkClickLog::class);
  66. }
  67. public function postMeta()
  68. {
  69. return $this->hasMany(PostMeta::class);
  70. }
  71. public function postTag()
  72. {
  73. return $this->hasMany(PostTag::class);
  74. }
  75. public function comments()
  76. {
  77. return $this->hasMany(Comment::Class);
  78. }
  79. /**
  80. * 게시물 조회
  81. */
  82. public function data(SearchData $params)
  83. {
  84. $query = $this->query();
  85. $query->select(
  86. 'tb_board.name AS boardName',
  87. 'tb_board.code',
  88. 'tb_post.*',
  89. 'users.sid',
  90. 'users.name'
  91. );
  92. if($params->keyword) {
  93. switch ($params->field) {
  94. case 'tb_post.id' :
  95. case 'users.id' :
  96. case 'users.sid' :
  97. $query->where($params->field, '=', $params->keyword);
  98. break;
  99. case 'tb_post.subject' :
  100. case 'tb_post.content' :
  101. case 'users.name' :
  102. case 'users.email' :
  103. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  104. break;
  105. }
  106. }
  107. $query->where('tb_post.is_delete', '=', 0);
  108. if($params->startDate || $params->endDate) {
  109. if($params->startDate) {
  110. $query->where('tb_post.created_at', '>=', $params->startDate . ' 00:00:00');
  111. }
  112. if($params->endDate) {
  113. $query->where('tb_post.created_at', '<=', $params->endDate . ' 23:59:59');
  114. }
  115. }
  116. if($params->boardID) {
  117. $query->where('tb_post.board_id', '=', $params->boardID);
  118. }
  119. $query->join('tb_board', 'tb_board.id', '=', 'tb_post.board_id');
  120. $query->leftJoin('users', 'users.id', '=', 'tb_post.user_id');
  121. $query->orderByDesc('tb_post.id');
  122. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  123. $total = $query->count();
  124. $rows = $list->count();
  125. return (object)[
  126. 'total' => $total,
  127. 'rows' => $rows,
  128. 'list' => $list
  129. ];
  130. }
  131. /**
  132. * 게시글 등록
  133. */
  134. public function register(array $params): Post
  135. {
  136. return $this->create($params);
  137. }
  138. /**
  139. * 게시글 수정
  140. */
  141. public function updater(int $postID, array $params): Post
  142. {
  143. return $this->updateOrCreate([$this->primaryKey => $postID], $params);
  144. }
  145. /**
  146. * 게시글 임시 삭제
  147. */
  148. public function remove(Post $post, ?User $user): mixed
  149. {
  150. return DB::transaction(function() use($post, $user)
  151. {
  152. // 게시글 삭제 여부 승인
  153. $this->updater($post->id, [
  154. 'is_delete' => 1,
  155. 'deleted_at' => now()
  156. ]);
  157. // 게시글 삭제 정보 추가
  158. (new PostDeleted)->register([
  159. 'board_id' => $post->board_id,
  160. 'post_id' => $post->id,
  161. 'user_id' => $user?->id,
  162. 'ip_address' => IP_ADDRESS,
  163. 'user_agent' => USER_AGENT,
  164. 'created_at' => now()
  165. ]);
  166. return true;
  167. });
  168. }
  169. /**
  170. * 게시글 조회
  171. */
  172. public function get(int $postID): Post
  173. {
  174. return $this->from('tb_post AS PST')->select('PST.*')
  175. ->selectRaw(
  176. '(SELECT id FROM tb_post WHERE id < PST.id AND board_id = PST.board_id AND is_delete = 0 ORDER BY id DESC LIMIT 1) AS beforePostID'
  177. )->selectRaw(
  178. '(SELECT id FROM tb_post WHERE id > PST.id AND board_id = PST.board_id AND is_delete = 0 ORDER BY id ASC LIMIT 1) AS nextPostID'
  179. )->with(['board', 'user'])->where([
  180. ['PST.id', $postID],
  181. ['PST.is_delete', 0]
  182. ])->firstOrNew();
  183. }
  184. /**
  185. * 댓글 갯수 갱신
  186. */
  187. public function updateCommentRows(int $postID): int
  188. {
  189. $sql = "
  190. UPDATE tb_post A
  191. SET A.comment_rows =
  192. (
  193. SELECT COUNT(*) AS total FROM tb_comment CMT WHERE CMT.post_id = A.id AND
  194. CASE WHEN
  195. (
  196. CMT.is_delete = 1
  197. AND ((SELECT (COUNT(*) - 1) FROM tb_comment WHERE lft BETWEEN CMT.lft AND CMT.rgt) <= 0)
  198. AND ((SELECT (COUNT(*) - 1) FROM tb_comment WHERE CMT.rgt BETWEEN lft AND rgt) <= 0)
  199. ) THEN FALSE ELSE TRUE END
  200. )
  201. WHERE A.id = ?;
  202. ";
  203. return DB::update($sql, [$postID]);
  204. }
  205. /**
  206. * 파일 첨부 갯수 갱신
  207. */
  208. public function updateFileRows(int $postID): int
  209. {
  210. $sql = "
  211. UPDATE tb_post A
  212. SET A.file_rows = (SELECT COUNT(*) AS total FROM tb_post_file WHERE post_id = A.id)
  213. WHERE A.id = ?;
  214. ";
  215. return DB::update($sql, [$postID]);
  216. }
  217. /**
  218. * 이미지 파일 갯수 갱신
  219. */
  220. public function updateImageRows(int $postID): int
  221. {
  222. $sql = "
  223. UPDATE tb_post A
  224. SET A.image_rows = ((SELECT COUNT(*) AS total FROM tb_post_file WHERE post_id = A.id AND is_image = 1) + A.image_rows)
  225. WHERE A.id = ?;
  226. ";
  227. return DB::update($sql, [$postID]);
  228. }
  229. /**
  230. * 게시글 태그 갯수 갱신
  231. */
  232. public function updateTagRows(int $postID): int
  233. {
  234. $sql = "
  235. UPDATE tb_post A
  236. SET A.tag_rows = (SELECT COUNT(*) AS total FROM tb_post_tag WHERE post_id = A.id)
  237. WHERE A.id = ?;
  238. ";
  239. return DB::update($sql, [$postID]);
  240. }
  241. /**
  242. * 게시글 링크 갯수 갱신
  243. */
  244. public function updateLinkRows(int $postID): int
  245. {
  246. $sql = "
  247. UPDATE tb_post A
  248. SET A.link_rows = (SELECT COUNT(*) AS total FROM tb_post_link WHERE post_id = A.id)
  249. WHERE A.id = ?;
  250. ";
  251. return DB::update($sql, [$postID]);
  252. }
  253. /**
  254. * 게시글 조회 수 증가
  255. */
  256. public function increaseHit(int $postID): int
  257. {
  258. return $this->where('id', $postID)->increment('hit');
  259. }
  260. /**
  261. * 게시글 신고 수 증가
  262. */
  263. public function increaseBlame(int $postID): int
  264. {
  265. return $this->where('id', $postID)->increment('blame');
  266. }
  267. /**
  268. * 게시글 신고 수 증감
  269. */
  270. public function decreaseBlame(int $postID): int
  271. {
  272. return $this->where('id', $postID)->where('blame', '>', '0')->decrement('blame', 1);
  273. }
  274. /**
  275. * 게시글 좋아요 수 증가
  276. */
  277. public function increaseLike(int $postID): int
  278. {
  279. return $this->where('id', $postID)->increment('like');
  280. }
  281. /**
  282. * 게시글 좋아요 수 증감
  283. */
  284. public function decreaseLike(int $postID): int
  285. {
  286. return $this->where('id', $postID)->where('like', '>', '0')->decrement('like', 1);
  287. }
  288. /**
  289. * 게시글 싫어요 수 증가
  290. */
  291. public function increaseDisLike(int $postID): int
  292. {
  293. return $this->where('id', $postID)->increment('dislike');
  294. }
  295. /**
  296. * 게시글 싫어요 수 증감
  297. */
  298. public function decreaseDisLike(int $postID): int
  299. {
  300. return $this->where('id', $postID)->where('dislike', '>', '0')->decrement('dislike');
  301. }
  302. /**
  303. * 게시글 존재 여부
  304. */
  305. public function isExists(int $postID)
  306. {
  307. $sql = "
  308. SELECT IF(COUNT(*) > 0, 1, 0) AS isExists
  309. FROM tb_post PST JOIN tb_board BRD ON BRD.id = PST.board_id
  310. WHERE PST.id = ? AND PST.is_delete = 0;
  311. ";
  312. return DB::selectOne($sql, [$postID])->isExists;
  313. }
  314. /**
  315. * 게시글의 현재 페이지 번호
  316. */
  317. public function findPageNumber(int $postID, int $perPage = DEFAULT_LIST_PER_PAGE): int
  318. {
  319. $sql = "
  320. SELECT ROUND(COUNT(*) / :perPage) + 1 AS page
  321. FROM
  322. tb_post,
  323. (SELECT @postID := id, @boardID := board_id
  324. FROM tb_post WHERE id = :postID
  325. ) AS N
  326. WHERE id > @postID AND board_id = @boardID AND is_delete = 0;
  327. ";
  328. return DB::selectOne($sql, [
  329. 'postID' => $postID,
  330. 'perPage' => $perPage
  331. ])->page;
  332. }
  333. /**
  334. * 추가 게시글 번호
  335. */
  336. public function lastedKey(): int
  337. {
  338. return intval(DB::selectOne(
  339. "SELECT AUTO_INCREMENT AS id FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'tb_post';"
  340. )->id);
  341. }
  342. /**
  343. * 게시글 조회수 증가
  344. */
  345. public function addHit(Post $post): bool
  346. {
  347. return views($post)->cooldown(now()->addYear())->record();
  348. }
  349. }