-- ============================================
-- V2.3__space_node_equipment_fields.sql
-- 同步 mdm_space_node 表与 SpaceNode 实体
-- ============================================

-- 添加基础字段（如果有缺失）
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS parent_id UUID;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS project_id UUID;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS tree_path VARCHAR(1000);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS tree_path_name VARCHAR(1000);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS full_name VARCHAR(200);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS short_name VARCHAR(100);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS node_category VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS usage_type VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS building_area DECIMAL(12,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS usable_area DECIMAL(12,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS shared_area DECIMAL(12,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS land_area DECIMAL(12,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS floor_number INTEGER;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS level INTEGER;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS province VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS city VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS district VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS street VARCHAR(200);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS longitude DECIMAL(10,6);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS latitude DECIMAL(10,6);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS altitude DECIMAL(10,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS delivery_status VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS decoration_status VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN DEFAULT FALSE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS created_by UUID;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS updated_by UUID;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS attributes TEXT;

-- 设备扩展字段
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS is_equipment BOOLEAN DEFAULT FALSE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS design_life_years INTEGER;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS rated_power DECIMAL(10,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS rated_voltage DECIMAL(10,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS rated_current DECIMAL(10,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_contract_no VARCHAR(100);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_contract_start DATE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_contract_end DATE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_vendor VARCHAR(200);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_vendor_phone VARCHAR(20);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS maintenance_vendor_contact VARCHAR(100);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS special_equipment_type VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS special_equipment_cert VARCHAR(100);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS inspection_cycle INTEGER;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS next_inspection_date DATE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS last_inspection_date DATE;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS last_inspection_result VARCHAR(20);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS common_spare_parts TEXT;
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS energy_consumption_standard DECIMAL(12,2);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS installation_environment VARCHAR(50);
ALTER TABLE mdm_space_node ADD COLUMN IF NOT EXISTS protection_level VARCHAR(20);

-- 添加索引
CREATE INDEX IF NOT EXISTS idx_space_node_project ON mdm_space_node(project_id);
CREATE INDEX IF NOT EXISTS idx_space_node_parent ON mdm_space_node(parent_id);
CREATE INDEX IF NOT EXISTS idx_space_node_type ON mdm_space_node(node_type);

-- ============================================
-- ProjectConfig 表（如不存在则创建）
-- ============================================
CREATE TABLE IF NOT EXISTS mdm_project_config (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id UUID NOT NULL,
    enable_reservation BOOLEAN DEFAULT FALSE,
    enable_visitor BOOLEAN DEFAULT FALSE,
    enable_complaint BOOLEAN DEFAULT TRUE,
    enable_payment BOOLEAN DEFAULT FALSE,
    enable_announcement BOOLEAN DEFAULT TRUE,
    enable_survey BOOLEAN DEFAULT FALSE,
    enable_vote BOOLEAN DEFAULT FALSE,
    enable_maintenance BOOLEAN DEFAULT TRUE,
    enable_asset BOOLEAN DEFAULT FALSE,
    custom_config TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_project_config_project ON mdm_project_config(project_id);
