UserLabelCateServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\label;
  12. use app\dao\other\CategoryDao;
  13. use app\services\BaseServices;
  14. use crmeb\services\CacheService;
  15. use crmeb\services\FormBuilder;
  16. use crmeb\traits\ServicesTrait;
  17. use think\exception\ValidateException;
  18. /**
  19. * 用户标签分类
  20. * Class UserLabelCateServices
  21. * @package app\services\user\label
  22. * @mixin CategoryDao
  23. */
  24. class UserLabelCateServices extends BaseServices
  25. {
  26. /**
  27. * 在分类库中0
  28. */
  29. const GROUP = 0;
  30. use ServicesTrait;
  31. /**
  32. * 标签分类缓存
  33. * @var string
  34. */
  35. protected $cacheName = 'label_list_all';
  36. /**
  37. * UserLabelCateServices constructor.
  38. * @param CategoryDao $dao
  39. */
  40. public function __construct(CategoryDao $dao)
  41. {
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * 获取标签分类
  46. * @param array $where
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getLabelList(array $where)
  53. {
  54. [$page, $limit] = $this->getPageValue();
  55. $list = $this->dao->getCateList($where, $page, $limit);
  56. $count = $this->dao->count($where);
  57. return compact('list', 'count');
  58. }
  59. /**
  60. * 获取tree数据
  61. * @param array $where
  62. * @param array|string[] $field
  63. * @param array $with
  64. * @return array
  65. */
  66. public function getLabelTree(array $where, array $field = ['*'], array $with = [])
  67. {
  68. return $this->dao->getCateList($where, 0, 0, $field, $with);
  69. }
  70. /**
  71. * 删除分类缓存
  72. * @param int $type
  73. * @param int $relation_id
  74. * @return bool
  75. * @throws \Psr\SimpleCache\InvalidArgumentException
  76. */
  77. public function deleteCateCache(int $type = 0, int $relation_id = 0)
  78. {
  79. $key = $this->cacheName . '_' . $type . '_' . $relation_id;
  80. return CacheService::delete($key);
  81. }
  82. /**
  83. * 获取标签全部分类
  84. * @param int $type
  85. * @param int $relation_id
  86. * @return bool|mixed|null
  87. */
  88. public function getLabelCateAll(int $type = 0, int $relation_id = 0)
  89. {
  90. $key = $this->cacheName . '_' . $type . '_' . $relation_id;
  91. return CacheService::get($key, function () use ($type, $relation_id) {
  92. return $this->dao->getCateList(['type' => $type, 'owner_id' => 0, 'relation_id' => $relation_id, 'group' => 0]);
  93. });
  94. }
  95. /**
  96. * 标签分类表单
  97. * @param array $cataData
  98. * @return mixed
  99. */
  100. public function labelCateForm(array $cataData = [])
  101. {
  102. $f[] = FormBuilder::input('name', '分类名称', $cataData['name'] ?? '')->maxlength(20)->required();
  103. $f[] = FormBuilder::number('sort', '排序', (int)($cataData['sort'] ?? 0))->min(0);
  104. return $f;
  105. }
  106. /**
  107. * 创建表单
  108. * @return array
  109. * @throws \FormBuilder\Exception\FormBuilderException
  110. */
  111. public function createForm()
  112. {
  113. return create_form('添加标签分类', $this->labelCateForm(), $this->url('/user/user_label_cate'), 'POST');
  114. }
  115. /**
  116. * 修改分类标签表单
  117. * @param int $id
  118. * @return array
  119. * @throws \FormBuilder\Exception\FormBuilderException
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function updateForm(int $id)
  125. {
  126. $labelCate = $this->dao->get($id);
  127. if (!$labelCate) {
  128. throw new ValidateException('分类标签没有查到');
  129. }
  130. return create_form('编辑标签分类', $this->labelCateForm($labelCate->toArray()), $this->url('user/user_label_cate/' . $id), 'PUT');
  131. }
  132. /**
  133. * 用户标签列表
  134. * @param int $uid
  135. * @param int $type
  136. * @param int $relation_id
  137. * @return array
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function getUserLabel(int $uid = 0, int $type = 0, int $relation_id = 0)
  143. {
  144. $list = $this->dao->getAll(['group' => 0, 'type' => $type, 'relation_id' => $relation_id], ['label']);
  145. $labelIds = [];
  146. if ($uid) {
  147. /** @var UserLabelRelationServices $services */
  148. $services = app()->make(UserLabelRelationServices::class);
  149. $labelIds = $services->getUserLabels($uid, $type, $relation_id);
  150. }
  151. foreach ($list as $key => &$item) {
  152. if (is_array($item['label'])) {
  153. if (!$item['label']) {
  154. unset($list[$key]);
  155. continue;
  156. }
  157. foreach ($item['label'] as &$value) {
  158. if (in_array($value['id'], $labelIds)) {
  159. $value['disabled'] = true;
  160. } else {
  161. $value['disabled'] = false;
  162. }
  163. }
  164. }
  165. }
  166. return array_merge($list);
  167. }
  168. }