-- 达梦数据库测试环境初始化脚本
-- Schema: TEST（在 config.json 中配置 "dm": {"name": "TEST", ...}）
-- 执行前确保已切换到 TEST Schema，或以 SYSDBA 身份执行
-- 连接串示例: dm://SYSDBA:SC_sysdba2026@172.31.18.249:5236?schema=TEST

-- ===================== DDL =====================

CREATE TABLE "admin" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "name" VARCHAR(100),
    "phone" VARCHAR(20),
    "state" INT DEFAULT 0,
    "password" VARCHAR(100),
    "role_id" INT DEFAULT 0,
    "title" VARCHAR(100),
    "create_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "modify_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX "uk_admin_phone" ON "admin" ("phone");

CREATE TABLE "ctg" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "name" VARCHAR(100),
    "state" INT DEFAULT 0,
    "create_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "modify_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE "article" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "title" VARCHAR(200),
    "author" VARCHAR(100),
    "content" CLOB,
    "state" INT DEFAULT 0,
    "click_num" INT DEFAULT 0,
    "sort" INT DEFAULT 0,
    "img" VARCHAR(500),
    "ctg_id" INT DEFAULT 0,
    "admin_id" INT DEFAULT 0,
    "create_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    "modify_time" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE "test_batch" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "name" VARCHAR(100),
    "title" VARCHAR(200),
    "state" INT DEFAULT 0,
    "create_time" TIMESTAMP
);

CREATE TABLE "cached" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "key" VARCHAR(64),
    "value" CLOB,
    "endtime" BIGINT,
    "time" BIGINT
);

CREATE TABLE "hotime_cache" (
    "id" INT IDENTITY(1,1) PRIMARY KEY,
    "key" VARCHAR(64) NOT NULL,
    "value" TEXT,
    "end_time" TIMESTAMP,
    "state" INT DEFAULT 0,
    "create_time" TIMESTAMP,
    "modify_time" TIMESTAMP,
    CONSTRAINT "uk_hotime_cache_key" UNIQUE ("key")
);
CREATE INDEX "idx_hotime_cache_end_time" ON "hotime_cache" ("end_time");

-- ===================== DML 种子数据 =====================

INSERT INTO "ctg" ("name","state","create_time","modify_time") VALUES ('新闻资讯', 0, NOW(), NOW());
INSERT INTO "ctg" ("name","state","create_time","modify_time") VALUES ('技术文档', 0, NOW(), NOW());
INSERT INTO "ctg" ("name","state","create_time","modify_time") VALUES ('公告通知', 0, NOW(), NOW());
INSERT INTO "ctg" ("name","state","create_time","modify_time") VALUES ('产品介绍', 0, NOW(), NOW());
INSERT INTO "ctg" ("name","state","create_time","modify_time") VALUES ('已归档',   1, NOW(), NOW());

INSERT INTO "admin" ("name","phone","state","password","role_id","title","create_time","modify_time") VALUES ('超级管理员','13800000001',1,'admin123', 1,'系统管理员',NOW(),NOW());
INSERT INTO "admin" ("name","phone","state","password","role_id","title","create_time","modify_time") VALUES ('编辑员',    '13800000002',1,'editor123',2,'内容编辑',  NOW(),NOW());
INSERT INTO "admin" ("name","phone","state","password","role_id","title","create_time","modify_time") VALUES ('审核员',    '13800000003',1,'review123',3,'内容审核',  NOW(),NOW());

INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('数据库入门指南',        '管理员','国产关系型数据库管理系统入门...',    0, 128,  10,1,1,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('新闻发布系统上线公告',  '编辑员','本系统已正式上线运行...',            0, 256,   5,1,2,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('Go语言最佳实践',        '管理员','Go语言在后端开发中的最佳实践总结...',0, 512,   8,2,1,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('API接口设计规范',       '审核员','RESTful API设计的基本规范和标准...', 0,  64,  15,2,3,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('系统维护通知',          '管理员','系统将于本周日凌晨进行维护升级...',  0,  32,   1,3,1,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('产品功能更新说明',      '编辑员','新版本增加了以下功能...',             0,  96,  12,4,2,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('数据库性能优化指南',    '管理员','数据库性能优化的常见方法和技巧...',  0,1024,   3,2,1,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('缓存机制深入分析',      '审核员','缓存在Web应用中的重要作用...',        0, 200,   7,2,3,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('旧版公告（已归档）',    '管理员','此公告已过期归档...',                 1,  10,   0,5,1,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('测试文章（隐藏）',      '编辑员','测试用文章不公开显示...',             2,   0,   0,1,2,NOW(),NOW());
INSERT INTO "article" ("title","author","content","state","click_num","sort","ctg_id","admin_id","create_time","modify_time") VALUES ('高点击量文章',          '编辑员','这是一篇点击量很高的文章...',         0,9999,   2,1,2,NOW(),NOW());
