| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2025 赤店商城 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- use app\modules\admin\models\localPublicRanking\LocalPublicRankingForm;
- class LocalPublicRankingUser extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%local_public_ranking_user}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- LocalPublicRankingForm::afterPRUSave($this, $insert, $changedAttributes);
- }
- public static function initUser($store_id, $user_id, $order) {
- $has = self::findOne(['user_id' => $user_id]);
- if($has){
- return [
- 'code' => 0,
- 'data' => $has,
- ];
- }
- return self::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()));
- }
- debug_log([__LINE__, '首单加入公排成功', $store_id, $user_id, $order['id']], __CLASS__);
- 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 upStart($user_id, $star = null) {
- if($star === null){
- return self::updateAllCounters(['star' => 1], ['user_id' => $user_id]);
- }else{
- return self::updateAll(['star' => $star], ['user_id' => $user_id]);
- }
- }
-
- public static function getUpperLevelsKaryTree($current, $k) {
- $upperLevels = [];
- while ($current > 0) {
- $upper = floor(($current - 1) / $k); // 上级排号
- if($upper <= 0){
- break;
- }
- $upperLevels[] = (int)$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;
- }
- public static function getPos($sort, $PublicRankingRow) {
- if(!$sort){
- return '0-0';
- }
- $sorts = self::getUpperLevelsKaryTree($sort, $PublicRankingRow);
- $row = count($sorts) + 1;
- $total = (int)((pow($PublicRankingRow, $row) - $PublicRankingRow) / ($PublicRankingRow - 1));
- $n = $sort - $total;
- return ($row + 1) . '-' . $n;
- }
- public static function posToSort($pos, $PublicRankingRow) {
- $total = 0;
- $posArr = explode('-', $pos);
- if($posArr[0]){
- $total = (int)((pow($PublicRankingRow, $posArr[0] - 1) - $PublicRankingRow) / ($PublicRankingRow - 1));
- }
- if($posArr[1]){
- $total += $posArr[1];
- }
- return $total;
- }
- }
|