-- 云印享 - 付费打印服务系统数据库初始化脚本
-- 数据库: cloudprint

CREATE DATABASE IF NOT EXISTS `cloudprint` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `cloudprint`;

-- 用户表
CREATE TABLE IF NOT EXISTS `user` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `openid` VARCHAR(64) UNIQUE NOT NULL COMMENT '微信OpenID',
    `nickname` VARCHAR(128) DEFAULT '' COMMENT '昵称',
    `avatar` VARCHAR(512) DEFAULT '' COMMENT '头像URL',
    `balance` DECIMAL(10,2) DEFAULT 0.00 COMMENT '余额',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX `idx_openid` (`openid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 管理员表
CREATE TABLE IF NOT EXISTS `admin` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(64) UNIQUE NOT NULL COMMENT '用户名',
    `password_hash` VARCHAR(256) NOT NULL COMMENT '密码哈希',
    `nickname` VARCHAR(128) DEFAULT '' COMMENT '昵称',
    `role` TINYINT DEFAULT 1 COMMENT '1:普通管理员 2:超级管理员',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员表';

-- 打印机表
CREATE TABLE IF NOT EXISTS `printer` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(128) UNIQUE NOT NULL COMMENT '系统打印机名',
    `display_name` VARCHAR(128) NOT NULL COMMENT '显示名称',
    `api_key` VARCHAR(64) UNIQUE NOT NULL COMMENT '客户端认证Key',
    `is_active` TINYINT DEFAULT 1 COMMENT '是否启用',
    `status` TINYINT DEFAULT 0 COMMENT '0:空闲 1:打印中 2:离线',
    `last_heartbeat` DATETIME DEFAULT NULL COMMENT '最后心跳时间',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_name` (`name`),
    INDEX `idx_api_key` (`api_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='打印机表';

-- 订单表
CREATE TABLE IF NOT EXISTS `order` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `order_no` VARCHAR(32) UNIQUE NOT NULL COMMENT '订单号',
    `user_id` INT NOT NULL COMMENT '用户ID',
    `status` TINYINT DEFAULT 0 COMMENT '0:待支付 1:已支付 2:打印中 3:已完成 4:已取消',
    `total_amount` DECIMAL(10,2) NOT NULL COMMENT '总金额',
    `remark` VARCHAR(512) DEFAULT '' COMMENT '备注',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `paid_at` DATETIME DEFAULT NULL COMMENT '支付时间',
    INDEX `idx_order_no` (`order_no`),
    INDEX `idx_user_id` (`user_id`),
    INDEX `idx_status` (`status`),
    INDEX `idx_created_at` (`created_at`),
    FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单表';

-- 订单明细表
CREATE TABLE IF NOT EXISTS `order_item` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `order_id` INT NOT NULL COMMENT '订单ID',
    `file_id` INT NOT NULL COMMENT '文件ID',
    `printer_id` INT DEFAULT NULL COMMENT '打印机ID',
    `copies` INT DEFAULT 1 COMMENT '份数',
    `color` TINYINT DEFAULT 0 COMMENT '是否彩色 0:黑白 1:彩色',
    `duplex` TINYINT DEFAULT 0 COMMENT '是否双面 0:单面 1:双面',
    `page_range` VARCHAR(64) DEFAULT 'all' COMMENT '页面范围',
    `price` DECIMAL(10,2) NOT NULL COMMENT '价格',
    INDEX `idx_order_id` (`order_id`),
    INDEX `idx_file_id` (`file_id`),
    FOREIGN KEY (`order_id`) REFERENCES `order`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`file_id`) REFERENCES `file`(`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单明细表';

-- 文件表
CREATE TABLE IF NOT EXISTS `file` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `file_name` VARCHAR(256) NOT NULL COMMENT '文件名',
    `file_path` VARCHAR(512) NOT NULL COMMENT '文件路径',
    `file_type` VARCHAR(32) NOT NULL COMMENT '文件类型',
    `file_size` BIGINT NOT NULL COMMENT '文件大小(字节)',
    `uploader_type` VARCHAR(16) NOT NULL COMMENT '上传者类型: user/admin/system',
    `uploader_id` INT NOT NULL COMMENT '上传者ID',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_uploader` (`uploader_type`, `uploader_id`),
    INDEX `idx_file_type` (`file_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文件表';

-- 文库分类表
CREATE TABLE IF NOT EXISTS `library_category` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(128) NOT NULL COMMENT '分类名称',
    `icon` VARCHAR(128) DEFAULT '' COMMENT '图标',
    `sort_order` INT DEFAULT 0 COMMENT '排序',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文库分类表';

-- 文库文件表
CREATE TABLE IF NOT EXISTS `library_file` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `category_id` INT NOT NULL COMMENT '分类ID',
    `file_id` INT NOT NULL COMMENT '文件ID',
    `title` VARCHAR(256) NOT NULL COMMENT '标题',
    `description` VARCHAR(512) DEFAULT '' COMMENT '描述',
    `page_count` INT DEFAULT 0 COMMENT '页数',
    `download_count` INT DEFAULT 0 COMMENT '下载次数',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_category_id` (`category_id`),
    INDEX `idx_file_id` (`file_id`),
    FOREIGN KEY (`category_id`) REFERENCES `library_category`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`file_id`) REFERENCES `file`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='文库文件表';

-- 打印任务表
CREATE TABLE IF NOT EXISTS `print_job` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `job_no` VARCHAR(32) UNIQUE NOT NULL COMMENT '任务号',
    `printer_id` INT NOT NULL COMMENT '打印机ID',
    `order_item_id` INT NOT NULL COMMENT '订单明细ID',
    `status` VARCHAR(16) DEFAULT 'pending' COMMENT 'pending/printing/completed/failed/cancelled',
    `progress` INT DEFAULT 0 COMMENT '进度(0-100)',
    `error_msg` VARCHAR(512) DEFAULT '' COMMENT '错误信息',
    `started_at` DATETIME DEFAULT NULL COMMENT '开始时间',
    `completed_at` DATETIME DEFAULT NULL COMMENT '完成时间',
    INDEX `idx_job_no` (`job_no`),
    INDEX `idx_printer_id` (`printer_id`),
    INDEX `idx_status` (`status`),
    FOREIGN KEY (`printer_id`) REFERENCES `printer`(`id`) ON DELETE RESTRICT,
    FOREIGN KEY (`order_item_id`) REFERENCES `order_item`(`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='打印任务表';

-- 价目表
CREATE TABLE IF NOT EXISTS `price_list` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(128) NOT NULL COMMENT '项目名称',
    `type` VARCHAR(32) NOT NULL COMMENT '纸张类型: A4/A3/照片纸',
    `price` DECIMAL(10,2) NOT NULL COMMENT '单价',
    `unit` VARCHAR(16) NOT NULL COMMENT '单位: 页/张/份',
    `color_enabled` TINYINT DEFAULT 1 COMMENT '是否支持彩色',
    `is_active` TINYINT DEFAULT 1 COMMENT '是否启用',
    `sort_order` INT DEFAULT 0 COMMENT '排序',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_type` (`type`),
    INDEX `idx_is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='价目表';

-- 支付记录表
CREATE TABLE IF NOT EXISTS `payment` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `order_no` VARCHAR(32) UNIQUE NOT NULL COMMENT '订单号',
    `pay_type` VARCHAR(16) DEFAULT 'wechat' COMMENT '支付类型: wechat/balance',
    `pay_status` TINYINT DEFAULT 0 COMMENT '0:待支付 1:支付中 2:成功 3:失败',
    `transaction_id` VARCHAR(64) DEFAULT '' COMMENT '微信交易号',
    `paid_at` DATETIME DEFAULT NULL COMMENT '支付时间',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX `idx_order_no` (`order_no`),
    INDEX `idx_pay_status` (`pay_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支付记录表';

-- 初始化管理员账号 (密码: admin123)
-- 密码哈希使用 bcrypt 生成
INSERT INTO `admin` (`username`, `password_hash`, `nickname`, `role`) VALUES
('admin', '$2a$10$N9qo8uLOickgx2ZMRZoMy.MqrqJlmZ2j3.N9qo8uLOickgx2ZMRZoG', '系统管理员', 2);

-- 初始化默认价目表
INSERT INTO `price_list` (`name`, `type`, `price`, `unit`, `color_enabled`, `is_active`, `sort_order`) VALUES
('A4黑白打印', 'A4', 0.10, '页', 0, 1, 1),
('A4彩色打印', 'A4', 0.30, '页', 1, 1, 2),
('A3黑白打印', 'A3', 0.20, '页', 0, 1, 3),
('A3彩色打印', 'A3', 0.60, '页', 1, 1, 4),
('照片打印', '照片纸', 2.00, '张', 1, 1, 5);

-- 初始化默认文库分类
INSERT INTO `library_category` (`name`, `icon`, `sort_order`) VALUES
('学习资料', 'book', 1),
('考试真题', 'exam', 2),
('简历模板', 'resume', 3),
('合同协议', 'contract', 4),
('其他资料', 'other', 5);
