CommunityReplyRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\community;
  12. use app\common\dao\community\CommunityReplyDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\RelevanceRepository;
  15. use Carbon\Exceptions\InvalidDateException;
  16. use FormBuilder\Factory\Elm;
  17. use think\exception\ValidateException;
  18. use think\facade\Db;
  19. use think\facade\Route;
  20. /**
  21. * 社区评论
  22. */
  23. class CommunityReplyRepository extends BaseRepository
  24. {
  25. /**
  26. * @var CommunityReplyDao
  27. */
  28. protected $dao;
  29. /**
  30. * CommunityReplyRepository constructor.
  31. * @param CommunityReplyDao $dao
  32. */
  33. public function __construct(CommunityReplyDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 获取列表数据
  39. *
  40. * 根据给定的条件和分页信息,从数据库中检索满足条件的数据列表。此方法主要用于查询文章或帖子等信息,
  41. * 同时附带作者、社区和回复等相关信息,以丰富的数据集合形式返回。
  42. *
  43. * @param array $where 查询条件数组,用于指定查询的数据项。
  44. * @param int $page 当前页码,用于分页查询。
  45. * @param int $limit 每页数据的数量,用于分页查询。
  46. * @return array 返回包含总数和列表数据的数组。
  47. */
  48. public function getList(array $where, int $page, int $limit)
  49. {
  50. // 默认不查询已删除的数据
  51. $where['is_del'] = 0;
  52. // 构建查询,同时加载关联信息,如社区、作者和回复等
  53. $query = $this->dao->search($where)->with([
  54. 'community' => function ($query) {
  55. // 加载社区的基本信息,包括社区ID和标题
  56. $query->field('community_id,title');
  57. },
  58. 'author' => function ($query) {
  59. // 加载作者的信息,包括用户ID、昵称和头像
  60. $query->field('uid,nickname,avatar');
  61. },
  62. 'reply' => function ($query) {
  63. // 加载第一条回复的信息,包括用户ID、昵称和头像
  64. $query->field('uid,nickname,avatar');
  65. },
  66. 'hasReply' => function ($query) {
  67. // 加载是否含有回复的信息,包括回复的父ID、回复ID和状态
  68. $query->field('pid, reply_id, status');
  69. },
  70. ]);
  71. // 计算满足条件的数据总数
  72. $count = $query->count();
  73. // 根据当前页码和每页数据数量,获取满足条件的数据列表
  74. $list = $query->page($page, $limit)->select();
  75. // 将数据总数和数据列表打包成数组返回
  76. return compact('count', 'list');
  77. }
  78. /**
  79. * 获取API列表
  80. * 根据给定的条件和分页信息,获取社区内容的列表。
  81. * 这包括对内容存在性的验证,以及根据用户信息和条件进行的特定查询设置。
  82. *
  83. * @param array $where 查询条件数组,包含社区ID等条件
  84. * @param int $page 当前页码
  85. * @param int $limit 每页显示的数量
  86. * @param object $userInfo 用户信息对象,包含用户ID等
  87. * @return array 返回包含所有、开始、计数和列表的数组
  88. * @throws ValidateException 如果内容不存在则抛出异常
  89. */
  90. public function getApiList(array $where, int $page, int $limit, $userInfo)
  91. {
  92. // 实例化社区仓库类
  93. $make = app()->make(CommunityRepository::class);
  94. // 设置显示状态的查询条件
  95. $where_ = CommunityRepository::IS_SHOW_WHERE;
  96. // 初始化查询条件中的社区ID
  97. $where_['community_id'] = $where['community_id'];
  98. // 如果用户信息存在,并且用户ID存在于指定的社区中,则修改查询条件
  99. if ($userInfo && $make->uidExists((int)$where['community_id'], $userInfo->uid)) {
  100. $where_ = ['is_del' => 0];
  101. }
  102. // 重新设置社区ID的查询条件
  103. $where_['community_id'] = $where['community_id'];
  104. // 验证内容是否存在
  105. if (!$make->getWhereCount($where_)) {
  106. throw new ValidateException('内容不存在,可能被删删除了哦~');
  107. }
  108. // 设置查询状态为已发布
  109. $where['status'] = 1;
  110. // 计算符合条件的总内容数量
  111. $all = $this->dao->getSearch($where)->count();
  112. // 计算已开始的内容数量
  113. $start = $this->dao->getSearch($where)->sum('count_start');
  114. // 设置查询父级内容的条件
  115. $where['pid'] = 0;
  116. // 设置查询顺序和隐藏字段,并加载关联数据
  117. $query = $this->dao->getSearch($where)
  118. ->order('create_time DESC')
  119. ->hidden(['refusal'])
  120. ->with([
  121. 'author' => function ($query) {
  122. $query->field('uid,nickname,avatar');
  123. },
  124. 'is_start' => function ($query) use ($userInfo) {
  125. $query->where('left_id', $userInfo->uid ?? null);
  126. },
  127. 'children' => [
  128. 'author' => function ($query) {
  129. $query->field('uid,nickname,avatar');
  130. },
  131. 'reply' => function ($query) {
  132. $query->field('uid,nickname,avatar')->order('create_time ASC');
  133. },
  134. 'is_start' => function ($query) use ($userInfo) {
  135. $query->where('left_id', $userInfo->uid ?? null);
  136. }
  137. ],
  138. ]);
  139. // 计算查询结果的总数量
  140. $count = $query->count();
  141. // 获取当前页的列表数据
  142. $list = $query->page($page, $limit)->select();
  143. // 返回包含所有、开始、计数和列表的数组
  144. return compact('all', 'start', 'count', 'list');
  145. }
  146. /**
  147. * 发表评论
  148. * @param int $replyId
  149. * @param array $data
  150. * @author Qinii
  151. * @day 10/29/21
  152. */
  153. public function create(int $replyId, array $data)
  154. {
  155. $make = app()->make(CommunityRepository::class);
  156. if (!$make->exists($data['community_id']))
  157. throw new ValidateException('内容不存在,可能已被删除了哦~');
  158. $data['pid'] = $replyId;
  159. if ($replyId) {
  160. $get = $this->dao->get($replyId);
  161. if (!$get) throw new ValidateException('您回复的评论不存在');
  162. if ($get->pid) {
  163. $data['re_uid'] = $get->uid;
  164. $data['pid'] = $get->pid;
  165. }
  166. }
  167. $res = Db::transaction(function () use ($replyId, $data, $make) {
  168. $res = $this->dao->create($data);
  169. if ($replyId) $this->dao->incField($data['pid'], 'count_reply', 1);
  170. return $res;
  171. });
  172. $ret = $this->dao->getWhere(['reply_id' => $res->reply_id], '*', [
  173. 'author' => function ($query) {
  174. $query->field('uid,nickname,avatar');
  175. },
  176. 'reply' => function ($query) {
  177. $query->field('uid,nickname,avatar')->order('create_time ASC');
  178. },
  179. ]);
  180. return $ret;
  181. }
  182. /**
  183. * 删除指定ID的帖子
  184. *
  185. * 本函数通过使用事务处理来确保删除操作的完整性。它首先检查帖子是否存在父帖子,
  186. * 如果存在,则减少父帖子的回复计数。然后,它减少关联社区的回复计数。最后,它删除指定的帖子。
  187. * 使用事务的原因是为了避免在操作过程中发生的数据不一致问题。
  188. *
  189. * @param int $id 帖子的唯一标识符
  190. */
  191. public function delete($id)
  192. {
  193. // 开启事务处理
  194. Db::transaction(function () use ($id) {
  195. // 获取要删除的帖子对象
  196. $get = $this->dao->get($id);
  197. // 实例化社区仓库类
  198. $make = app()->make(CommunityRepository::class);
  199. // 如果帖子有父帖子,减少父帖子的回复计数
  200. if ($get->pid) $this->dao->decField($get['pid'], 'count_reply', 1);
  201. // 减少帖子所在社区的回复计数
  202. $make->decField($get['community_id'], 'count_reply', 1);
  203. // 删除帖子
  204. $get->delete();
  205. });
  206. }
  207. /**
  208. * 社区评论 点赞和取消点赞 操作
  209. * @param int $id
  210. * @param int $uid
  211. * @param int $status
  212. * @return void
  213. * @author Qinii
  214. */
  215. public function setStart(int $id, int $uid, int $status)
  216. {
  217. $make = app()->make(RelevanceRepository::class);
  218. $check = $make->checkHas($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START);
  219. if ($status) {
  220. if ($check) throw new ValidateException('您已经赞过他了~');
  221. $make->create($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START, true);
  222. $this->dao->incField($id, 'count_start', 1);
  223. } else {
  224. if (!$check) throw new ValidateException('您还未赞过他哦~');
  225. $make->destory($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START);
  226. $this->dao->decField($id, 'count_start', 1);
  227. }
  228. return;
  229. }
  230. /**
  231. * 后台审核表单
  232. * @param int $id
  233. * @return \FormBuilder\Form
  234. * @author Qinii
  235. */
  236. public function statusForm(int $id)
  237. {
  238. $formData = $this->dao->get($id)->toArray();
  239. if ($formData['status'] !== 0) throw new ValidateException('请勿重复审核');
  240. $form = Elm::createForm(Route::buildUrl('systemCommunityReplyStatus', ['id' => $id])->build());
  241. $form->setRule([
  242. Elm::textarea('content', '评论内容:')->placeholder('请输入评论内容')->disabled(true),
  243. Elm::radio('status', '审核状态:', 1)->options([
  244. ['value' => -1, 'label' => '未通过'],
  245. ['value' => 1, 'label' => '通过']]
  246. )->control([
  247. ['value' => -1, 'rule' => [
  248. Elm::textarea('refusal', '未通过原因', '')->required()
  249. ]]
  250. ]),
  251. ]);
  252. $formData['status'] = 1;
  253. return $form->setTitle('审核评论')->formData($formData);
  254. }
  255. }