'integer', 'allow_steal' => 'boolean', 'allow_help' => 'boolean', 'allow_visit' => 'boolean', 'steal_protection_hours' => 'integer', 'daily_steal_limit' => 'integer', 'daily_help_limit' => 'integer', 'notification_enabled' => 'boolean', 'auto_revenge' => 'boolean', 'friend_only' => 'boolean', 'blacklist_users' => 'array', 'whitelist_users' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 默认属性值 */ protected $attributes = [ 'allow_steal' => true, 'allow_help' => true, 'allow_visit' => true, 'steal_protection_hours' => 0, 'daily_steal_limit' => 10, 'daily_help_limit' => 20, 'notification_enabled' => true, 'auto_revenge' => false, 'friend_only' => true, 'blacklist_users' => null, 'whitelist_users' => null, ]; /** * 获取用户信息 */ public function user(): BelongsTo { return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id'); } /** * 检查用户是否在黑名单中 */ public function isUserBlacklisted(int $userId): bool { if (empty($this->blacklist_users)) { return false; } return in_array($userId, $this->blacklist_users); } /** * 检查用户是否在白名单中 */ public function isUserWhitelisted(int $userId): bool { if (empty($this->whitelist_users)) { return false; } return in_array($userId, $this->whitelist_users); } /** * 添加用户到黑名单 */ public function addToBlacklist(int $userId): void { $blacklist = $this->blacklist_users ?: []; if (!in_array($userId, $blacklist)) { $blacklist[] = $userId; $this->blacklist_users = $blacklist; $this->save(); } } /** * 从黑名单移除用户 */ public function removeFromBlacklist(int $userId): void { $blacklist = $this->blacklist_users ?: []; $key = array_search($userId, $blacklist); if ($key !== false) { unset($blacklist[$key]); $this->blacklist_users = array_values($blacklist); $this->save(); } } /** * 添加用户到白名单 */ public function addToWhitelist(int $userId): void { $whitelist = $this->whitelist_users ?: []; if (!in_array($userId, $whitelist)) { $whitelist[] = $userId; $this->whitelist_users = $whitelist; $this->save(); } } /** * 从白名单移除用户 */ public function removeFromWhitelist(int $userId): void { $whitelist = $this->whitelist_users ?: []; $key = array_search($userId, $whitelist); if ($key !== false) { unset($whitelist[$key]); $this->whitelist_users = array_values($whitelist); $this->save(); } } /** * 检查是否允许偷菜 */ public function canBeStolen(int $stealerId): bool { // 基础设置检查 if (!$this->allow_steal) { return false; } // 黑名单检查 if ($this->isUserBlacklisted($stealerId)) { return false; } // 如果有白名单,检查是否在白名单中 if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($stealerId)) { return false; } return true; } /** * 检查是否允许互助 */ public function canBeHelped(int $helperId): bool { // 基础设置检查 if (!$this->allow_help) { return false; } // 黑名单检查 if ($this->isUserBlacklisted($helperId)) { return false; } // 如果有白名单,检查是否在白名单中 if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($helperId)) { return false; } return true; } /** * 检查是否允许访问 */ public function canBeVisited(int $visitorId): bool { // 基础设置检查 if (!$this->allow_visit) { return false; } // 黑名单检查 if ($this->isUserBlacklisted($visitorId)) { return false; } // 如果有白名单,检查是否在白名单中 if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($visitorId)) { return false; } return true; } /** * 获取保护结束时间 */ public function getProtectionEndTime(): ?\Carbon\Carbon { if ($this->steal_protection_hours <= 0) { return null; } // 这里需要根据实际的保护机制来计算 // 可能需要查询最近的保护记录 return now()->addHours($this->steal_protection_hours); } /** * 是否在保护期内 */ public function isUnderProtection(): bool { $endTime = $this->getProtectionEndTime(); return $endTime && now()->lt($endTime); } /** * 获取黑名单用户数量 */ public function getBlacklistCountAttribute(): int { return count($this->blacklist_users ?: []); } /** * 获取白名单用户数量 */ public function getWhitelistCountAttribute(): int { return count($this->whitelist_users ?: []); } /** * 作用域:按用户查询 */ public function scopeByUser($query, int $userId) { return $query->where('user_id', $userId); } /** * 作用域:允许偷菜的用户 */ public function scopeAllowSteal($query) { return $query->where('allow_steal', true); } /** * 作用域:允许互助的用户 */ public function scopeAllowHelp($query) { return $query->where('allow_help', true); } /** * 作用域:允许访问的用户 */ public function scopeAllowVisit($query) { return $query->where('allow_visit', true); } /** * 获取或创建用户设置 */ public static function getOrCreateForUser(int $userId): self { return static::firstOrCreate( ['user_id' => $userId], [ 'allow_steal' => true, 'allow_help' => true, 'allow_visit' => true, 'steal_protection_hours' => 0, 'daily_steal_limit' => 10, 'daily_help_limit' => 20, 'notification_enabled' => true, 'auto_revenge' => false, 'friend_only' => true, ] ); } /** * 批量更新设置 */ public function updateSettings(array $settings): bool { $allowedFields = [ 'allow_steal', 'allow_help', 'allow_visit', 'steal_protection_hours', 'daily_steal_limit', 'daily_help_limit', 'notification_enabled', 'auto_revenge', 'friend_only' ]; $updateData = []; foreach ($allowedFields as $field) { if (array_key_exists($field, $settings)) { $updateData[$field] = $settings[$field]; } } if (empty($updateData)) { return false; } return $this->update($updateData); } }