-- Postgres
-- 建表DDL
-- 默认schema是public
-- 默认tablespace是pg_default
-- 默认的索引结构btree

-- 操作人员数据权限
-- DROP TABLE IF EXISTS operator_right;
CREATE TABLE operator_right (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL DEFAULT '',
    level SMALLINT NOT NULL DEFAULT -1, -- 角色级别：0 超管；1 网；2 区；3 站；4 元；5 点；
    rights JSONB NOT NULL DEFAULT '[]', -- operator_right.rights
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE operator_right IS '操作人员权限';
COMMENT ON COLUMN operator_right.id IS 'id';
COMMENT ON COLUMN operator_right.name IS '人员姓名';
COMMENT ON COLUMN operator_right.level IS '角色级别';
COMMENT ON COLUMN operator_right.rights IS '人员权限';
COMMENT ON COLUMN operator_right.operator_id IS '操作人id';
COMMENT ON COLUMN operator_right.operate_at IS '操作时间';


-- 操作日志
-- DROP TABLE IF EXISTS operation_log;
CREATE TABLE operation_log (
    id BIGSERIAL PRIMARY KEY,
    type VARCHAR(50) NOT NULL DEFAULT '', -- 增/删/改：1 增；2 删；3 改；

    detail TEXT NOT NULL DEFAULT '', -- 在网/区/站进行增/删/改了什么

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX op_at_key ON operation_log(operate_at);
COMMENT ON TABLE operation_log IS '操作日志';
COMMENT ON COLUMN operation_log.id IS 'id';
COMMENT ON COLUMN operation_log.type IS '操作类型';
COMMENT ON COLUMN operation_log.detail IS '操作详情';
COMMENT ON COLUMN operation_log.operator_id IS '操作人id';
COMMENT ON COLUMN operation_log.operate_at IS '操作时间';


-- 事件识别的条件
-- DROP TABLE IF EXISTS event_condition;
CREATE TABLE event_condition (
    id SERIAL PRIMARY KEY,
    -- how to discribe the condition? json? (condition_windows)
    com_type VARCHAR(50) NOT NULL DEFAULT '', -- 元件类型(bus)
    phy_type VARCHAR(50) NOT NULL DEFAULT '', -- 物理量类型(v)
    condition JSONB NOT NULL DEFAULT '{}',
    -- inner fields: threshold_unit,threshold,duration_unit,duration

    -- 信号量事件的分类如何表达（warn/alarm?）
    event_type VARCHAR(50) NOT NULL DEFAULT '', -- 如何分类型(模拟量lower_warn/lower_alarm/upper_warn/upper_alarm)
    event_name VARCHAR(100) NOT NULL DEFAULT '',
    event_level SMALLINT NOT NULL DEFAULT -1, -- 0/1/2...

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE event_condition IS '事件识别条件';
COMMENT ON COLUMN event_condition.id IS '事件识别条件id';
COMMENT ON COLUMN event_condition.com_type IS '元件类型';
COMMENT ON COLUMN event_condition.phy_type IS '物理量类型';
COMMENT ON COLUMN event_condition.condition IS '事件识别条件';
COMMENT ON COLUMN event_condition.event_type IS '事件类型';
COMMENT ON COLUMN event_condition.event_name IS '事件名称';
COMMENT ON COLUMN event_condition.event_level IS '事件级别';
COMMENT ON COLUMN event_condition.operator_id IS '操作人id';
COMMENT ON COLUMN event_condition.operate_at IS '操作时间';


-- 事件记录
-- 事件的redis数据记录格式：
-- DROP TABLE IF EXISTS events;
CREATE TABLE events (
    id BIGSERIAL PRIMARY KEY,
    type VARCHAR(50) NOT NULL DEFAULT '', -- 如何分类型
    name VARCHAR(100) NOT NULL DEFAULT '',
    level SMALLINT NOT NULL DEFAULT -1, -- 0/1/2... 级别需要定义

    refer_data JSONB NOT NULL DEFAULT '{}', -- events.refer_data

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);                                
COMMENT ON TABLE events IS '事件记录';
COMMENT ON COLUMN events.event_id IS '事件id';
COMMENT ON COLUMN events.event_type IS '事件类型';
COMMENT ON COLUMN events.event_name IS '事件名称';
COMMENT ON COLUMN events.event_level IS '事件级别';
COMMENT ON COLUMN events.refer_data IS '引用数据';
COMMENT ON COLUMN events.operator_id IS '操作人id';
COMMENT ON COLUMN events.operate_at IS '操作时间';


-- 案例记录
-- DROP TABLE IF EXISTS cases;
CREATE TABLE cases (
    id BIGSERIAL PRIMARY KEY,
    type VARCHAR(50) NOT NULL DEFAULT '', -- 如何分类型
    name VARCHAR(100) NOT NULL DEFAULT '',
    level SMALLINT NOT NULL DEFAULT -1, -- 0/1/2... 级别需要定义

    refer_data JSONB NOT NULL DEFAULT '{}', -- cases.refer_data

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE cases IS '案例记录';
COMMENT ON COLUMN cases.id IS '案例id';
COMMENT ON COLUMN cases.case_type IS '案例类型';
COMMENT ON COLUMN cases.case_name IS '案例名称';
COMMENT ON COLUMN cases.case_level IS '案例级别';
COMMENT ON COLUMN cases.refer_data IS '引用数据';
COMMENT ON COLUMN cases.operator_id IS '操作人id';
COMMENT ON COLUMN cases.operate_at IS '操作时间';


-- DROP TABLE IF EXISTS grid;
CREATE TABLE grid (
    id SERIAL PRIMARY KEY, 
    tag VARCHAR(50) NOT NULL DEFAULT '', -- 是不是最好在tag中保持一个前缀（对象类型）？
    name VARCHAR(50) NOT NULL DEFAULT '',
    comment VARCHAR(500) NOT NULL DEFAULT '',

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    UNIQUE(tag)
);
COMMENT ON TABLE grid IS '电网';
COMMENT ON COLUMN grid.id IS 'id';
COMMENT ON COLUMN grid.tag IS '标签';
COMMENT ON COLUMN grid.name IS '名称';
COMMENT ON COLUMN grid.comment IS '注解';
COMMENT ON COLUMN grid.operator_id IS '操作人id';
COMMENT ON COLUMN grid.operate_at IS '操作时间';


-- DROP TABLE IF EXISTS zone;
CREATE TABLE zone (
    id SERIAL PRIMARY KEY, 
    tag VARCHAR(50) NOT NULL DEFAULT '',
    name VARCHAR(50) NOT NULL DEFAULT '',
    comment VARCHAR(500) NOT NULL DEFAULT '',

    grid_id INTEGER NOT NULL DEFAULT -1,

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(grid_id) REFERENCES grid(id),
    UNIQUE(tag)
);
COMMENT ON TABLE zone IS '区域';
COMMENT ON COLUMN zone.id IS 'id';
COMMENT ON COLUMN zone.tag IS '标签';
COMMENT ON COLUMN zone.name IS '名字';
COMMENT ON COLUMN zone.comment IS '注解';
COMMENT ON COLUMN zone.grid_id IS '电网id';
COMMENT ON COLUMN zone.operator_id IS '操作人id';
COMMENT ON COLUMN zone.operate_at IS '操作时间';


-- DROP TABLE IF EXISTS station;
CREATE TABLE station (
    id SERIAL PRIMARY KEY, 
    tag VARCHAR(50) NOT NULL DEFAULT '',
    name VARCHAR(50) NOT NULL DEFAULT '',
    comment VARCHAR(500) NOT NULL DEFAULT '',
    is_local BOOLEAN NOT NULL DEFAULT FALSE,

    grid_id INTEGER NOT NULL DEFAULT -1,
    zone_id INTEGER NOT NULL DEFAULT -1,

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(grid_id) REFERENCES grid(id),
    FOREIGN KEY(zone_id) REFERENCES zone(id),
    UNIQUE(tag)
);
COMMENT ON TABLE station IS '场站';
COMMENT ON COLUMN station.id IS 'id';
COMMENT ON COLUMN station.tag IS '标签';
COMMENT ON COLUMN station.name IS '名称';
COMMENT ON COLUMN station.comment IS '注解';
COMMENT ON COLUMN station.grid_id IS '电网id';
COMMENT ON COLUMN station.zone_id IS '区域id';
COMMENT ON COLUMN station.operator_id IS '操作人id';
COMMENT ON COLUMN station.operate_at IS '操作时间';


-- 组态图(目前不会跨站)
-- DROP TABLE IF EXISTS page;
CREATE TABLE page (
    id SERIAL PRIMARY KEY, 
    tag VARCHAR(50) NOT NULL DEFAULT '', -- 如果元件与端子是站内唯一，此列意义不大
    name VARCHAR(50) NOT NULL DEFAULT '',
    comment VARCHAR(500) NOT NULL DEFAULT '',

    context JSONB NOT NULL DEFAULT '{}', -- 数据格式暂定jsonb，需要存放什么样的UI侧的数据
    status SMALLINT NOT NULL DEFAULT -1,

    station_id INTEGER NOT NULL DEFAULT -1, -- 基于组态图属于某一站
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(station_id) REFERENCES station(id)
);
COMMENT ON TABLE page IS '组态图';
COMMENT ON COLUMN page.id IS 'id'; 
COMMENT ON COLUMN page.name IS '组态名';
COMMENT ON COLUMN page.comment IS '注解';
COMMENT ON COLUMN page.station_id IS '站id';
COMMENT ON COLUMN page.context IS '组态数据';
COMMENT ON COLUMN page.status IS '组态状态';
COMMENT ON COLUMN page.operator_id IS '操作人id';
COMMENT ON COLUMN page.operate_at IS '操作时间';


-- 组态元件
-- DROP TABLE IF EXISTS component;
CREATE TABLE component (
    id SERIAL PRIMARY KEY,
    global_uuid UUID DEFAULT uuid_generate_v4(),
    visible_id VARCHAR(50) NOT NULL DEFAULT '',
    comment VARCHAR(500) NOT NULL DEFAULT '', 
    name VARCHAR(50) NOT NULL DEFAULT '', -- 可见字符即可
    type VARCHAR(50) NOT NULL DEFAULT '', 
    in_service BOOLEAN NOT NULL DEFAULT FALSE,
    state SMALLINT NOT NULL DEFAULT -1, -- 具体涉及哪些场景，数据类型是否用数值，用于拓扑分析
    connected_bus INTEGER NOT NULL DEFAULT -1, 

    --position JSONB NOT NULL DEFAULT '{}', -- {"x":0,"y":0}，是否放在数据中
    context JSONB NOT NULL DEFAULT '{}', -- 数据格式暂定jsonb，需要存放什么样的UI侧的数据
    
    station_id INTEGER NOT NULL DEFAULT -1,
    page_id INTEGER NOT NULL DEFAULT -1, -- 组态图id
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(station_id) REFERENCES station(id),
    FOREIGN KEY(page_id) REFERENCES page(id)
);
COMMENT ON TABLE component IS '设备信息';
COMMENT ON COLUMN component.id IS 'id';
COMMENT ON COLUMN component.tag IS '标签';
COMMENT ON COLUMN component.name IS '名字';
--COMMENT ON COLUMN component.comment IS '注解';
COMMENT ON COLUMN component.type IS '设备类型';
COMMENT ON COLUMN component.in_service IS '服役状态';
COMMENT ON COLUMN component.state IS '设备状态';
COMMENT ON COLUMN component.connect_bus IS '连接母线';
--COMMENT ON COLUMN component.position IS '组态图中位置';
COMMENT ON COLUMN component.context IS '元件数据';
COMMENT ON COLUMN component.station_id IS '站';
COMMENT ON COLUMN component.page_id IS '归属组态图id';
COMMENT ON COLUMN component.operator_id IS '操作人id';
COMMENT ON COLUMN component.operate_at IS '操作时间';


-- 组态拓扑关系
-- DROP TABLE IF EXISTS topologic;
CREATE TABLE topologic (
    id SERIAL PRIMARY KEY, 
    com_from INTEGER NOT NULL DEFAULT -1, -- 电路来向元件（默认方向）
    com_to INTEGER NOT NULL DEFAULT -1, -- 电路去向元件（默认方向）
    flag SMALLINT NOT NULL DEFAULT 1, -- 电路状态：正向 1；断路 0；逆向 -1；

    station_id INTEGER NOT NULL DEFAULT -1, -- 基于组态图属于某一站
    page_id INTEGER NOT NULL DEFAULT -1,
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(com_from) REFERENCES component(id),
    FOREIGN KEY(com_to) REFERENCES component(id),
    FOREIGN KEY(station_id) REFERENCES station(id),
    FOREIGN KEY(page_id) REFERENCES page(id)
);
COMMENT ON TABLE topologic IS '拓扑关系';
COMMENT ON COLUMN topologic.id IS 'id';
COMMENT ON COLUMN topologic.com_from IS '电路来向元件';
COMMENT ON COLUMN topologic.com_to IS '电路去向元件';
COMMENT ON COLUMN topologic.flag IS '电路状态';
COMMENT ON COLUMN topologic.station_id IS '归属站id';
COMMENT ON COLUMN topologic.page_id IS '归属组态图id';
COMMENT ON COLUMN topologic.operator_id IS '操作人id';
COMMENT ON COLUMN topologic.operate_at IS '操作时间';


-- 时序数据中field对应的terminal信息（映射的生成时机在什么时候：redis.hash）
-- DROP TABLE IF EXISTS terminal_mapping;
CREATE TABLE terminal_mapping (
    id SERIAL PRIMARY KEY, 
    --terminal_station VARCHAR(50) NOT NULL DEFAULT '', -- tag，是否考虑上面station与terminal_station不同
    component VARCHAR(50) NOT NULL DEFAULT '', -- tag，根据excel关联事件，是否关联component，考虑UI侧会对端子数据附加在component上
    tag VARCHAR(50) NOT NULL DEFAULT '', -- 至少保证站内唯一，是否必须，端子唯一标识的命名动作位于哪个流程
    --terminal_type VARCHAR(50) NOT NULL DEFAULT '', -- 根据excel关联事件（bus）
    name VARCHAR(100) NOT NULL DEFAULT '', -- name和tag是否类同，必要性不强
    unit VARCHAR(10) NOT NULL DEFAULT '', -- type与unit是不是一致的

    -- other fields? 
    -- terminal_value DECIMAL(10,2) NOT NULL DEFAULT 0, -- 隐性

    measure VARCHAR(50) NOT NULL DEFAULT '', -- tag（来自报文）
    station VARCHAR(50) NOT NULL DEFAULT '', -- tag（来自报文）
    device VARCHAR(50) NOT NULL DEFAULT '', -- tag（来自报文，设备和数据共用）
    field VARCHAR(50) NOT NULL DEFAULT '', -- tag（来自报文）

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(station) REFERENCES station(name),
    --FOREIGN KEY(terminal_station) REFERENCES station(name),
    FOREIGN KEY(terminal_com) REFERENCES component(name)
);
COMMENT ON TABLE terminal_mapping IS '端子映射表';
COMMENT ON COLUMN terminal_mapping.component IS '端子元件';
COMMENT ON COLUMN terminal_mapping.tag IS '端子标签';
COMMENT ON COLUMN terminal_mapping.unit IS '端子单位';
COMMENT ON COLUMN terminal_mapping.name IS '端子名称';
COMMENT ON COLUMN terminal_mapping.station IS '站点';
COMMENT ON COLUMN terminal_mapping.device IS '设备';
COMMENT ON COLUMN terminal_mapping.field IS '字段';
COMMENT ON COLUMN terminal_mapping.operator_id IS '操作人id';
COMMENT ON COLUMN terminal_mapping.operate_at IS '操作时间';


-- 二次设备库（从excel中看无个数限制）
-- DROP TABLE IF EXISTS secondary;
CREATE TABLE secondary_lib (
    id SERIAL PRIMARY KEY, 
    type VARCHAR(50) NOT NULL DEFAULT '', -- 如果需要主次类型，可以下划线分割或者增加列
    manufacturer VARCHAR(50) NOT NULL DEFAULT '',
    model VARCHAR(50) NOT NULL DEFAULT '', -- 型号
    args JSONB NOT NULL DEFAULT '{}', -- 二次设备的参数对应于端子测点，是否引出到另一张表
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE secondary_lib IS '二次设备库';
COMMENT ON COLUMN secondary_lib.id IS 'id';
COMMENT ON COLUMN secondary_lib.type IS '类型';
COMMENT ON COLUMN secondary_lib.manufacturer IS '厂商';
COMMENT ON COLUMN secondary_lib.model IS '型号';
COMMENT ON COLUMN secondary_lib.args IS '二次设备端子测点';
COMMENT ON COLUMN secondary_lib.operator_id IS '操作人id';
COMMENT ON COLUMN secondary_lib.operate_at IS '操作时间';


-- 一次设备与二次设备组态关系
-- DROP TABLE IF EXISTS primary_secondary;
CREATE TABLE primary_secondary (
    id SERIAL PRIMARY KEY, 
    primary_type VARCHAR(50) NOT NULL DEFAULT '', -- 比如bus/mtr
    secondary_type VARCHAR(50) NOT NULL DEFAULT '', -- 用于寻找二次设备库中对应类型设备
    --
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(primary_type) REFERENCES component(type),
    FOREIGN KEY(secondary_type) REFERENCES secondary(type)
);
COMMENT ON TABLE primary_secondary IS '一二次设备组态关系';
COMMENT ON COLUMN primary_secondary.id IS 'id';
COMMENT ON COLUMN primary_secondary.primary_type IS '一次设备类型';
COMMENT ON COLUMN primary_secondary.secondary_type IS '二次设备类型';
COMMENT ON COLUMN primary_secondary.operator_id IS '操作人id';
COMMENT ON COLUMN primary_secondary.operate_at IS '操作时间';


-- 基本参数(要转移到component表中)
-- DROP TABLE IF EXISTS basic_info;
CREATE TABLE basic_info(
    id SERIAL PRIMARY KEY, 
    component_id INTEGER NOT NULL DEFAULT -1,
    name VARCHAR(50) NOT NULL DEFAULT '', -- Bus(X) len(20)
    nominal_voltage DECIMAL(10,2) NOT NULL DEFAULT 35, -- 0.01~500
    discription VARCHAR(200) NOT NULL DEFAULT '', -- len(100)
    service_state VARCHAR(50) NOT NULL DEFAULT '运行', -- 运行/退出
    detail_state VARCHAR(50) NOT NULL DEFAULT '现役', -- 现役/新建/计划/检修/库存可用/库存报废
    grid_name VARCHAR(50) NOT NULL DEFAULT '',
    zone_name VARCHAR(50) NOT NULL DEFAULT '',
    station_name VARCHAR(50) NOT NULL DEFAULT '',
    bus_code INTEGER NOT NULL DEFAULT -1, -- VARCHAR(50) NOT NULL DEFAULT '',

    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(component_id) REFERENCES component(id)  
);


-- 母线-模型参数
-- DROP TABLE IF EXISTS model_info;
CREATE TABLE model_info(
    id SERIAL PRIMARY KEY, 
    component_id INTEGER NOT NULL DEFAULT -1,
    voltage_v_per DECIMAL(10,2) NOT NULL DEFAULT 100, -- 1~200
    voltage_v DECIMAL(10,2) NOT NULL DEFAULT 35, -- 0.01~1000
    voltage_phase DECIMAL(10,2) NOT NULL DEFAULT 0, -- -180~180
    rated_current DECIMAL(10,2) NOT NULL DEFAULT 1000, -- 0.01~65536
    dynamic_current DECIMAL(10,2) NOT NULL DEFAULT 40, -- 0.01~65536
    load_adjust_rate_min SMALLINT NOT NULL DEFAULT 100, -- 0~100
    load_adjust_rate_max SMALLINT NOT NULL DEFAULT 100, -- 0~100
    bus_type VARCHAR(50) NOT NULL DEFAULT 'PQ', -- 1 平衡母线/PV母线/PQ母线
    short_circuit_capacity_s3max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_s3min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_i3max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_i3min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_z3max DECIMAL(10,2) NOT NULL DEFAULT 0.05, -- 0~100
    short_circuit_capacity_z3min DECIMAL(10,2) NOT NULL DEFAULT 0.1, -- 0~100
    short_circuit_capacity_s1max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_s1min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_i1max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_i1min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    short_circuit_capacity_z1max DECIMAL(10,2) NOT NULL DEFAULT 0.05, -- 0~100
    short_circuit_capacity_z1min DECIMAL(10,2) NOT NULL DEFAULT 0.1, -- 0~100
    reference_voltage DECIMAL(10,2) NOT NULL DEFAULT 37, -- 0.01~500
    reference_capacity DECIMAL(10,2) NOT NULL DEFAULT 100, -- 0~65536
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(component_id) REFERENCES component(id) 
);


-- 母线-稳定参数
-- DROP TABLE IF EXISTS bus_stable;
CREATE TABLE stable_info(
    id SERIAL PRIMARY KEY, 
    component_id INTEGER NOT NULL DEFAULT -1,
    undervoltage_warning_threshold DECIMAL(10,2) NOT NULL DEFAULT 95, -- 0~100(%)
    undervoltage_warning_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
    undervoltage_warning_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
    undervoltage_alarm_threshold DECIMAL(10,2) NOT NULL DEFAULT 90, -- 0~100(%)
    undervoltage_alarm_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
    undervoltage_alarm_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
    overvoltage_warning_limit DECIMAL(10,2) NOT NULL DEFAULT 105, -- 100~500(%)
    overvoltage_warning_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
    overvoltage_warning_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
    overvoltage_alarm_limit DECIMAL(10,2) NOT NULL DEFAULT 110, -- 100~500(%)
    overvoltage_alarm_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
    overvoltage_alarm_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
    voltage_margin_pmax DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    voltage_margin_qmax DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
    voltage_margin_ulim DECIMAL(10,2) NOT NULL DEFAULT 90, -- 0~1000
    active_power_margin_lim DECIMAL(10,2) NOT NULL DEFAULT 30, -- 0~100(%)
    reactive_power_margin_lim DECIMAL(10,2) NOT NULL DEFAULT 30, -- 0~100(%)
    operator_id INTEGER NOT NULL DEFAULT -1,
    operate_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY(component_id) REFERENCES component(id)  
);


-- 母线-二次设备（是否必要）
-- DROP TABLE IF EXISTS secondary_info;
CREATE TABLE secondary_info (
    id SERIAL PRIMARY KEY, 
    component_id INTEGER NOT NULL DEFAULT -1,
    secondaries JSONB NOT NULL DEFAULT '{}', -- secondary.id，其实相当于关联二次设备型号各多少个
    terminals JSONB NOT NULL DEFAULT '{}', -- field_terminal.id还是tag/name，需要明确每一个端子测点

    FOREIGN KEY(component_id) REFERENCES component(id)  
);


---- 母线信息
---- DROP TABLE IF EXISTS bus;
--CREATE TABLE bus (
--    id SERIAL PRIMARY KEY, 
--    bus_name VARCHAR(50) NOT NULL DEFAULT '', -- Bus(X) len(20)
--    nominal_voltage DECIMAL(10,2) NOT NULL DEFAULT 35, -- 0.01~500
--    discription VARCHAR(200) NOT NULL DEFAULT '', -- len(100)
--    service_state VARCHAR(50) NOT NULL DEFAULT '运行', -- 运行/退出
--    detail_state VARCHAR(50) NOT NULL DEFAULT '现役', -- 现役/新建/计划/检修/库存可用/库存报废
--    grid VARCHAR(50) NOT NULL DEFAULT '',
--    zone VARCHAR(50) NOT NULL DEFAULT '',
--    station VARCHAR(50) NOT NULL DEFAULT '',
--    bus_code INTEGER NOT NULL DEFAULT -1, -- 
--
--    voltage_v_per DECIMAL(10,2) NOT NULL DEFAULT 100, -- 1~200
--    voltage_v DECIMAL(10,2) NOT NULL DEFAULT 35, -- 0.01~1000
--    voltage_phase DECIMAL(10,2) NOT NULL DEFAULT 0, -- -180~180
--    rated_current DECIMAL(10,2) NOT NULL DEFAULT 1000, -- 0.01~65536
--    dynamic_current DECIMAL(10,2) NOT NULL DEFAULT 40, -- 0.01~65536
--    load_adjust_rate_min SMALLINT NOT NULL DEFAULT 100, -- 0~100
--    load_adjust_rate_max SMALLINT NOT NULL DEFAULT 100, -- 0~100
--    bus_type VARCHAR(50) NOT NULL DEFAULT 'PQ', -- 1 平衡母线/PV母线/PQ母线
--    short_circuit_capacity_s3max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_s3min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_i3max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_i3min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_z3max DECIMAL(10,2) NOT NULL DEFAULT 0.05, -- 0~100
--    short_circuit_capacity_z3min DECIMAL(10,2) NOT NULL DEFAULT 0.1, -- 0~100
--    short_circuit_capacity_s1max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_s1min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_i1max DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_i1min DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    short_circuit_capacity_z1max DECIMAL(10,2) NOT NULL DEFAULT 0.05, -- 0~100
--    short_circuit_capacity_z1min DECIMAL(10,2) NOT NULL DEFAULT 0.1, -- 0~100
--    reference_voltage DECIMAL(10,2) NOT NULL DEFAULT 37, -- 0.01~500
--    reference_capacity DECIMAL(10,2) NOT NULL DEFAULT 100, -- 0~65536
--
--    undervoltage_warning_threshold DECIMAL(10,2) NOT NULL DEFAULT 95, -- 0~100(%)
--    undervoltage_warning_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
--    undervoltage_warning_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
--    undervoltage_alarm_threshold DECIMAL(10,2) NOT NULL DEFAULT 90, -- 0~100(%)
--    undervoltage_alarm_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
--    undervoltage_alarm_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
--    overvoltage_warning_limit DECIMAL(10,2) NOT NULL DEFAULT 105, -- 100~500(%)
--    overvoltage_warning_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
--    overvoltage_warning_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
--    overvoltage_alarm_limit DECIMAL(10,2) NOT NULL DEFAULT 110, -- 100~500(%)
--    overvoltage_alarm_time_unit VARCHAR(10) NOT NULL DEFAULT '秒', -- 周波/秒/分钟/小时
--    overvoltage_alarm_run_time DECIMAL(10,2) NOT NULL DEFAULT 10, -- 0~100
--
--    voltage_margin_pmax DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    voltage_margin_qmax DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~65536
--    voltage_margin_ulim DECIMAL(10,2) NOT NULL DEFAULT 90, -- 0~1000
--    active_power_margin_lim DECIMAL(10,2) NOT NULL DEFAULT 30, -- 0~100(%)
--    reactive_power_margin_lim DECIMAL(10,2) NOT NULL DEFAULT 30, -- 0~100(%)
--
--    operator_id INTEGER NOT NULL DEFAULT -1,
--    create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
--    update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
--);
--
--
---- 异步电动机信息
---- DROP TABLE IF EXISTS async_motor;
--CREATE TABLE async_motor (
--    id SERIAL PRIMARY KEY, 
--    motor_name VARCHAR(50) NOT NULL DEFAULT '', -- len(20) Mtr(X)
--    discription VARCHAR(200) NOT NULL DEFAULT '', -- len(100)
--    efficiency_level VARCHAR(10) NOT NULL DEFAULT 'IE3', -- IE1/IE2/IE3/IE4/IE5
--    stator_winding VARCHAR(50) NOT NULL DEFAULT 'Y', -- Y/△
--    rotor_winding VARCHAR(50) NOT NULL DEFAULT '鼠笼型', --鼠笼型/绕线型
--    service_state VARCHAR(50) NOT NULL DEFAULT '运行', -- 1 on_service; 2 out_service;
--    detail_state VARCHAR(50) NOT NULL DEFAULT '现役', -- 现役/新建/计划/检修/库存可用/库存报废
--    grid VARCHAR(50) NOT NULL DEFAULT '',
--    zone VARCHAR(50) NOT NULL DEFAULT '',
--    station VARCHAR(50) NOT NULL DEFAULT '',
--    bus_id INTEGER NOT NULL DEFAULT -1,
--    bus_name VARCHAR(50) NOT NULL DEFAULT '',
--
--    rated_voltage DECIMAL(10,2) NOT NULL DEFAULT 0.4, -- 0.4~35
--    rated_power DECIMAL(10,2) NOT NULL DEFAULT 75, -- 0.01~
--    rated_efficiency_100fl DECIMAL(10,2) NOT NULL DEFAULT 96.8, -- 0~100(%)
--    rated_efficiency_75fl DECIMAL(10,2) NOT NULL DEFAULT 97.5, -- 0~100(%)
--    rated_efficiency_50fl DECIMAL(10,2) NOT NULL DEFAULT 97.5, -- 0~100(%)
--    rated_efficiency_0fl DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~100(%)
--    rated_pf_100fl DECIMAL(10,2) NOT NULL DEFAULT 88, -- 0~100(%)
--    rated_pf_75fl DECIMAL(10,2) NOT NULL DEFAULT 85, -- 0~100(%)
--    rated_pf_50fl DECIMAL(10,2) NOT NULL DEFAULT 80, -- 0~100(%)
--    rated_pf_0fl DECIMAL(10,2) NOT NULL DEFAULT 70, -- 0~100(%)
--    full_load_current DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~100(%)
--    sf DECIMAL(10,2) NOT NULL DEFAULT 1, -- 0~10
--    no_load_current DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~999
--    rated_rotor_current DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0.01~
--    rated_open_voltage DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0.4~35
--
--    startup_current_ratio_lst DECIMAL(10,2) NOT NULL DEFAULT 6.5, -- 1~20
--    startup_current_lst DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0.01~65536
--    startup_time_100un DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~999
--    startup_time_80un DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~999
--    pf DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~100(%)
--    xr DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~100
--    poles SMALLINT NOT NULL DEFAULT 4, -- 1~200
--    synchronous_speed INTEGER NOT NULL DEFAULT 1500, -- 30~3000
--    rated_speed INTEGER NOT NULL DEFAULT 1498, -- 0~3000
--    rated_slip DECIMAL(10,2) NOT NULL DEFAULT 98.5, -- 1~100(%)
--    rated_torque DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0.01~
--    max_torque_pct DECIMAL(10,2) NOT NULL DEFAULT 200, -- 100~500
--    stall_torque_pct DECIMAL(10,2) NOT NULL DEFAULT 60, -- 1~100
--
--    model_args JSONB NOT NULL DEFAULT '', -- 
--    no_load_u DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~35000
--    no_load_i DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~65536
--    no_load_p DECIMAL(10,2) NOT NULL DEFAULT 0, -- 1~65536
--    stall_f1 DECIMAL(10,2) NOT NULL DEFAULT 50, -- 0~100
--    stall_f2 DECIMAL(10,2) NOT NULL DEFAULT -1, -- 0~100
--    stall_u DECIMAL(10,2) NOT NULL DEFAULT -1, -- 1~35000
--    stall_i DECIMAL(10,2) NOT NULL DEFAULT -1, -- 1~
--    stall_p DECIMAL(10,2) NOT NULL DEFAULT -1, -- 1~
--    dc_resistance_a DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~
--    dc_resistance_b DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~
--    dc_resistance_c DECIMAL(10,2) NOT NULL DEFAULT 0, -- 0~
--
--    duty_type VARCHAR(10) NOT NULL DEFAULT 'S1', 
--    duty_detail JSONB NOT NULL DEFAULT '', 
--    thermal
--);
--
--
---- 子网（作为设备）信息
---- DROP TABLE IF EXISTS grid;
--CREATE TABLE grid (
--
--);