-- OneOS 车辆品牌一次性只读校准
--
-- 边界：
-- 1. 只读取 ln_asset_management.vehicle_info / vehicle_model。
-- 2. 只更新中台 vehicle_identity_binding.oem 的空值，不覆盖已有品牌。
-- 3. 每辆车在 vehicle_identity_oem_audit 留存来源编码、车型、版本和操作人。
-- 4. 必须先应用 deploy/migrations/018_vehicle_oem_audit.sql。
-- 5. 不得把本脚本改为定时跨库同步；后续持续同步应走 OneOS 正式 API。

SET SESSION MAX_EXECUTION_TIME = 10000;
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

SELECT
    COUNT(*) AS candidate_rows,
    COUNT(DISTINCT b.vin) AS candidate_vehicles,
    COUNT(DISTINCT CONCAT(b.vin, '|', LOWER(TRIM(vm.brand)), '|', COALESCE(TRIM(vm.model), ''))) AS distinct_source_values
FROM vehicle_identity_binding b
JOIN ln_asset_management.vehicle_info vi
  ON BINARY vi.vin = BINARY b.vin
 AND vi.del_flag = '0'
JOIN ln_asset_management.vehicle_model vm
  ON vm.id = vi.vehicle_model_id
 AND vm.del_flag = '0'
WHERE COALESCE(NULLIF(TRIM(b.oem), ''), '') = ''
  AND COALESCE(NULLIF(TRIM(vm.brand), ''), '') <> '';

-- candidate_rows、candidate_vehicles、distinct_source_values 必须相等后才能继续。
START TRANSACTION;

INSERT INTO vehicle_identity_oem_audit (
    vin,
    before_oem,
    after_oem,
    source_system,
    source_version,
    source_brand_code,
    source_model_name,
    source_updated_at,
    actor
)
SELECT
    b.vin,
    COALESCE(b.oem, ''),
    CASE LOWER(TRIM(vm.brand))
        WHEN 'hyundai' THEN
            CASE
                WHEN vm.model LIKE '%帕力安%' OR vm.model LIKE '%冷链%' OR vm.model LIKE '%双飞翼%' THEN '帕力安牌'
                ELSE '现代'
            END
        WHEN 'yuejin' THEN '跃进'
        WHEN 'feichi' THEN '飞驰'
        WHEN 'sulong' THEN '苏龙'
        WHEN 'higer' THEN '海格'
        WHEN 'dongfeng' THEN '东风'
        WHEN 'yutong' THEN '宇通'
        WHEN 'chufeng' THEN '楚风'
        WHEN 'tonghua' THEN '通华'
        WHEN 'maxus' THEN '大通'
        WHEN 'mingwei' THEN '明威'
        WHEN 'wanfeng' THEN '万风'
        WHEN 'shujie' THEN '舒捷'
        WHEN 'denza' THEN '腾势'
        WHEN 'hongyan' THEN '红岩'
        WHEN 'yuanchang brand' THEN '远程牌'
        WHEN 'others' THEN '其他'
        ELSE TRIM(vm.brand)
    END,
    'oneos_vehicle_model',
    'oneos-vehicle-model-20260716',
    TRIM(vm.brand),
    COALESCE(TRIM(vm.model), ''),
    vm.update_time,
    'p1-02-master-data-calibration'
FROM vehicle_identity_binding b
JOIN ln_asset_management.vehicle_info vi
  ON BINARY vi.vin = BINARY b.vin
 AND vi.del_flag = '0'
JOIN ln_asset_management.vehicle_model vm
  ON vm.id = vi.vehicle_model_id
 AND vm.del_flag = '0'
WHERE COALESCE(NULLIF(TRIM(b.oem), ''), '') = ''
  AND COALESCE(NULLIF(TRIM(vm.brand), ''), '') <> ''
ON DUPLICATE KEY UPDATE id = vehicle_identity_oem_audit.id;

UPDATE vehicle_identity_binding b
JOIN (
    SELECT
        vi.vin,
        CASE LOWER(TRIM(vm.brand))
            WHEN 'hyundai' THEN
                CASE
                    WHEN vm.model LIKE '%帕力安%' OR vm.model LIKE '%冷链%' OR vm.model LIKE '%双飞翼%' THEN '帕力安牌'
                    ELSE '现代'
                END
            WHEN 'yuejin' THEN '跃进'
            WHEN 'feichi' THEN '飞驰'
            WHEN 'sulong' THEN '苏龙'
            WHEN 'higer' THEN '海格'
            WHEN 'dongfeng' THEN '东风'
            WHEN 'yutong' THEN '宇通'
            WHEN 'chufeng' THEN '楚风'
            WHEN 'tonghua' THEN '通华'
            WHEN 'maxus' THEN '大通'
            WHEN 'mingwei' THEN '明威'
            WHEN 'wanfeng' THEN '万风'
            WHEN 'shujie' THEN '舒捷'
            WHEN 'denza' THEN '腾势'
            WHEN 'hongyan' THEN '红岩'
            WHEN 'yuanchang brand' THEN '远程牌'
            WHEN 'others' THEN '其他'
            ELSE TRIM(vm.brand)
        END AS brand_name
    FROM ln_asset_management.vehicle_info vi
    JOIN ln_asset_management.vehicle_model vm
      ON vm.id = vi.vehicle_model_id
     AND vm.del_flag = '0'
    WHERE vi.del_flag = '0'
      AND COALESCE(NULLIF(TRIM(vi.vin), ''), '') <> ''
      AND COALESCE(NULLIF(TRIM(vm.brand), ''), '') <> ''
) master
  ON BINARY master.vin = BINARY b.vin
SET b.oem = master.brand_name
WHERE COALESCE(NULLIF(TRIM(b.oem), ''), '') = '';

COMMIT;

SELECT
    COUNT(*) AS audited_rows,
    COUNT(DISTINCT vin) AS audited_vehicles
FROM vehicle_identity_oem_audit
WHERE source_system = 'oneos_vehicle_model'
  AND source_version = 'oneos-vehicle-model-20260716';

SELECT COUNT(*) AS remaining_missing_oem
FROM vehicle_identity_binding
WHERE COALESCE(NULLIF(TRIM(oem), ''), '') = '';
