SET @station_path_len_weight_percent = (
  SELECT COALESCE(MAX(REPLACE(TRIM(`value`), '%', '')), '50')
  FROM `sys_config`
  WHERE `code` = 'stationPathLenWeightPercent'
);

SET @station_path_cong_weight_percent = (
  SELECT COALESCE(MAX(REPLACE(TRIM(`value`), '%', '')), '50')
  FROM `sys_config`
  WHERE `code` = 'stationPathCongWeightPercent'
);

SET @station_path_pass_other_out_station_weight_percent = (
  SELECT COALESCE(MAX(REPLACE(TRIM(`value`), '%', '')), '100')
  FROM `sys_config`
  WHERE `code` = 'stationPathPassOtherOutStationWeightPercent'
);

SET @station_path_pass_other_out_station_force_skip = (
  SELECT COALESCE(MAX(TRIM(`value`)), '0')
  FROM `sys_config`
  WHERE `code` = 'stationPathPassOtherOutStationForceSkip'
);

UPDATE `asr_bas_station_path_profile`
SET `config_json` = JSON_SET(
  CASE
    WHEN `config_json` IS NULL OR TRIM(`config_json`) = '' THEN '{}'
    ELSE `config_json`
  END,
  '$.stationPathLenWeightPercent', CAST(@station_path_len_weight_percent AS DECIMAL(10,2)),
  '$.stationPathCongWeightPercent', CAST(@station_path_cong_weight_percent AS DECIMAL(10,2)),
  '$.stationPathPassOtherOutStationWeightPercent', CAST(@station_path_pass_other_out_station_weight_percent AS DECIMAL(10,2)),
  '$.stationPathPassOtherOutStationForceSkip',
  CASE
    WHEN LOWER(@station_path_pass_other_out_station_force_skip) IN ('1', 'true', 'yes', 'y', 'on') THEN TRUE
    ELSE FALSE
  END
)
WHERE `id` IS NOT NULL;

DELETE FROM `sys_config`
WHERE `code` IN (
  'stationPathLenWeightPercent',
  'stationPathCongWeightPercent',
  'stationPathPassOtherOutStationWeightPercent',
  'stationPathPassOtherOutStationForceSkip'
);
