-- =============================================
-- 智慧水务管理系统 - 营业收费相关 DDL
-- 版本: V1
-- =============================================

-- 用水户
CREATE TABLE IF NOT EXISTS rev_customer (
    id BIGSERIAL PRIMARY KEY,
    customer_no VARCHAR(30) UNIQUE NOT NULL,
    customer_name VARCHAR(100) NOT NULL,
    customer_type VARCHAR(20) NOT NULL,      -- residential/business/enterprise/institution
    area VARCHAR(50) NOT NULL,
    address VARCHAR(300),
    phone VARCHAR(20),
    id_card VARCHAR(18),
    contract_no VARCHAR(50),
    open_date DATE,
    meter_count INT DEFAULT 0,
    status VARCHAR(20) DEFAULT 'active',     -- active/suspended/closed
    remark VARCHAR(500),
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_customer IS '用水户表';

-- 水价阶梯
CREATE TABLE IF NOT EXISTS rev_water_price (
    id BIGSERIAL PRIMARY KEY,
    customer_type VARCHAR(20) NOT NULL,      -- residential/business/enterprise/institution
    tier_no INT NOT NULL,                    -- 第几阶梯
    range_start DECIMAL(12,2) DEFAULT 0,     -- 起始水量(立方米)
    range_end DECIMAL(12,2),                 -- 结束水量(null=无上限)
    water_price DECIMAL(10,4) NOT NULL,      -- 水价(元/立方米)
    sewage_price DECIMAL(10,4) DEFAULT 0,    -- 污水处理费
    effective_date DATE NOT NULL,
    status SMALLINT DEFAULT 1,
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_water_price IS '水价阶梯配置表';

-- 水表档案
CREATE TABLE IF NOT EXISTS rev_meter (
    id BIGSERIAL PRIMARY KEY,
    meter_no VARCHAR(50) UNIQUE NOT NULL,
    customer_id BIGINT REFERENCES rev_customer(id),
    device_id BIGINT,                        -- 关联 IoT 设备
    caliber VARCHAR(10),                     -- DN15/DN20/DN40/DN80+
    meter_type VARCHAR(20),                  -- mechanical/ultrasonic/electromagnetic
    manufacturer VARCHAR(100),
    max_reading DECIMAL(10,2) DEFAULT 99999,
    initial_reading DECIMAL(10,2) DEFAULT 0,
    current_reading DECIMAL(10,2) DEFAULT 0,
    install_date DATE,
    install_address VARCHAR(300),
    status VARCHAR(20) DEFAULT 'active',     -- active/dismantled/scrapped/repaired/warehouse
    remark VARCHAR(500),
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_meter IS '水表档案表';

-- 水表操作记录（全生命周期）
CREATE TABLE IF NOT EXISTS rev_meter_log (
    id BIGSERIAL PRIMARY KEY,
    meter_id BIGINT REFERENCES rev_meter(id),
    operation_type VARCHAR(30) NOT NULL,     -- install/dismantle/repair/change/scrap/calibrate/refurbish
    old_reading DECIMAL(10,2),
    new_reading DECIMAL(10,2),
    new_meter_no VARCHAR(50),
    operator_id BIGINT,
    operator_name VARCHAR(50),
    photos JSONB,                            -- 现场照片URL数组
    remark VARCHAR(500),
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_meter_log IS '水表操作记录表（全生命周期）';

-- 抄表记录
CREATE TABLE IF NOT EXISTS rev_reading (
    id BIGSERIAL PRIMARY KEY,
    meter_id BIGINT REFERENCES rev_meter(id),
    reading_date DATE NOT NULL,
    reading_period VARCHAR(10),              -- 2026-06
    prev_reading DECIMAL(10,2),
    curr_reading DECIMAL(10,2),
    consumption DECIMAL(10,2),
    read_type VARCHAR(20) DEFAULT 'manual',  -- manual/remote/estimate
    reader_id BIGINT,
    reader_name VARCHAR(50),
    photo_urls JSONB,                        -- 拍照图片URL数组
    abnormal_flag SMALLINT DEFAULT 0,        -- 0:正常 1:异常
    verified SMALLINT DEFAULT 0,             -- 0:未审核 1:已审核
    verified_by BIGINT,
    verified_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_reading IS '抄表记录表';
CREATE INDEX IF NOT EXISTS idx_reading_period ON rev_reading(reading_period);

-- 水费账单
CREATE TABLE IF NOT EXISTS rev_bill (
    id BIGSERIAL PRIMARY KEY,
    bill_no VARCHAR(30) UNIQUE NOT NULL,
    customer_id BIGINT REFERENCES rev_customer(id),
    meter_id BIGINT REFERENCES rev_meter(id),
    reading_id BIGINT REFERENCES rev_reading(id),
    bill_period VARCHAR(10) NOT NULL,        -- 2026-06
    prev_reading DECIMAL(10,2),
    curr_reading DECIMAL(10,2),
    consumption DECIMAL(10,2),
    water_fee DECIMAL(10,2),
    sewage_fee DECIMAL(10,2),
    other_fee DECIMAL(10,2) DEFAULT 0,       -- 污水处理费/垃圾处理费等
    total_fee DECIMAL(10,2),
    paid_fee DECIMAL(10,2) DEFAULT 0,
    discount_fee DECIMAL(10,2) DEFAULT 0,
    status VARCHAR(20) DEFAULT 'pending',    -- pending/partial/paid/overdue/cancelled
    due_date DATE,
    paid_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_bill IS '水费账单表';
CREATE INDEX IF NOT EXISTS idx_bill_customer ON rev_bill(customer_id, bill_period);
CREATE INDEX IF NOT EXISTS idx_bill_status ON rev_bill(status);

-- 缴费记录
CREATE TABLE IF NOT EXISTS rev_payment (
    id BIGSERIAL PRIMARY KEY,
    bill_id BIGINT REFERENCES rev_bill(id),
    customer_id BIGINT REFERENCES rev_customer(id),
    payment_no VARCHAR(50) UNIQUE NOT NULL,
    amount DECIMAL(10,2) NOT NULL,
    pay_method VARCHAR(20),                  -- wechat/alipay/cash/bank_transfer/pos
    pay_channel VARCHAR(30),                 -- counter/app/wechat_mini/third_party
    transaction_id VARCHAR(100),
    operator_id BIGINT,
    remark VARCHAR(500),
    paid_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_payment IS '缴费记录表';

-- 报装申请
CREATE TABLE IF NOT EXISTS rev_install (
    id BIGSERIAL PRIMARY KEY,
    application_no VARCHAR(30) UNIQUE NOT NULL,
    applicant_name VARCHAR(50) NOT NULL,
    applicant_phone VARCHAR(20) NOT NULL,
    applicant_id_card VARCHAR(18),
    area VARCHAR(50),
    address VARCHAR(300),
    customer_type VARCHAR(20),               -- residential/business/enterprise
    caliber VARCHAR(10),                     -- 申请管径
    purpose VARCHAR(200),
    status VARCHAR(20) DEFAULT 'pre_apply',  -- pre_apply/engineering/pending_review/approved/rejected/completed
    survey_date DATE,
    survey_result TEXT,
    approved_by BIGINT,
    approved_at TIMESTAMP,
    completed_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);
COMMENT ON TABLE rev_install IS '报装申请表';
