CREATE TABLE IF NOT EXISTS vehicle_open_hydrogen_stream_state (
    vin VARCHAR(64) NOT NULL,
    stat_date DATE NOT NULL,
    source_endpoint VARCHAR(128) NOT NULL DEFAULT '',
    first_mass_kg DECIMAL(18,3) NOT NULL,
    last_mass_kg DECIMAL(18,3) NOT NULL,
    consumption_kg DECIMAL(18,3) NOT NULL DEFAULT 0,
    sample_count BIGINT UNSIGNED NOT NULL DEFAULT 1,
    refuel_count INT UNSIGNED NOT NULL DEFAULT 0,
    abnormal_drop_count INT UNSIGNED NOT NULL DEFAULT 0,
    first_event_time DATETIME(3) NOT NULL,
    last_event_time DATETIME(3) NOT NULL,
    last_event_id VARCHAR(128) NOT NULL DEFAULT '',
    quality_status VARCHAR(24) NOT NULL DEFAULT 'NO_DATA',
    quality_reason VARCHAR(255) NOT NULL DEFAULT '有效车载氢量样本不足2条',
    created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (vin, stat_date, source_endpoint),
    INDEX idx_hydrogen_stream_date_quality (stat_date, quality_status, vin),
    CONSTRAINT chk_hydrogen_stream_quality CHECK (quality_status IN ('OK', 'NO_DATA', 'SUSPECT'))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
