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; } }