SocialFarmSetting.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. namespace App\Module\SocialFarm\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * 社交农场设置模型
  7. *
  8. * @property int $id 主键ID
  9. * @property int $user_id 用户ID
  10. * @property bool $allow_steal 允许偷菜
  11. * @property bool $allow_help 允许互助
  12. * @property bool $allow_visit 允许访问
  13. * @property int $steal_protection_hours 偷菜保护时长(小时)
  14. * @property int $daily_steal_limit 每日被偷次数限制
  15. * @property int $daily_help_limit 每日被帮助次数限制
  16. * @property bool $notification_enabled 通知开关
  17. * @property bool $auto_revenge 自动反偷
  18. * @property bool $friend_only 仅好友可访问
  19. * @property array|null $blacklist_users 黑名单用户ID列表
  20. * @property array|null $whitelist_users 白名单用户ID列表
  21. * @property \Carbon\Carbon $created_at 创建时间
  22. * @property \Carbon\Carbon $updated_at 更新时间
  23. */
  24. class SocialFarmSetting extends ModelCore
  25. {
  26. /**
  27. * 数据表名
  28. */
  29. protected $table = 'social_farm_settings';
  30. /**
  31. * 可批量赋值的属性
  32. */
  33. protected $fillable = [
  34. 'user_id',
  35. 'allow_steal',
  36. 'allow_help',
  37. 'allow_visit',
  38. 'steal_protection_hours',
  39. 'daily_steal_limit',
  40. 'daily_help_limit',
  41. 'notification_enabled',
  42. 'auto_revenge',
  43. 'friend_only',
  44. 'blacklist_users',
  45. 'whitelist_users',
  46. ];
  47. /**
  48. * 属性类型转换
  49. */
  50. protected $casts = [
  51. 'user_id' => 'integer',
  52. 'allow_steal' => 'boolean',
  53. 'allow_help' => 'boolean',
  54. 'allow_visit' => 'boolean',
  55. 'steal_protection_hours' => 'integer',
  56. 'daily_steal_limit' => 'integer',
  57. 'daily_help_limit' => 'integer',
  58. 'notification_enabled' => 'boolean',
  59. 'auto_revenge' => 'boolean',
  60. 'friend_only' => 'boolean',
  61. 'blacklist_users' => 'array',
  62. 'whitelist_users' => 'array',
  63. 'created_at' => 'datetime',
  64. 'updated_at' => 'datetime',
  65. ];
  66. /**
  67. * 默认属性值
  68. */
  69. protected $attributes = [
  70. 'allow_steal' => true,
  71. 'allow_help' => true,
  72. 'allow_visit' => true,
  73. 'steal_protection_hours' => 0,
  74. 'daily_steal_limit' => 10,
  75. 'daily_help_limit' => 20,
  76. 'notification_enabled' => true,
  77. 'auto_revenge' => false,
  78. 'friend_only' => true,
  79. 'blacklist_users' => null,
  80. 'whitelist_users' => null,
  81. ];
  82. /**
  83. * 获取用户信息
  84. */
  85. public function user(): BelongsTo
  86. {
  87. return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id');
  88. }
  89. /**
  90. * 检查用户是否在黑名单中
  91. */
  92. public function isUserBlacklisted(int $userId): bool
  93. {
  94. if (empty($this->blacklist_users)) {
  95. return false;
  96. }
  97. return in_array($userId, $this->blacklist_users);
  98. }
  99. /**
  100. * 检查用户是否在白名单中
  101. */
  102. public function isUserWhitelisted(int $userId): bool
  103. {
  104. if (empty($this->whitelist_users)) {
  105. return false;
  106. }
  107. return in_array($userId, $this->whitelist_users);
  108. }
  109. /**
  110. * 添加用户到黑名单
  111. */
  112. public function addToBlacklist(int $userId): void
  113. {
  114. $blacklist = $this->blacklist_users ?: [];
  115. if (!in_array($userId, $blacklist)) {
  116. $blacklist[] = $userId;
  117. $this->blacklist_users = $blacklist;
  118. $this->save();
  119. }
  120. }
  121. /**
  122. * 从黑名单移除用户
  123. */
  124. public function removeFromBlacklist(int $userId): void
  125. {
  126. $blacklist = $this->blacklist_users ?: [];
  127. $key = array_search($userId, $blacklist);
  128. if ($key !== false) {
  129. unset($blacklist[$key]);
  130. $this->blacklist_users = array_values($blacklist);
  131. $this->save();
  132. }
  133. }
  134. /**
  135. * 添加用户到白名单
  136. */
  137. public function addToWhitelist(int $userId): void
  138. {
  139. $whitelist = $this->whitelist_users ?: [];
  140. if (!in_array($userId, $whitelist)) {
  141. $whitelist[] = $userId;
  142. $this->whitelist_users = $whitelist;
  143. $this->save();
  144. }
  145. }
  146. /**
  147. * 从白名单移除用户
  148. */
  149. public function removeFromWhitelist(int $userId): void
  150. {
  151. $whitelist = $this->whitelist_users ?: [];
  152. $key = array_search($userId, $whitelist);
  153. if ($key !== false) {
  154. unset($whitelist[$key]);
  155. $this->whitelist_users = array_values($whitelist);
  156. $this->save();
  157. }
  158. }
  159. /**
  160. * 检查是否允许偷菜
  161. */
  162. public function canBeStolen(int $stealerId): bool
  163. {
  164. // 基础设置检查
  165. if (!$this->allow_steal) {
  166. return false;
  167. }
  168. // 黑名单检查
  169. if ($this->isUserBlacklisted($stealerId)) {
  170. return false;
  171. }
  172. // 如果有白名单,检查是否在白名单中
  173. if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($stealerId)) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. /**
  179. * 检查是否允许互助
  180. */
  181. public function canBeHelped(int $helperId): bool
  182. {
  183. // 基础设置检查
  184. if (!$this->allow_help) {
  185. return false;
  186. }
  187. // 黑名单检查
  188. if ($this->isUserBlacklisted($helperId)) {
  189. return false;
  190. }
  191. // 如果有白名单,检查是否在白名单中
  192. if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($helperId)) {
  193. return false;
  194. }
  195. return true;
  196. }
  197. /**
  198. * 检查是否允许访问
  199. */
  200. public function canBeVisited(int $visitorId): bool
  201. {
  202. // 基础设置检查
  203. if (!$this->allow_visit) {
  204. return false;
  205. }
  206. // 黑名单检查
  207. if ($this->isUserBlacklisted($visitorId)) {
  208. return false;
  209. }
  210. // 如果有白名单,检查是否在白名单中
  211. if (!empty($this->whitelist_users) && !$this->isUserWhitelisted($visitorId)) {
  212. return false;
  213. }
  214. return true;
  215. }
  216. /**
  217. * 获取保护结束时间
  218. */
  219. public function getProtectionEndTime(): ?\Carbon\Carbon
  220. {
  221. if ($this->steal_protection_hours <= 0) {
  222. return null;
  223. }
  224. // 这里需要根据实际的保护机制来计算
  225. // 可能需要查询最近的保护记录
  226. return now()->addHours($this->steal_protection_hours);
  227. }
  228. /**
  229. * 是否在保护期内
  230. */
  231. public function isUnderProtection(): bool
  232. {
  233. $endTime = $this->getProtectionEndTime();
  234. return $endTime && now()->lt($endTime);
  235. }
  236. /**
  237. * 获取黑名单用户数量
  238. */
  239. public function getBlacklistCountAttribute(): int
  240. {
  241. return count($this->blacklist_users ?: []);
  242. }
  243. /**
  244. * 获取白名单用户数量
  245. */
  246. public function getWhitelistCountAttribute(): int
  247. {
  248. return count($this->whitelist_users ?: []);
  249. }
  250. /**
  251. * 作用域:按用户查询
  252. */
  253. public function scopeByUser($query, int $userId)
  254. {
  255. return $query->where('user_id', $userId);
  256. }
  257. /**
  258. * 作用域:允许偷菜的用户
  259. */
  260. public function scopeAllowSteal($query)
  261. {
  262. return $query->where('allow_steal', true);
  263. }
  264. /**
  265. * 作用域:允许互助的用户
  266. */
  267. public function scopeAllowHelp($query)
  268. {
  269. return $query->where('allow_help', true);
  270. }
  271. /**
  272. * 作用域:允许访问的用户
  273. */
  274. public function scopeAllowVisit($query)
  275. {
  276. return $query->where('allow_visit', true);
  277. }
  278. /**
  279. * 获取或创建用户设置
  280. */
  281. public static function getOrCreateForUser(int $userId): self
  282. {
  283. return static::firstOrCreate(
  284. ['user_id' => $userId],
  285. [
  286. 'allow_steal' => true,
  287. 'allow_help' => true,
  288. 'allow_visit' => true,
  289. 'steal_protection_hours' => 0,
  290. 'daily_steal_limit' => 10,
  291. 'daily_help_limit' => 20,
  292. 'notification_enabled' => true,
  293. 'auto_revenge' => false,
  294. 'friend_only' => true,
  295. ]
  296. );
  297. }
  298. /**
  299. * 批量更新设置
  300. */
  301. public function updateSettings(array $settings): bool
  302. {
  303. $allowedFields = [
  304. 'allow_steal', 'allow_help', 'allow_visit',
  305. 'steal_protection_hours', 'daily_steal_limit', 'daily_help_limit',
  306. 'notification_enabled', 'auto_revenge', 'friend_only'
  307. ];
  308. $updateData = [];
  309. foreach ($allowedFields as $field) {
  310. if (array_key_exists($field, $settings)) {
  311. $updateData[$field] = $settings[$field];
  312. }
  313. }
  314. if (empty($updateData)) {
  315. return false;
  316. }
  317. return $this->update($updateData);
  318. }
  319. }