6
0
Fork 0

优化用户信息表结构

release
李静 2021-12-23 20:02:02 +08:00
parent 527b4537ee
commit e8cc9ccc82
5 changed files with 140 additions and 9 deletions

View File

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

View File

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

View File

@ -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();

View File

@ -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('头像');

View File

@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddDistributionColumnsToUserInfosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_infos', function (Blueprint $table) {
$table->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',
]);
});
}
}