# 农业服务数据库设计文档

## 1. 数据库环境说明

- 数据库名称: xlxumu_farming
- 字符集: utf8mb4
- 排序规则: utf8mb4_unicode_ci

## 2. 数据表设计

### 2.1 农田信息表 (farmlands)

存储农田基本信息。

```sql
CREATE TABLE `farmlands` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `name` VARCHAR(100) NOT NULL COMMENT '农田名称',
  `location` VARCHAR(200) NOT NULL COMMENT '地理位置',
  `area` DECIMAL(10,2) NOT NULL COMMENT '面积(亩)',
  `crop_type` VARCHAR(50) NOT NULL COMMENT '作物类型',
  `planting_date` DATE COMMENT '种植日期',
  `status` VARCHAR(20) NOT NULL DEFAULT 'ACTIVE' COMMENT '状态(ACTIVE/INACTIVE)',
  `description` TEXT COMMENT '描述',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_crop_type` (`crop_type`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='农田信息表';
```

### 2.2 农事活动记录表 (farming_activities)

存储农事活动记录。

```sql
CREATE TABLE `farming_activities` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `farmland_id` BIGINT UNSIGNED NOT NULL COMMENT '农田ID',
  `activity_type` VARCHAR(50) NOT NULL COMMENT '活动类型(播种/施肥/除草/收割等)',
  `activity_date` DATE NOT NULL COMMENT '活动日期',
  `description` TEXT COMMENT '活动描述',
  `cost` DECIMAL(10,2) COMMENT '费用',
  `operator` VARCHAR(50) COMMENT '操作人员',
  `images` JSON COMMENT '活动图片URL列表',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_farmland_id` (`farmland_id`),
  KEY `idx_activity_type` (`activity_type`),
  KEY `idx_activity_date` (`activity_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='农事活动记录表';
```

### 2.3 作物生长记录表 (crop_growth_records)

存储作物生长情况记录。

```sql
CREATE TABLE `crop_growth_records` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `farmland_id` BIGINT UNSIGNED NOT NULL COMMENT '农田ID',
  `record_date` DATE NOT NULL COMMENT '记录日期',
  `growth_stage` VARCHAR(50) NOT NULL COMMENT '生长阶段',
  `height` DECIMAL(5,2) COMMENT '株高(cm)',
  `leaf_count` INT COMMENT '叶片数',
  `health_status` VARCHAR(50) COMMENT '健康状况',
  `notes` TEXT COMMENT '备注',
  `images` JSON COMMENT '图片URL列表',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_farmland_id` (`farmland_id`),
  KEY `idx_record_date` (`record_date`),
  KEY `idx_growth_stage` (`growth_stage`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='作物生长记录表';
```

### 2.4 病虫害记录表 (pest_disease_records)

存储病虫害发生及防治记录。

```sql
CREATE TABLE `pest_disease_records` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `farmland_id` BIGINT UNSIGNED NOT NULL COMMENT '农田ID',
  `record_date` DATE NOT NULL COMMENT '记录日期',
  `type` VARCHAR(20) NOT NULL COMMENT '类型(病害/虫害)',
  `name` VARCHAR(100) NOT NULL COMMENT '病虫害名称',
  `severity` VARCHAR(20) NOT NULL COMMENT '严重程度(轻/中/重)',
  `affected_area` DECIMAL(5,2) COMMENT '受害面积(亩)',
  `control_method` TEXT COMMENT '防治方法',
  `control_date` DATE COMMENT '防治日期',
  `control_result` TEXT COMMENT '防治效果',
  `notes` TEXT COMMENT '备注',
  `images` JSON COMMENT '图片URL列表',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_farmland_id` (`farmland_id`),
  KEY `idx_record_date` (`record_date`),
  KEY `idx_type` (`type`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='病虫害记录表';
```

### 2.5 农田环境数据表 (environmental_data)

存储农田环境监测数据。

```sql
CREATE TABLE `environmental_data` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `farmland_id` BIGINT UNSIGNED NOT NULL COMMENT '农田ID',
  `record_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
  `temperature` DECIMAL(5,2) COMMENT '温度(℃)',
  `humidity` DECIMAL(5,2) COMMENT '湿度(%)',
  `soil_moisture` DECIMAL(5,2) COMMENT '土壤湿度(%)',
  `ph_value` DECIMAL(4,2) COMMENT '土壤pH值',
  `nitrogen` DECIMAL(8,2) COMMENT '氮含量(mg/kg)',
  `phosphorus` DECIMAL(8,2) COMMENT '磷含量(mg/kg)',
  `potassium` DECIMAL(8,2) COMMENT '钾含量(mg/kg)',
  `notes` TEXT COMMENT '备注',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_farmland_id` (`farmland_id`),
  KEY `idx_record_time` (`record_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='农田环境数据表';
```

### 2.6 收获记录表 (harvest_records)

存储农作物收获记录。

```sql
CREATE TABLE `harvest_records` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `farmland_id` BIGINT UNSIGNED NOT NULL COMMENT '农田ID',
  `harvest_date` DATE NOT NULL COMMENT '收获日期',
  `yield` DECIMAL(10,2) NOT NULL COMMENT '产量(公斤)',
  `quality` VARCHAR(50) COMMENT '质量等级',
  `storage_location` VARCHAR(200) COMMENT '存储位置',
  `selling_price` DECIMAL(10,2) COMMENT '销售价格(元/公斤)',
  `sold_quantity` DECIMAL(10,2) COMMENT '已售数量(公斤)',
  `notes` TEXT COMMENT '备注',
  `images` JSON COMMENT '图片URL列表',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_farmland_id` (`farmland_id`),
  KEY `idx_harvest_date` (`harvest_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收获记录表';
```