255], [['discount'], 'number', 'min' => 0, 'max' => 10], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'level' => '会员等级', 'name' => '等级名称', 'pic_url' => '图标', 'discount' => '折扣', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'expired_day' => 'Expired Day', 'is_open_expired' => 'Is Open Expired', 'member_price_is_show' => 'Member Price Is Show', 'is_specified_level' => '指定等级会员价展示[0否;1是]', 'specified_level' => '指定等级' ]; } public function getMemberBenefitList() { return $this->hasMany(MemberBenefit::class, ['level_id' => 'id']); } public function getUpgradeCondition() { return $this->hasOne(UserLevelUpgradeCondition::class, ['user_level_id'=>'id']); } public function getEqualSubsidy() { return $this->hasOne(EqualSubsidyUserLevelConfig::class, ['user_level_id'=>'id']); } /** * 保存数据 * @param array $params * @return bool|UserLevel */ public static function setData(array $params) { $t = Yii::$app->db->beginTransaction(); try { if (!empty($params['id'])) { $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) throw new Exception('服务不存在或已删除'); $where[] = ['<>', 'id', $params['id']]; } else { $model = new self(); $model->loadDefaultValues(); } $where[] = ['mall_id' => $params['mall_id']]; $where[] = ['level' => $params['level']]; $res = UserLevel::getUserLevelOne($where); if (!empty($res)) throw new Exception('会员等级出现重复'); if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage()); $params['user_level_id'] = $model->id; $res = UserLevelUpgradeCondition::setData($params); if ($res === false) throw new Exception(UserLevelUpgradeCondition::getStaticError()); if(!empty($params['benefits'])){ $params['level_id'] = $model->id; $res = MemberBenefit::setData($params); if ($res === false) throw new Exception(MemberBenefit::getStaticError()); }else{ $old_data = MemberBenefit::findAll(['level_id' => $model->id, 'status' => StatusEnum::ENABLED]); foreach($old_data as $benefit){ $benefit->status = StatusEnum::DELETE; $benefit->save(); } } $t->commit(); return $model; } catch (Exception $e) { $t->rollBack(); self::$static_error = $e->getMessage(); return false; } } /** * 删除 * @param $params * @param $status * @return bool */ public static function del($params, $status) { $t = Yii::$app->db->beginTransaction(); try { $query = self::find()->where(['and', ['id' => $params['id']], ['<>', 'status', StatusEnum::DELETE] ]); $model = $query->one(); $res = User::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]); if($res) throw new Exception('该等级关联了会员'); $model->status = $status; $res = $model->save(); if ($res === false) throw new Exception('删除失败'); $where[] = ['mall_id' => $params['mall_id']]; $where[] = ['user_level_id' => $params['id']]; $res = UserLevelUpgradeCondition::del($where, $status); if ($res === false) throw new Exception(UserLevelUpgradeCondition::getStaticError()); $t->commit(); return true; } catch (\Exception $e) { $t->rollBack(); self::$static_error = $e->getMessage(); return false; } } /** * 获取等级列表 * @return array */ public static function getLevelList($mall_id) { $levelList = []; $list = self::find()->select('level')->where(['and',['<>', 'status', StatusEnum::DELETE],['mall_id'=>$mall_id]])->column(); for ($i = 1; $i <= 50; $i++) { $levelList[] = [ 'name' => '权重' . $i, 'level' => $i, 'disabled' => in_array($i, $list), ]; } return $levelList; } /** * 获取会员等级列表 * @param array $cond * @param array $with * @param int $is_array * @param string $select * @return array|UserLevel[]|\yii\db\ActiveRecord[] */ public static function getUserLevel(array $cond, array $with = [], int $is_array = 0, string $select = '*', $orderBy = 'level asc') { $default_cond = [ ['<>', 'status', StatusEnum::DELETE] ]; $cond = array_merge($cond, $default_cond); $query = self::find()->select($select); self::paramPack(['where' => $cond, 'with' => $with], $query); return $query->asArray($is_array)->orderBy($orderBy)->all(); } /** * 获取会员等级列表 * @param array $cond * @param array $with * @param int $is_array * @param string $select * @return array|UserLevel[]|\yii\db\ActiveRecord[] */ public static function getUserLevelOne(array $cond, array $with = [], int $is_array = 0, string $select = '*') { $default_cond = [ ['<>', 'status', StatusEnum::DELETE] ]; $cond = array_merge($cond, $default_cond); $query = self::find()->select($select); self::paramPack(['where' => $cond, 'with' => $with], $query); return $query->asArray($is_array)->one(); } /** * 会员等级数组 * @param $mall_id * @param $type * @return array */ public static function getUserLevelArr($mall_id, $type = 1, $is_array = false) { $list = self::getUserLevel([['mall_id' => $mall_id]], [], $is_array, 'name,level'); if ($type == 1) { return array_column($list, 'name', 'level'); } else { return $list; } } /** * 用户列表获取 * @param $mall_id * @param string $select * @return array|UserLevel[]|\yii\db\ActiveRecord[] */ public static function getUserLevelList($mall_id, string $select = 'name,level') { $list = self::getUserLevel([['mall_id' => $mall_id]], [], true, $select); if (!empty($list)) { $list = array_column($list, null, 'level'); } return $list; } /** * 条件升级数据源使用 * @param $params * @return array */ public static function getUserListByUpgrade($params) { $rows = []; if (!empty($params['mall_id'])) { $where[] = ['mall_id' => $params['mall_id']]; $list = self::getUserLevel($where, [], 0); foreach ($list as $v) { $rows[] = ['level' => $v['level'], 'name' => $v['name']]; } } return $rows; } public static function getListIndexByLevelColumn($mall_id,$status=StatusEnum::ENABLED){ $where = ['mall_id'=>$mall_id]; if($status) $where['status'] = $status; return self::find() ->select('name') ->where($where) ->asArray() ->indexBy('level') ->orderBy('level desc') ->column(); } /** * 获取包含默认等级的会员等级列表 * * @param integer $mall_id * @return array */ public static function getList(int $mall_id) { $list = static::find()->where(['mall_id' => $mall_id]) ->andWhere(['>', 'status', StatusEnum::DELETE]) ->select('level, name') ->asArray() ->all(); array_unshift($list, ['level' => 0, 'name' => MallSetting::getDefaultLevelName($mall_id)]); return $list; } /** * 获取包含默认等级的会员等级列表 只返回启用的等级 * * @param integer $mall_id * @return array */ public static function getEnableLevelList(int $mall_id) { $list = static::find()->where(['mall_id' => $mall_id]) ->andWhere(['=', 'status', StatusEnum::ENABLED]) ->select('level, name') ->orderBy('level asc') ->asArray() ->all(); array_unshift($list, ['level' => 0, 'name' => MallSetting::getDefaultLevelName($mall_id)]); return $list; } }