-- ============================================================
-- 桃育种系统 - 亲本资源表 (bre_germplasm)
-- ------------------------------------------------------------
-- 依据: doc/桃育种系统业务功能规划报告.md (2026-07-28) 模块1
-- 对齐: FastapiAdmin 框架审计字段约定 (app/core/base_model.py)
--       id / uuid / is_deleted / created_time / updated_time /
--       deleted_time / created_id / updated_id / deleted_id
-- 用途: 建表后由「代码生成器」导入，一键生成前后端 CRUD
-- 运行: psql -h localhost -U dpb -d dpb -f backend/sql/bre_germplasm.sql
-- 说明: 可直接重复执行 (CREATE TABLE / INDEX / COMMENT 均带 IF NOT EXISTS)
-- ============================================================

CREATE TABLE IF NOT EXISTS bre_germplasm (
    -- 主键与框架审计字段
    id                  SERIAL        PRIMARY KEY,
    uuid                VARCHAR(64)   NOT NULL UNIQUE,
    is_deleted          BOOLEAN       NOT NULL DEFAULT FALSE,

    -- 业务字段
    cultivar_name       VARCHAR(100)  NOT NULL,                       -- 品种/种质名称
    variety_type        VARCHAR(20)   NULL,                           -- 变种类型, 字典: peach_variety_type
    origin              VARCHAR(200)  NULL,                           -- 来源
    preservation_site   VARCHAR(200)  NULL,                           -- 保存地
    avg_fruit_weight    NUMERIC(8,2)  NULL,                           -- 平均果重(g)
    ssc                 NUMERIC(4,1)  NULL,                           -- 可溶性固形物(%)
    firmness            VARCHAR(20)   NULL,                           -- 硬度, 字典: firmness
    maturity_period     VARCHAR(20)   NULL,                           -- 成熟期(早/中/晚)
    flowering_period    VARCHAR(20)   NULL,                           -- 花期
    chilling_requirement INTEGER      NULL,                           -- 需冷量(h)
    disease_resistance  TEXT          NULL,                           -- 抗病性描述
    can_be_female       BOOLEAN       NOT NULL DEFAULT FALSE,          -- 可作母本
    can_be_male         BOOLEAN       NOT NULL DEFAULT FALSE,          -- 可作父本
    pedigree_note       TEXT          NULL,                           -- 系谱备注
    photo_path          VARCHAR(500)  NULL,                           -- 照片路径

    -- 审计字段 (框架约定, 与 ModelMixin/UserMixin 一致)
    created_time        TIMESTAMPTZ   NOT NULL DEFAULT now(),
    updated_time        TIMESTAMPTZ   NOT NULL DEFAULT now(),
    deleted_time        TIMESTAMPTZ   NULL,
    created_id          INTEGER       NULL REFERENCES sys_user(id) ON DELETE SET NULL,
    updated_id          INTEGER       NULL REFERENCES sys_user(id) ON DELETE SET NULL,
    deleted_id          INTEGER       NULL REFERENCES sys_user(id) ON DELETE SET NULL
);

-- 索引
CREATE INDEX IF NOT EXISTS ix_bre_germplasm_is_deleted       ON bre_germplasm (is_deleted);
CREATE INDEX IF NOT EXISTS ix_bre_germplasm_created_deleted ON bre_germplasm (created_time, is_deleted);
CREATE INDEX IF NOT EXISTS ix_bre_germplasm_cultivar_name   ON bre_germplasm (cultivar_name);
CREATE INDEX IF NOT EXISTS ix_bre_germplasm_variety_type    ON bre_germplasm (variety_type);

-- 列注释 (供代码生成器生成表单标签/列表标题)
COMMENT ON TABLE  bre_germplasm IS '亲本资源表(桃育种)';
COMMENT ON COLUMN bre_germplasm.id                 IS '主键ID';
COMMENT ON COLUMN bre_germplasm.uuid               IS 'UUID全局唯一标识';
COMMENT ON COLUMN bre_germplasm.is_deleted         IS '是否已删除(0:未删除 1:已删除)';
COMMENT ON COLUMN bre_germplasm.cultivar_name      IS '品种/种质名称';
COMMENT ON COLUMN bre_germplasm.variety_type       IS '变种类型(普通桃/油桃/蟠桃/油蟠桃/黄肉)';
COMMENT ON COLUMN bre_germplasm.origin             IS '来源';
COMMENT ON COLUMN bre_germplasm.preservation_site  IS '保存地';
COMMENT ON COLUMN bre_germplasm.avg_fruit_weight   IS '平均果重(g)';
COMMENT ON COLUMN bre_germplasm.ssc                IS '可溶性固形物(%)';
COMMENT ON COLUMN bre_germplasm.firmness           IS '硬度';
COMMENT ON COLUMN bre_germplasm.maturity_period    IS '成熟期(早/中/晚)';
COMMENT ON COLUMN bre_germplasm.flowering_period   IS '花期';
COMMENT ON COLUMN bre_germplasm.chilling_requirement IS '需冷量(h)';
COMMENT ON COLUMN bre_germplasm.disease_resistance IS '抗病性描述';
COMMENT ON COLUMN bre_germplasm.can_be_female      IS '可作母本';
COMMENT ON COLUMN bre_germplasm.can_be_male        IS '可作父本';
COMMENT ON COLUMN bre_germplasm.pedigree_note       IS '系谱备注';
COMMENT ON COLUMN bre_germplasm.photo_path         IS '照片路径';
COMMENT ON COLUMN bre_germplasm.created_time       IS '创建时间';
COMMENT ON COLUMN bre_germplasm.updated_time       IS '更新时间';
COMMENT ON COLUMN bre_germplasm.deleted_time       IS '删除时间';
COMMENT ON COLUMN bre_germplasm.created_id         IS '创建人ID';
COMMENT ON COLUMN bre_germplasm.updated_id         IS '更新人ID';
COMMENT ON COLUMN bre_germplasm.deleted_id         IS '删除人ID';
