CommunityReplyRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. class CommunityReplyRepository extends BaseRepository
  21. {
  22. /**
  23. * @var CommunityReplyDao
  24. */
  25. protected $dao;
  26. /**
  27. * CommunityReplyRepository constructor.
  28. * @param CommunityReplyDao $dao
  29. */
  30. public function __construct(CommunityReplyDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. public function getList(array $where, int $page, int $limit)
  35. {
  36. $where['is_del'] = 0;
  37. $query = $this->dao->search($where)->with([
  38. 'community' => function ($query) {
  39. $query->field('community_id,title');
  40. },
  41. 'author' => function ($query) {
  42. $query->field('uid,nickname,avatar');
  43. },
  44. 'reply' => function ($query) {
  45. $query->field('uid,nickname,avatar');
  46. },
  47. 'hasReply' => function ($query) {
  48. $query->field('pid, reply_id, status');
  49. },
  50. ]);
  51. $count = $query->count();
  52. $list = $query->page($page, $limit)->select();
  53. return compact('count', 'list');
  54. }
  55. public function getApiList(array $where, int $page, int $limit, $userInfo)
  56. {
  57. $make = app()->make(CommunityRepository::class);
  58. $where_['community_id'] = $where['community_id'];
  59. $where_ = CommunityRepository::IS_SHOW_WHERE;
  60. if ($userInfo && $make->uidExists((int)$where['community_id'], $userInfo->uid)) {
  61. $where_ = ['is_del' => 0];
  62. }
  63. $where_['community_id'] = $where['community_id'];
  64. if (!$make->getWhereCount($where_)) throw new ValidateException('内容不存在,可能被删删除了哦~');
  65. $where['status'] = 1;
  66. $all = $this->dao->getSearch($where)->count();
  67. $start = $this->dao->getSearch($where)->sum('count_start');
  68. $where['pid'] = 0;
  69. $query = $this->dao->getSearch($where)
  70. ->order('create_time DESC')
  71. ->hidden(['refusal'])
  72. ->with([
  73. 'author' => function ($query) {
  74. $query->field('uid,nickname,avatar');
  75. },
  76. 'is_start' => function ($query) use ($userInfo) {
  77. $query->where('left_id', $userInfo->uid ?? null);
  78. },
  79. 'children' => [
  80. 'author' => function ($query) {
  81. $query->field('uid,nickname,avatar');
  82. },
  83. 'reply' => function ($query) {
  84. $query->field('uid,nickname,avatar')->order('create_time ASC');
  85. },
  86. 'is_start' => function ($query) use ($userInfo) {
  87. $query->where('left_id', $userInfo->uid ?? null);
  88. }
  89. ],
  90. ]);
  91. $count = $query->count();
  92. $list = $query->page($page, $limit)->select();
  93. return compact('all', 'start', 'count', 'list');
  94. }
  95. /**
  96. * TODO 发表评论
  97. * @param int $replyId
  98. * @param array $data
  99. * @author Qinii
  100. * @day 10/29/21
  101. */
  102. public function create(int $replyId, array $data)
  103. {
  104. $make = app()->make(CommunityRepository::class);
  105. if (!$make->exists($data['community_id']))
  106. throw new ValidateException('内容不存在,可能已被删除了哦~');
  107. $data['pid'] = $replyId;
  108. if ($replyId) {
  109. $get = $this->dao->get($replyId);
  110. if (!$get) throw new ValidateException('您回复的评论不存在');
  111. if ($get->pid) {
  112. $data['re_uid'] = $get->uid;
  113. $data['pid'] = $get->pid;
  114. }
  115. }
  116. $res = Db::transaction(function () use ($replyId, $data, $make) {
  117. $res = $this->dao->create($data);
  118. if ($replyId) $this->dao->incField($data['pid'], 'count_reply', 1);
  119. return $res;
  120. });
  121. $ret = $this->dao->getWhere(['reply_id' => $res->reply_id], '*', [
  122. 'author' => function ($query) {
  123. $query->field('uid,nickname,avatar');
  124. },
  125. 'reply' => function ($query) {
  126. $query->field('uid,nickname,avatar')->order('create_time ASC');
  127. },
  128. ]);
  129. return $ret;
  130. }
  131. public function delete($id)
  132. {
  133. Db::transaction(function () use ($id) {
  134. $get = $this->dao->get($id);
  135. $make = app()->make(CommunityRepository::class);
  136. if ($get->pid) $this->dao->decField($get['pid'], 'count_reply', 1);
  137. $make->decField($get['community_id'], 'count_reply', 1);
  138. $get->delete();
  139. });
  140. }
  141. public function setStart(int $id, int $uid, int $status)
  142. {
  143. $make = app()->make(RelevanceRepository::class);
  144. $check = $make->checkHas($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START);
  145. if ($status) {
  146. if ($check) throw new ValidateException('您已经赞过过他了~');
  147. $make->create($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START, true);
  148. $this->dao->incField($id, 'count_start', 1);
  149. } else {
  150. if (!$check) throw new ValidateException('您还未赞过他哦~');
  151. $make->destory($uid, $id, RelevanceRepository::TYPE_COMMUNITY_REPLY_START);
  152. $this->dao->decField($id, 'count_start', 1);
  153. }
  154. return;
  155. }
  156. public function statusForm(int $id)
  157. {
  158. $formData = $this->dao->get($id)->toArray();
  159. if ($formData['status'] !== 0) throw new ValidateException('请勿重复审核');
  160. $form = Elm::createForm(Route::buildUrl('systemCommunityReplyStatus', ['id' => $id])->build());
  161. $form->setRule([
  162. Elm::textarea('content', '评论内容')->disabled(true),
  163. Elm::radio('status', '审核状态', 1)->options([
  164. ['value' => -1, 'label' => '未通过'],
  165. ['value' => 1, 'label' => '通过']]
  166. )->control([
  167. ['value' => -1, 'rule' => [
  168. Elm::textarea('refusal', '未通过原因', '')->required()
  169. ]]
  170. ]),
  171. ]);
  172. $formData['status'] = 1;
  173. return $form->setTitle('审核评论')->formData($formData);
  174. }
  175. }