| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace common\models\user;
- use common\enums\StatusEnum;
- use common\enums\UserBehaviorLogsEnum;
- use common\events\user\UserLevelExpiredEvent;
- use common\models\base\BaseActiveRecord;
- use PHPUnit\Util\Exception;
- use common\events\census\UserUpgradeAfterEvent;
- use common\models\user\User;
- 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
- */
- class UserLevelExpired extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_level_expired}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level','user_id'], 'required'],
- [['mall_id', 'level', 'user_id','status', 'created_at', 'updated_at','expired_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'level' => '会员等级',
- 'user_id' => 'User ID',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'expired_at' => 'Expired At',
- ];
- }
- /**
- * 保存数据
- * @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();
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- } catch (Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 会员过期(过期后恢复到默认等级)
- * @param array $params
- * @return bool|UserLevel
- */
- public static function levelExpired($mall_id){
- try{
- $list = self::find()
- ->where(['status'=>StatusEnum::ENABLED,'mall_id'=>$mall_id])
- ->andWhere(['<','expired_at',time()])
- ->all();
- if(!empty($list)){
- foreach($list as $item){
- $user_extra = User::findOne(['id'=>$item->user_id,'level'=>$item->level]);
- if(!$user_extra){
- continue;
- }
- //查询修改前用户信息
- $user = User::getUser([['id' => $item->user_id,'mall_id' => $mall_id]],'*',[],1);
- $params = [];
- $params['id'] = $item->user_id;
- $params['user_id'] = $item->user_id;
- $params['mall_id'] = $mall_id;
- $params['level'] = 0;
- $res = User::change($params);
- if($res){
- \Yii::error('会员等级过期'.$item->user_id);
- $item->status = StatusEnum::DELETE;
- $item->save();
- //触发事件
- $event = new UserUpgradeAfterEvent($mall_id);
- $data = ['after_level' => $params['level'], 'before_level' => $user['level'],'mall_id'=>$mall_id,'user_id' => $params['user_id'], 'system_auto' => UserBehaviorLogsEnum::AUTO_DOWNGRADE_USER_LEVEL_EXPIRED];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- // 会员过期事件
- $event = new UserLevelExpiredEvent($mall_id);
- $data = ['mall_id'=>$mall_id, 'user_id' => $params['user_id'], 'before_level' => $user['level'], 'after_level' => $params['level']];
- \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- }
- }
- }
- return true;
- }catch(\Exception $e){
- \Yii::error('会员等级过期'.$e->getMessage());
- return false;
- }
- }
- }
|