sample_customer_service_register.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """客户服务快捷登记:payload 规范化与字典回写(逐步扩展)。"""
  2. from typing import Any, Dict, FrozenSet, Optional
  3. CUSTOMER_SERVICE_FIELD_KEYS: FrozenSet[str] = frozenset(
  4. {
  5. "sms_send_date",
  6. "sms_send_status",
  7. "microbe_followup_date",
  8. "microbe_followup_status",
  9. "fullmoon_followup_date",
  10. "fullmoon_followup_status",
  11. "household_registered",
  12. "einvoice_send_time",
  13. "effective_address",
  14. }
  15. )
  16. def normalize_dict_value(raw) -> Optional[str]:
  17. """Trim; empty/None -> None."""
  18. if raw is None:
  19. return None
  20. text = str(raw).strip()
  21. return text or None
  22. def _is_empty_merge_value(value: Any) -> bool:
  23. if value is None:
  24. return True
  25. if isinstance(value, str):
  26. return not value.strip()
  27. return False
  28. def merge_customer_service_fields(
  29. existing: Optional[Dict[str, Any]],
  30. incoming: Optional[Dict[str, Any]],
  31. ) -> Dict[str, Any]:
  32. """Merge incoming onto existing; empty/None/whitespace incoming values do not overwrite."""
  33. existing = existing or {}
  34. incoming = incoming or {}
  35. keys = (set(existing.keys()) | set(incoming.keys())) & CUSTOMER_SERVICE_FIELD_KEYS
  36. merged: Dict[str, Any] = {k: existing.get(k) for k in keys}
  37. for key, value in incoming.items():
  38. if key not in CUSTOMER_SERVICE_FIELD_KEYS:
  39. continue
  40. if _is_empty_merge_value(value):
  41. continue
  42. merged[key] = value
  43. return merged
  44. def has_any_customer_service_payload(incoming: Optional[Dict[str, Any]]) -> bool:
  45. """True if any known customer-service field has a non-empty value."""
  46. incoming = incoming or {}
  47. for key in CUSTOMER_SERVICE_FIELD_KEYS:
  48. if key not in incoming:
  49. continue
  50. if not _is_empty_merge_value(incoming.get(key)):
  51. return True
  52. return False