// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\services\system\config; use app\services\user\UserServices; use qiniu\basic\BaseServices; use app\model\system\config\SystemUserLevel; use qiniu\exceptions\AdminException; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; /** * 系统设置用户等级 * Class SystemUserLevelServices * @package app\services\user\level * @mixin SystemUserLevel */ class SystemUserLevelServices extends BaseServices { /** * SystemUserLevelServices constructor. * @param SystemUserLevel $model */ public function __construct(SystemUserLevel $model) { $this->model = $model; } /** * 单个等级 * @param int|array $id * @param string $field * @return array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getLevel($id, string $field = '*') { if (is_array($id)) return $this->getOne($id, $field); return $this->getOne(['id' => $id], $field); } /** * 获取所有等级列表 * @param array $where * @param string $field * @return array * @throws DbException */ public function getLevelList(array $where, string $field = '*') { $where_data = []; if (isset($where['is_show']) && $where['is_show'] !== '') $where_data[] = ['is_show', '=', $where['is_show']]; if (isset($where['title']) && $where['title']) $where_data[] = ['name', 'LIKE', "%$where[title]%"]; [$page, $limit] = $this->getPageValue(); $list = $this->getList($where_data, $field ?? '*', $page, $limit); foreach ($list as &$item) { $item['image'] = set_file_url($item['image']); $item['icon'] = set_file_url($item['icon']); } $count = $this->getCount($where_data); return compact('list', 'count'); } /** * 会员等级添加或者修改 * @param array $data * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function create(array $data) { $gradeLevel = $this->getLevel(['grade' => $data['grade']]); $nameLevel = $this->getLevel(['name' => $data['name']]); if ($gradeLevel || $nameLevel) { throw new AdminException('已检测到您设置过的会员等级,此等级不可重复'); } /** @var SystemUserLevelTaskServices $taskService */ $taskService = app()->make(SystemUserLevelTaskServices::class); $data['task'] = $taskService->checkTask($data['task'] ?? []); return parent::create($data); } public function update($id, array $data, ?string $key = null) { $gradeLevel = $this->getLevel(['grade' => $data['grade']]); $nameLevel = $this->getLevel(['name' => $data['name']]); if (($gradeLevel && $gradeLevel['id'] != $id) || ($nameLevel && $nameLevel['id'] != $id)) { throw new AdminException('已检测到您设置过的会员等级,此等级不可重复'); } /** @var SystemUserLevelTaskServices $taskService */ $taskService = app()->make(SystemUserLevelTaskServices::class); $data['task'] = $taskService->checkTask($data['task'] ?? []); return parent::update($id, $data); } public function delete($id, ?string $key = null) { $level = $this->getLevel($id); if (!$level) { throw new AdminException('数据不存在'); } /** @var UserServices $userServices */ $userServices = app()->make(UserServices::class); if ($userServices->be(['level' => $level['id']])) { throw new AdminException('存在用户已是该等级,无法删除'); } return parent::delete($id, $key); // TODO: Change the autogenerated stub } }