截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommunityComment.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\common\model\community;
  3. use app\common\basics\Models;
  4. use app\common\enum\CommunityCommentEnum;
  5. use app\common\model\user\User;
  6. /**
  7. * 种草社区评论
  8. * Class CommunityComment
  9. * @package app\common\model\community
  10. */
  11. class CommunityComment extends Models
  12. {
  13. /**
  14. * @notes 关联用户
  15. * @return \think\model\relation\HasOne
  16. * @author 段誉
  17. * @date 2022/5/7 15:22
  18. */
  19. public function user()
  20. {
  21. return $this->hasOne(User::class, 'id', 'user_id')
  22. ->bind(['nickname', 'avatar', 'sn']);
  23. }
  24. /**
  25. * @notes 子级
  26. * @return \think\model\relation\HasMany
  27. * @author 段誉
  28. * @date 2022/5/10 11:45
  29. */
  30. public function child()
  31. {
  32. return $this->hasMany(self::class, 'id', 'pid');
  33. }
  34. /**
  35. * @notes 关联文章
  36. * @return \think\model\relation\HasOne
  37. * @author 段誉
  38. * @date 2022/5/10 11:46
  39. */
  40. public function article()
  41. {
  42. return $this->hasOne(CommunityArticle::class, 'id', 'article_id');
  43. }
  44. /**
  45. * @notes 一级评论的所有子级评论
  46. * @param $value
  47. * @param $data
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @author 段誉
  53. * @date 2022/5/9 14:28
  54. */
  55. public function getChildAttr($value, $data)
  56. {
  57. $lists = self::with(['user'])
  58. ->whereFindInSet('ancestor_relation', $data['id'])
  59. ->where(['del' => 0, 'status' => CommunityCommentEnum::STATUS_SUCCESS])
  60. ->order(['like' => 'desc'])
  61. ->select()->toArray();
  62. return $lists;
  63. }
  64. /**
  65. * @notes 增加点赞数量
  66. * @param $id
  67. * @return mixed
  68. * @author 段誉
  69. * @date 2022/5/9 15:35
  70. */
  71. public static function incLike($id)
  72. {
  73. return self::where(['id' => $id])->inc('like')->update();
  74. }
  75. /**
  76. * @notes 减少点赞数量
  77. * @param $id
  78. * @return mixed
  79. * @author 段誉
  80. * @date 2022/5/9 15:38
  81. */
  82. public static function decLike($id)
  83. {
  84. $where = [
  85. ['id', '=', $id],
  86. ['like', '>=', 1]
  87. ];
  88. return self::where($where)->dec('like')->update();
  89. }
  90. }