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

-- 2. 创建文档统计定义表
CREATE TABLE "public"."doc_statistics_definition" (
  -- 主键ID
  "id" int8 NOT NULL DEFAULT nextval('doc_statistics_definition_id_seq'::regclass),
  
  -- 实体类字段
  "name" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "type_enum" int4 NOT NULL,
  "code" varchar(100) COLLATE "pg_catalog"."default" NOT NULL,
  "required" int4 NOT NULL DEFAULT 0,
  "remark" text COLLATE "pg_catalog"."default",
  "is_statistics" int4 NOT NULL DEFAULT 0,
  "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_pkey" PRIMARY KEY ("id")
);

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

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

COMMENT ON COLUMN "public"."doc_statistics_definition"."id" IS '主键ID';
COMMENT ON COLUMN "public"."doc_statistics_definition"."name" IS '属性名称';
COMMENT ON COLUMN "public"."doc_statistics_definition"."type_enum" IS '属性类型: 1-Input, 2-Date, 3-Checkbox, 4-Radio, 5-Select, 6-Textarea';
COMMENT ON COLUMN "public"."doc_statistics_definition"."code" IS '属性代码';
COMMENT ON COLUMN "public"."doc_statistics_definition"."required" IS '是否必填: 1-必填, 0-不必填';
COMMENT ON COLUMN "public"."doc_statistics_definition"."remark" IS '备注';
COMMENT ON COLUMN "public"."doc_statistics_definition"."is_statistics" IS '是否统计: 0-不统计, 1-统计';
COMMENT ON COLUMN "public"."doc_statistics_definition_option"."display_order" IS '显示顺序';
COMMENT ON COLUMN "public"."doc_statistics_definition"."tenant_id" IS '租户ID';
COMMENT ON COLUMN "public"."doc_statistics_definition"."delete_flag" IS '删除标志: 0-未删除, 1-已删除';
COMMENT ON COLUMN "public"."doc_statistics_definition"."create_by" IS '创建人';
COMMENT ON COLUMN "public"."doc_statistics_definition"."create_time" IS '创建时间';
COMMENT ON COLUMN "public"."doc_statistics_definition"."update_by" IS '更新人';
COMMENT ON COLUMN "public"."doc_statistics_definition"."update_time" IS '更新时间';

-- -- 5. 索引设计
-- -- 按属性代码查询
-- CREATE INDEX "idx_doc_statistics_code" ON "public"."doc_statistics_definition" ("code");
-- -- 按是否统计查询
-- CREATE INDEX "idx_doc_statistics_is_stat" ON "public"."doc_statistics_definition" ("is_statistics");
-- -- 按属性类型查询
-- CREATE INDEX "idx_doc_statistics_type" ON "public"."doc_statistics_definition" ("type_enum");
-- -- 按租户ID查询
-- CREATE INDEX "idx_doc_statistics_tenant" ON "public"."doc_statistics_definition" ("tenant_id");
    



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