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

-- 创建数据库（通常需要超级用户权限，也可以手动创建）
-- CREATE DATABASE cloudprint WITH ENCODING = 'UTF8';
-- \c cloudprint

-- 用户表
CREATE TABLE IF NOT EXISTS "user" (
    "id" SERIAL PRIMARY KEY,
    "openid" VARCHAR(64) UNIQUE NOT NULL,
    "nickname" VARCHAR(128) DEFAULT '',
    "avatar" VARCHAR(512) DEFAULT '',
    "balance" DECIMAL(10,2) DEFAULT 0.00,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_user_openid" ON "user" ("openid");

-- 管理员表
CREATE TABLE IF NOT EXISTS "admin" (
    "id" SERIAL PRIMARY KEY,
    "username" VARCHAR(64) UNIQUE NOT NULL,
    "password_hash" VARCHAR(256) NOT NULL,
    "nickname" VARCHAR(128) DEFAULT '',
    "role" SMALLINT DEFAULT 1,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_admin_username" ON "admin" ("username");

-- 打印机表
CREATE TABLE IF NOT EXISTS "printer" (
    "id" SERIAL PRIMARY KEY,
    "name" VARCHAR(128) UNIQUE NOT NULL,
    "display_name" VARCHAR(128) NOT NULL,
    "api_key" VARCHAR(64) UNIQUE NOT NULL,
    "is_active" BOOLEAN DEFAULT TRUE,
    "status" SMALLINT DEFAULT 0,
    "last_heartbeat" TIMESTAMP DEFAULT NULL,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_printer_name" ON "printer" ("name");
CREATE INDEX IF NOT EXISTS "idx_printer_api_key" ON "printer" ("api_key");

-- 文件表
CREATE TABLE IF NOT EXISTS "file" (
    "id" SERIAL PRIMARY KEY,
    "file_name" VARCHAR(256) NOT NULL,
    "file_path" VARCHAR(512) NOT NULL,
    "file_type" VARCHAR(32) NOT NULL,
    "file_size" BIGINT NOT NULL,
    "uploader_type" VARCHAR(16) NOT NULL,
    "uploader_id" INTEGER NOT NULL,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_file_uploader" ON "file" ("uploader_type", "uploader_id");
CREATE INDEX IF NOT EXISTS "idx_file_type" ON "file" ("file_type");

-- 订单表
CREATE TABLE IF NOT EXISTS "order" (
    "id" SERIAL PRIMARY KEY,
    "order_no" VARCHAR(32) UNIQUE NOT NULL,
    "user_id" INTEGER NOT NULL,
    "status" SMALLINT DEFAULT 0,
    "total_amount" DECIMAL(10,2) NOT NULL,
    "remark" VARCHAR(512) DEFAULT '',
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "paid_at" TIMESTAMP DEFAULT NULL,
    FOREIGN KEY ("user_id") REFERENCES "user"("id") ON DELETE RESTRICT
);
CREATE INDEX IF NOT EXISTS "idx_order_order_no" ON "order" ("order_no");
CREATE INDEX IF NOT EXISTS "idx_order_user_id" ON "order" ("user_id");
CREATE INDEX IF NOT EXISTS "idx_order_status" ON "order" ("status");
CREATE INDEX IF NOT EXISTS "idx_order_created_at" ON "order" ("created_at");

-- 订单明细表
CREATE TABLE IF NOT EXISTS "order_item" (
    "id" SERIAL PRIMARY KEY,
    "order_id" INTEGER NOT NULL,
    "file_id" INTEGER NOT NULL,
    "printer_id" INTEGER DEFAULT NULL,
    "copies" INTEGER DEFAULT 1,
    "color" BOOLEAN DEFAULT FALSE,
    "duplex" BOOLEAN DEFAULT FALSE,
    "page_range" VARCHAR(64) DEFAULT 'all',
    "price" DECIMAL(10,2) NOT NULL,
    FOREIGN KEY ("order_id") REFERENCES "order"("id") ON DELETE CASCADE,
    FOREIGN KEY ("file_id") REFERENCES "file"("id") ON DELETE RESTRICT
);
CREATE INDEX IF NOT EXISTS "idx_order_item_order_id" ON "order_item" ("order_id");
CREATE INDEX IF NOT EXISTS "idx_order_item_file_id" ON "order_item" ("file_id");

-- 文库分类表
CREATE TABLE IF NOT EXISTS "library_category" (
    "id" SERIAL PRIMARY KEY,
    "name" VARCHAR(128) NOT NULL,
    "icon" VARCHAR(128) DEFAULT '',
    "sort_order" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 文库文件表
CREATE TABLE IF NOT EXISTS "library_file" (
    "id" SERIAL PRIMARY KEY,
    "category_id" INTEGER NOT NULL,
    "file_id" INTEGER NOT NULL,
    "title" VARCHAR(256) NOT NULL,
    "description" VARCHAR(512) DEFAULT '',
    "page_count" INTEGER DEFAULT 0,
    "download_count" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY ("category_id") REFERENCES "library_category"("id") ON DELETE CASCADE,
    FOREIGN KEY ("file_id") REFERENCES "file"("id") ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS "idx_library_file_category_id" ON "library_file" ("category_id");
CREATE INDEX IF NOT EXISTS "idx_library_file_file_id" ON "library_file" ("file_id");

-- 打印任务表
CREATE TABLE IF NOT EXISTS "print_job" (
    "id" SERIAL PRIMARY KEY,
    "job_no" VARCHAR(32) UNIQUE NOT NULL,
    "printer_id" INTEGER NOT NULL,
    "order_item_id" INTEGER NOT NULL,
    "status" VARCHAR(16) DEFAULT 'pending',
    "progress" INTEGER DEFAULT 0,
    "error_msg" VARCHAR(512) DEFAULT '',
    "started_at" TIMESTAMP DEFAULT NULL,
    "completed_at" TIMESTAMP DEFAULT NULL,
    FOREIGN KEY ("printer_id") REFERENCES "printer"("id") ON DELETE RESTRICT,
    FOREIGN KEY ("order_item_id") REFERENCES "order_item"("id") ON DELETE RESTRICT
);
CREATE INDEX IF NOT EXISTS "idx_print_job_job_no" ON "print_job" ("job_no");
CREATE INDEX IF NOT EXISTS "idx_print_job_printer_id" ON "print_job" ("printer_id");
CREATE INDEX IF NOT EXISTS "idx_print_job_status" ON "print_job" ("status");

-- 价目表
CREATE TABLE IF NOT EXISTS "price_list" (
    "id" SERIAL PRIMARY KEY,
    "name" VARCHAR(128) NOT NULL,
    "type" VARCHAR(32) NOT NULL,
    "price" DECIMAL(10,2) NOT NULL,
    "unit" VARCHAR(16) NOT NULL,
    "color_enabled" BOOLEAN DEFAULT TRUE,
    "is_active" BOOLEAN DEFAULT TRUE,
    "sort_order" INTEGER DEFAULT 0,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_price_list_type" ON "price_list" ("type");
CREATE INDEX IF NOT EXISTS "idx_price_list_is_active" ON "price_list" ("is_active");

-- 支付记录表
CREATE TABLE IF NOT EXISTS "payment" (
    "id" SERIAL PRIMARY KEY,
    "order_no" VARCHAR(32) UNIQUE NOT NULL,
    "pay_type" VARCHAR(16) DEFAULT 'wechat',
    "pay_status" SMALLINT DEFAULT 0,
    "transaction_id" VARCHAR(64) DEFAULT '',
    "paid_at" TIMESTAMP DEFAULT NULL,
    "created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_payment_order_no" ON "payment" ("order_no");
CREATE INDEX IF NOT EXISTS "idx_payment_pay_status" ON "payment" ("pay_status");

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

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

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