| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- namespace App\Module\SocialFarm\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 社交农场设置模型
- *
- * @property int $id 主键ID
- * @property int $user_id 用户ID
- * @property bool $allow_steal 允许偷菜
- * @property bool $allow_help 允许互助
- * @property bool $allow_visit 允许访问
- * @property int $steal_protection_hours 偷菜保护时长(小时)
- * @property int $daily_steal_limit 每日被偷次数限制
- * @property int $daily_help_limit 每日被帮助次数限制
- * @property bool $notification_enabled 通知开关
- * @property bool $auto_revenge 自动反偷
- * @property bool $friend_only 仅好友可访问
- * @property array|null $blacklist_users 黑名单用户ID列表
- * @property array|null $whitelist_users 白名单用户ID列表
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- class SocialFarmSetting extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'social_farm_settings';
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- 'user_id',
- 'allow_steal',
- 'allow_help',
- 'allow_visit',
- 'steal_protection_hours',
- 'daily_steal_limit',
- 'daily_help_limit',
- 'notification_enabled',
- 'auto_revenge',
- 'friend_only',
- 'blacklist_users',
- 'whitelist_users',
- ];
- /**
- * 属性类型转换
- */
- protected $casts = [
- 'user_id' => '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);
- }
- }
|