|
|
@@ -0,0 +1,309 @@
|
|
|
+# SocialFarm模块数据库设计
|
|
|
+
|
|
|
+## 概述
|
|
|
+
|
|
|
+SocialFarm模块的数据库设计围绕社交农场互动功能,主要包括偷菜记录、访问记录、互助记录和用户社交设置等核心数据表。
|
|
|
+
|
|
|
+## 数据表设计
|
|
|
+
|
|
|
+### 1. 偷菜记录表 (kku_social_farm_steal_logs)
|
|
|
+
|
|
|
+记录所有偷菜行为的详细信息。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_steal_logs` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `stealer_id` bigint(20) unsigned NOT NULL COMMENT '偷菜者用户ID',
|
|
|
+ `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
|
|
|
+ `land_id` bigint(20) unsigned NOT NULL COMMENT '被偷的土地ID',
|
|
|
+ `crop_id` bigint(20) unsigned NOT NULL COMMENT '被偷的作物ID',
|
|
|
+ `item_id` bigint(20) unsigned NOT NULL COMMENT '偷到的物品ID',
|
|
|
+ `item_amount` int(11) NOT NULL DEFAULT '0' COMMENT '偷到的物品数量',
|
|
|
+ `original_amount` int(11) NOT NULL DEFAULT '0' COMMENT '作物原始数量',
|
|
|
+ `steal_ratio` decimal(5,4) NOT NULL DEFAULT '0.0000' COMMENT '偷菜比例',
|
|
|
+ `steal_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '偷菜时间',
|
|
|
+ `steal_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '偷菜状态:1成功,2失败,3被保护',
|
|
|
+ `ip_address` varchar(45) DEFAULT NULL COMMENT '偷菜者IP地址',
|
|
|
+ `user_agent` text COMMENT '用户代理信息',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_stealer_time` (`stealer_id`, `steal_time`),
|
|
|
+ KEY `idx_owner_time` (`owner_id`, `steal_time`),
|
|
|
+ KEY `idx_land_crop` (`land_id`, `crop_id`),
|
|
|
+ KEY `idx_steal_status` (`steal_status`, `steal_time`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='偷菜记录表';
|
|
|
+```
|
|
|
+
|
|
|
+### 2. 农场访问记录表 (kku_social_farm_visit_logs)
|
|
|
+
|
|
|
+记录用户访问好友农场的行为。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_visit_logs` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `visitor_id` bigint(20) unsigned NOT NULL COMMENT '访问者用户ID',
|
|
|
+ `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
|
|
|
+ `visit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '访问时间',
|
|
|
+ `visit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '访问类型:1普通访问,2偷菜访问,3互助访问',
|
|
|
+ `actions` json DEFAULT NULL COMMENT '访问期间的行为记录',
|
|
|
+ `duration` int(11) NOT NULL DEFAULT '0' COMMENT '访问持续时间(秒)',
|
|
|
+ `steal_count` int(11) NOT NULL DEFAULT '0' COMMENT '本次访问偷菜次数',
|
|
|
+ `help_count` int(11) NOT NULL DEFAULT '0' COMMENT '本次访问互助次数',
|
|
|
+ `ip_address` varchar(45) DEFAULT NULL COMMENT '访问者IP地址',
|
|
|
+ `user_agent` text COMMENT '用户代理信息',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_visitor_time` (`visitor_id`, `visit_time`),
|
|
|
+ KEY `idx_owner_time` (`owner_id`, `visit_time`),
|
|
|
+ KEY `idx_visit_type` (`visit_type`, `visit_time`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='农场访问记录表';
|
|
|
+```
|
|
|
+
|
|
|
+### 3. 社交设置表 (kku_social_farm_settings)
|
|
|
+
|
|
|
+存储用户的社交农场相关设置。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_settings` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
|
|
|
+ `allow_steal` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许偷菜:1允许,0禁止',
|
|
|
+ `allow_help` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许互助:1允许,0禁止',
|
|
|
+ `allow_visit` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许访问:1允许,0禁止',
|
|
|
+ `steal_protection_hours` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜保护时长(小时)',
|
|
|
+ `daily_steal_limit` int(11) NOT NULL DEFAULT '10' COMMENT '每日被偷次数限制',
|
|
|
+ `daily_help_limit` int(11) NOT NULL DEFAULT '20' COMMENT '每日被帮助次数限制',
|
|
|
+ `notification_enabled` tinyint(4) NOT NULL DEFAULT '1' COMMENT '通知开关:1开启,0关闭',
|
|
|
+ `auto_revenge` tinyint(4) NOT NULL DEFAULT '0' COMMENT '自动反偷:1开启,0关闭',
|
|
|
+ `friend_only` tinyint(4) NOT NULL DEFAULT '1' COMMENT '仅好友可访问:1是,0否',
|
|
|
+ `blacklist_users` json DEFAULT NULL COMMENT '黑名单用户ID列表',
|
|
|
+ `whitelist_users` json DEFAULT NULL COMMENT '白名单用户ID列表',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ UNIQUE KEY `uk_user_id` (`user_id`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='社交农场设置表';
|
|
|
+```
|
|
|
+
|
|
|
+### 4. 互助记录表 (kku_social_farm_help_logs)
|
|
|
+
|
|
|
+记录用户间的互助行为。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_help_logs` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `helper_id` bigint(20) unsigned NOT NULL COMMENT '帮助者用户ID',
|
|
|
+ `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
|
|
|
+ `land_id` bigint(20) unsigned NOT NULL COMMENT '帮助的土地ID',
|
|
|
+ `help_type` tinyint(4) NOT NULL COMMENT '帮助类型:1浇水,2施肥,3除草,4杀虫,5收获',
|
|
|
+ `help_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '帮助时间',
|
|
|
+ `item_consumed_id` bigint(20) unsigned DEFAULT NULL COMMENT '消耗的道具ID',
|
|
|
+ `item_consumed_amount` int(11) NOT NULL DEFAULT '0' COMMENT '消耗的道具数量',
|
|
|
+ `reward_item_id` bigint(20) unsigned DEFAULT NULL COMMENT '奖励物品ID',
|
|
|
+ `reward_amount` int(11) NOT NULL DEFAULT '0' COMMENT '奖励数量',
|
|
|
+ `exp_reward` int(11) NOT NULL DEFAULT '0' COMMENT '经验奖励',
|
|
|
+ `help_effect` json DEFAULT NULL COMMENT '帮助效果详情',
|
|
|
+ `ip_address` varchar(45) DEFAULT NULL COMMENT '帮助者IP地址',
|
|
|
+ `user_agent` text COMMENT '用户代理信息',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_helper_time` (`helper_id`, `help_time`),
|
|
|
+ KEY `idx_owner_time` (`owner_id`, `help_time`),
|
|
|
+ KEY `idx_land_type` (`land_id`, `help_type`),
|
|
|
+ KEY `idx_help_type` (`help_type`, `help_time`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='互助记录表';
|
|
|
+```
|
|
|
+
|
|
|
+### 5. 社交统计表 (kku_social_farm_stats)
|
|
|
+
|
|
|
+记录用户的社交行为统计数据。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_stats` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
|
|
|
+ `stat_date` date NOT NULL COMMENT '统计日期',
|
|
|
+ `steal_count` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜次数',
|
|
|
+ `stolen_count` int(11) NOT NULL DEFAULT '0' COMMENT '被偷次数',
|
|
|
+ `help_count` int(11) NOT NULL DEFAULT '0' COMMENT '帮助次数',
|
|
|
+ `helped_count` int(11) NOT NULL DEFAULT '0' COMMENT '被帮助次数',
|
|
|
+ `visit_count` int(11) NOT NULL DEFAULT '0' COMMENT '访问次数',
|
|
|
+ `visited_count` int(11) NOT NULL DEFAULT '0' COMMENT '被访问次数',
|
|
|
+ `steal_items_gained` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜获得物品数',
|
|
|
+ `steal_items_lost` int(11) NOT NULL DEFAULT '0' COMMENT '被偷失去物品数',
|
|
|
+ `help_rewards_gained` int(11) NOT NULL DEFAULT '0' COMMENT '互助获得奖励数',
|
|
|
+ `total_exp_gained` int(11) NOT NULL DEFAULT '0' COMMENT '总经验获得',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ UNIQUE KEY `uk_user_date` (`user_id`, `stat_date`),
|
|
|
+ KEY `idx_stat_date` (`stat_date`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='社交农场统计表';
|
|
|
+```
|
|
|
+
|
|
|
+### 6. 偷菜保护记录表 (kku_social_farm_protections)
|
|
|
+
|
|
|
+记录偷菜保护状态。
|
|
|
+
|
|
|
+```sql
|
|
|
+CREATE TABLE `kku_social_farm_protections` (
|
|
|
+ `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
|
|
+ `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
|
|
|
+ `land_id` bigint(20) unsigned DEFAULT NULL COMMENT '土地ID(NULL表示全农场保护)',
|
|
|
+ `protection_type` tinyint(4) NOT NULL COMMENT '保护类型:1时间保护,2道具保护,3系统保护',
|
|
|
+ `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '保护开始时间',
|
|
|
+ `end_time` timestamp NOT NULL COMMENT '保护结束时间',
|
|
|
+ `protection_item_id` bigint(20) unsigned DEFAULT NULL COMMENT '保护道具ID',
|
|
|
+ `is_active` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否激活:1激活,0失效',
|
|
|
+ `created_by` bigint(20) unsigned DEFAULT NULL COMMENT '创建者ID',
|
|
|
+ `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
+ `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_user_active` (`user_id`, `is_active`),
|
|
|
+ KEY `idx_land_active` (`land_id`, `is_active`),
|
|
|
+ KEY `idx_end_time` (`end_time`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='偷菜保护记录表';
|
|
|
+```
|
|
|
+
|
|
|
+## 索引设计说明
|
|
|
+
|
|
|
+### 主要索引策略
|
|
|
+
|
|
|
+1. **时间范围查询索引**
|
|
|
+ - `idx_stealer_time`: 支持查询某用户的偷菜历史
|
|
|
+ - `idx_owner_time`: 支持查询某农场的被偷历史
|
|
|
+ - `idx_visitor_time`: 支持查询访问记录
|
|
|
+
|
|
|
+2. **状态查询索引**
|
|
|
+ - `idx_steal_status`: 支持按偷菜状态查询
|
|
|
+ - `idx_visit_type`: 支持按访问类型查询
|
|
|
+ - `idx_help_type`: 支持按帮助类型查询
|
|
|
+
|
|
|
+3. **关联查询索引**
|
|
|
+ - `idx_land_crop`: 支持土地作物关联查询
|
|
|
+ - `idx_user_active`: 支持用户保护状态查询
|
|
|
+
|
|
|
+## 数据完整性约束
|
|
|
+
|
|
|
+### 外键关系
|
|
|
+
|
|
|
+虽然表结构中没有显式定义外键约束,但存在以下逻辑外键关系:
|
|
|
+
|
|
|
+1. **用户关系**
|
|
|
+ - `stealer_id`, `owner_id`, `visitor_id`, `helper_id` → `kku_users.id`
|
|
|
+ - `user_id` → `kku_users.id`
|
|
|
+
|
|
|
+2. **农场关系**
|
|
|
+ - `land_id` → `kku_farm_land_users.id`
|
|
|
+ - `crop_id` → `kku_farm_crop_users.id`
|
|
|
+
|
|
|
+3. **物品关系**
|
|
|
+ - `item_id`, `reward_item_id` → `kku_item_configs.id`
|
|
|
+
|
|
|
+### 数据验证规则
|
|
|
+
|
|
|
+1. **偷菜记录验证**
|
|
|
+ - `stealer_id` ≠ `owner_id` (不能偷自己的菜)
|
|
|
+ - `item_amount` > 0 (偷到的数量必须大于0)
|
|
|
+ - `steal_ratio` ∈ [0, 1] (偷菜比例在0-1之间)
|
|
|
+
|
|
|
+2. **访问记录验证**
|
|
|
+ - `visitor_id` ≠ `owner_id` (不记录访问自己农场)
|
|
|
+ - `duration` ≥ 0 (访问时长不能为负)
|
|
|
+
|
|
|
+3. **设置表验证**
|
|
|
+ - `steal_protection_hours` ≥ 0 (保护时长不能为负)
|
|
|
+ - `daily_steal_limit` > 0 (每日限制必须大于0)
|
|
|
+
|
|
|
+## 性能优化建议
|
|
|
+
|
|
|
+### 1. 分区策略
|
|
|
+
|
|
|
+对于日志类表,建议按时间分区:
|
|
|
+
|
|
|
+```sql
|
|
|
+-- 偷菜记录表按月分区
|
|
|
+ALTER TABLE kku_social_farm_steal_logs
|
|
|
+PARTITION BY RANGE (YEAR(steal_time) * 100 + MONTH(steal_time)) (
|
|
|
+ PARTITION p202501 VALUES LESS THAN (202502),
|
|
|
+ PARTITION p202502 VALUES LESS THAN (202503),
|
|
|
+ -- ... 更多分区
|
|
|
+);
|
|
|
+```
|
|
|
+
|
|
|
+### 2. 数据清理策略
|
|
|
+
|
|
|
+定期清理过期数据:
|
|
|
+
|
|
|
+```sql
|
|
|
+-- 清理3个月前的访问记录
|
|
|
+DELETE FROM kku_social_farm_visit_logs
|
|
|
+WHERE visit_time < DATE_SUB(NOW(), INTERVAL 3 MONTH);
|
|
|
+
|
|
|
+-- 清理6个月前的偷菜记录
|
|
|
+DELETE FROM kku_social_farm_steal_logs
|
|
|
+WHERE steal_time < DATE_SUB(NOW(), INTERVAL 6 MONTH);
|
|
|
+```
|
|
|
+
|
|
|
+### 3. 缓存策略
|
|
|
+
|
|
|
+建议缓存的数据:
|
|
|
+
|
|
|
+1. **用户社交设置**: 缓存时间1小时
|
|
|
+2. **每日统计数据**: 缓存时间30分钟
|
|
|
+3. **保护状态**: 缓存时间10分钟
|
|
|
+4. **好友农场状态**: 缓存时间5分钟
|
|
|
+
|
|
|
+## 数据迁移注意事项
|
|
|
+
|
|
|
+### 1. 初始化数据
|
|
|
+
|
|
|
+新用户注册时需要初始化社交设置:
|
|
|
+
|
|
|
+```sql
|
|
|
+INSERT INTO kku_social_farm_settings (user_id)
|
|
|
+VALUES (?)
|
|
|
+ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;
|
|
|
+```
|
|
|
+
|
|
|
+### 2. 历史数据处理
|
|
|
+
|
|
|
+如果有历史的偷菜数据需要迁移,注意:
|
|
|
+
|
|
|
+1. 数据格式转换
|
|
|
+2. 时间戳统一
|
|
|
+3. 状态码映射
|
|
|
+4. 用户ID验证
|
|
|
+
|
|
|
+### 3. 数据一致性检查
|
|
|
+
|
|
|
+定期执行数据一致性检查:
|
|
|
+
|
|
|
+```sql
|
|
|
+-- 检查孤立的偷菜记录
|
|
|
+SELECT * FROM kku_social_farm_steal_logs s
|
|
|
+LEFT JOIN kku_users u1 ON s.stealer_id = u1.id
|
|
|
+LEFT JOIN kku_users u2 ON s.owner_id = u2.id
|
|
|
+WHERE u1.id IS NULL OR u2.id IS NULL;
|
|
|
+```
|
|
|
+
|
|
|
+## 备份和恢复策略
|
|
|
+
|
|
|
+### 1. 备份策略
|
|
|
+
|
|
|
+- **全量备份**: 每日凌晨执行
|
|
|
+- **增量备份**: 每4小时执行
|
|
|
+- **日志备份**: 实时备份binlog
|
|
|
+
|
|
|
+### 2. 恢复策略
|
|
|
+
|
|
|
+- **数据恢复**: 基于时间点恢复
|
|
|
+- **表恢复**: 单表恢复机制
|
|
|
+- **应急恢复**: 快速恢复关键表
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+本文档详细描述了SocialFarm模块的数据库设计,包括表结构、索引、约束和优化建议。在实际开发中,请根据具体需求调整表结构和索引策略。
|