from __future__ import annotations from decimal import Decimal, InvalidOperation from typing import Any from .mappings import customer_type_map, fabric_type_map, product_type_map from .models import Customer, Fabric, Product def pick_customer(fields: dict[str, Any]) -> Customer: """从字段字典中提取客户信息""" data = {k: fields.get(v, "") for k, v in customer_type_map.items()} return Customer(**data) def pick_product(fields: dict[str, Any]) -> Product: """从字段字典中提取产品信息""" data = {k: fields.get(v, "") for k, v in product_type_map.items()} def _to_int(value): if value in ("", None): return None try: return int(value) except (TypeError, ValueError): return None def _to_decimal(value): if value in ("", None): return None try: return Decimal(str(value)) except (InvalidOperation, TypeError, ValueError): return None data["pieces"] = _to_int(data.get("pieces")) data["segment_size"] = _to_decimal(data.get("segment_size")) return Product(**data) def pick_fabric(fields: dict[str, Any]) -> Fabric: """从字段字典中提取面料信息""" data = {k: fields.get(v, "") for k, v in fabric_type_map.items()} return Fabric(**data)