CommunityTopicRepository.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\CommunityTopicDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Factory\Elm;
  15. use think\exception\ValidateException;
  16. use think\facade\Route;
  17. /**
  18. * 社区话题
  19. */
  20. class CommunityTopicRepository extends BaseRepository
  21. {
  22. /**
  23. * @var CommunityTopicDao
  24. */
  25. protected $dao;
  26. /**
  27. * CommunityTopicRepository constructor.
  28. * @param CommunityTopicDao $dao
  29. */
  30. public function __construct(CommunityTopicDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 创建或编辑社区话题表单
  36. *
  37. * 根据传入的$id$判断是创建新话题还是编辑已有的话题。如果$id$为空,则创建新话题;
  38. * 否则,根据$id$查询已有的话题信息,并进行编辑。
  39. *
  40. * @param int|null $id 话题ID,如果为null则表示创建新话题,否则表示编辑已有的话题。
  41. * @return \Encore\Admin\Widgets\Form 表单实例,包含表单的规则和数据。
  42. */
  43. public function form(?int $id)
  44. {
  45. // 初始化表单数据数组
  46. $formData = [];
  47. // 判断$id$是否为空,如果为空则创建新话题,否则编辑已有的话题
  48. if (!$id) {
  49. // 创建新话题的表单,表单提交地址为系统生成的创建话题的URL
  50. $form = Elm::createForm(Route::buildUrl('systemCommunityTopicCreate')->build());
  51. } else {
  52. // 根据$id$查询已有的话题信息,并转换为数组格式
  53. $formData = $this->dao->get($id)->toArray();
  54. // 创建编辑话题的表单,表单提交地址为系统生成的更新话题的URL,包含话题ID
  55. $form = Elm::createForm(Route::buildUrl('systemCommunityTopicUpdate', ['id' => $id])->build());
  56. }
  57. // 配置表单的规则和字段
  58. $form->setRule([
  59. // 社区分类选择字段,动态获取分类选项,必选
  60. Elm::select('category_id', '社区分类:')->options(function () {
  61. return app()->make(CommunityCategoryRepository::class)->options();
  62. })->placeholder('请选择社区分类')->requiredNum(),
  63. // 图标选择字段,使用iframe嵌入式选择图片,必选
  64. Elm::frameImage('pic', '图标:', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=pic&type=1')
  65. ->modal(['modal' => false])
  66. ->icon('el-icon-camera')
  67. ->width('896px')
  68. ->height('480px'),
  69. // 话题名称输入字段,必填
  70. Elm::input('topic_name', '社区话题:')->placeholder('请输入社区话题')->required(),
  71. // 是否显示开关,默认开启,可切换
  72. Elm::switches('status', '是否显示:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  73. // 是否推荐开关,默认开启,可切换
  74. Elm::switches('is_hot', '是否推荐:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  75. // 排序数字输入字段,最大值99999,精度为0
  76. Elm::number('sort', '排序:')->precision(0)->max(99999),
  77. ]);
  78. // 设置表单标题,根据$id$是否为空决定是添加话题还是编辑话题
  79. // 设置表单数据,如果$id$不为空,则使用查询到的话题数据填充表单
  80. return $form->setTitle(is_null($id) ? '添加话题' : '编辑话题')->formData($formData);
  81. }
  82. /**
  83. * 删除指定ID的社区帖子
  84. *
  85. * 此函数用于删除社区帖子,但首先它会检查该帖子下是否还有显示中的数据。
  86. * 如果存在显示中的数据,则抛出一个验证异常,防止删除操作继续进行。
  87. * 这种做法是为了确保不会意外删除那些仍然有活跃数据的帖子,从而影响用户体验或数据完整性。
  88. *
  89. * @param int $id 要删除的帖子的ID
  90. * @throws ValidateException 如果帖子下存在显示中的数据,则抛出此异常
  91. */
  92. public function delete(int $id)
  93. {
  94. // 通过依赖注入获取CommunityRepository实例
  95. $make = app()->make(CommunityRepository::class);
  96. // 检查帖子下是否存在显示中的数据
  97. if ( $make->getWhereCount(CommunityRepository::IS_SHOW_WHERE) ) {
  98. throw new ValidateException('该话题下存在数据');
  99. }
  100. // 如果没有显示中的数据,执行删除操作
  101. $this->dao->delete($id);
  102. }
  103. /**
  104. * 根据条件获取列表数据
  105. *
  106. * 本函数用于根据给定的条件、分页和限制从数据库获取列表数据。它首先应用条件查询,其中包括一个固定的条件(is_del = 0),
  107. * 然后按照排序规则获取数据的总数和实际数据。最后,它以数组的形式返回数据总数和数据列表。
  108. *
  109. * @param array $where 查询条件数组
  110. * @param int $page 当前页码
  111. * @param int $limit 每页数据的数量
  112. * @return array 包含数据总数和数据列表的数组
  113. */
  114. public function getList(array $where, int $page, int $limit)
  115. {
  116. // 将删除标记设置为0,确保只获取未被删除的数据
  117. $where['is_del'] = 0;
  118. // 初始化查询,应用条件、关联加载和排序规则
  119. $query = $this->dao->getSearch($where)->with(['category'])
  120. ->order('sort DESC,create_time DESC');
  121. // 计算满足条件的数据总数
  122. $count = $query->count();
  123. // 根据当前页码和每页数据数量进行分页查询,并获取数据列表
  124. $list = $query->page($page, $limit)->select();
  125. // 返回包含数据总数和数据列表的数组
  126. return compact('count','list');
  127. }
  128. /**
  129. * 获取推荐的话题
  130. * @return array
  131. * @author Qinii
  132. * @day 10/27/21
  133. */
  134. public function getHotList()
  135. {
  136. $list = $this->dao->getSearch([
  137. 'is_hot' => 1,
  138. 'status' => 1,
  139. 'is_del' => 0
  140. ])
  141. ->setOption('field',[])->field('category_id,topic_name,topic_id,pic,count_view,count_use')
  142. ->order('create_time DESC')->select();
  143. return compact('list');
  144. }
  145. /**
  146. * 统计话题被使用数量
  147. * @param int|null $id
  148. * @author Qinii
  149. * @day 11/3/21
  150. */
  151. public function sumCountUse(?int $id)
  152. {
  153. if (!$id) {
  154. $id = $this->dao->getSearch(['status' => 1,'is_del' =>0])->column('topic_id');
  155. } else {
  156. $id = [$id];
  157. }
  158. foreach ($id as $item) {
  159. $count = app()->make(CommunityRepository::class)
  160. ->getSearch(CommunityRepository::IS_SHOW_WHERE)->where('topic_id',$item)->count();
  161. $this->dao->update($item, ['count_use' => $count]);
  162. }
  163. }
  164. /**
  165. * 获取可选选项
  166. * 此方法用于查询数据库中符合条件的记录,并将其转换为特定格式的数据,以供前端选择组件使用。
  167. * 查询条件包括状态为激活且未被删除的记录,排序依据是排序值降序和创建时间降序。
  168. * 返回的数据格式为键值对,其中键为topic_id,值为topic_name。
  169. *
  170. * @return array 返回查询结果转换后的数组,如果无数据则返回空数组。
  171. */
  172. public function options()
  173. {
  174. // 查询数据库,获取状态为1,且is_del为0的记录,按sort和create_time降序排列
  175. // 选择字段为topic_id和topic_name,重命名为value和label
  176. $data = $this->dao->getSearch(['status' => 1,'is_del' =>0])->order('sort DESC,create_time DESC')
  177. ->field('topic_id as value,topic_name as label')->select();
  178. // 如果查询到数据,将其转换为数组形式
  179. if ($data) $res = $data->toArray();
  180. // 返回转换后的数据数组
  181. return $res;
  182. }
  183. }