dingdanquanliucheng/backend/app/models/system.py
taiyi 34e9be8912 为 core 层和 models 层添加中文注释
- core: config、security、exceptions、responses、error_codes、db、main
- models: base、business(17个模型类)、system(6个模型类)、__init__
- 每个函数和类均标注作用、返回值、被调用方

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 06:57:21 +08:00

112 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""系统管理相关 ORM 模型。
包含用户、角色、菜单、系统配置、审计日志等系统级数据表定义。
"""
from sqlalchemy import DateTime, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column
from backend.app.db import Base
from backend.app.models.base import TimestampMixin
class User(TimestampMixin, Base):
"""系统用户表sys_user
存储登录账号、密码哈希、真实姓名、手机号、角色关联等信息。
open_id 用于微信小程序订阅消息推送。
"""
__tablename__ = "sys_user"
id: Mapped[int] = mapped_column(primary_key=True)
username: Mapped[str] = mapped_column(String(64), unique=True, index=True)
password_hash: Mapped[str] = mapped_column(String(255))
real_name: Mapped[str] = mapped_column(String(64))
mobile: Mapped[str] = mapped_column(String(32), default="")
role_id: Mapped[int] = mapped_column(Integer, index=True)
open_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
status: Mapped[int] = mapped_column(Integer, default=1)
class Role(TimestampMixin, Base):
"""角色表sys_role
定义系统角色(如 admin、manager、salesman
role_code 用于权限判断和路由守卫。
"""
__tablename__ = "sys_role"
id: Mapped[int] = mapped_column(primary_key=True)
role_name: Mapped[str] = mapped_column(String(64))
role_code: Mapped[str] = mapped_column(String(64), unique=True, index=True)
status: Mapped[int] = mapped_column(Integer, default=1)
remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
class RoleMenu(TimestampMixin, Base):
"""角色-菜单关联表sys_role_menu
多对多关系,记录每个角色可以访问哪些菜单/权限。
"""
__tablename__ = "sys_role_menu"
id: Mapped[int] = mapped_column(primary_key=True)
role_id: Mapped[int] = mapped_column(Integer, index=True)
menu_id: Mapped[int] = mapped_column(Integer, index=True)
class Menu(TimestampMixin, Base):
"""菜单权限表sys_menu
支持树形结构parent_id用于前端侧边栏渲染和后端权限校验。
permission_code 是唯一的权限标识,前端路由守卫据此判断访问权限。
"""
__tablename__ = "sys_menu"
id: Mapped[int] = mapped_column(primary_key=True)
parent_id: Mapped[int] = mapped_column(Integer, default=0)
menu_name: Mapped[str] = mapped_column(String(64))
menu_path: Mapped[str] = mapped_column(String(128), default="")
menu_type: Mapped[str] = mapped_column(String(32), default="page")
permission_code: Mapped[str] = mapped_column(String(128), unique=True, index=True)
icon: Mapped[str | None] = mapped_column(String(64), nullable=True)
sort_no: Mapped[int] = mapped_column(Integer, default=0)
status: Mapped[int] = mapped_column(Integer, default=1)
class SystemConfig(TimestampMixin, Base):
"""系统配置表system_config
存储可动态调整的业务参数,如物流超时时间、沉默客户天数等。
前端配置中心页面读取和修改此表。
"""
__tablename__ = "system_config"
id: Mapped[int] = mapped_column(primary_key=True)
config_key: Mapped[str] = mapped_column(String(128), unique=True, index=True)
config_value: Mapped[str] = mapped_column(String(2000), default="")
config_name: Mapped[str] = mapped_column(String(128))
remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
status: Mapped[int] = mapped_column(Integer, default=1)
class AuditLog(Base):
"""审计日志表audit_log
记录所有关键业务操作(创建、修改、审批、状态变更等),
用于追溯操作历史和合规审计。before_value/after_value 存储变更前后的 JSON 快照。
"""
__tablename__ = "audit_log"
id: Mapped[int] = mapped_column(primary_key=True)
operator_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
operator_name: Mapped[str | None] = mapped_column(String(64), nullable=True)
operate_time: Mapped[DateTime] = mapped_column(DateTime, server_default=func.now(), index=True)
operate_type: Mapped[str] = mapped_column(String(64))
biz_type: Mapped[str] = mapped_column(String(64), index=True)
biz_id: Mapped[int] = mapped_column(Integer, index=True)
before_value: Mapped[str | None] = mapped_column(String(4000), nullable=True)
after_value: Mapped[str | None] = mapped_column(String(4000), nullable=True)
result: Mapped[str] = mapped_column(String(32), default="success")
remark: Mapped[str | None] = mapped_column(String(1000), nullable=True)