| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace addons\Rebate\common\models;
- use addons\Rebate\common\services\SettingService;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\user\User;
- use Exception;
- use Yii;
- /**
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property int $upgrade_status
- * @property int $level 等级
- * @property int $before_level 升级前等级;0为无
- * @property int $status
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $upgrade_at 等级升级时间
- * @property User $user
- */
- class RebateUser extends BaseActiveRecord
- {
- public $limit = 10;
- public $page = 1;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_rebate_user}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id'], 'required'],
- [['mall_id', 'user_id', 'upgrade_status', 'level', 'before_level', 'status', 'created_at', 'updated_at', 'upgrade_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'upgrade_status' => '升级类型;1条件升级;2购物升级',
- 'level' => '等级',
- 'before_level' => '升级前等级;0为无',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'upgrade_at' => '升级时间',
- ];
- }
- public function getUser()
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- public static function setData(array $params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function sync($params)
- {
- \Yii::$app->db->close();
- try {
- $mall_id = $params['mall_id'];
- $settingService = new SettingService(['mall_id' => $mall_id]);
- $i = 1;
- $page_size = 1000;
- $rebate_user_level_arr = RebateUserLevel::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::ENABLED]]
- ],
- 'select' => 'id,level',
- 'index' => 'level'
- ]);
- $rebate_user_level_arr[0] = ['id' => 0, 'level' => 0];
- $field = [
- 'mall_id', 'user_id', 'upgrade_status', 'level', 'before_level', 'status', 'created_at', 'updated_at', 'upgrade_at'
- ];
- $time = time();
- $setting = $settingService->getSetting();
- while ($list = User::find()
- ->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('id,level,mall_id')
- ->offset(($i - 1) * $page_size)
- ->limit($page_size)->all()) {
- $user_ids = array_column($list, 'id');
- $user_arr = array_column($list, 'level', 'id');
- $rebate_user_list = RebateUser::lists([
- 'where' => [
- ['user_id' => $user_ids],
- ],
- ]);
- $rebate_user_ids = array_column($rebate_user_list, 'user_id');
- $uids = array_diff($user_ids, $rebate_user_ids);
- $insert = [];
- foreach ($uids as $uid) {
- if (!isset($user_arr[$uid])) continue;
- $level = $user_arr[$uid];
- if ($level == 0 && !empty($setting['is_apply'])) continue;
- if (!isset($rebate_user_level_arr[$level])) continue;
- $insert[] = [
- $mall_id, $uid, 4, $level, 0, StatusEnum::ENABLED, $time, $time, 0
- ];
- }
- if (!empty($insert)) {
- Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- }
- $i++;
- }
- } catch (\Exception $e) {
- var_dump('RebateUser:sync:' . $e->getMessage() . $e->getFile() . $e->getLine());
- return true;
- }
- }
- }
|