| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace common\models\user;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\common\MallSetting;
- use addons\EqualSubsidy\common\models\EqualSubsidyUserLevelConfig;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "{{%user_level}}".
- *
- *
- * @property int $id
- * @property int $mall_id
- * @property int $level 会员等级
- * @property string $name 等级名称
- * @property string $pic_url 图标
- * @property string $discount 折扣
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $is_specified_level 指定等级会员价展示[0否;1是]
- * @property string $specified_level 指定等级
- */
- class UserLevel extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level'], 'required'],
- [['mall_id', 'level', 'status', 'created_at', 'updated_at','is_open_expired','expired_day','member_price_is_show', 'is_specified_level'], 'integer'],
- [['name', 'pic_url', 'specified_level'], 'string', 'max' => 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;
- }
- }
|