123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\services\user\label;
- use app\dao\other\CategoryDao;
- use app\services\BaseServices;
- use crmeb\services\CacheService;
- use crmeb\services\FormBuilder;
- use crmeb\traits\ServicesTrait;
- use think\exception\ValidateException;
- class UserLabelCateServices extends BaseServices
- {
-
- const GROUP = 0;
- use ServicesTrait;
-
- protected $cacheName = 'label_list_all';
-
- public function __construct(CategoryDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function getLabelList(array $where)
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getCateList($where, $page, $limit);
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
-
- public function getLabelTree(array $where, array $field = ['*'], array $with = [])
- {
- return $this->dao->getCateList($where, 0, 0, $field, $with);
- }
-
- public function deleteCateCache(int $type = 0, int $relation_id = 0)
- {
- $key = $this->cacheName . '_' . $type . '_' . $relation_id;
- return CacheService::delete($key);
- }
-
- public function getLabelCateAll(int $type = 0, int $relation_id = 0)
- {
- $key = $this->cacheName . '_' . $type . '_' . $relation_id;
- return CacheService::get($key, function () use ($type, $relation_id) {
- return $this->dao->getCateList(['type' => $type, 'owner_id' => 0, 'relation_id' => $relation_id, 'group' => 0]);
- });
- }
-
- public function labelCateForm(array $cataData = [])
- {
- $f[] = FormBuilder::input('name', '分类名称', $cataData['name'] ?? '')->maxlength(20)->required();
- $f[] = FormBuilder::number('sort', '排序', (int)($cataData['sort'] ?? 0))->min(0);
- return $f;
- }
-
- public function createForm()
- {
- return create_form('添加标签分类', $this->labelCateForm(), $this->url('/user/user_label_cate'), 'POST');
- }
-
- public function updateForm(int $id)
- {
- $labelCate = $this->dao->get($id);
- if (!$labelCate) {
- throw new ValidateException('分类标签没有查到');
- }
- return create_form('编辑标签分类', $this->labelCateForm($labelCate->toArray()), $this->url('user/user_label_cate/' . $id), 'PUT');
- }
-
- public function getUserLabel(int $uid = 0, int $type = 0, int $relation_id = 0)
- {
- $list = $this->dao->getAll(['group' => 0, 'type' => $type, 'relation_id' => $relation_id], ['label']);
- $labelIds = [];
- if ($uid) {
-
- $services = app()->make(UserLabelRelationServices::class);
- $labelIds = $services->getUserLabels($uid, $type, $relation_id);
- }
- foreach ($list as $key => &$item) {
- if (is_array($item['label'])) {
- if (!$item['label']) {
- unset($list[$key]);
- continue;
- }
- foreach ($item['label'] as &$value) {
- if (in_array($value['id'], $labelIds)) {
- $value['disabled'] = true;
- } else {
- $value['disabled'] = false;
- }
- }
- }
- }
- return array_merge($list);
- }
- }
|