-- KaMiXiTong MySQL数据库初始化脚本
-- 创建所有必要的表结构和初始数据

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 删除已存在的表（按依赖关系逆序）
DROP TABLE IF EXISTS `api_key`;
DROP TABLE IF EXISTS `api_version`;
DROP TABLE IF EXISTS `api`;
DROP TABLE IF EXISTS `order`;
DROP TABLE IF EXISTS `package`;
DROP TABLE IF EXISTS `audit_log`;
DROP TABLE IF EXISTS `ticket`;
DROP TABLE IF EXISTS `device`;
DROP TABLE IF EXISTS `license`;
DROP TABLE IF EXISTS `version`;
DROP TABLE IF EXISTS `product`;
DROP TABLE IF EXISTS `admin`;

-- ----------------------------
-- Table structure for admin
-- ----------------------------
CREATE TABLE `admin` (
  `admin_id` int NOT NULL AUTO_INCREMENT COMMENT '管理员ID',
  `username` varchar(32) NOT NULL COMMENT '用户名',
  `password_hash` varchar(128) NOT NULL COMMENT '密码哈希',
  `email` varchar(64) DEFAULT NULL COMMENT '邮箱',
  `role` int NOT NULL DEFAULT '0' COMMENT '角色: 0=普通管理员, 1=超级管理员',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=正常',
  `is_deleted` int NOT NULL DEFAULT '0' COMMENT '软删除: 0=未删除, 1=已删除',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
  `last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
  `last_login_ip` varchar(32) DEFAULT NULL COMMENT '最后登录IP',
  PRIMARY KEY (`admin_id`),
  UNIQUE KEY `uk_username` (`username`),
  KEY `ix_admin_is_deleted` (`is_deleted`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='管理员表';

-- ----------------------------
-- Table structure for product
-- ----------------------------
CREATE TABLE `product` (
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `product_name` varchar(64) NOT NULL COMMENT '产品名称',
  `description` text COMMENT '产品描述',
  `image_path` varchar(255) DEFAULT NULL COMMENT '产品图片路径',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=启用',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='产品表';

-- ----------------------------
-- Table structure for version
-- ----------------------------
CREATE TABLE `version` (
  `version_id` int NOT NULL AUTO_INCREMENT COMMENT '版本ID',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `version_num` varchar(16) NOT NULL COMMENT '版本号',
  `platform` varchar(32) DEFAULT NULL COMMENT '平台信息',
  `description` text COMMENT '版本描述',
  `update_log` text COMMENT '更新日志',
  `download_url` varchar(255) DEFAULT NULL COMMENT '下载URL',
  `min_license_version` varchar(16) DEFAULT NULL COMMENT '最低卡密版本要求',
  `force_update` int NOT NULL DEFAULT '0' COMMENT '强制更新: 0=否, 1=是',
  `download_status` int NOT NULL DEFAULT '1' COMMENT '下载状态: 0=禁用, 1=启用',
  `publish_status` int NOT NULL DEFAULT '0' COMMENT '发布状态: 0=草稿, 1=已发布, 2=已回滚',
  `file_hash` varchar(64) DEFAULT NULL COMMENT '文件SHA256哈希值',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`version_id`),
  UNIQUE KEY `uk_product_version` (`product_id`,`version_num`),
  CONSTRAINT `fk_version_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版本表';

-- ----------------------------
-- Table structure for license
-- ----------------------------
CREATE TABLE `license` (
  `license_id` int NOT NULL AUTO_INCREMENT COMMENT '卡密ID',
  `license_key` varchar(35) NOT NULL COMMENT '卡密key',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `type` int NOT NULL DEFAULT '1' COMMENT '类型: 0=试用, 1=正式',
  `status` int NOT NULL DEFAULT '0' COMMENT '状态: 0=未激活, 1=已激活, 2=已过期, 3=已禁用',
  `valid_days` int NOT NULL COMMENT '有效期(天)',
  `bind_machine_code` varchar(64) DEFAULT NULL COMMENT '绑定的机器码',
  `activate_time` datetime DEFAULT NULL COMMENT '激活时间',
  `expire_time` datetime DEFAULT NULL COMMENT '过期时间',
  `last_verify_time` datetime DEFAULT NULL COMMENT '最后验证时间',
  `unbind_count` int DEFAULT '0' COMMENT '解绑次数',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`license_id`),
  UNIQUE KEY `uk_license_key` (`license_key`),
  KEY `ix_license_key` (`license_key`),
  CONSTRAINT `fk_license_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='卡密表';

-- ----------------------------
-- Table structure for device
-- ----------------------------
CREATE TABLE `device` (
  `device_id` int NOT NULL AUTO_INCREMENT COMMENT '设备ID',
  `machine_code` varchar(64) NOT NULL COMMENT '机器码',
  `license_id` int DEFAULT NULL COMMENT '卡密ID',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `software_version` varchar(16) DEFAULT NULL COMMENT '软件版本',
  `ip_address` varchar(45) DEFAULT NULL COMMENT 'IP地址',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=正常',
  `activate_time` datetime DEFAULT NULL COMMENT '激活时间',
  `last_verify_time` datetime DEFAULT NULL COMMENT '最后验证时间',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`device_id`),
  UNIQUE KEY `uk_machine_code` (`machine_code`),
  KEY `ix_device_machine_code` (`machine_code`),
  CONSTRAINT `fk_device_license` FOREIGN KEY (`license_id`) REFERENCES `license` (`license_id`) ON DELETE SET NULL,
  CONSTRAINT `fk_device_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='设备表';

-- ----------------------------
-- Table structure for ticket
-- ----------------------------
CREATE TABLE `ticket` (
  `ticket_id` int NOT NULL AUTO_INCREMENT COMMENT '工单ID',
  `title` varchar(128) NOT NULL COMMENT '标题',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `software_version` varchar(16) DEFAULT NULL COMMENT '软件版本',
  `machine_code` varchar(64) DEFAULT NULL COMMENT '机器码',
  `license_key` varchar(32) DEFAULT NULL COMMENT '卡密',
  `description` text NOT NULL COMMENT '描述',
  `priority` int NOT NULL DEFAULT '1' COMMENT '优先级: 0=低, 1=中, 2=高',
  `status` int NOT NULL DEFAULT '0' COMMENT '状态: 0=待处理, 1=处理中, 2=已解决, 3=已关闭',
  `operator` varchar(32) DEFAULT NULL COMMENT '处理人',
  `remark` text COMMENT '处理备注',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `resolve_time` datetime DEFAULT NULL COMMENT '解决时间',
  `close_time` datetime DEFAULT NULL COMMENT '关闭时间',
  PRIMARY KEY (`ticket_id`),
  CONSTRAINT `fk_ticket_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='工单表';

-- ----------------------------
-- Table structure for audit_log
-- ----------------------------
CREATE TABLE `audit_log` (
  `log_id` int NOT NULL AUTO_INCREMENT COMMENT '日志ID',
  `admin_id` int NOT NULL COMMENT '管理员ID',
  `action` varchar(32) NOT NULL COMMENT '操作类型',
  `target_type` varchar(32) NOT NULL COMMENT '目标类型',
  `target_id` varchar(32) DEFAULT NULL COMMENT '目标ID',
  `details` text COMMENT '操作详情',
  `ip_address` varchar(32) DEFAULT NULL COMMENT 'IP地址',
  `user_agent` varchar(256) DEFAULT NULL COMMENT '用户代理',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`log_id`),
  KEY `ix_audit_log_admin_id` (`admin_id`),
  KEY `ix_audit_log_action` (`action`),
  KEY `ix_audit_log_create_time` (`create_time`),
  CONSTRAINT `fk_audit_log_admin` FOREIGN KEY (`admin_id`) REFERENCES `admin` (`admin_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审计日志表';

-- ----------------------------
-- Table structure for package
-- ----------------------------
CREATE TABLE `package` (
  `package_id` varchar(64) NOT NULL COMMENT '套餐ID',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `name` varchar(64) NOT NULL COMMENT '套餐名称',
  `description` text COMMENT '套餐描述',
  `price` float NOT NULL COMMENT '价格',
  `duration` int NOT NULL COMMENT '时长(天)',
  `max_devices` int NOT NULL DEFAULT '1' COMMENT '最大设备数',
  `stock` int NOT NULL DEFAULT '-1' COMMENT '库存',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=启用',
  `sort_order` int NOT NULL DEFAULT '0' COMMENT '排序',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`package_id`),
  CONSTRAINT `fk_package_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='套餐表';

-- ----------------------------
-- Table structure for order
-- ----------------------------
CREATE TABLE `order` (
  `order_id` int NOT NULL AUTO_INCREMENT COMMENT '订单ID',
  `order_number` varchar(32) NOT NULL COMMENT '订单号',
  `product_id` varchar(32) NOT NULL COMMENT '产品ID',
  `package_id` varchar(64) NOT NULL COMMENT '套餐ID',
  `contact_person` varchar(64) NOT NULL COMMENT '联系人',
  `phone` varchar(20) NOT NULL COMMENT '手机号',
  `quantity` int NOT NULL DEFAULT '1' COMMENT '数量',
  `amount` float NOT NULL COMMENT '金额',
  `status` int NOT NULL DEFAULT '0' COMMENT '状态: 0=待支付, 1=已支付, 2=已取消, 3=已完成',
  `payment_method` varchar(20) DEFAULT NULL COMMENT '支付方式',
  `payment_time` datetime DEFAULT NULL COMMENT '支付时间',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`order_id`),
  UNIQUE KEY `uk_order_number` (`order_number`),
  KEY `ix_order_order_number` (`order_number`),
  CONSTRAINT `fk_order_product` FOREIGN KEY (`product_id`) REFERENCES `product` (`product_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='订单表';

-- ----------------------------
-- Table structure for api
-- ----------------------------
CREATE TABLE `api` (
  `api_id` varchar(32) NOT NULL COMMENT 'API ID',
  `api_name` varchar(64) NOT NULL COMMENT 'API名称',
  `description` text COMMENT '描述',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=启用',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`api_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='API表';

-- ----------------------------
-- Table structure for api_version
-- ----------------------------
CREATE TABLE `api_version` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `version_num` varchar(32) NOT NULL COMMENT '版本号',
  `api_id` varchar(32) NOT NULL COMMENT 'API ID',
  `description` text COMMENT '描述',
  `publish_status` int NOT NULL DEFAULT '0' COMMENT '发布状态: 0=草稿, 1=已发布',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_api_version_api` FOREIGN KEY (`api_id`) REFERENCES `api` (`api_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='API版本表';

-- ----------------------------
-- Table structure for api_key
-- ----------------------------
CREATE TABLE `api_key` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `key` varchar(64) NOT NULL COMMENT '密钥',
  `api_id` varchar(32) NOT NULL COMMENT 'API ID',
  `name` varchar(64) NOT NULL COMMENT '名称',
  `description` text COMMENT '描述',
  `status` int NOT NULL DEFAULT '1' COMMENT '状态: 0=禁用, 1=启用',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `expire_time` datetime DEFAULT NULL COMMENT '过期时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_api_key` (`key`),
  KEY `ix_api_key_api_id` (`api_id`),
  CONSTRAINT `fk_api_key_api` FOREIGN KEY (`api_id`) REFERENCES `api` (`api_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='API密钥表';

-- ----------------------------
-- 插入初始数据
-- ----------------------------

-- 插入默认管理员账户 (admin/admin123)
INSERT INTO `admin` (`username`, `password_hash`, `email`, `role`, `status`) VALUES 
('admin', '$pbkdf2-sha256$29000$N2aBd1I5Eaz5bYY2CXbu2A$1lEXwDoX9S5slrv0cFHsQ8fAj55m43.1mPbX5f.Ra0U', 'admin@example.com', 1, 1);

-- 插入默认产品
INSERT INTO `product` (`product_id`, `product_name`, `description`, `status`) VALUES 
('PROD_DEFAULT', '默认产品', '系统默认产品', 1);

-- 插入默认套餐
INSERT INTO `package` (`package_id`, `product_id`, `name`, `description`, `price`, `duration`, `max_devices`, `status`) VALUES 
('PKG_DEFAULT', 'PROD_DEFAULT', '默认套餐', '系统默认套餐', 0, 365, 1, 1);

SET FOREIGN_KEY_CHECKS = 1;
