| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2025 赤店商城 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- class PublicRankingUser extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%public_ranking_user}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
- public static function initUser($store_id, $user_id, $order) {
- $has = PublicRankingUser::findOne(['user_id' => $user_id]);
- if($has){
- return [
- 'code' => 0,
- 'data' => $has,
- ];
- }
- return PublicRankingUser::add($store_id, $user_id, $order);
- }
- public static function add($store_id, $user_id, $order) {
- try{
- $has = self::findOne(['user_id' => $user_id]);
- if($has){
- return [
- 'code' => 0,
- 'data' => $has,
- ];
- }
- $max = (int)self::find()->where(['store_id' => $store_id])->max('sort');
- $model = new self([
- 'store_id' => $store_id,
- 'user_id' => $user_id,
- 'sort' => $max + 1,
- ]);
- $model->first_order_id = (int)$order['id'];
- $model->first_order_time = (int)$order['created_at'];
- if(!$model->save()){
- throw new \Exception('数据保存错误:' . array_shift($model->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'data' => $model,
- ];
- } catch (\Exception $ex) {
- debug_log([$store_id, $user_id, $ex->getMessage()], __CLASS__);
- \Yii::error($ex);
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
-
- public static function getUpperLevelsKaryTree($current, $k) {
- $upperLevels = [];
- while ($current > 0) {
- $upper = floor(($current - 1) / $k); // 上级排号
- if($upper <= 0){
- break;
- }
- $upperLevels[] = $upper;
- $current = $upper;
- }
- return $upperLevels;
- }
- public static function getParentSorts($sort, $PublicRankingRow, $count) {
- $sorts = self::getUpperLevelsKaryTree($sort, $PublicRankingRow);
- $ret = array_slice($sorts, 0, $count);
- return $ret;
- }
- }
|