变更经销商等级
parent
ec9cbcfe5a
commit
1ba8ad3847
|
|
@ -61,8 +61,102 @@ class Dealer extends Model
|
|||
return $this->hasMany(DealerManageSubsidyLog::class, 'user_id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 属于此经销商的升级日志
|
||||
*/
|
||||
public function lvlUpgradeLogs()
|
||||
{
|
||||
return $this->hasMany(DealerLvlUpgradeLog::class, 'user_id', 'user_id');
|
||||
}
|
||||
|
||||
public function getLvlTextAttribute()
|
||||
{
|
||||
return $this->lvl->text();
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认此经销商是否是金牌经销商
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isGoldDealer()
|
||||
{
|
||||
return $this->lvl === DealerLvl::Gold;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认此经销商是否是特邀经销商
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSpecialDealer()
|
||||
{
|
||||
return $this->lvl === DealerLvl::Special;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取此用户的所有上级经销商
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDealers()
|
||||
{
|
||||
if (empty($pids = $this->userInfo->parent_ids)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$ancestors = UserInfo::with(['dealer'])
|
||||
->whereIn('user_id', $pids)
|
||||
->latest('depth')
|
||||
->get(['user_id']);
|
||||
|
||||
return $ancestors->map(function ($item) {
|
||||
return $item->dealer;
|
||||
})->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取此用户的所有直属上级经销商
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRealDealers()
|
||||
{
|
||||
if (empty($pids = $this->userInfo->real_parent_ids)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$ancestors = UserInfo::with(['dealer'])
|
||||
->whereIn('user_id', $pids)
|
||||
->latest('depth')
|
||||
->get(['user_id']);
|
||||
|
||||
return $ancestors->map(function ($item) {
|
||||
return $item->dealer;
|
||||
})->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 变更经销商等级
|
||||
*
|
||||
* @param DealerLvl $lvl
|
||||
* @param string $remark
|
||||
* @return void
|
||||
*/
|
||||
public function changeLvl(DealerLvl $lvl, string $remark = null)
|
||||
{
|
||||
$beforeLvl = $this->lvl;
|
||||
|
||||
$this->update([
|
||||
'lvl' => $lvl,
|
||||
]);
|
||||
|
||||
if ($this->wasChanged('lvl')) {
|
||||
$this->lvlUpgradeLogs()->create([
|
||||
'change_lvl' => $this->lvl,
|
||||
'before_lvl' => $beforeLvl,
|
||||
'remark' => $remark,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\DealerLvl;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DealerLvlUpgradeLog extends Model
|
||||
{
|
||||
protected $casts = [
|
||||
'change_lvl' => DealerLvl::class,
|
||||
'before_lvl' => DealerLvl::class,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'change_lvl',
|
||||
'before_lvl',
|
||||
'remark',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDealerLvlUpgradeLogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('dealer_lvl_upgrade_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id');
|
||||
$table->tinyInteger('change_lvl')->comment('变更后的等级');
|
||||
$table->tinyInteger('before_lvl')->comment('变更前的等级');
|
||||
$table->string('remark')->nullable()->comment('备注');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('dealer_lvl_upgrade_logs');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue