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

-- 创建订单主表
CREATE TABLE "order_main" (
    -- 主键字段
    "id" int8 NOT NULL DEFAULT nextval('order_main_id_seq'::regclass),

    -- 业务核心字段
    "order_no" varchar(32) COLLATE "pg_catalog"."default" NOT NULL,          -- 订单号
    "patient_id" int8,                                                       -- 患者ID
    "patient_name" varchar(50) COLLATE "pg_catalog"."default",                 -- 患者姓名
    "medical_card" varchar(32) COLLATE "pg_catalog"."default",                  -- 就诊卡号
    "phone" varchar(20) COLLATE "pg_catalog"."default",                       -- 手机号
    "gender" int4,                                                           -- 性别（1-男，2-女）
    "schedule_id" int8,                                                       -- 排班ID
    "slot_id" int8,                                                           -- 号源ID
    "department_id" int8,                                                      -- 科室ID
    "department_name" varchar(100) COLLATE "pg_catalog"."default",              -- 科室名称
    "doctor_id" int8,                                                          -- 医生ID
    "doctor_name" varchar(50) COLLATE "pg_catalog"."default",                   -- 医生姓名
    "reg_type" varchar(10) COLLATE "pg_catalog"."default",                     -- 号别（普通/专家）
    "fee" numeric(10,2),                                                      -- 挂号费
    "appointment_date" timestamptz(6),                                       -- 预约日期
    "appointment_time" timestamptz(6),                                        -- 预约时间
    "cancel_time" timestamptz(6),                                            -- 取消时间
    "cancel_reason" varchar(200) COLLATE "pg_catalog"."default",                 -- 取消原因
    "status" int4 NOT NULL DEFAULT 1,                                          -- 状态（1-预约中，2-已完成，3-已取消）
    "pay_status" int4 NOT NULL DEFAULT 0,                                       -- 支付状态（0-未支付，1-已支付）
    "version" int4 NOT NULL DEFAULT 0,                                          -- 版本号（乐观锁）

    -- 继承HisBaseEntity的审计字段
    "tenant_id" int8 NOT NULL,                                                 -- 租户ID
    "delete_flag" char(1) COLLATE "pg_catalog"."default" NOT NULL DEFAULT '0',  -- 删除标志（0-未删除，1-已删除）
    "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 "order_main_pkey" PRIMARY KEY ("id")
);

-- 设置表所有者（根据实际数据库用户修改，如果不存在postgres角色可注释掉此行）
-- ALTER TABLE "order_main" OWNER TO "postgres";

-- 表及字段注释
COMMENT ON TABLE "order_main" IS '订单主表：存储门诊预约挂号订单信息';

COMMENT ON COLUMN "order_main"."id" IS '主键ID';
COMMENT ON COLUMN "order_main"."order_no" IS '订单号：唯一标识订单，系统自动生成';
COMMENT ON COLUMN "order_main"."patient_id" IS '患者ID：关联adm_patient表';
COMMENT ON COLUMN "order_main"."patient_name" IS '患者姓名：冗余字段，便于查询';
COMMENT ON COLUMN "order_main"."medical_card" IS '就诊卡号：患者就诊卡编号';
COMMENT ON COLUMN "order_main"."phone" IS '手机号：患者联系电话';
COMMENT ON COLUMN "order_main"."gender" IS '性别：1-男，2-女';
COMMENT ON COLUMN "order_main"."schedule_id" IS '排班ID：关联排班表';
COMMENT ON COLUMN "order_main"."slot_id" IS '号源ID：关联clinical_ticket表';
COMMENT ON COLUMN "order_main"."department_id" IS '科室ID：关联科室表';
COMMENT ON COLUMN "order_main"."department_name" IS '科室名称：冗余字段，便于查询';
COMMENT ON COLUMN "order_main"."doctor_id" IS '医生ID：关联医生表';
COMMENT ON COLUMN "order_main"."doctor_name" IS '医生姓名：冗余字段，便于查询';
COMMENT ON COLUMN "order_main"."reg_type" IS '号别：普通/专家';
COMMENT ON COLUMN "order_main"."fee" IS '挂号费：挂号费用金额';
COMMENT ON COLUMN "order_main"."appointment_date" IS '预约日期：患者预约的就诊日期';
COMMENT ON COLUMN "order_main"."appointment_time" IS '预约时间：患者预约的具体时间';
COMMENT ON COLUMN "order_main"."cancel_time" IS '取消时间：订单取消的时间';
COMMENT ON COLUMN "order_main"."cancel_reason" IS '取消原因：订单取消的原因说明';
COMMENT ON COLUMN "order_main"."status" IS '状态：1-预约中，2-已完成，3-已取消';
COMMENT ON COLUMN "order_main"."pay_status" IS '支付状态：0-未支付，1-已支付';
COMMENT ON COLUMN "order_main"."version" IS '版本号：乐观锁，防止并发冲突';
COMMENT ON COLUMN "order_main"."tenant_id" IS '租户ID：多租户系统隔离字段';
COMMENT ON COLUMN "order_main"."delete_flag" IS '删除标志：0-未删除，1-已删除';
COMMENT ON COLUMN "order_main"."create_by" IS '创建人：存储创建者用户名';
COMMENT ON COLUMN "order_main"."create_time" IS '创建时间：默认当前时间，带时区';
COMMENT ON COLUMN "order_main"."update_by" IS '更新人：存储更新者用户名';
COMMENT ON COLUMN "order_main"."update_time" IS '更新时间：带时区';

-- 创建索引
CREATE INDEX "idx_order_main_order_no" 
    ON "order_main" ("order_no")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_patient_id" 
    ON "order_main" ("patient_id")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_slot_id" 
    ON "order_main" ("slot_id")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_department_id" 
    ON "order_main" ("department_id")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_doctor_id" 
    ON "order_main" ("doctor_id")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_status" 
    ON "order_main" ("status")
    WHERE "delete_flag" = '0';

CREATE INDEX "idx_order_main_tenant_id" 
    ON "order_main" ("tenant_id", "create_time")
    WHERE "delete_flag" = '0';

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