DealerLvl::None, 'is_sale' => false, 'is_manager' => false, ]; protected $casts = [ 'lvl' => DealerLvl::class, 'is_sale' => 'bool', 'is_manager' => 'bool', 'pay_info'=>JsonArray::class, ]; protected $fillable = [ 'user_id', 'lvl', 'is_sale', 'is_manager', 'pay_info', ]; public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * 属于此经销商的用户信息 */ public function userInfo() { return $this->hasOne(UserInfo::class, 'user_id', 'user_id'); } /** * 属于此经销商的管理者津贴 */ public function managerSalesLogs() { return $this->hasMany(DealerManagerSalesLog::class, 'user_id', 'user_id'); } /** * 属于此经销商的管理者津贴 */ public function manageSubsidyLogs() { 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, ]); } } }