6
0
Fork 0

经销商升级日志

release
李静 2022-01-17 16:10:48 +08:00
parent fb1fc3160f
commit 87530ceef1
3 changed files with 101 additions and 3 deletions

View File

@ -21,7 +21,8 @@ class Dealer extends Model
'lvl' => DealerLvl::class,
'is_sale' => 'bool',
'is_manager' => 'bool',
'pay_info'=>JsonArray::class,
'pay_info' => JsonArray::class,
'contracted_lvl_at' => 'datetime',
];
protected $fillable = [
@ -30,8 +31,21 @@ class Dealer extends Model
'is_sale',
'is_manager',
'pay_info',
'contracted_lvl_at',
];
/**
* {@inheritdoc}
*/
protected static function booted()
{
static::creating(function ($dealer) {
if ($dealer->last_upgrade_at === null) {
$dealer->last_upgrade_at = now();
}
});
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
@ -64,9 +78,9 @@ class Dealer extends Model
/**
* 属于此经销商的升级日志
*/
public function lvlUpgradeLogs()
public function upgradeLogs()
{
return $this->hasMany(DealerLvlUpgradeLog::class, 'user_id', 'user_id');
return $this->hasMany(DealerUpgradeLog::class, 'user_id', 'user_id');
}
public function getLvlTextAttribute()
@ -135,4 +149,35 @@ class Dealer extends Model
return $item->dealer;
})->all();
}
/**
* 变更等级
*
* @param \App\Enums\DealerLvl $lvl
* @param string|null $remark
* @return void
*/
public function changeLvl(DealerLvl $lvl, ?string $remark = null)
{
if ($this->lvl === $lvl) {
return;
}
$before = $this->lvl;
if ($lvl->value < DealerLvl::Contracted) {
$this->contracted_lvl_at = null;
} elseif ($lvl->value >= DealerLvl::Contracted->value && $this->contracted_lvl_at === null) {
$this->contracted_lvl_at = now();
}
$this->lvl = $lvl;
$this->save();
$this->upgradeLogs()->create([
'before_lvl' => $before,
'change_lvl' => $lvl,
'remark' => $remark,
]);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use App\Enums\DealerLvl;
use Illuminate\Database\Eloquent\Model;
class DealerUpgradeLog extends Model
{
protected $casts = [
'before_lvl' => DealerLvl::class,
'change_lvl' => DealerLvl::class,
];
protected $fillable = [
'user_id',
'before_lvl',
'change_lvl',
'remark',
];
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddContractedLvlAtToDealersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealers', function (Blueprint $table) {
$table->timestamp('contracted_lvl_at')->nullable()->comment('成为签约的时间');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dealers', function (Blueprint $table) {
$table->dropColumn(['contracted_lvl_at']);
});
}
}