/*
 Navicat Premium Dump SQL

 Source Server         : yuyun-us1.stormrain.cn
 Source Server Type    : MySQL
 Source Server Version : 50744 (5.7.44)
 Source Host           : yuyun-us1.stormrain.cn:3306
 Source Schema         : mqtt_power

 Target Server Type    : MySQL
 Target Server Version : 50744 (5.7.44)
 File Encoding         : 65001

 Date: 22/05/2025 19:36:58
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for activation_code
-- ----------------------------
DROP TABLE IF EXISTS `activation_code`;
CREATE TABLE `activation_code`  (
  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `code` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '激活码字符串，确保全局唯一',
  `value` decimal(10, 2) NOT NULL COMMENT '激活码对应的面值（金额）',
  `is_used` tinyint(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT '是否已使用：0-未使用，1-已使用',
  `user_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT '使用者用户ID (如果已使用)',
  `use_time` datetime NULL DEFAULT NULL COMMENT '激活码使用时间 (如果已使用)',
  `expire_time` datetime NULL DEFAULT NULL COMMENT '激活码过期时间 (NULL表示永不过期)',
  `batch_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '生成批次号，方便管理员追踪管理',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `is_delete` tinyint(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT '逻辑删除标志：0-未删除，1-已删除',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uk_code`(`code`) USING BTREE COMMENT '激活码唯一索引',
  INDEX `idx_user_id`(`user_id`) USING BTREE COMMENT '使用者用户ID索引',
  INDEX `idx_expire_time`(`expire_time`) USING BTREE COMMENT '过期时间索引',
  INDEX `idx_batch_id`(`batch_id`) USING BTREE COMMENT '批次号索引'
) ENGINE = InnoDB AUTO_INCREMENT = 102 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '激活码表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for charging_robot
-- ----------------------------
DROP TABLE IF EXISTS `charging_robot`;
CREATE TABLE `charging_robot`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `robot_uid` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '机器人唯一标识符 (例如，硬件序列号)',
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'IDLE' COMMENT '机器人状态 (IDLE, MOVING, CHARGING, ERROR, MAINTENANCE, OFFLINE)',
  `current_location` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '当前位置描述 (例如，坐标或区域ID)',
  `battery_level` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '电池电量百分比 (0-100)',
  `current_task_id` bigint(20) NULL DEFAULT NULL COMMENT '当前执行的任务ID (关联 robot_task.id)',
  `last_heartbeat_time` datetime NULL DEFAULT NULL COMMENT '最后心跳时间',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
  `is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标志 (0:未删, 1:已删)',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `robot_uid`(`robot_uid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '充电机器人表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for charging_session
-- ----------------------------
DROP TABLE IF EXISTS `charging_session`;
CREATE TABLE `charging_session`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID，充电会话唯一标识',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID (关联 user.id)',
  `robot_id` bigint(20) NULL DEFAULT NULL COMMENT '服务的机器人ID (关联 charging_robot.id)',
  `robot_uid_snapshot` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '服务机器人的UID快照 (冗余字段，方便查询历史)',
  `spot_id` bigint(20) NOT NULL COMMENT '车位ID (关联 parking_spot.id)',
  `spot_uid_snapshot` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '车位UID快照 (冗余字段)',
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'REQUESTED' COMMENT '充电会话状态 (REQUESTED, ROBOT_ASSIGNED, ROBOT_MOVING, CHARGING_STARTED, CHARGING_IN_PROGRESS, COMPLETED, CANCELLED_BY_USER, CANCELLED_BY_SYSTEM, ERROR, ROBOT_TASK_TIMEOUT)',
  `request_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '用户请求充电时间',
  `robot_assigned_time` datetime NULL DEFAULT NULL COMMENT '机器人分配时间',
  `robot_arrival_time` datetime NULL DEFAULT NULL COMMENT '机器人到达车位时间',
  `charge_start_time` datetime NULL DEFAULT NULL COMMENT '充电开始时间',
  `charge_end_time` datetime NULL DEFAULT NULL COMMENT '充电结束时间',
  `total_duration_seconds` int(11) NULL DEFAULT NULL COMMENT '总充电时长 (秒)',
  `energy_consumed_kwh` decimal(10, 3) NULL DEFAULT NULL COMMENT '消耗电量 (kWh)',
  `cost` decimal(10, 2) NULL DEFAULT NULL COMMENT '本次充电费用 (元)',
  `payment_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT 'PENDING' COMMENT '支付状态 (PENDING, PAID, FAILED)',
  `error_code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '错误码 (如果会话出错)',
  `error_message` text CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL COMMENT '错误信息',
  `related_robot_task_id` bigint(20) NULL DEFAULT NULL COMMENT '启动本次会话或关键步骤的 RobotTask ID',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录创建时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '记录更新时间',
  `is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标志 (0:未删, 1:已删)',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '充电记录表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for mqtt_communication_log
-- ----------------------------
DROP TABLE IF EXISTS `mqtt_communication_log`;
CREATE TABLE `mqtt_communication_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '日志ID',
  `message_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '消息的唯一标识 (例如 MQTT v5 的 Message ID 或应用生成的UUID)',
  `direction` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息方向: UPSTREAM (设备->服务器), DOWNSTREAM (服务器->设备)',
  `client_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '相关的客户端ID',
  `topic` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'MQTT 主题',
  `payload_format` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT 'TEXT' COMMENT 'Payload 格式 (TEXT, JSON, BINARY)',
  `payload` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '消息原文 (Payload)',
  `qos` tinyint(4) NULL DEFAULT NULL COMMENT '消息QoS级别',
  `is_retained` tinyint(1) NULL DEFAULT NULL COMMENT '是否为保留消息',
  `log_timestamp` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '日志记录时间戳 (精确到毫秒)',
  `backend_processing_status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '后端处理状态 (RECEIVED, PROCESSING, SUCCESS, FAILED)',
  `backend_processing_info` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '后端处理附加信息 (如错误信息、关联业务ID)',
  `related_session_id` bigint(20) NULL DEFAULT NULL COMMENT '关联的充电会话ID (如果适用)',
  `related_task_id` bigint(20) NULL DEFAULT NULL COMMENT '关联的机器人任务ID (如果适用)',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_log_timestamp`(`log_timestamp`) USING BTREE,
  INDEX `idx_topic`(`topic`(255)) USING BTREE,
  INDEX `idx_client_id`(`client_id`) USING BTREE,
  INDEX `idx_related_session_id`(`related_session_id`) USING BTREE,
  INDEX `idx_related_task_id`(`related_task_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = 'MQTT通信日志表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for parking_spot
-- ----------------------------
DROP TABLE IF EXISTS `parking_spot`;
CREATE TABLE `parking_spot`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `spot_uid` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '车位唯一标识符 (例如，车位号)',
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'AVAILABLE' COMMENT '车位状态 (AVAILABLE, OCCUPIED_BY_CAR, RESERVED, CHARGING, MAINTENANCE, UNAVAILABLE)',
  `location_description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '位置描述 (例如，楼层、区域)',
  `robot_assignable` tinyint(1) NULL DEFAULT 1 COMMENT '此车位是否可指派机器人前往',
  `current_session_id` bigint(20) NULL DEFAULT NULL COMMENT '当前占用此车位的充电会话ID (关联 charging_session.id)',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
  `is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标志 (0:未删, 1:已删)',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `spot_uid`(`spot_uid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '车位表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for robot_task
-- ----------------------------
DROP TABLE IF EXISTS `robot_task`;
CREATE TABLE `robot_task`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `robot_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '机器人ID',
  `command_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '命令类型 (MOVE_TO_SPOT, START_CHARGE, STOP_CHARGE, QUERY_STATUS)',
  `command_payload` text CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL COMMENT '命令参数 (JSON格式)',
  `status` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'PENDING' COMMENT '任务状态 (PENDING, SENT, ACKNOWLEDGED_SUCCESS, ACKNOWLEDGED_FAILURE, TIMED_OUT)',
  `sent_time` datetime NULL DEFAULT NULL COMMENT '命令发送时间',
  `ack_time` datetime NULL DEFAULT NULL COMMENT '命令确认时间',
  `related_session_id` bigint(20) NULL DEFAULT NULL COMMENT '关联的充电会话ID (可选)',
  `error_message` text CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL COMMENT '失败或超时的错误信息',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `is_delete` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标志 (0:未删, 1:已删)',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `idx_robot_status`(`robot_id`, `status`) USING BTREE COMMENT '机器人和状态索引，便于查询'
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '机器人指令任务表' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '用户名',
  `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT '密码 (加密存储)',
  `role` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL DEFAULT 'user' COMMENT '角色 (user/admin)',
  `balance` decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '账户余额',
  `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `is_deleted` tinyint(1) NULL DEFAULT 0 COMMENT '逻辑删除标志 (0:未删, 1:已删)',
  `isDeleted` tinyint(1) NULL DEFAULT 0 COMMENT '是否删除(0-未删, 1-已删)',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `username`(`username`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci COMMENT = '用户表' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;
