-- 1. 创建序列
CREATE SEQUENCE doc_statistics_definition_option_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

-- 2. 创建文档统计定义选项表
CREATE TABLE "doc_statistics_definition_option" (
  -- 主键ID
  "id" int8 NOT NULL DEFAULT nextval('doc_statistics_definition_option_id_seq'::regclass),
  
  -- 实体类字段
  "doc_statistics_definition_id" int8 NOT NULL,
  "option" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "display_order" int4 NOT NULL DEFAULT 0,
  
  -- 审计字段
  "tenant_id" int8 NOT NULL,
  "delete_flag" char(1) COLLATE "pg_catalog"."default" NOT NULL DEFAULT '0',
  "create_by" varchar(32) COLLATE "pg_catalog"."default" NOT NULL DEFAULT '',
  "create_time" timestamptz(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "update_by" varchar(32) COLLATE "pg_catalog"."default",
  "update_time" timestamptz(6),
  
  -- 主键约束
  CONSTRAINT "doc_statistics_definition_option_pkey" PRIMARY KEY ("id"),
  -- 外键约束（关联到文档统计定义表）
  CONSTRAINT "fk_doc_statistics_option_definition" FOREIGN KEY ("doc_statistics_definition_id") 
    REFERENCES "doc_statistics_definition" ("id") ON DELETE CASCADE
);

-- 3. 设置表所有者
ALTER TABLE "doc_statistics_definition_option" OWNER TO "postgres";

-- 4. 表和字段注释
COMMENT ON TABLE "doc_statistics_definition_option" IS '文档统计定义选项表';

COMMENT ON COLUMN "doc_statistics_definition_option"."id" IS '主键ID';
COMMENT ON COLUMN "doc_statistics_definition_option"."doc_statistics_definition_id" IS '属性编码（关联文档统计定义表ID）';
COMMENT ON COLUMN "doc_statistics_definition_option"."option" IS '选项值（当属性类型为Checkbox、Radio、Select时使用）';
COMMENT ON COLUMN "doc_statistics_definition_option"."display_order" IS '显示顺序';
COMMENT ON COLUMN "doc_statistics_definition_option"."tenant_id" IS '租户ID';
COMMENT ON COLUMN "doc_statistics_definition_option"."delete_flag" IS '删除标志: 0-未删除,1-已删除';
COMMENT ON COLUMN "doc_statistics_definition_option"."create_by" IS '创建人';
COMMENT ON COLUMN "doc_statistics_definition_option"."create_time" IS '创建时间';
COMMENT ON COLUMN "doc_statistics_definition_option"."update_by" IS '更新人';
COMMENT ON COLUMN "doc_statistics_definition_option"."update_time" IS '更新时间';

-- -- 5. 索引设计
-- -- 按统计定义ID查询（主关联查询）
-- CREATE INDEX "idx_doc_statistics_option_defid" ON "doc_statistics_definition_option" ("doc_statistics_definition_id");
-- -- 按显示顺序查询
-- CREATE INDEX "idx_doc_statistics_option_order" ON "doc_statistics_definition_option" ("display_order");
-- -- 按租户ID查询
-- CREATE INDEX "idx_doc_statistics_option_tenant" ON "doc_statistics_definition_option" ("tenant_id");




-- 向迁移历史表插入记录
INSERT INTO __MigrationsHistory (MigrationId, ProductVersion)
VALUES ('202509011634 doc_statistics_definition_option', '1.0.0');