From e8cc9ccc8278f5db46ec82fa4c636c6f3647d9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Thu, 23 Dec 2021 20:02:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E8=A1=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 14 +++- app/Models/UserInfo.php | 81 ++++++++++++++++++- app/Providers/AppServiceProvider.php | 3 + ...1_11_19_142529_create_user_infos_table.php | 3 +- ...stribution_columns_to_user_infos_table.php | 48 +++++++++++ 5 files changed, 140 insertions(+), 9 deletions(-) create mode 100644 database/migrations/2021_12_23_164706_add_distribution_columns_to_user_infos_table.php diff --git a/app/Models/User.php b/app/Models/User.php index bdba36b7..830627f3 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -257,9 +257,17 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac { $user = static::query()->create($attributes); - $user->userInfo()->create([ - 'inviter_id' => $inviter?->id, - ]); + $user->userInfo()->create( + $inviter ? [ + 'inviter_id' => $inviter->id, + 'depth' => $inviter->userInfo->depth + 1, + 'path' => $inviter->userInfo->path.$inviter->id.'/', + ] : [ + 'inviter_id' => null, + 'depth' => 1, + 'path' => '/', + ]) + ; return $user; } diff --git a/app/Models/UserInfo.php b/app/Models/UserInfo.php index 40a34b3a..406e73f3 100644 --- a/app/Models/UserInfo.php +++ b/app/Models/UserInfo.php @@ -3,34 +3,58 @@ namespace App\Models; use App\Helpers\Str; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class UserInfo extends Model { - use HasFactory; - public const GENDER_UNKNOWN = 'unknown'; // 未知 public const GENDER_MALE = 'male'; // 男性 public const GENDER_FEMALE = 'female'; // 女性 + public const AGENT_LEVEL_CIVILIAN = 0; + public const AGENT_LEVEL_VIP = 1; + public const AGENT_LEVEL_COMMUNITY = 2; + public const AGENT_LEVEL_DISTRICT = 3; + public const AGENT_LEVEL_CITY = 4; + public const AGENT_LEVEL_PROVINCE = 5; + public const AGENT_LEVEL_BRANCH = 6; + public const AGENT_LEVEL_DIRECTOR = 7; + + /** + * @var string + */ + protected $primaryKey = 'user_id'; + + /** + * @var bool + */ + public $incrementing = false; + /** * @var array */ protected $attributes = [ 'gender' => self::GENDER_UNKNOWN, + 'bonusable' => true, ]; /** * @var array */ protected $fillable = [ - 'user_id', 'inviter_id', 'nickname', 'avatar', 'gender', 'birthday', + 'bonusable', + 'depth', + 'path', + 'agent_level', + 'quota_v1', + 'quota_v2', + 'growth_value', + 'group_sales_value', ]; /** @@ -38,6 +62,21 @@ class UserInfo extends Model */ protected $casts = [ 'birthday' => 'date', + 'bonusable' => 'bool', + ]; + + /** + * @var array + */ + public static $agentLevelMap = [ + self::AGENT_LEVEL_CIVILIAN => 'civilian', + self::AGENT_LEVEL_VIP => 'vip', + self::AGENT_LEVEL_COMMUNITY => 'community', + self::AGENT_LEVEL_DISTRICT => 'district', + self::AGENT_LEVEL_CITY => 'city', + self::AGENT_LEVEL_PROVINCE => 'province', + self::AGENT_LEVEL_BRANCH => 'branch', + self::AGENT_LEVEL_DIRECTOR => 'director', ]; /** @@ -56,4 +95,38 @@ class UserInfo extends Model } }); } + + /** + * 获取此用户的邀请人信息 + * + * @return void + */ + public function inviter() + { + return $this->belongsTo(UserInfo::class, 'inviter_id'); + } + + /** + * 获取此用户的所有父级ID + * + * @return array + */ + public function getParentIdsAttribute(): array + { + if ($path = trim($this->path, '/')) { + return explode($path, '/'); + } + + return []; + } + + /** + * 获取代理角色 + * + * @return string + */ + public function getAgentRoleAttribute(): string + { + return static::$agentLevelMap[$this->agent_level] ?? 'unknown'; + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 97ea6236..087e3800 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -33,6 +33,9 @@ class AppServiceProvider extends ServiceProvider { Relation::enforceMorphMap([ 'user' => \App\Models\User::class, + 'order' => \App\Models\Order::class, + 'order_refund_log' => \App\Models\OrderRefundLog::class, + 'after_sale' => \App\Models\AfterSale::class, ]); JsonResource::withoutWrapping(); diff --git a/database/migrations/2021_11_19_142529_create_user_infos_table.php b/database/migrations/2021_11_19_142529_create_user_infos_table.php index 6ec275dc..adf885a7 100644 --- a/database/migrations/2021_11_19_142529_create_user_infos_table.php +++ b/database/migrations/2021_11_19_142529_create_user_infos_table.php @@ -14,8 +14,7 @@ class CreateUserInfosTable extends Migration public function up() { Schema::create('user_infos', function (Blueprint $table) { - $table->id(); - $table->unsignedBigInteger('user_id')->unique()->comment('用户ID'); + $table->unsignedBigInteger('user_id')->primary()->comment('用户ID'); $table->unsignedBigInteger('inviter_id')->index()->nullable()->comment('邀请人ID'); $table->string('nickname', 100)->nullable()->comment('昵称'); $table->string('avatar')->nullable()->comment('头像'); diff --git a/database/migrations/2021_12_23_164706_add_distribution_columns_to_user_infos_table.php b/database/migrations/2021_12_23_164706_add_distribution_columns_to_user_infos_table.php new file mode 100644 index 00000000..97b5159a --- /dev/null +++ b/database/migrations/2021_12_23_164706_add_distribution_columns_to_user_infos_table.php @@ -0,0 +1,48 @@ +boolean('bonusable')->default(true)->comment('是否可享受奖励'); + $table->unsignedInteger('depth')->comment('分销深度'); + $table->text('path')->nullable()->comment('分销路径'); + $table->tinyInteger('agent_level')->default(0)->comment('代理等级'); + $table->unsignedDecimal('quota_v2', 18, 3)->default(0)->comment('新配额'); + $table->unsignedDecimal('quota_v1', 18, 3)->default(0)->comment('旧配额'); + $table->unsignedDecimal('growth_value', 18, 2)->default(0)->comment('成长值'); + $table->unsignedDecimal('group_sales_value', 18, 2)->default(0)->comment('团队销售值'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('user_infos', function (Blueprint $table) { + $table->dropColumn([ + 'bonusable', + 'depth', + 'path', + 'agent_level', + 'quota_v2', + 'quota_v1', + 'growth_value', + 'group_sales_value', + ]); + }); + } +}