-- 营业收费系统表结构
-- 客户信息表
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) DEFAULT 'residential', -- residential/business/enterprise/institution
    area VARCHAR(50),
    address VARCHAR(300),
    phone VARCHAR(20),
    id_card VARCHAR(18),
    contract_no VARCHAR(50),
    status VARCHAR(20) DEFAULT 'active',
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- 水表档案表
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...
    meter_type VARCHAR(20), -- mechanical/ultrasonic/electromagnetic
    initial_reading DECIMAL(10,2),
    install_date DATE,
    status VARCHAR(20) DEFAULT 'active', -- active/dismantled/scrapped/repaired
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- 抄表记录表
CREATE TABLE IF NOT EXISTS rev_reading (
    id BIGSERIAL PRIMARY KEY,
    meter_id BIGINT REFERENCES rev_meter(id),
    reading_date DATE NOT NULL,
    prev_reading DECIMAL(10,2),
    curr_reading DECIMAL(10,2),
    consumption DECIMAL(10,2), -- 用水量
    read_type VARCHAR(20), -- manual/remote/estimate
    reader_id BIGINT,
    photo_url VARCHAR(500),
    verified TINYINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- 水费账单表
CREATE TABLE IF NOT EXISTS rev_bill (
    id BIGSERIAL PRIMARY KEY,
    customer_id BIGINT REFERENCES rev_customer(id),
    bill_period VARCHAR(10) NOT NULL, -- 2026-06
    consumption DECIMAL(10,2),
    water_fee DECIMAL(10,2),
    sewage_fee DECIMAL(10,2),
    total_fee DECIMAL(10,2),
    paid_fee DECIMAL(10,2) DEFAULT 0,
    status VARCHAR(20) DEFAULT 'pending', -- pending/partial/paid/overdue
    due_date DATE,
    paid_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    UNIQUE(customer_id, bill_period)
);

-- 报装申请表
CREATE TABLE IF NOT EXISTS rev_install (
    id BIGSERIAL PRIMARY KEY,
    app_no VARCHAR(50) UNIQUE NOT NULL,
    customer_name VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    area VARCHAR(50) NOT NULL,
    address VARCHAR(300) NOT NULL,
    customer_type VARCHAR(20) NOT NULL,
    caliber VARCHAR(10) NOT NULL,
    status VARCHAR(20) DEFAULT 'pre_apply', -- pre_apply/engineering/completed/terminated
    apply_time TIMESTAMP DEFAULT NOW(),
    complete_time TIMESTAMP,
    engineer_id BIGINT,
    remark TEXT,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW()
);

-- 知识库字典类型
INSERT INTO sys_dict_type (dict_key, dict_name, status, created_at) VALUES 
('knowledge_base', '客服知识库', 1, NOW())
ON CONFLICT (dict_key) DO NOTHING;

-- 知识库字典数据
INSERT INTO sys_dict_data (dict_type_id, dict_label, dict_value, dict_sort, status) 
VALUES (
    (SELECT id FROM sys_dict_type WHERE dict_key = 'knowledge_base'),
    '水费缴纳方式', 
    '支持微信、支付宝、银行卡等多种缴费方式，可通过微信公众号、营业厅或自助终端缴纳。',
    1, 
    1
),
(
    (SELECT id FROM sys_dict_type WHERE dict_key = 'knowledge_base'),
    '水费计算规则', 
    '水费 = 基本水费 + 超额水费 + 污水处理费。阶梯水价：第一级0-12m³/户，第二级12-24m³/户，第三级24m³以上/户。',
    2, 
    1
),
(
    (SELECT id FROM sys_dict_type WHERE dict_key = 'knowledge_base'),
    '报装流程', 
    '1. 提交申请 2. 现场勘查 3. 方案制定 4. 工程施工 5. 验收通水 6. 资料归档。一般7-15个工作日完成。',
    3, 
    1
),
(
    (SELECT id FROM sys_dict_type WHERE dict_key = 'knowledge_base'),
    '水质问题处理', 
    '如发现水质异常，请立即拨打客服热线400-123-4567，我们会安排工作人员24小时内上门处理。',
    4, 
    1
)
ON CONFLICT (dict_value) DO NOTHING;

-- 公告板字典类型
INSERT INTO sys_dict_type (dict_key, dict_name, status, created_at) VALUES 
('notice_water_stop', '停水公告', 1, NOW()),
('notice_water_quality', '水质公告', 1, NOW()),
('notice_service', '服务通知', 1, NOW())
ON CONFLICT (dict_key) DO NOTHING;

-- 示例停水公告
INSERT INTO sys_dict_data (dict_type_id, dict_label, dict_value, created_at) 
VALUES (
    (SELECT id FROM sys_dict_type WHERE dict_key = 'notice_water_stop'),
    '精芒片区计划停水通知',
    '因管道维修，精芒片区将于2026年6月15日9:00-17:00停水，请提前储水。'
) ON CONFLICT (dict_value) DO NOTHING;

INSERT INTO sys_dict_data (dict_type_id, dict_label, dict_value, created_at) 
VALUES (
    (SELECT id FROM sys_dict_type WHERE dict_key = 'notice_service'),
    '营业厅服务时间调整',
    '精河营业厅周末服务时间调整为9:00-17:00，欢迎大家前来办理业务。'
) ON CONFLICT (dict_value) DO NOTHING;

-- 示例数据
-- 创建一些测试客户
INSERT INTO rev_customer (customer_no, customer_name, phone, area, address) VALUES 
('C001', '张三', '13812345678', '精芒片区', '精河县精芒街道123号'),
('C002', '李四', '13987654321', '托里片区', '精河县托里路456号'),
('C003', '王五', '13555666777', '八家户片区', '精河县八家户街789号')
ON CONFLICT (customer_no) DO NOTHING;

-- 创建测试水表
INSERT INTO rev_meter (meter_no, customer_id, caliber, meter_type, install_date) VALUES 
('M001', 1, 'DN15', 'mechanical', '2025-01-01'),
('M002', 2, 'DN20', 'electromagnetic', '2025-02-01'),
('M003', 3, 'DN15', 'ultrasonic', '2025-03-01')
ON CONFLICT (meter_no) DO NOTHING;

-- 创建测试抄表记录
INSERT INTO rev_reading (meter_id, reading_date, prev_reading, curr_reading, consumption, read_type) VALUES 
(1, '2026-05-01', 1000.00, 1100.00, 100.00, 'remote'),
(2, '2026-05-01', 2000.00, 2100.00, 100.00, 'manual'),
(3, '2026-05-01', 3000.00, 3200.00, 200.00, 'remote')
ON CONFLICT (id) DO NOTHING;

-- 创建测试账单
INSERT INTO rev_bill (customer_id, bill_period, consumption, water_fee, sewage_fee, total_fee, status, due_date) VALUES 
(1, '2026-05', 100.00, 45.00, 15.00, 60.00, 'pending', '2026-06-20'),
(2, '2026-05', 100.00, 45.00, 15.00, 60.00, 'paid', '2026-06-15'),
(3, '2026-05', 200.00, 90.00, 30.00, 120.00, 'overdue', '2026-06-10')
ON CONFLICT (id) DO NOTHING;

-- 创建测试报装申请
INSERT INTO rev_install (app_no, customer_name, phone, area, address, customer_type, caliber, status) VALUES 
('A001', '赵六', '13666777888', '大镇阿合其片区', '精河县大镇路999号', 'residential', 'DN15', 'completed'),
('A002', '钱七', '13777888999', '托托片区', '精河县托托街111号', 'business', 'DN20', 'engineering')
ON CONFLICT (app_no) DO NOTHING;