dingdanquanliucheng/backend/app/models/system.py

112 lines
4.6 KiB
Python
Raw Normal View History

"""系统管理相关 ORM 模型。
包含用户角色菜单系统配置审计日志等系统级数据表定义
"""
from sqlalchemy import DateTime, Integer, String, func
2026-05-14 13:51:06 +08:00
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 用于微信小程序订阅消息推送
"""
2026-05-14 13:51:06 +08:00
__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)
2026-05-26 11:36:57 +08:00
open_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
2026-05-14 13:51:06 +08:00
status: Mapped[int] = mapped_column(Integer, default=1)
class Role(TimestampMixin, Base):
"""角色表sys_role
定义系统角色 adminmanagersalesman
role_code 用于权限判断和路由守卫
"""
2026-05-14 13:51:06 +08:00
__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)
2026-05-14 13:51:06 +08:00
class Menu(TimestampMixin, Base):
"""菜单权限表sys_menu
支持树形结构parent_id用于前端侧边栏渲染和后端权限校验
permission_code 是唯一的权限标识前端路由守卫据此判断访问权限
"""
2026-05-14 13:51:06 +08:00
__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
存储可动态调整的业务参数如物流超时时间沉默客户天数等
前端配置中心页面读取和修改此表
"""
2026-05-14 13:51:06 +08:00
__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)