-- Align GB/T 32960 business terminology and concentration units with the
-- authoritative protocol-field reference. RAW ppm remains available as
-- evidence, while catalog, charts and new alert rules use percent.
INSERT INTO vehicle_metric_definition
(metric_key,label,description,unit,category,value_type,protocols_json,searchable,chartable,alertable,enabled,sort_order) VALUES
('hydrogen_concentration_percent','最高氢浓度','氢气浓度最高值','%','fuel-cell','numeric','["GB32960"]',1,1,1,1,170)
ON DUPLICATE KEY UPDATE
label=VALUES(label),description=VALUES(description),unit=VALUES(unit),category=VALUES(category),
value_type=VALUES(value_type),protocols_json=VALUES(protocols_json),searchable=VALUES(searchable),
chartable=VALUES(chartable),alertable=VALUES(alertable),enabled=VALUES(enabled),sort_order=VALUES(sort_order);

INSERT INTO vehicle_metric_protocol_mapping(metric_key,protocol,source_field) VALUES
('hydrogen_concentration_percent','GB32960','gb32960.fuel_cell.max_hydrogen_concentration_percent')
ON DUPLICATE KEY UPDATE source_field=VALUES(source_field);

UPDATE vehicle_metric_definition
SET label='最高氢浓度原始值', description='协议原始浓度计数，仅用于历史证据兼容；业务展示使用百分比', enabled=0
WHERE metric_key='hydrogen_concentration_ppm';

DELETE FROM vehicle_alert_candidate
WHERE rule_id IN (SELECT id FROM vehicle_alert_rule WHERE metric='hydrogen_concentration_ppm');

DELETE FROM vehicle_alert_rule_state
WHERE rule_id IN (SELECT id FROM vehicle_alert_rule WHERE metric='hydrogen_concentration_ppm');

UPDATE vehicle_alert_rule
SET metric='hydrogen_concentration_percent',
    threshold_value=threshold_value / 10000,
    threshold_high=threshold_high / 10000,
    recovery_threshold=recovery_threshold / 10000,
    description=REPLACE(description, 'ppm', '%'),
    version=version + 1
WHERE metric='hydrogen_concentration_ppm';

UPDATE vehicle_metric_definition
SET label='车辆状态', description='车辆运行、停止等状态编码'
WHERE metric_key='vehicle_status';

UPDATE vehicle_metric_definition
SET label='充电状态', description='车辆充电状态编码'
WHERE metric_key='charge_status';

UPDATE vehicle_metric_definition
SET description='动力系统总电压'
WHERE metric_key='total_voltage_v';

UPDATE vehicle_metric_definition
SET description='动力系统总电流'
WHERE metric_key='total_current_a';

UPDATE vehicle_metric_definition
SET description='高压系统绝缘电阻'
WHERE metric_key='insulation_kohm';

UPDATE vehicle_metric_definition
SET label='氢耗', description='燃料电池氢气消耗率'
WHERE metric_key='hydrogen_consumption_kg_per_100km';

UPDATE vehicle_metric_definition
SET label='最高氢压力', description='氢系统最高压力'
WHERE metric_key='hydrogen_pressure_mpa';

UPDATE vehicle_metric_definition
SET label='氢系统最高温度', description='氢系统最高温度'
WHERE metric_key='hydrogen_temperature_c';
