social_farm_tables.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. -- SocialFarm模块数据表创建SQL
  2. -- 创建时间: 2025-07-02
  3. -- 模块: SocialFarm
  4. -- 描述: 社交农场系统相关数据表
  5. -- 1. 偷菜记录表
  6. CREATE TABLE `kku_social_farm_steal_logs` (
  7. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  8. `stealer_id` bigint(20) unsigned NOT NULL COMMENT '偷菜者用户ID',
  9. `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
  10. `land_id` bigint(20) unsigned NOT NULL COMMENT '被偷的土地ID',
  11. `crop_id` bigint(20) unsigned NOT NULL COMMENT '被偷的作物ID',
  12. `item_id` bigint(20) unsigned NOT NULL COMMENT '偷到的物品ID',
  13. `item_amount` int(11) NOT NULL DEFAULT '0' COMMENT '偷到的物品数量',
  14. `original_amount` int(11) NOT NULL DEFAULT '0' COMMENT '作物原始数量',
  15. `steal_ratio` decimal(5,4) NOT NULL DEFAULT '0.0000' COMMENT '偷菜比例',
  16. `steal_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '偷菜时间',
  17. `steal_status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '偷菜状态:1成功,2失败,3被保护',
  18. `ip_address` varchar(45) DEFAULT NULL COMMENT '偷菜者IP地址',
  19. `user_agent` text COMMENT '用户代理信息',
  20. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  21. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  22. PRIMARY KEY (`id`),
  23. KEY `idx_stealer_time` (`stealer_id`, `steal_time`),
  24. KEY `idx_owner_time` (`owner_id`, `steal_time`),
  25. KEY `idx_land_crop` (`land_id`, `crop_id`),
  26. KEY `idx_steal_status` (`steal_status`, `steal_time`)
  27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='偷菜记录表';
  28. -- 2. 农场访问记录表
  29. CREATE TABLE `kku_social_farm_visit_logs` (
  30. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  31. `visitor_id` bigint(20) unsigned NOT NULL COMMENT '访问者用户ID',
  32. `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
  33. `visit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '访问时间',
  34. `visit_type` tinyint(4) NOT NULL DEFAULT '1' COMMENT '访问类型:1普通访问,2偷菜访问,3互助访问',
  35. `actions` json DEFAULT NULL COMMENT '访问期间的行为记录',
  36. `duration` int(11) NOT NULL DEFAULT '0' COMMENT '访问持续时间(秒)',
  37. `steal_count` int(11) NOT NULL DEFAULT '0' COMMENT '本次访问偷菜次数',
  38. `help_count` int(11) NOT NULL DEFAULT '0' COMMENT '本次访问互助次数',
  39. `ip_address` varchar(45) DEFAULT NULL COMMENT '访问者IP地址',
  40. `user_agent` text COMMENT '用户代理信息',
  41. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  42. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  43. PRIMARY KEY (`id`),
  44. KEY `idx_visitor_time` (`visitor_id`, `visit_time`),
  45. KEY `idx_owner_time` (`owner_id`, `visit_time`),
  46. KEY `idx_visit_type` (`visit_type`, `visit_time`)
  47. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='农场访问记录表';
  48. -- 3. 社交设置表
  49. CREATE TABLE `kku_social_farm_settings` (
  50. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  51. `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
  52. `allow_steal` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许偷菜:1允许,0禁止',
  53. `allow_help` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许互助:1允许,0禁止',
  54. `allow_visit` tinyint(4) NOT NULL DEFAULT '1' COMMENT '允许访问:1允许,0禁止',
  55. `steal_protection_hours` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜保护时长(小时)',
  56. `daily_steal_limit` int(11) NOT NULL DEFAULT '10' COMMENT '每日被偷次数限制',
  57. `daily_help_limit` int(11) NOT NULL DEFAULT '20' COMMENT '每日被帮助次数限制',
  58. `notification_enabled` tinyint(4) NOT NULL DEFAULT '1' COMMENT '通知开关:1开启,0关闭',
  59. `auto_revenge` tinyint(4) NOT NULL DEFAULT '0' COMMENT '自动反偷:1开启,0关闭',
  60. `friend_only` tinyint(4) NOT NULL DEFAULT '1' COMMENT '仅好友可访问:1是,0否',
  61. `blacklist_users` json DEFAULT NULL COMMENT '黑名单用户ID列表',
  62. `whitelist_users` json DEFAULT NULL COMMENT '白名单用户ID列表',
  63. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  64. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  65. PRIMARY KEY (`id`),
  66. UNIQUE KEY `uk_user_id` (`user_id`)
  67. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='社交农场设置表';
  68. -- 4. 互助记录表
  69. CREATE TABLE `kku_social_farm_help_logs` (
  70. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  71. `helper_id` bigint(20) unsigned NOT NULL COMMENT '帮助者用户ID',
  72. `owner_id` bigint(20) unsigned NOT NULL COMMENT '农场主用户ID',
  73. `land_id` bigint(20) unsigned NOT NULL COMMENT '帮助的土地ID',
  74. `help_type` tinyint(4) NOT NULL COMMENT '帮助类型:1浇水,2施肥,3除草,4杀虫,5收获',
  75. `help_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '帮助时间',
  76. `item_consumed_id` bigint(20) unsigned DEFAULT NULL COMMENT '消耗的道具ID',
  77. `item_consumed_amount` int(11) NOT NULL DEFAULT '0' COMMENT '消耗的道具数量',
  78. `reward_item_id` bigint(20) unsigned DEFAULT NULL COMMENT '奖励物品ID',
  79. `reward_amount` int(11) NOT NULL DEFAULT '0' COMMENT '奖励数量',
  80. `exp_reward` int(11) NOT NULL DEFAULT '0' COMMENT '经验奖励',
  81. `help_effect` json DEFAULT NULL COMMENT '帮助效果详情',
  82. `ip_address` varchar(45) DEFAULT NULL COMMENT '帮助者IP地址',
  83. `user_agent` text COMMENT '用户代理信息',
  84. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  85. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  86. PRIMARY KEY (`id`),
  87. KEY `idx_helper_time` (`helper_id`, `help_time`),
  88. KEY `idx_owner_time` (`owner_id`, `help_time`),
  89. KEY `idx_land_type` (`land_id`, `help_type`),
  90. KEY `idx_help_type` (`help_type`, `help_time`)
  91. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='互助记录表';
  92. -- 5. 社交统计表
  93. CREATE TABLE `kku_social_farm_stats` (
  94. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  95. `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
  96. `stat_date` date NOT NULL COMMENT '统计日期',
  97. `steal_count` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜次数',
  98. `stolen_count` int(11) NOT NULL DEFAULT '0' COMMENT '被偷次数',
  99. `help_count` int(11) NOT NULL DEFAULT '0' COMMENT '帮助次数',
  100. `helped_count` int(11) NOT NULL DEFAULT '0' COMMENT '被帮助次数',
  101. `visit_count` int(11) NOT NULL DEFAULT '0' COMMENT '访问次数',
  102. `visited_count` int(11) NOT NULL DEFAULT '0' COMMENT '被访问次数',
  103. `steal_items_gained` int(11) NOT NULL DEFAULT '0' COMMENT '偷菜获得物品数',
  104. `steal_items_lost` int(11) NOT NULL DEFAULT '0' COMMENT '被偷失去物品数',
  105. `help_rewards_gained` int(11) NOT NULL DEFAULT '0' COMMENT '互助获得奖励数',
  106. `total_exp_gained` int(11) NOT NULL DEFAULT '0' COMMENT '总经验获得',
  107. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  108. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  109. PRIMARY KEY (`id`),
  110. UNIQUE KEY `uk_user_date` (`user_id`, `stat_date`),
  111. KEY `idx_stat_date` (`stat_date`)
  112. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='社交农场统计表';
  113. -- 6. 偷菜保护记录表
  114. CREATE TABLE `kku_social_farm_protections` (
  115. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  116. `user_id` bigint(20) unsigned NOT NULL COMMENT '用户ID',
  117. `land_id` bigint(20) unsigned DEFAULT NULL COMMENT '土地ID(NULL表示全农场保护)',
  118. `protection_type` tinyint(4) NOT NULL COMMENT '保护类型:1时间保护,2道具保护,3系统保护',
  119. `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '保护开始时间',
  120. `end_time` timestamp NOT NULL COMMENT '保护结束时间',
  121. `protection_item_id` bigint(20) unsigned DEFAULT NULL COMMENT '保护道具ID',
  122. `is_active` tinyint(4) NOT NULL DEFAULT '1' COMMENT '是否激活:1激活,0失效',
  123. `created_by` bigint(20) unsigned DEFAULT NULL COMMENT '创建者ID',
  124. `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  125. `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  126. PRIMARY KEY (`id`),
  127. KEY `idx_user_active` (`user_id`, `is_active`),
  128. KEY `idx_land_active` (`land_id`, `is_active`),
  129. KEY `idx_end_time` (`end_time`)
  130. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='偷菜保护记录表';
  131. -- 插入默认配置数据
  132. -- 注意:实际使用时需要根据具体需求调整这些默认值
  133. -- 为现有用户创建默认社交设置(如果需要)
  134. -- INSERT INTO kku_social_farm_settings (user_id)
  135. -- SELECT id FROM kku_users
  136. -- WHERE id NOT IN (SELECT user_id FROM kku_social_farm_settings);
  137. -- 创建索引优化查询性能
  138. -- 这些索引在表创建时已经包含,这里仅作为说明
  139. -- 偷菜记录表的复合索引
  140. -- ALTER TABLE kku_social_farm_steal_logs ADD INDEX idx_stealer_owner_time (stealer_id, owner_id, steal_time);
  141. -- 访问记录表的复合索引
  142. -- ALTER TABLE kku_social_farm_visit_logs ADD INDEX idx_visitor_owner_time (visitor_id, owner_id, visit_time);
  143. -- 互助记录表的复合索引
  144. -- ALTER TABLE kku_social_farm_help_logs ADD INDEX idx_helper_owner_time (helper_id, owner_id, help_time);
  145. -- 统计表的日期范围索引
  146. -- ALTER TABLE kku_social_farm_stats ADD INDEX idx_user_date_range (user_id, stat_date);
  147. -- 保护表的时间范围索引
  148. -- ALTER TABLE kku_social_farm_protections ADD INDEX idx_time_range (start_time, end_time);
  149. -- 创建视图用于常用查询(可选)
  150. -- 用户每日偷菜统计视图
  151. CREATE VIEW v_daily_steal_stats AS
  152. SELECT
  153. user_id,
  154. DATE(steal_time) as steal_date,
  155. COUNT(*) as steal_count,
  156. SUM(item_amount) as total_items
  157. FROM kku_social_farm_steal_logs
  158. WHERE steal_status = 1
  159. GROUP BY user_id, DATE(steal_time);
  160. -- 用户社交活跃度视图
  161. CREATE VIEW v_user_social_activity AS
  162. SELECT
  163. u.id as user_id,
  164. u.nickname,
  165. COALESCE(s.steal_count, 0) as today_steal_count,
  166. COALESCE(s.help_count, 0) as today_help_count,
  167. COALESCE(s.visit_count, 0) as today_visit_count,
  168. COALESCE(settings.allow_steal, 1) as allow_steal,
  169. COALESCE(settings.allow_help, 1) as allow_help,
  170. COALESCE(settings.allow_visit, 1) as allow_visit
  171. FROM kku_users u
  172. LEFT JOIN kku_social_farm_stats s ON u.id = s.user_id AND s.stat_date = CURDATE()
  173. LEFT JOIN kku_social_farm_settings settings ON u.id = settings.user_id;
  174. -- 添加表注释说明
  175. ALTER TABLE kku_social_farm_steal_logs COMMENT = '偷菜记录表 - 记录所有偷菜行为的详细信息';
  176. ALTER TABLE kku_social_farm_visit_logs COMMENT = '农场访问记录表 - 记录用户访问好友农场的行为';
  177. ALTER TABLE kku_social_farm_settings COMMENT = '社交设置表 - 存储用户的社交农场相关设置';
  178. ALTER TABLE kku_social_farm_help_logs COMMENT = '互助记录表 - 记录用户间的互助行为';
  179. ALTER TABLE kku_social_farm_stats COMMENT = '社交统计表 - 记录用户的社交行为统计数据';
  180. ALTER TABLE kku_social_farm_protections COMMENT = '偷菜保护记录表 - 记录偷菜保护状态';
  181. -- 数据表创建完成
  182. -- 请根据实际需求调整表结构和索引策略
  183. -- 建议在生产环境部署前进行充分的性能测试