fourcal-pms/backend/app/models/dictionary.py

104 lines
5.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""数据字典:下拉框取值的唯一存放处。
系统里的下拉框原本分三处写死:model 里的 ``LABELS``、API 里临时拼的
``[{"value": …, "label": …}]``、前端视图里的常量数组。三处各写一份的结局是
对不上——项目类型在 model 里是"工程集成类/设备集成类/战略合作类",前端列表页
自己抄成了"系统集成/软件开发/运维服务",谁都没报错,只是两边各说各话。
这一张表把取值收到库里,界面上(系统 → 数据字典)就能改:
* **``source=system``(代码字典)**:取值来自 :mod:`app.dicts` 里的代码枚举,
启动时同步进来。业务代码按 ``value`` 判分支,所以 **value 与 code 不许改**,
能改的只有显示名、排序、启停、配色;删掉的条目相当于隐藏,代码读到未知值仍会
退回代码里的名字,不会渲染成空白。
* **``source=custom``(自建字典)**:完全由界面维护,随便加删。新增一个"客户等级"
这类下拉框不必再发版。
字典是**展示与候选项**的事实来源,不是**校验**的事实来源:能不能送审、走哪条分支
仍然看代码里的常量,否则改个名字就能把状态机改出洞。
"""
from __future__ import annotations
from sqlalchemy import ForeignKey, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base, SoftDeleteMixin, TimestampMixin, u_bigint, u_tinyint
class DictSource:
"""字典来源。"""
SYSTEM = "system" # 代码枚举同步进来的,value 不可改
CUSTOM = "custom" # 界面上自建的
LABELS = {SYSTEM: "代码内置", CUSTOM: "自定义"}
class DictValueType:
"""``value`` 列的存储类型。
取值一律存字符串,读的时候按这个键还原成 int——项目阶段、账号状态这些列在库里
是数字,前端 Select 也是按数字比对的,混成字符串会出现"选了但选不上"的毛病。
"""
STRING = "string"
INT = "int"
LABELS = {STRING: "文本", INT: "整数"}
class DictType(Base, TimestampMixin, SoftDeleteMixin):
"""字典类型:一个下拉框对应一条。"""
__tablename__ = "dict_type"
__table_args__ = (
UniqueConstraint("code", name="uk_dict_type_code"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
code: Mapped[str] = mapped_column(
String(64), nullable=False, comment="字典编码,前后端按它取值(camelCase)")
name: Mapped[str] = mapped_column(String(128), nullable=False, comment="显示名称")
group: Mapped[str] = mapped_column(String(64), default="common", server_default="common",
nullable=False, index=True, comment="维护页里的分组")
value_type: Mapped[str] = mapped_column(
String(8), default=DictValueType.STRING, server_default=DictValueType.STRING,
nullable=False, comment="取值类型 string/int,决定下发时要不要转成数字")
source: Mapped[str] = mapped_column(
String(16), default=DictSource.CUSTOM, server_default=DictSource.CUSTOM, nullable=False,
comment="system=代码枚举同步,value 不可改;custom=界面自建")
enabled: Mapped[bool] = mapped_column(u_tinyint(), default=True, server_default="1",
nullable=False, comment="停用后下拉框取到空列表")
remark: Mapped[str] = mapped_column(String(500), default="", nullable=False)
class DictItem(Base, TimestampMixin, SoftDeleteMixin):
"""字典条目:下拉框里的一个选项。"""
__tablename__ = "dict_item"
__table_args__ = (
UniqueConstraint("dict_type_id", "value", name="uk_dict_item_type_value"),
Index("idx_dict_item_type_sort", "dict_type_id", "sort"),
{"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"},
)
id: Mapped[int] = mapped_column(u_bigint(), primary_key=True, autoincrement=True)
dict_type_id: Mapped[int] = mapped_column(
u_bigint(), ForeignKey("dict_type.id", ondelete="CASCADE"), nullable=False,
comment="所属字典类型")
value: Mapped[str] = mapped_column(String(128), nullable=False, comment="存给后端的值")
label: Mapped[str] = mapped_column(String(128), nullable=False, comment="给人看的名字")
sort: Mapped[int] = mapped_column(Integer, default=100, server_default="100", nullable=False,
comment="越小越靠前")
enabled: Mapped[bool] = mapped_column(u_tinyint(), default=True, server_default="1",
nullable=False, comment="停用即从下拉框里摘掉,历史数据不受影响")
tag: Mapped[str] = mapped_column(
String(16), default="", server_default="", nullable=False,
comment="状态色(success/warning/danger/info/gray),给 StatusBadge 用")
builtin: Mapped[bool] = mapped_column(u_tinyint(), default=False, server_default="0",
nullable=False, comment="代码枚举同步来的条目,value 不可改")
remark: Mapped[str] = mapped_column(String(500), default="", nullable=False,
comment="备注,界面上以下拉提示语展示")