UserLabelCateServices.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user;
  12. use app\dao\other\CategoryDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\FormBuilder;
  17. use think\Model;
  18. /**
  19. * Class UserLabelCateServices
  20. * @package app\services\user
  21. * @method delete($id, ?string $key = null) 删除
  22. * @method update($id, array $data, ?string $key = null) 更新数据
  23. * @method save(array $data) 保存数据
  24. * @method array|Model|null get($id, ?array $field = [], ?array $with = []) 获取一条数据
  25. * @method getAll(array $with = []) 获取全部标签分类
  26. */
  27. class UserLabelCateServices extends BaseServices
  28. {
  29. /**
  30. * 标签分类缓存
  31. * @var string
  32. */
  33. protected $cacheName = 'label_list_all';
  34. /**
  35. * UserLabelCateServices constructor.
  36. * @param CategoryDao $dao
  37. */
  38. public function __construct(CategoryDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * 获取标签分类
  44. * @param array $where
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getLabelList(array $where)
  51. {
  52. [$page, $limit] = $this->getPageValue();
  53. $list = $this->dao->getCateList($where, $page, $limit);
  54. $count = $this->dao->count($where);
  55. return compact('list', 'count');
  56. }
  57. /**
  58. * 删除分类缓存
  59. * @return bool
  60. * @throws \Psr\SimpleCache\InvalidArgumentException
  61. */
  62. public function deleteCateCache()
  63. {
  64. return CacheService::delete($this->cacheName);
  65. }
  66. /**
  67. * 获取标签全部分类
  68. * @return bool|mixed|null
  69. */
  70. public function getLabelCateAll()
  71. {
  72. return CacheService::remember($this->cacheName, function () {
  73. return $this->dao->getCateList(['type' => 0]);
  74. });
  75. }
  76. /**
  77. * 标签分类表单
  78. * @param array $cataData
  79. * @return mixed
  80. */
  81. public function labelCateForm(array $cataData = [])
  82. {
  83. $f[] = FormBuilder::input('name', '分类名称', $cataData['name'] ?? '')->required();
  84. $f[] = FormBuilder::number('sort', '排序', (int)($cataData['sort'] ?? 0));
  85. return $f;
  86. }
  87. /**
  88. * 创建表单
  89. * @return array
  90. * @throws \FormBuilder\Exception\FormBuilderException
  91. */
  92. public function createForm()
  93. {
  94. return create_form('添加标签分类', $this->labelCateForm(), $this->url('/user/user_label_cate'), 'POST');
  95. }
  96. /**
  97. * 修改分类标签表单
  98. * @param int $id
  99. * @return array
  100. * @throws \FormBuilder\Exception\FormBuilderException
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. */
  105. public function updateForm(int $id)
  106. {
  107. $labelCate = $this->dao->get($id);
  108. if (!$labelCate) {
  109. throw new AdminException(100026);
  110. }
  111. return create_form('编辑标签分类', $this->labelCateForm($labelCate->toArray()), $this->url('user/user_label_cate/' . $id), 'PUT');
  112. }
  113. /**
  114. * 用户标签列表
  115. * @param int $uid
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function getUserLabel(int $uid)
  122. {
  123. $list = $this->dao->getAll(['type' => 0], ['label']);
  124. /** @var UserLabelRelationServices $services */
  125. $services = app()->make(UserLabelRelationServices::class);
  126. $labelIds = $services->getUserLabels($uid) ?? [];
  127. foreach ($list as $key => &$item) {
  128. if (is_array($item['label'])) {
  129. if (!$item['label']) {
  130. unset($list[$key]);
  131. continue;
  132. }
  133. foreach ($item['label'] as &$value) {
  134. if (in_array($value['id'], $labelIds)) {
  135. $value['disabled'] = true;
  136. } else {
  137. $value['disabled'] = false;
  138. }
  139. }
  140. }
  141. }
  142. return array_merge($list);
  143. }
  144. }