Files
shanxia2.0/backend/app/services/pl_quota_units.py
T
wuzilongandCursor da3ec62719 feat(pl): 吨千克克和升毫升按固定比例折算用证量。
同质量或体积单位不再要求手填系数,普通 http 页面也能生成幂等操作号。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 21:14:15 +08:00

51 lines
1.6 KiB
Python

"""证件与计划数量单位都按字典编码比较。历史证面可能直接存了中文标签。"""
from decimal import Decimal
_LABEL_TO_CODE = {
'克': 'g',
'千克': 'kg',
'吨': 'dun',
'升': 'sheng',
'瓶': 'ping',
'袋': 'dai',
'桶': 'tong',
'毫升': 'hs',
'套': 'set',
'个': 'piece',
'台': 'unit',
}
# 同一量纲内的固定比例。数值是该单位折成最小单位后的倍数。
_MASS_IN_GRAMS = {'g': Decimal(1), 'kg': Decimal(1000), 'dun': Decimal(1000000)}
_VOLUME_IN_MILLILITRES = {'hs': Decimal(1), 'sheng': Decimal(1000)}
def canonical_quantity_unit(unit) -> str:
text = str(unit or '').strip()
return _LABEL_TO_CODE.get(text, text)
_CODE_TO_LABEL = {code: label for label, code in _LABEL_TO_CODE.items()}
def display_quantity_unit(unit) -> str:
text = str(unit or '').strip()
code = canonical_quantity_unit(text)
return _CODE_TO_LABEL.get(code, text or code)
def same_quantity_unit(left, right) -> bool:
return canonical_quantity_unit(left) == canonical_quantity_unit(right)
def standard_conversion_ratio(source_unit, permit_unit):
"""1 个计划单位等于多少证面单位。同单位、空单位和跨量纲返回 None。"""
source = canonical_quantity_unit(source_unit)
permit = canonical_quantity_unit(permit_unit)
if not source or not permit or source == permit:
return None
for table in (_MASS_IN_GRAMS, _VOLUME_IN_MILLILITRES):
if source in table and permit in table:
return table[source] / table[permit]
return None