CommunityTopicRepository.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\CommunityTopicDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Factory\Elm;
  15. use think\exception\ValidateException;
  16. use think\facade\Route;
  17. class CommunityTopicRepository extends BaseRepository
  18. {
  19. /**
  20. * @var CommunityTopicDao
  21. */
  22. protected $dao;
  23. /**
  24. * CommunityTopicRepository constructor.
  25. * @param CommunityTopicDao $dao
  26. */
  27. public function __construct(CommunityTopicDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. public function form(?int $id)
  32. {
  33. $formData = [];
  34. if (!$id) {
  35. $form = Elm::createForm(Route::buildUrl('systemCommunityTopicCreate')->build());
  36. } else {
  37. $formData = $this->dao->get($id)->toArray();
  38. $form = Elm::createForm(Route::buildUrl('systemCommunityTopicUpdate', ['id' => $id])->build());
  39. }
  40. $form->setRule([
  41. Elm::select('category_id', '社区分类')->options(function () {
  42. return app()->make(CommunityCategoryRepository::class)->options();
  43. })->requiredNum(),
  44. Elm::frameImage('pic', '图标', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=pic&type=1')
  45. ->modal(['modal' => false])
  46. ->width('896px')
  47. ->height('480px'),
  48. Elm::input('topic_name', '社区话题')->required(),
  49. Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  50. Elm::switches('is_hot', '是否推荐', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  51. Elm::number('sort', '排序')->precision(0)->max(99999),
  52. ]);
  53. return $form->setTitle(is_null($id) ? '添加话题' : '编辑话题')->formData($formData);
  54. }
  55. public function delete(int $id)
  56. {
  57. $make = app()->make(CommunityRepository::class);
  58. if ( $make->getWhereCount(CommunityRepository::IS_SHOW_WHERE) ) throw new ValidateException('该话题下存在数据');
  59. $this->dao->delete($id);
  60. }
  61. public function getList(array $where, int $page, int $limit)
  62. {
  63. $where['is_del'] = 0;
  64. $query = $this->dao->getSearch($where)->with(['category'])
  65. ->order('sort DESC,create_time DESC');
  66. $count = $query->count();
  67. $list = $query->page($page, $limit)->select();
  68. return compact('count','list');
  69. }
  70. /**
  71. * TODO 获取推荐的话题
  72. * @return array
  73. * @author Qinii
  74. * @day 10/27/21
  75. */
  76. public function getHotList()
  77. {
  78. $list = $this->dao->getSearch([
  79. 'is_hot' => 1,
  80. 'status' => 1,
  81. 'is_del' => 0
  82. ])
  83. ->setOption('field',[])->field('category_id,topic_name,topic_id,pic,count_view,count_use')
  84. ->order('create_time DESC')->select();
  85. return compact('list');
  86. }
  87. /**
  88. * TODO
  89. * @param int|null $id
  90. * @author Qinii
  91. * @day 11/3/21
  92. */
  93. public function sumCountUse(?int $id)
  94. {
  95. if (!$id) {
  96. $id = $this->dao->getSearch(['status' => 1,'is_del' =>0])->column('topic_id');
  97. } else {
  98. $id = [$id];
  99. }
  100. foreach ($id as $item) {
  101. $count = app()->make(CommunityRepository::class)
  102. ->getSearch(CommunityRepository::IS_SHOW_WHERE)->where('topic_id',$item)->count();
  103. $this->dao->update($item, ['count_use' => $count]);
  104. }
  105. }
  106. public function options()
  107. {
  108. $data = $this->dao->getSearch(['status' => 1,'is_del' =>0])->order('sort DESC,create_time DESC')
  109. ->field('topic_id as value,topic_name as label')->select();
  110. if ($data) $res = $data->toArray();
  111. return $res;
  112. }
  113. }