session->get('o2o'); // if(empty($store)){ $list = StoreBossLevel::find()->select('level,name')->where([ 'mall_id' =>$store ['mall_id'], 'store_id' => $store['id'], 'is_delete' => 0 ])->orderBy(['level' => SORT_ASC])->all(); $level_list = array_column($list,'level'); $newList = []; $matchList = []; for ($i = 1; $i <= 10; $i++) { $newList[] = [ 'name' => '权重' . $i, 'level' => $i, 'disabled' => in_array($i, $level_list), ]; if(isset($list[$i-1])){ $matchList[] = [ 'name' => $list[$i-1]['name'], 'level' => "$i", 'disabled' => in_array($i, $level_list), ]; } } return ['newList'=>$newList,'matchList'=>$matchList]; } /** * 详情 * @param $id * @return BossLevel|null */ public function getDetail($id) { if (!$id) { return null; } $store = Yii::$app->session->get('o2o'); /* @var BossLevel $bossLevel */ $bossLevel = StoreBossLevel::findOne([ 'id' => $id, 'mall_id' => $store['mall_id'], 'is_delete' => 0 ]); return $bossLevel; } /** * 删除 * @param $id * @return bool * @throws \Exception */ public function del($id) { $bossLevel = $this->getDetail($id); if (!$bossLevel) { throw new \Exception('所选择的股东等级不存在或已删除,请刷新后重试'); } $store = Yii::$app->session->get('o2o'); $bossExists = StoreBoss::find()->where([ 'mall_id' => $this->$store['mall_id'], 'is_delete' => 0,'store_id' =>$store['id'], 'level' => $bossLevel->level ])->asArray()->one(); if ($bossExists) { throw new \Exception('该股东等级下还有股东存在,暂时不能删除'); } $bossLevel->is_delete = 1; if (!$bossLevel->save()) { throw new \Exception($this->responseErrorMsg($bossLevel)); } return true; } /** * @param $level * @return bool * @throws \Exception */ public function changeLevel($level, $upgrade_status) { $boss = $this->getBoss(); if (!$boss) { throw new \Exception('股东不存在'); } $boss->level = $level; if ($upgrade_status) { $boss->upgrade_status = $upgrade_status; } $boss->upgrade_level_at = time(); if (!$boss->save()) { \Yii::error('升级股东设置等级出错'); throw new \Exception($this->responseErrorMsg($boss)); } return true; } /** * 获取股东 * @return Boss|null * @throws \Exception * */ public function getBoss() { if ($this->boss) { return $this->boss; } $store = Yii::$app->session->get('o2o'); $boss = StoreBoss::findOne([ 'user_id' => $this->userId,'store_id' => $store['id'], 'status' => 1, 'mall_id' => $store['mall_id'] ]); if (!$boss) { throw new \Exception('不存在该门店股东'); } $this->boss = $boss; return $boss; } protected $BossLevelList; /** * 通过股东等级来获取经销等级 * @param $level * @param $storeId * @return BossLevel|null * */ public function getBossLevelByLevel($level) { if (!$level) { return null; } if (isset($this->BossLevelList[$level]) && $this->BossLevelList[$level]) { return $this->BossLevelList[$level]; } /* @var BossLevel $bossLevel */ $store = Yii::$app->session->get('o2o'); $bossLevel = StoreBossLevel::find()->where([ 'level' => $level, 'store_id' => $store['id'], 'mall_id' => $store['mall_id'], 'is_delete' => 0 ])->one(); $this->BossLevelList[$level] = $bossLevel; return $bossLevel; } public function getList() { $levelList = []; $store = Yii::$app->session->get('o2o'); $levelList = StoreBossLevel::find()->where([ 'mall_id' => $store['mall_id'],'store_id' => $store['id'], 'is_delete' => 0, 'is_enable' => 1, ])->select(['id', 'level', 'name'])->orderBy(['level' => SORT_ASC])->all(); array_unshift($levelList, [ 'id' => 0, 'level' => 0, 'name' => '默认等级' ]); return $levelList; } public static function getEnableLevelList() { $store = Yii::$app->session->get('o2o'); // $storeId = \Yii::$app->admin->identity->store_id; $levelList = StoreBossLevel::find()->where([ 'mall_id' => $store['mall_id'],'store_id' => $store['id'], 'is_delete' => 0, 'is_enable' => 1, ])->select(['id', 'level', 'name'])->orderBy(['level' => SORT_ASC])->all(); array_unshift($levelList, [ 'id' => 0, 'level' => 0, 'name' => '默认等级' ]); return $levelList; } public function getSetting() { return $this->hasOne(AddonsBossLevelSetting::class, ['level' => 'level']); } }