create table if not exists region (
    code varchar(15) not null primary key,
    name varchar(50) not null,
    level smallint not null default 0,
    parent varchar(15) not null default '0'
);

create table if not exists user (
    created_at timestamptz not null,
    id varchar(120) not null primary key,
    username varchar(30) not null,
    password varchar(256) not null,
    reset_needed boolean not null default false,
    type smallint not null,
    enabled boolean not null default true
);

create table if not exists user_detail (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    deleted_at timestamptz,
    deleted_by varchar(100),
    id varchar(120) not null primary key,
    name varchar(100),
    abbr varchar(50),
    region varchar(10),
    address varchar(120),
    contact varchar(100),
    phone varchar(50),
    unit_service_fee numeric(8,2) not null default 0,
    service_expiration date not null
);

create table if not exists user_charge (
    created_at timestamptz not null,
    seq bigint not null primary key,
    user_id varchar(120) not null,
    fee numeric(12,2),
    discount numeric(5,4),
    amount numeric(12,2),
    charge_to date not null,
    settled boolean not null default false,
    settled_at timestamptz,
    cancelled boolean not null default false,
    cancelled_at timestamptz,
    refunded boolean not null default false,
    refunded_at timestamptz
);

create table if not exists park (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    deleted_at timestamptz,
    deleted_by varchar(100),
    id varchar(120) not null primary key,
    user_id varchar(120) not null,
    name varchar(70) not null,
    abbr varchar(50),
    area numeric(14,2),
    tenement_quantity numeric(8,0),
    capacity numeric(16,2),
    category smallint not null default 0,
    meter_04kv_type smallint not null default 0,
    region varchar(10),
    address varchar(120),
    contact varchar(100),
    phone varchar(50),
    enabled boolean not null default true
);

create table if not exists meter_04kv (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    code varchar(120) not null,
    park_id varchar(120) not null,
    address varchar(100),
    customer_name varchar(100),
    contact_name varchar(70),
    contact_phone varchar(50),
    ratio numeric(8,4) not null default 1,
    seq bigint not null default 0,
    public_meter boolean not null default false,
    dilute boolean not null default false,
    enabled boolean not null default true,
    primary key (code, park_id)
);

create table if not exists maintenance_fee (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    deleted_at timestamptz,
    deleted_by varchar(100),
    id varchar(120) not null primary key,
    park_id varchar(120) not null,
    name varchar(50) not null,
    fee numeric(8,2) not null default 0,
    memo text,
    enabled boolean not null default true
);

create table if not exists report (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    id varchar(120) not null primary key,
    park_id varchar(120) not null,
    period date not null,
    category smallint not null default 0,
    meter_04kv_type smallint not null default 0,
    step_state jsonb not null,
    published boolean not null default false,
    published_at timestamptz,
    withdraw smallint not null default 0,
    last_withdraw_applied_at timestamptz,
    last_withdraw_audit_at timestamptz
);

create table if not exists report_summary (
    report_id varchar(120) not null primary key,
    overall numeric(14,2) not null default 0,
    overall_fee numeric(14,2) not null default 0,
    consumption_fee numeric(14,2),
    overall_price numeric(16,8),
    critical numeric(14,2) not null default 0,
    critical_fee numeric(14,2) not null default 0,
    critical_price numeric(16,8),
    peak numeric(14,2) not null default 0,
    peak_fee numeric(14,2) not null default 0,
    peak_price numeric(16,8),
    flat numeric(14,2) not null default 0,
    flat_fee numeric(14,2) not null default 0,
    flat_price numeric(16,8),
    valley numeric(14,2) not null default 0,
    valley_fee numeric(14,2) not null default 0,
    valley_price numeric(16,8),
    loss numeric(14,2),
    loss_fee numeric(16,2),
    loss_proportion numeric(16,15),
    customer_consumption numeric(16,2),
    customer_consumption_fee numeric(14,2),
    customer_consumption_critical numeric(16,2),
    customer_consumption_critical_fee numeric(14,2),
    customer_consumption_peak numeric(16,2),
    customer_consumption_peak_fee numeric(14,2),
    customer_consumption_flat numeric(16,2),
    customer_consumption_flat_fee numeric(14,2),
    customer_consumption_valley numeric(16,2),
    customer_consumption_valley_fee numeric(14,2),
    public_consumption numeric(16,2),
    public_consumption_fee numeric(14,2),
    public_consumption_proportion numeric(16,15),
    public_consumption_critical numeric(16,2),
    public_consumption_critical_fee numeric(14,2),
    public_consumption_peak numeric(16,2),
    public_consumption_peak_fee numeric(14,2),
    public_consumption_flat numeric(16,2),
    public_consumption_flat_fee numeric(14,2),
    public_consumption_valley numeric(16,2),
    public_consumption_valley_fee numeric(14,2),
    basic_fee numeric(14,2) not null default 0,
    basic_diluted_price numeric(18,8),
    adjust_fee numeric(14,2) not null default 0,
    adjust_diluted_price numeric(18,8),
    maintenance_diluted_price numeric(16,8),
    loss_diluted_price numeric(16,8),
    public_consumption_diluted_price numeric(16,8),
    maintenance_overall numeric(16,8),
    final_diluted_overall numeric(14,2)
);

create table if not exists will_diluted_fee (
    id varchar(120) not null primary key,
    report_id varchar(120) not null,
    source_id varchar(120),
    name varchar(50) not null,
    fee numeric(8,2) not null default 0,
    memo text
);

create table if not exists end_user_detail (
    created_at timestamptz not null,
    created_by varchar(100),
    last_modified_at timestamptz,
    last_modified_by varchar(100),
    report_id varchar(120) not null,
    park_id varchar(120) not null,
    meter_04kv_id varchar(120) not null,
    seq bigint not null default 0,
    ratio numeric(8,4) not null default 1,
    address varchar(100),
    customer_name varchar(100),
    contact_name varchar(70),
    contact_phone varcahar(50),
    public_meter boolean not null default false,
    dilute boolean not null default false,
    last_period_overall numeric(14,2) not null default 0,
    last_period_critical numeric(14,2) not null default 0,
    last_period_peak numeric(14,2) not null default 0,
    last_period_flat numeric(14,2) not null default 0,
    last_period_valley numeric(14,2) not null default 0,
    current_period_overall numeric(14,2) not null default 0,
    current_period_critical numeric(14,2) not null default 0,
    current_period_peak numeric(14,2) not null default 0,
    current_period_flat numeric(14,2) not null default 0,
    current_period_valley numeric(14,2) not null default 0,
    adjust_overall numeric(14,2) not null default 0,
    adjust_critical numeric(14,2) not null default 0,
    adjust_peak numeric(14,2) not null default 0,
    adjust_flat numeric(14,2) not null default 0,
    adjust_valley numeric(14,2) not null default 0,
    overall numeric(14,2),
    overall_fee numeric(14,2),
    overall_proportion numeric(16,15) not null default 0,
    critical numeric(14,2),
    critical_fee numeric(18,8),
    peak numeric(14,2),
    peak_fee numeric(18,8),
    flat numeric(14,2),
    flat_fee numeric(18,8),
    valley numeric(14,2),
    valley_fee numeric(18,8),
    basic_fee_diluted numeric(18,8),
    adjust_fee_diluted numeric(18,8),
    loss_diluted numeric(18,8),
    loss_fee_diluted numeric(18,8),
    maintenance_fee_diluted numeric(18,8),
    public_consumption_diluted numeric(18,8),
    final_diluted numeric(14,2),
    final_charge numeric(14,2),
    primary key (report_id, park_id, meter_04kv_id)
);