CommunityCategoryRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\CommunityCategoryDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\controller\api\community\CommunityTopic;
  15. use FormBuilder\Factory\Elm;
  16. use think\exception\ValidateException;
  17. use think\facade\Route;
  18. /**
  19. * 社区分类
  20. */
  21. class CommunityCategoryRepository extends BaseRepository
  22. {
  23. /**
  24. * @var CommunityCategoryDao
  25. */
  26. protected $dao;
  27. /**
  28. * CommunityCategoryRepository constructor.
  29. * @param CommunityCategoryDao $dao
  30. */
  31. public function __construct(CommunityCategoryDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 创建/编辑表单
  37. * @param int|null $id
  38. * @return \FormBuilder\Form
  39. * @author Qinii
  40. */
  41. public function form(?int $id)
  42. {
  43. $formData = [];
  44. if (!$id) {
  45. $form = Elm::createForm(Route::buildUrl('systemCommunityCategoryCreate')->build());
  46. } else {
  47. $formData = $this->dao->get($id)->toArray();
  48. $form = Elm::createForm(Route::buildUrl('systemCommunityCategoryUpdate', ['id' => $id])->build());
  49. }
  50. $form->setRule([
  51. Elm::input('cate_name', '分类名称:')->placeholder('请输入分类名称')->required(),
  52. Elm::switches('is_show', '是否显示:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  53. Elm::number('sort', '排序:')->precision(0)->max(99999),
  54. ]);
  55. return $form->setTitle(is_null($id) ? '添加分类' : '编辑分类')->formData($formData);
  56. }
  57. /**
  58. * 删除特定ID的分类。
  59. *
  60. * 此方法用于删除一个分类,但在删除之前,它会检查该分类下是否还有未被删除的数据。
  61. * 如果存在未删除的数据,则抛出一个验证异常,防止数据丢失或错误的删除操作。
  62. * 这种做法保证了数据的完整性,并且提供了更安全的分类管理机制。
  63. *
  64. * @param int $id 分类的唯一标识ID。
  65. * @throws ValidateException 如果该分类下存在未删除的数据,则抛出此异常。
  66. */
  67. public function delete(int $id)
  68. {
  69. // 通过依赖注入的方式获取社区主题仓库实例
  70. $make = app()->make(CommunityTopicRepository::class);
  71. // 检查该分类下是否存在未删除的数据
  72. if ($make->getWhereCount(['category_id' => $id, 'is_del' => 0])) {
  73. throw new ValidateException('该分类下存在数据');
  74. }
  75. // 如果没有未删除的数据,则执行删除操作
  76. $this->dao->delete($id);
  77. }
  78. /**
  79. * 根据条件获取列表数据
  80. *
  81. * 本函数用于根据给定的条件数组 $where,从数据库中检索满足条件的数据列表。
  82. * 它支持分页查询,每页的数据数量由 $limit 参数指定,查询结果将返回当前页的数据列表以及总数据量。
  83. *
  84. * @param array $where 查询条件数组,用于指定数据库查询的条件。
  85. * @param int $page 当前页码,用于指定要返回的数据页。
  86. * @param int $limit 每页的数据数量,用于指定每页返回的数据条数。
  87. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 表示总数据量,'list' 表示当前页的数据列表。
  88. */
  89. public function getList(array $where, int $page, int $limit)
  90. {
  91. // 根据条件构造查询语句,并按排序字段排序
  92. $query = $this->dao->getSearch($where)->order('sort DESC,category_id DESC');
  93. // 计算满足条件的总数据量
  94. $count = $query->count();
  95. // 执行分页查询,并返回当前页的数据列表
  96. $list = $query->page($page, $limit)->select();
  97. // 将总数据量和当前页的数据列表一起返回
  98. return compact('count','list');
  99. }
  100. /**
  101. * 下拉筛选
  102. * @return array
  103. * @author Qinii
  104. */
  105. public function options()
  106. {
  107. $data = $this->dao->getSearch(['is_show' => 1])->order('sort DESC,category_id DESC')
  108. ->field('category_id as value,cate_name as label')->select();
  109. if ($data) $res = $data->toArray();
  110. return $res;
  111. }
  112. /**
  113. * 所有列表
  114. * @return array
  115. * @author Qinii
  116. */
  117. public function getApiList()
  118. {
  119. $res = $this->dao->getSearch(['is_show' => 1])->order('sort DESC')
  120. ->setOption('filed',[])->field('category_id,cate_name')->with(['children'])->order('sort DESC,category_id DESC')->select();
  121. $list = [];
  122. if ($res) $list = $res->toArray();
  123. // $hot = app()->make(CommunityTopicRepository::class)->getHotList();
  124. // $data[] = [
  125. // 'category_id' => 0,
  126. // "cate_name" => "推荐",
  127. // "children" => $hot['list']
  128. // ];
  129. // return array_merge($data,$list);
  130. return $list;
  131. }
  132. /**
  133. * 清除移动的缓存 - 已弃用
  134. * @return void
  135. * @author Qinii
  136. */
  137. public function clearCahe()
  138. {
  139. // CacheService::clearByTag(0, CacheService::TAG_TOPIC);
  140. }
  141. }