refactor: destinationCountry 统一归一为中文国名

This commit is contained in:
yeuimu
2026-08-02 15:58:51 +00:00
parent 4b2ef120b5
commit 7ed1893b83
2 changed files with 58 additions and 6 deletions
+56 -4
View File
@@ -79,14 +79,66 @@ def _from_epoch_ms(value: Any) -> datetime | None:
# ---------- 国家/地区归一 ----------
_US_ALIASES = {"US", "UNITED STATES", "USA", "ESTADOS UNIDOS", "美国加州发货仓"}
_MX_ALIASES = {"MX", "MEXICO", "MÉXICO", "MEX", "ME"}
_MX_ALIASES = {"MX", "MEXICO", "MÉXICO", "MEX", "ME", "MÉXICO"}
_CA_ALIASES = {"加拿大工厂", "CA"}
_PL_ALIASES = {"欧州波兰发货仓", "欧洲波兰法兰克福发货仓"}
_WAREHOUSE_MAP = {
"印美达澳洲发货仓": "澳洲", "欧洲德国发货仓": "德国", "欧洲匈牙利发货仓": "匈牙利",
"印美达澳洲发货仓": "澳大利亚", "欧洲德国发货仓": "德国", "欧洲匈牙利发货仓": "匈牙利",
"欧洲西班牙发货仓": "西班牙", "欧洲意大利发货仓": "意大利",
"印美达美西发货仓": "美国", "印美达美东新泽西发货仓": "美国",
"印美达美西毛毯发货仓": "美国", "印美达亚特兰大发货仓": "美国",
}
_DIRECT_MAP = {"Saudi Arabia": "沙特", "Japan": "日本"}
_ISO2_CN: dict[str, str] = {
"US": "美国", "GB": "英国", "UK": "英国", "JP": "日本", "MX": "墨西哥",
"DE": "德国", "SA": "沙特", "ES": "西班牙", "BR": "巴西", "IT": "意大利",
"KR": "韩国", "CA": "加拿大", "FR": "法国", "PL": "波兰", "NL": "荷兰",
"AU": "澳大利亚", "AT": "奥地利", "PT": "葡萄牙", "BE": "比利时", "SE": "瑞典",
"CZ": "捷克", "RO": "罗马尼亚", "HU": "匈牙利", "SK": "斯洛伐克", "DK": "丹麦",
"LV": "拉脱维亚", "LT": "立陶宛", "IE": "爱尔兰", "CN": "中国", "HR": "克罗地亚",
"SI": "斯洛文尼亚", "EE": "爱沙尼亚", "GR": "希腊", "FI": "芬兰", "LU": "卢森堡",
"BG": "保加利亚", "CY": "塞浦路斯", "MT": "马耳他", "MEX": "墨西哥",
"NO": "挪威", "IS": "冰岛", "TR": "土耳其", "AE": "阿联酋", "IL": "以色列",
"NZ": "新西兰", "SG": "新加坡", "MY": "马来西亚", "TH": "泰国", "PH": "菲律宾",
"ID": "印度尼西亚", "VN": "越南", "IN": "印度", "RU": "俄罗斯", "UA": "乌克兰",
}
_EN_CN: dict[str, str] = {
"UNITED STATES": "美国", "UNITED KINGDOM": "英国", "MEXICO": "墨西哥",
"SAUDI ARABIA": "沙特", "JAPAN": "日本", "GERMANY": "德国",
"SPAIN": "西班牙", "BRAZIL": "巴西", "ITALY": "意大利", "CANADA": "加拿大",
"FRANCE": "法国", "POLAND": "波兰", "NETHERLANDS": "荷兰", "AUSTRALIA": "澳大利亚",
"CROATIA": "克罗地亚", "CZECH REPUBLIC": "捷克", "ESTADOS UNIDOS": "美国",
"SLOVAKIA (SLOVAK REPUBLIC)": "斯洛伐克", "AUSTRALIA": "澳大利亚",
}
_CN_NORMALIZE: dict[str, str] = {
"澳洲": "澳大利亚",
}
def normalize_destination_country(raw: str | None) -> str | None:
"""将 destinationCountry 归一为中文国名。"""
if not raw:
return None
trimmed = raw.strip()
if not trimmed:
return None
if trimmed in _WAREHOUSE_MAP:
return _WAREHOUSE_MAP[trimmed]
if trimmed in _DIRECT_MAP:
return _DIRECT_MAP[trimmed]
if trimmed in _PL_ALIASES:
return "波兰"
if trimmed in _CN_NORMALIZE:
return _CN_NORMALIZE[trimmed]
upper = trimmed.upper()
if upper in _ISO2_CN:
return _ISO2_CN[upper]
if upper in _EN_CN:
return _EN_CN[upper]
return trimmed
_EXCLUDED_COUNTRIES = {
"Croatia", "Czech Republic", "丹麦", "保加利亚", "克罗地亚", "卢森堡", "奥地利", "希腊",
"拉脱维亚", "捷克", "斯洛伐克", "斯洛文尼亚", "比利时", "法国", "爱尔兰", "爱沙尼亚",
@@ -187,7 +239,7 @@ def map_riin_order(raw: Any) -> OrderUpsertInput:
completedAt=completed_at,
shipDeadline=ship_deadline,
consigneeName=raw.consignee_name,
destinationCountry=raw.receiver_country,
destinationCountry=normalize_destination_country(raw.receiver_country),
stateProvince=raw.receiver_province,
city=raw.receiver_city,
addressLine1=raw.address,
@@ -287,7 +339,7 @@ def map_sds_order(raw: Any, channel_type_map: dict[int, str | None], pre_courier
completedAt=completed_at,
shipDeadline=ship_deadline,
consigneeName=addr.consignee if addr else None,
destinationCountry=addr.country if addr else None,
destinationCountry=normalize_destination_country(addr.country) if addr else None,
stateProvince=addr.province if addr else None,
city=addr.city if addr else None,
addressLine1=addr.address_detail1 if addr else None,