RebateUser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\Rebate\common\models;
  3. use addons\Rebate\common\services\SettingService;
  4. use common\enums\AddonsEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\user\User;
  8. use Exception;
  9. use Yii;
  10. /**
  11. * @property int $id
  12. * @property int $mall_id
  13. * @property int $user_id
  14. * @property int $upgrade_status
  15. * @property int $level 等级
  16. * @property int $before_level 升级前等级;0为无
  17. * @property int $status
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 修改时间
  20. * @property int $upgrade_at 等级升级时间
  21. * @property User $user
  22. */
  23. class RebateUser extends BaseActiveRecord
  24. {
  25. public $limit = 10;
  26. public $page = 1;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_rebate_user}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'user_id'], 'required'],
  41. [['mall_id', 'user_id', 'upgrade_status', 'level', 'before_level', 'status', 'created_at', 'updated_at', 'upgrade_at'], 'integer'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => 'Mall ID',
  52. 'user_id' => 'User ID',
  53. 'upgrade_status' => '升级类型;1条件升级;2购物升级',
  54. 'level' => '等级',
  55. 'before_level' => '升级前等级;0为无',
  56. 'status' => '状态[-1:删除;0:禁用;1启用]',
  57. 'created_at' => '创建时间',
  58. 'updated_at' => '修改时间',
  59. 'upgrade_at' => '升级时间',
  60. ];
  61. }
  62. public function getUser()
  63. {
  64. return $this->hasOne(User::class, ['id' => 'user_id']);
  65. }
  66. public static function setData(array $params)
  67. {
  68. try {
  69. if (!empty($params['id'])) {
  70. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  71. if (empty($model)) throw new \Exception('服务不存在或已删除');
  72. } else {
  73. $model = new self();
  74. $model->loadDefaultValues();
  75. }
  76. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  77. if (!$model->save()) throw new Exception($model->getErrorMessage());
  78. return $model;
  79. } catch (\Exception $e) {
  80. self::$static_error = $e->getMessage();
  81. return false;
  82. }
  83. }
  84. public static function sync($params)
  85. {
  86. \Yii::$app->db->close();
  87. try {
  88. $mall_id = $params['mall_id'];
  89. $settingService = new SettingService(['mall_id' => $mall_id]);
  90. $i = 1;
  91. $page_size = 1000;
  92. $rebate_user_level_arr = RebateUserLevel::lists([
  93. 'where' => [
  94. ['mall_id' => $mall_id],
  95. ['status' => [StatusEnum::ENABLED]]
  96. ],
  97. 'select' => 'id,level',
  98. 'index' => 'level'
  99. ]);
  100. $rebate_user_level_arr[0] = ['id' => 0, 'level' => 0];
  101. $field = [
  102. 'mall_id', 'user_id', 'upgrade_status', 'level', 'before_level', 'status', 'created_at', 'updated_at', 'upgrade_at'
  103. ];
  104. $time = time();
  105. $setting = $settingService->getSetting();
  106. while ($list = User::find()
  107. ->where(['mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  108. ->select('id,level,mall_id')
  109. ->offset(($i - 1) * $page_size)
  110. ->limit($page_size)->all()) {
  111. $user_ids = array_column($list, 'id');
  112. $user_arr = array_column($list, 'level', 'id');
  113. $rebate_user_list = RebateUser::lists([
  114. 'where' => [
  115. ['user_id' => $user_ids],
  116. ],
  117. ]);
  118. $rebate_user_ids = array_column($rebate_user_list, 'user_id');
  119. $uids = array_diff($user_ids, $rebate_user_ids);
  120. $insert = [];
  121. foreach ($uids as $uid) {
  122. if (!isset($user_arr[$uid])) continue;
  123. $level = $user_arr[$uid];
  124. if ($level == 0 && !empty($setting['is_apply'])) continue;
  125. if (!isset($rebate_user_level_arr[$level])) continue;
  126. $insert[] = [
  127. $mall_id, $uid, 4, $level, 0, StatusEnum::ENABLED, $time, $time, 0
  128. ];
  129. }
  130. if (!empty($insert)) {
  131. Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  132. }
  133. $i++;
  134. }
  135. } catch (\Exception $e) {
  136. var_dump('RebateUser:sync:' . $e->getMessage() . $e->getFile() . $e->getLine());
  137. return true;
  138. }
  139. }
  140. }