提交
This commit is contained in:
1 parent
6dc8cfb2c1
commit
d26fb23fcf
181 files changed
+25727
-24557
No files matched your search
@@ -0,0 +1,129 @@
|
||||
"""业务页字典 ensure:白名单内按 label 复用或插入 sys_dict_data。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Dict, Optional, Set
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.utils import generate_uuid
|
||||
from app.models.models import SysDictData
|
||||
|
||||
DICT_ENSURE_WHITELIST: Set[str] = {
|
||||
"crm_product_gg",
|
||||
"crm_shfs",
|
||||
"crm_product_unit",
|
||||
"crm_customer_park",
|
||||
}
|
||||
|
||||
|
||||
class DictEnsureError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def ensure_dict_data(
|
||||
db: Session,
|
||||
*,
|
||||
tenant_no: str,
|
||||
dict_type: str,
|
||||
label: str,
|
||||
user_id: str = "",
|
||||
preferred_value: Optional[str] = None,
|
||||
) -> Dict[str, object]:
|
||||
label = (label or "").strip()
|
||||
if not label:
|
||||
raise DictEnsureError("字典标签不能为空")
|
||||
if dict_type not in DICT_ENSURE_WHITELIST:
|
||||
raise DictEnsureError("不支持的字典类型")
|
||||
|
||||
preferred = (preferred_value or "").strip() or label
|
||||
|
||||
existing_label = (
|
||||
db.query(SysDictData)
|
||||
.filter(
|
||||
SysDictData.tenant_no == tenant_no,
|
||||
SysDictData.dict_type == dict_type,
|
||||
SysDictData.dict_label == label,
|
||||
SysDictData.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing_label:
|
||||
return {
|
||||
"label": existing_label.dict_label,
|
||||
"value": existing_label.dict_value,
|
||||
"created": False,
|
||||
}
|
||||
|
||||
by_value = (
|
||||
db.query(SysDictData)
|
||||
.filter(
|
||||
SysDictData.tenant_no == tenant_no,
|
||||
SysDictData.dict_type == dict_type,
|
||||
SysDictData.dict_value == preferred,
|
||||
SysDictData.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if by_value:
|
||||
return {
|
||||
"label": by_value.dict_label,
|
||||
"value": by_value.dict_value,
|
||||
"created": False,
|
||||
}
|
||||
|
||||
obj = SysDictData(
|
||||
id=generate_uuid(),
|
||||
dict_label=label,
|
||||
dict_value=preferred,
|
||||
dict_type=dict_type,
|
||||
tag_type="",
|
||||
tenant_no=tenant_no,
|
||||
order_num=0,
|
||||
remark=None,
|
||||
create_by=user_id or None,
|
||||
create_dept_id="",
|
||||
create_time=datetime.now(),
|
||||
update_by=user_id or None,
|
||||
update_time=datetime.now(),
|
||||
del_flag=0,
|
||||
)
|
||||
db.add(obj)
|
||||
db.flush()
|
||||
return {"label": obj.dict_label, "value": obj.dict_value, "created": True}
|
||||
|
||||
|
||||
def ensure_remove_dict_data(
|
||||
db: Session,
|
||||
*,
|
||||
tenant_no: str,
|
||||
dict_type: str,
|
||||
value: str,
|
||||
user_id: str = "",
|
||||
) -> None:
|
||||
value = (value or "").strip()
|
||||
if not value:
|
||||
raise DictEnsureError("字典值不能为空")
|
||||
if dict_type not in DICT_ENSURE_WHITELIST:
|
||||
raise DictEnsureError("不支持的字典类型")
|
||||
|
||||
row = (
|
||||
db.query(SysDictData)
|
||||
.filter(
|
||||
SysDictData.tenant_no == tenant_no,
|
||||
SysDictData.dict_type == dict_type,
|
||||
SysDictData.dict_value == value,
|
||||
SysDictData.del_flag == 0,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not row:
|
||||
raise DictEnsureError("字典数据不存在")
|
||||
if (row.create_by or "") != (user_id or ""):
|
||||
raise DictEnsureError("无权删除该字典项")
|
||||
|
||||
row.del_flag = 1
|
||||
row.update_by = user_id or None
|
||||
row.update_time = datetime.now()
|
||||
db.add(row)
|
||||
db.flush()
|
||||
Reference in new issue
Block a user