diff --git a/app/Models/Dealer.php b/app/Models/Dealer.php index 494b9513..3b549789 100644 --- a/app/Models/Dealer.php +++ b/app/Models/Dealer.php @@ -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, + ]); + } + } } diff --git a/app/Models/DealerLvlUpgradeLog.php b/app/Models/DealerLvlUpgradeLog.php new file mode 100644 index 00000000..38caffa9 --- /dev/null +++ b/app/Models/DealerLvlUpgradeLog.php @@ -0,0 +1,21 @@ + DealerLvl::class, + 'before_lvl' => DealerLvl::class, + ]; + + protected $fillable = [ + 'user_id', + 'change_lvl', + 'before_lvl', + 'remark', + ]; +} diff --git a/database/migrations/2022_02_20_150611_create_dealer_lvl_upgrade_logs_table.php b/database/migrations/2022_02_20_150611_create_dealer_lvl_upgrade_logs_table.php new file mode 100644 index 00000000..d3770f0b --- /dev/null +++ b/database/migrations/2022_02_20_150611_create_dealer_lvl_upgrade_logs_table.php @@ -0,0 +1,35 @@ +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'); + } +}