| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace addons\OperationCenter\common\services;
- use addons\OperationCenter\common\models\Level;
- use addons\OperationCenter\common\models\UserCenter;
- /**
- * LevelService
- * @package addons\OperationCenter\common\services
- */
- class LevelService extends BaseService
- {
- /**
- * 获取分页列表
- *
- * @param array $params
- * @return array
- */
- public function getPageList(array $params = [])
- {
- return Level::getPageList(
- $this->getPage($params), $this->getLimit($params), function ($query) {
- $query->andWhere(['mall_id' => $this->mall_id]);
- },'level asc,id desc'
- );
- }
- /**
- * 获取等级列表
- *
- * @return array
- */
- public function getLevelList()
- {
- return Level::getOpenLevelList($this->mall_id);
- }
- /**
- * 获取等级数据
- *
- * @param integer|null $id
- * @return array
- */
- public function getLevelData(int $id = null)
- {
- $detail = [];
- $weights = Level::getLevelWeights($this->mall_id);
- if ($id) $detail = Level::getLevelDetail($this->mall_id, $id);
- if (!empty($detail)) {
- $detail = $detail->jsonDecode();
- $detail = $detail->toArray();
- }
- return compact('weights', 'detail');
- }
- /**
- * 编辑等级
- *
- * @param int|null $id
- * @param array $data
- * @return boolean
- */
- public function setLevelData($id, array $data)
- {
- $model = empty($id) ? new Level() : Level::getLevelDetail($this->mall_id, $id);
- $res = $model->setLevelData(array_merge($data, ['mall_id' => $this->mall_id]));
- return $res === true
- ? true
- : $this->modelError($res);
- }
- /**
- * 切换状态
- *
- * @param integer $id
- * @param integer $status
- * @return boolean
- */
- public function switchStatus(int $id, int $status)
- {
- $model = Level::getLevelDetail($this->mall_id, $id);
- if (empty($model)) return $this->error('等级不存在');
- $res = $model->setLevelData(['status' => $status]);
- return $res === true
- ? true
- : $this->modelError($res);
- }
- /**
- * 删除等级
- *
- * @param integer $id
- * @return boolean
- */
- public function delLevel(int $id)
- {
- $model = Level::getLevelDetail($this->mall_id, $id);
- if (empty($model)) return $this->error('等级不存在');
- // 被使用等级无法删除
- if (UserCenter::hasLevel($model)) return $this->error('该等级已被使用 无法删除');
- $res = $model->delLevel();
- return $res === true
- ? true
- : $this->modelError($res);
- }
- }
|