101 lines
3.6 KiB
PHP
101 lines
3.6 KiB
PHP
<?php
|
||
namespace app\common\logic;
|
||
|
||
final class ModelNaming
|
||
{
|
||
/** 生成稳定的 model_code(只用 *_code 与关键数字) */
|
||
public static function buildModelCode(array $s): string
|
||
{
|
||
$brand = self::tok($s['brand_code'] ?? '');
|
||
$series = self::tok($s['series_code'] ?? '');
|
||
$fuel = self::tok($s['fuel_type_code'] ?? 'UNK');
|
||
|
||
$parts = [$brand, $series];
|
||
$key = [];
|
||
|
||
switch ($fuel) {
|
||
case 'EV':
|
||
$key[] = 'EV';
|
||
if (!empty($s['range_km'])) {
|
||
$key[] = (string) (round((int)$s['range_km'] / 10) * 10); // 到十位
|
||
} elseif (!empty($s['battery_kwh'])) {
|
||
$key[] = self::numToken($s['battery_kwh']) . 'KWH';
|
||
}
|
||
break;
|
||
|
||
case 'GAS':
|
||
if (!empty($s['engine_disp_l'])) $key[] = self::dispToken($s['engine_disp_l']); // 1.5L -> 15L
|
||
if (!empty($s['engine_turbo']) && (int)$s['engine_turbo'] === 1) $key[] = 'T';
|
||
if (!empty($s['transmission_code'])) $key[] = self::tok($s['transmission_code']);
|
||
break;
|
||
|
||
case 'HEV':
|
||
case 'PHEV':
|
||
case 'EREV':
|
||
$key[] = $fuel;
|
||
if (!empty($s['engine_disp_l'])) $key[] = self::dispToken($s['engine_disp_l']);
|
||
if (!empty($s['motor_layout_code'])) $key[] = self::tok($s['motor_layout_code']); // 如 DM-I/DM-P/AWD
|
||
break;
|
||
|
||
default:
|
||
$key[] = $fuel;
|
||
}
|
||
|
||
if (!empty($s['drive_type_code'])) $key[] = self::tok($s['drive_type_code']);
|
||
|
||
$parts[] = implode('_', array_filter($key));
|
||
return strtoupper(implode('_', array_filter($parts)));
|
||
}
|
||
|
||
/** 生成中文可读的 model_name(走快照中文,不依赖 i18n) */
|
||
public static function buildModelName(array $s): string
|
||
{
|
||
$seriesName = trim((string)($s['series_name'] ?? $s['series_code'] ?? ''));
|
||
$yearPart = !empty($s['model_year']) ? ($s['model_year'] . '款 ') : '';
|
||
|
||
$fuel = strtoupper($s['fuel_type_code'] ?? '');
|
||
$tail = '';
|
||
|
||
if ($fuel === 'EV') {
|
||
$range = !empty($s['range_km']) ? (int)$s['range_km'] . 'KM ' : '';
|
||
$drive = trim((string)($s['drive_type'] ?? $s['drive_type_code'] ?? ''));
|
||
$tail = "EV {$range}{$drive}";
|
||
} elseif ($fuel === 'GAS') {
|
||
$disp = (!empty($s['engine_disp_l']))
|
||
? rtrim(rtrim(number_format((float)$s['engine_disp_l'], 1, '.', ''), '0'), '.') . 'L '
|
||
: '';
|
||
$trans = trim((string)($s['transmission'] ?? $s['transmission_code'] ?? ''));
|
||
$tail = "{$disp}{$trans}";
|
||
} else {
|
||
$tail = trim((string)($s['fuel_type'] ?? $fuel));
|
||
}
|
||
|
||
$extras = [];
|
||
foreach (['trim','trim_level','edition_name'] as $k) {
|
||
if (!empty($s[$k])) $extras[] = trim((string)$s[$k]);
|
||
}
|
||
$extraStr = $extras ? ' ' . implode(' ', $extras) : '';
|
||
|
||
return trim($seriesName . ' ' . $yearPart . $tail . $extraStr);
|
||
}
|
||
|
||
/* ===== Helpers ===== */
|
||
|
||
private static function tok($v): string
|
||
{
|
||
$v = strtoupper((string)$v);
|
||
return preg_replace('/[^A-Z0-9_]/', '', $v ?? '');
|
||
}
|
||
|
||
private static function numToken($v): string
|
||
{
|
||
return preg_replace('/[^0-9]/', '', (string)$v);
|
||
}
|
||
|
||
/** 1.5L -> 15L;2.0L -> 20L(便于 code 稳定检索) */
|
||
private static function dispToken($l): string
|
||
{
|
||
$x = (float)$l;
|
||
return (string)round($x * 10) . 'L';
|
||
}
|
||
} |