dict_ensure.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """业务页字典 ensure:白名单内按 label 复用或插入 sys_dict_data。"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import Dict, Optional, Set
  5. from sqlalchemy.orm import Session
  6. from app.core.utils import generate_uuid
  7. from app.models.models import SysDictData
  8. DICT_ENSURE_WHITELIST: Set[str] = {
  9. "crm_product_gg",
  10. "crm_shfs",
  11. "crm_product_unit",
  12. "crm_customer_park",
  13. }
  14. class DictEnsureError(ValueError):
  15. pass
  16. def ensure_dict_data(
  17. db: Session,
  18. *,
  19. tenant_no: str,
  20. dict_type: str,
  21. label: str,
  22. user_id: str = "",
  23. preferred_value: Optional[str] = None,
  24. ) -> Dict[str, object]:
  25. label = (label or "").strip()
  26. if not label:
  27. raise DictEnsureError("字典标签不能为空")
  28. if dict_type not in DICT_ENSURE_WHITELIST:
  29. raise DictEnsureError("不支持的字典类型")
  30. preferred = (preferred_value or "").strip() or label
  31. existing_label = (
  32. db.query(SysDictData)
  33. .filter(
  34. SysDictData.tenant_no == tenant_no,
  35. SysDictData.dict_type == dict_type,
  36. SysDictData.dict_label == label,
  37. SysDictData.del_flag == 0,
  38. )
  39. .first()
  40. )
  41. if existing_label:
  42. return {
  43. "label": existing_label.dict_label,
  44. "value": existing_label.dict_value,
  45. "created": False,
  46. }
  47. by_value = (
  48. db.query(SysDictData)
  49. .filter(
  50. SysDictData.tenant_no == tenant_no,
  51. SysDictData.dict_type == dict_type,
  52. SysDictData.dict_value == preferred,
  53. SysDictData.del_flag == 0,
  54. )
  55. .first()
  56. )
  57. if by_value:
  58. return {
  59. "label": by_value.dict_label,
  60. "value": by_value.dict_value,
  61. "created": False,
  62. }
  63. obj = SysDictData(
  64. id=generate_uuid(),
  65. dict_label=label,
  66. dict_value=preferred,
  67. dict_type=dict_type,
  68. tag_type="",
  69. tenant_no=tenant_no,
  70. order_num=0,
  71. remark=None,
  72. create_by=user_id or None,
  73. create_dept_id="",
  74. create_time=datetime.now(),
  75. update_by=user_id or None,
  76. update_time=datetime.now(),
  77. del_flag=0,
  78. )
  79. db.add(obj)
  80. db.flush()
  81. return {"label": obj.dict_label, "value": obj.dict_value, "created": True}
  82. def ensure_remove_dict_data(
  83. db: Session,
  84. *,
  85. tenant_no: str,
  86. dict_type: str,
  87. value: str,
  88. user_id: str = "",
  89. ) -> None:
  90. value = (value or "").strip()
  91. if not value:
  92. raise DictEnsureError("字典值不能为空")
  93. if dict_type not in DICT_ENSURE_WHITELIST:
  94. raise DictEnsureError("不支持的字典类型")
  95. row = (
  96. db.query(SysDictData)
  97. .filter(
  98. SysDictData.tenant_no == tenant_no,
  99. SysDictData.dict_type == dict_type,
  100. SysDictData.dict_value == value,
  101. SysDictData.del_flag == 0,
  102. )
  103. .first()
  104. )
  105. if not row:
  106. raise DictEnsureError("字典数据不存在")
  107. if (row.create_by or "") != (user_id or ""):
  108. raise DictEnsureError("无权删除该字典项")
  109. row.del_flag = 1
  110. row.update_by = user_id or None
  111. row.update_time = datetime.now()
  112. db.add(row)
  113. db.flush()