CommunityCategoryRepository.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\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. class CommunityCategoryRepository extends BaseRepository
  19. {
  20. /**
  21. * @var CommunityCategoryDao
  22. */
  23. protected $dao;
  24. /**
  25. * CommunityCategoryRepository constructor.
  26. * @param CommunityCategoryDao $dao
  27. */
  28. public function __construct(CommunityCategoryDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. public function form(?int $id)
  33. {
  34. $formData = [];
  35. if (!$id) {
  36. $form = Elm::createForm(Route::buildUrl('systemCommunityCategoryCreate')->build());
  37. } else {
  38. $formData = $this->dao->get($id)->toArray();
  39. $form = Elm::createForm(Route::buildUrl('systemCommunityCategoryUpdate', ['id' => $id])->build());
  40. }
  41. $form->setRule([
  42. Elm::input('cate_name', '分类名称')->required(),
  43. Elm::switches('is_show', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  44. Elm::number('sort', '排序')->precision(0)->max(99999),
  45. ]);
  46. return $form->setTitle(is_null($id) ? '添加分类' : '编辑分类')->formData($formData);
  47. }
  48. public function delete(int $id)
  49. {
  50. $make = app()->make(CommunityTopicRepository::class);
  51. if ( $make->getWhereCount(['category_id' => $id]) ) throw new ValidateException('该分类下存在数据');
  52. $this->dao->delete($id);
  53. }
  54. public function getList(array $where, int $page, int $limit)
  55. {
  56. $query = $this->dao->getSearch($where)->order('sort DESC,category_id DESC');
  57. $count = $query->count();
  58. $list = $query->page($page, $limit)->select();
  59. return compact('count','list');
  60. }
  61. public function options()
  62. {
  63. $data = $this->dao->getSearch(['is_show' => 1])->order('sort DESC,category_id DESC')
  64. ->field('category_id as value,cate_name as label')->select();
  65. if ($data) $res = $data->toArray();
  66. return $res;
  67. }
  68. public function getApiList()
  69. {
  70. $res = $this->dao->getSearch(['is_show' => 1])->order('sort DESC')
  71. ->setOption('filed',[])->field('category_id,cate_name')->with(['children'])->order('sort DESC,category_id DESC')->select();
  72. $list = [];
  73. if ($res) $list = $res->toArray();
  74. // $hot = app()->make(CommunityTopicRepository::class)->getHotList();
  75. // $data[] = [
  76. // 'category_id' => 0,
  77. // "cate_name" => "推荐",
  78. // "children" => $hot['list']
  79. // ];
  80. // return array_merge($data,$list);
  81. return $list;
  82. }
  83. public function checkName($name, $id)
  84. {
  85. $this->dao->fieldExists();
  86. $this->dao->getSearch(['cate_name' => $name]);
  87. if ($id) {
  88. $this->dao->getSearch(['cate_name' => $name])->where('categ');
  89. }
  90. }
  91. public function clearCahe()
  92. {
  93. // CacheService::clearByTag(0, CacheService::TAG_TOPIC);
  94. }
  95. }