Files
workspace/code/fms/FMS新系统核心表结构设计.md
T
2026-09-09 22:40:13 +08:00

22 KiB
Raw Blame History

FMS 新系统核心表结构

1. 用户表

-- 用户表
create table dbo.b_user (
    b_id varchar(50) not null primary key, -- 用户编码(业务键主键)
    b_name nvarchar(100) null,             -- 用户名
    b_bz nvarchar(400) null,               -- 备注
    b_password nvarchar(255) not null,     -- 密码
    b_canuse tinyint not null default 1,   -- 是否启用(0/1)
    b_logintime tinyint not null default 0 -- 登录次数
);

2. 模块

-- 模块配置表
create table dbo.s_module (
    b_id varchar(50) not null primary key,   -- 模块编码(业务键主键)
    b_parent_id varchar(50) null,            -- 父级模块编码,NULL 表示根节点
    b_name nvarchar(200) not null,           -- 模块名称
    b_i18n varchar(150) null,                -- 多语言资源键
    b_module_type varchar(20) not null,      -- 模块类型
    b_viewtable varchar(128) null,           -- 视图表名(列表查询主表)
    b_savetable varchar(128) null,           -- 保存表名(写入目标表)
    b_keyfield varchar(50) null,             -- 主键字段
    b_orderfield varchar(500) null,          -- 排序字段
    b_delete_policy varchar(20) null,        -- 删除策略
    b_canuse tinyint not null default 1,     -- 是否启用(0/1)
    b_xh int not null default 0,             -- 显示顺序
    b_bz nvarchar(2000) null,                -- 备注
    b_edit_mode varchar(10) null             -- 编辑模式
);

create index ix_s_module_parent
    on dbo.s_module (b_parent_id, b_xh, b_canuse, b_id);

create index ix_s_module_order
    on dbo.s_module (b_xh, b_id);

create index ix_s_module_savetable
    on dbo.s_module (b_savetable);

3. 字段和字段布局

-- 字段定义表
create table dbo.s_field (
    b_module_id varchar(50) not null,              -- 所属模块编码
    b_field varchar(50) not null,                  -- 字段名
    b_name nvarchar(200) not null,                 -- 字段名称
    b_i18n varchar(150) null,                      -- 多语言资源键
    b_type varchar(30) not null,                   -- 字段类型(业务类型)
    b_dbtype varchar(30) not null,                 -- 数据库类型
    b_length int null,                             -- 长度
    b_precision int null,                          -- 精度
    b_scale int null,                              -- 小数位
    b_nullable tinyint not null default 1,         -- 是否可空(0/1)
    b_default_value nvarchar(1000) null,           -- 默认值
    b_db_default nvarchar(1000) null,              -- 数据库默认值
    b_writemode varchar(20) not null default 'direct', -- 写入模式
    b_options nvarchar(max) null,                  -- 选项(下拉等)
    b_canuse tinyint not null default 1,           -- 是否启用(0/1)
    b_xh int not null default 0,                   -- 显示顺序(同步字段时按视图列序重排,10 起步)
    primary key (b_module_id, b_field)
);

create index ix_s_field_module_order
    on dbo.s_field (b_module_id, b_xh, b_field);

-- 字段分组表
create table dbo.s_field_group (
    b_id varchar(50) not null,                -- 分组编码
    b_module_id varchar(50) not null,         -- 所属模块编码
    b_parent_id varchar(50) null,             -- 父级分组编码,NULL 表示根节点
    b_title nvarchar(200) not null,           -- 分组标题
    b_i18n varchar(150) null,                 -- 多语言资源键
    b_xh int not null default 0,              -- 显示顺序
    b_canuse tinyint not null default 1,      -- 是否启用(0/1)
    b_colspan int not null default 48,        -- 列跨度
    b_layout_direction varchar(10) null default 'vertical', -- 布局方向(vertical/horizontal)
    primary key (b_module_id, b_id)
);

create index ix_s_field_group_parent
    on dbo.s_field_group (b_module_id, b_parent_id, b_xh, b_id);

create index ix_s_field_group_order
    on dbo.s_field_group (b_module_id, b_xh, b_id);

-- 列表视图字段配置表
create table dbo.s_field_view (
    b_module_id varchar(50) not null,     -- 所属模块编码
    b_field varchar(50) not null,         -- 字段名
    b_group_id varchar(50) null,          -- 所属分组(同模块 s_field_group.b_id)
    b_visible tinyint not null default 1, -- 是否展示(0/1)
    b_xh int not null default 0,          -- 显示顺序
    b_width int null,                     -- 列宽
    primary key (b_module_id, b_field)
);

create index ix_s_field_view_order
    on dbo.s_field_view (b_module_id, b_xh, b_field);

-- 编辑字段配置表
create table dbo.s_field_edit (
    b_module_id varchar(50) not null,      -- 所属模块编码
    b_field varchar(50) not null,          -- 字段名
    b_group_id varchar(50) null,           -- 所属分组(同模块 s_field_group.b_id)
    b_visible tinyint not null default 1,  -- 是否展示(0/1)
    b_required tinyint not null default 0, -- 是否必填(0/1)
    b_readonly tinyint not null default 0, -- 是否只读(0/1)
    b_disabled tinyint not null default 0, -- 是否禁用(0/1)
    b_xh int not null default 0,           -- 显示顺序
    b_width int null,                      -- 宽度
    b_placeholder nvarchar(400) null,      -- 占位提示
    b_colspan int not null default 12,     -- 列跨度
    b_height int null,                     -- 高度
    primary key (b_module_id, b_field)
);

create index ix_s_field_edit_order
    on dbo.s_field_edit (b_module_id, b_xh, b_field);

-- 查询字段配置表
create table dbo.s_field_query (
    b_module_id varchar(50) not null,         -- 所属模块编码
    b_field varchar(50) not null,             -- 字段名
    b_component varchar(30) null,             -- 查询组件
    b_condition_no int not null default 1,    -- 条件序号
    b_operator varchar(20) not null,          -- 操作符
    b_default_value nvarchar(1000) null,      -- 默认值
    b_canuse tinyint not null default 1,      -- 是否启用(0/1)
    b_xh int not null default 0,              -- 显示顺序
    primary key (b_module_id, b_field, b_condition_no)
);

create index ix_s_field_query_order
    on dbo.s_field_query (b_module_id, b_xh, b_field, b_condition_no);

4. 用户个性化

-- 用户字段偏好表
create table dbo.s_user_field_pref (
    b_user_id varchar(50) not null,      -- 用户编码
    b_module_id varchar(50) not null,    -- 模块编码
    b_field varchar(50) not null,        -- 字段名
    b_mode varchar(10) not null,         -- 模式(view / edit)
    b_visible tinyint not null default 1, -- 是否展示(0/1)
    b_xh int not null default 0,         -- 显示顺序
    b_width int null,                    -- 列宽
    b_updatedatetime datetime2 null,     -- 最后更新时间
    primary key (b_user_id, b_module_id, b_field, b_mode)
);

create index ix_s_user_field_pref_order
    on dbo.s_user_field_pref (b_user_id, b_module_id, b_mode, b_xh, b_field);

-- 用户查询字段偏好表
create table dbo.s_user_query_field_pref (
    b_user_id varchar(50) not null,       -- 用户编码
    b_module_id varchar(50) not null,     -- 模块编码
    b_field varchar(50) not null,         -- 字段名
    b_quick_visible tinyint not null default 1, -- 普通查询收起态是否显示(0/1)
    b_xh int not null default 0,          -- 显示顺序
    b_updatedatetime datetime2 null,      -- 最后更新时间
    primary key (b_user_id, b_module_id, b_field)
);

create index ix_s_user_query_field_pref_order
    on dbo.s_user_query_field_pref (b_user_id, b_module_id, b_xh, b_field);

5. 自动编码

-- 自动编码表
create table dbo.s_autocode (
    b_module_id varchar(50) not null,                 -- 模块编码
    b_field varchar(50) not null,                     -- 字段名
    b_prefix nvarchar(200) not null default N'',      -- 编码前缀
    b_dateformat varchar(30) not null default 'yyyyMM', -- 日期格式
    b_separator nvarchar(20) not null default N'-',   -- 分隔符
    b_seqwidth int not null default 5,                -- 序号宽度
    b_resettype varchar(10) not null default 'month', -- 重置类型
    b_startvalue bigint not null default 1,           -- 起始值
    b_currentperiod varchar(8) null,                  -- 当前周期
    b_currentvalue bigint not null default 0,         -- 当前值
    b_canuse tinyint not null default 1,              -- 是否启用(0/1)
    primary key (b_module_id, b_field)
);

6. 模块关系

-- 模块关系表
create table dbo.s_relation (
    b_module_id varchar(50) not null,          -- 源模块编码
    b_target_module_id varchar(50) not null,   -- 目标模块编码
    b_relation_type varchar(20) not null,      -- 关系类型
    b_field varchar(50) not null,              -- 源字段
    b_target_field varchar(50) not null,       -- 目标字段
    b_foreign_side varchar(10) null,           -- 外键侧
    b_junction_module_id varchar(50) null,     -- 中间表模块编码
    b_junction_field varchar(50) null,         -- 中间表字段
    b_junction_target_field varchar(50) null,  -- 中间表目标字段
    b_ownership varchar(20) not null default 'reference', -- 归属关系
    b_owner_side varchar(10) null,             -- 属主侧
    b_delete_policy varchar(20) null,          -- 删除策略
    b_order_module_id varchar(50) null,        -- 排序模块编码
    b_order_field varchar(50) null,            -- 排序字段
    b_canuse tinyint not null default 1,       -- 是否启用(0/1)
    b_xh int not null default 0,               -- 显示顺序
    primary key (b_module_id, b_field, b_target_module_id, b_target_field)
);

create index ix_s_relation_module
    on dbo.s_relation (b_module_id, b_canuse, b_xh, b_field, b_target_module_id);

create index ix_s_relation_target
    on dbo.s_relation (b_target_module_id);

7. 菜单

-- 菜单表
create table dbo.s_menu (
    b_id varchar(50) not null primary key, -- 菜单编码(业务键主键)
    b_parent_id varchar(50) null,          -- 父级菜单编码,NULL 表示根节点
    b_name nvarchar(200) not null,         -- 菜单名称
    b_i18n varchar(150) null,              -- 多语言资源键
    b_menu_type varchar(20) not null,      -- 菜单类型
    b_route varchar(200) null,             -- 路由
    b_icon varchar(50) null,               -- 图标
    b_xh int not null default 0,           -- 显示顺序
    b_canuse tinyint not null default 1    -- 是否启用(0/1)
);

create index ix_s_menu_parent
    on dbo.s_menu (b_parent_id, b_xh, b_canuse, b_id);

create index ix_s_menu_order
    on dbo.s_menu (b_xh, b_id);

create index ix_s_menu_route
    on dbo.s_menu (b_route, b_canuse, b_id);

8. 权限

权限定义与用户授权分开,但不增加菜单等级或权限计算中间表:

  • s_power 是权限点字典,定义系统中可以授予的权限。菜单入口权限使用 menu.<菜单编码>.access,模块权限使用 module.<模块编码>.<operation>;菜单权限仍然通过 s_power 定义,不另建菜单权限字典;
  • s_menu_module 只描述一个业务页面使用哪些模块,不直接授予用户权限;页面与模块的父子授权关系由授权界面统一处理,不在关系表中增加额外的必需标记或权限等级。
  • s_user_power 保存用户直接拥有的权限记录,客户授权界面可以维护这张表;菜单页面只是按业务场景组织这些权限,不产生另一套授权来源;
  • 同一个模块被多个菜单引用时,页面中展示的是同一个模块权限。模块权限记录只保存一份,撤销某个菜单入口不会自动删除该模块权限。
  • 授权界面按“菜单 → 页面模块 → 模块能力”展示父子层级,使用父子级联勾选和半选状态;级联只是配置交互,最终仍保存为各权限点对应的 s_user_power 记录。
-- 菜单模块关联表
create table dbo.s_menu_module (
    b_menu_id varchar(50) not null,        -- 菜单编码,引用 s_menu.b_id
    b_module_id varchar(50) not null,      -- 模块编码,引用 s_module.b_id
    b_xh int not null default 0,            -- 页面内模块顺序
    b_canuse tinyint not null default 1,    -- 是否启用(0/1)
    primary key (b_menu_id, b_module_id)
);

create index IX_s_menu_module_module
    on dbo.s_menu_module (b_module_id, b_canuse, b_menu_id);
-- 权限表
create table dbo.s_power (
    b_id varchar(100) not null,           -- 权限编码即主键
    b_name nvarchar(200) not null,        -- 权限名称
    b_i18n varchar(150) null,             -- 多语言资源键
    b_power_type varchar(20) not null,    -- 权限类型(page / menu / module / action / report)
    b_owner_type varchar(20) not null,    -- 归属类型(menu / module)
    b_owner_id varchar(50) not null,      -- 归属对象编码
    b_operation varchar(30) not null,     -- 权限操作(access / read / create / update / delete / export / audit...)
    b_canuse tinyint not null default 1,  -- 是否启用(0/1)
    b_xh int not null default 0,          -- 显示顺序
    primary key (b_id)
);

create index ix_s_power_owner
    on dbo.s_power (b_power_type, b_owner_type, b_owner_id, b_operation, b_canuse);
-- 用户权限表
create table dbo.s_user_power (
    b_user_id varchar(50) not null,    -- 用户编码
    b_power_id varchar(100) not null,  -- 权限编码(s_power.b_id)
    b_canuse tinyint not null default 1, -- 是否启用(0/1)
    b_updatedatetime datetime2 null,   -- 最后更新时间
    primary key (b_user_id, b_power_id)
);

create index ix_s_user_power_user
    on dbo.s_user_power (b_user_id, b_canuse, b_power_id);

s_user_power 的授权约定:

  • 菜单入口权限由 s_power 定义为 menu.<菜单编码>.access,授权时直接写入对应用户的 s_user_power;
  • 模块查看、新增、修改、删除、导出及业务动作同样由 s_power 定义,授权时直接写入对应用户的 s_user_power;
  • 客户界面可以在菜单页面下同时勾选入口权限和页面所用模块的权限,但保存的仍是同一张 s_user_power 表;
  • 页面只是配置展示层,后端执行时按权限编码和 s_menu_module 校验当前菜单使用的模块;角色表暂不在本阶段引入。
-- 用户字段权限表
create table dbo.s_user_field_permission (
    b_user_id varchar(50) not null,        -- 用户编码
    b_module_id varchar(50) not null,      -- 模块编码
    b_field varchar(128) not null,         -- 字段名
    b_access_mode varchar(10) not null,    -- 访问模式(hidden / view / edit)
    b_allow_query tinyint not null default 0,  -- 是否允许查询(0/1)
    b_allow_export tinyint not null default 0, -- 是否允许导出(0/1)
    b_canuse tinyint not null default 1,   -- 是否启用(0/1)
    b_updatedatetime datetime2 null,       -- 最后更新时间
    primary key (b_user_id, b_module_id, b_field)
);

create index ix_s_user_field_permission_module
    on dbo.s_user_field_permission (b_user_id, b_module_id, b_canuse, b_field);
-- 用户数据范围表
create table dbo.s_user_data_scope (
    b_user_id varchar(50) not null,    -- 用户编码
    b_module_id varchar(50) not null,  -- 模块编码
    b_operation varchar(30) not null,  -- 操作(* / read / create / update / delete / export)
    b_scope_level varchar(10) not null default 'default', -- 范围级别(default / override)
    b_scope_no int not null default 1, -- 范围序号
    b_scope_type varchar(30) not null, -- 范围类型(own / department / department_tree / all / custom)
    b_scope_field varchar(50) null,    -- 业务数据中的归属用户/部门字段
    b_scope_value varchar(100) null,   -- custom 时每个用户/部门值一行
    b_updatedatetime datetime2 null,   -- 最后更新时间
    primary key (b_user_id, b_module_id, b_operation, b_scope_level, b_scope_no),
    constraint ck_s_user_data_scope_level check (b_scope_level in ('default','override')),
    constraint ck_s_user_data_scope_operation check (
        (b_scope_level = 'default' and b_operation = '*')
        or (b_scope_level = 'override' and b_operation in ('read','create','update','delete','export'))
    )
);

create index ix_s_user_data_scope_module
    on dbo.s_user_data_scope (b_user_id, b_module_id, b_operation, b_scope_level, b_scope_type);

9. 多语言

-- 多语言类型表
create table dbo.s_i18n_type (
    b_id varchar(20) not null primary key, -- 类型编码
    b_name nvarchar(200) not null,         -- 类型名称
    b_canuse tinyint not null default 1,   -- 是否启用(0/1)
    b_default tinyint not null default 0,  -- 是否默认(0/1)
    b_xh int not null default 0            -- 显示顺序
);

-- 多语言资源表
create table dbo.s_i18n (
    b_id varchar(200) not null primary key, -- 资源编码(b_key + ':' + b_locale)
    b_key varchar(150) not null,            -- 资源键
    b_locale varchar(20) not null,          -- 语言区域
    b_value nvarchar(1000) not null,        -- 值
    b_i18ntype varchar(20) null,            -- 多语言类型
    b_canuse tinyint not null default 1,    -- 是否启用(0/1)
    b_module_id varchar(50) null,           -- 模块编码
    unique (b_key, b_locale)
);

create index ix_s_i18n_module_key
    on dbo.s_i18n (b_module_id, b_key, b_locale);

create index ix_s_i18n_locale
    on dbo.s_i18n (b_locale, b_canuse);

10. 日志

-- 技术日志表
create table dbo.s_log_technical (
    b_id uniqueidentifier not null,    -- 日志ID(UUIDv7)
    b_user_id varchar(50) null,        -- 用户编码(未认证请求时为 NULL)
    b_sourcetype varchar(20) null,     -- 来源类型
    b_loglevel varchar(16) not null,   -- 日志级别
    b_logcategory varchar(64) null,    -- 日志分类
    b_operation varchar(100) null,     -- 操作
    b_module_id varchar(50) null,      -- 模块编码
    b_request_id varchar(64) null,     -- 请求ID
    b_trace_id varchar(64) null,       -- 链路ID
    b_server_node varchar(128) null,   -- 服务器节点
    b_duration_ms bigint null,         -- 耗时(毫秒)
    b_result_code varchar(50) null,    -- 结果码
    b_error_code varchar(50) null,     -- 错误码
    b_error_message nvarchar(4000) null, -- 错误信息
    b_exception_text nvarchar(max) null, -- 异常文本
    b_context_text nvarchar(max) null, -- 上下文文本
    b_occurdatetime datetime2 default sysutcdatetime(), -- 发生时间
    constraint pk_s_log_technical primary key nonclustered (b_id)
);

-- 登录日志表
create table dbo.s_log_login (
    b_id uniqueidentifier not null,    -- 日志ID(UUIDv7)
    b_user_id varchar(50) null,        -- 用户编码(账号不存在时为 NULL)
    b_login_type varchar(20) not null, -- 登录类型
    b_login_result tinyint not null default 0, -- 登录结果(0/1)
    b_fail_reason nvarchar(400) null,  -- 失败原因
    b_ip varchar(50) null,             -- IP
    b_ip_location nvarchar(400) null,  -- IP 归属地
    b_browser varchar(100) null,       -- 浏览器
    b_browser_version varchar(50) null, -- 浏览器版本
    b_os varchar(100) null,            -- 操作系统
    b_os_version varchar(50) null,     -- 系统版本
    b_device_type varchar(20) null,    -- 设备类型
    b_user_agent nvarchar(max) null,   -- UserAgent
    b_session_id varchar(50) null,     -- 会话ID
    b_request_id varchar(64) null,     -- 请求ID
    b_trace_id varchar(64) null,       -- 链路ID
    b_occurdatetime datetime2 default sysutcdatetime(), -- 发生时间
    constraint pk_s_log_login primary key nonclustered (b_id)
);

-- 审计日志表
create table dbo.s_log_audit (
    b_id uniqueidentifier not null,    -- 日志ID(UUIDv7)
    b_event_code varchar(50) not null, -- 事件编码
    b_module_id varchar(50) null,      -- 模块编码
    b_data_id varchar(50) null,        -- 数据ID
    b_business_no varchar(50) null,    -- 业务单号
    b_operation varchar(100) null,     -- 操作
    b_operator_id varchar(50) null,    -- 操作人ID
    b_operator_name nvarchar(200) null, -- 操作人名称
    b_source_type varchar(20) not null default 'user', -- 来源类型
    b_visibility varchar(20) not null default 'internal', -- 可见性
    b_request_id varchar(64) null,     -- 请求ID
    b_trace_id varchar(64) null,       -- 链路ID
    b_payload_text nvarchar(max) null, -- 载荷文本
    b_occurdatetime datetime2 default sysutcdatetime(), -- 发生时间
    constraint pk_s_log_audit primary key nonclustered (b_id)
);

-- 审计字段明细表
create table dbo.s_log_audit_field (
    b_id uniqueidentifier not null,    -- 日志ID(UUIDv7)
    b_event_id uniqueidentifier not null, -- 审计事件ID
    b_module_id varchar(50) null,      -- 模块编码
    b_data_id varchar(50) null,        -- 数据ID
    b_field varchar(50) not null,      -- 字段名
    b_field_label nvarchar(200) null,  -- 字段标签
    b_before_value nvarchar(max) null, -- 变更前值
    b_after_value nvarchar(max) null,  -- 变更后值
    b_before_display nvarchar(1000) null, -- 变更前显示值
    b_after_display nvarchar(1000) null,  -- 变更后显示值
    b_visibility varchar(20) not null default 'internal', -- 可见性
    constraint pk_s_log_audit_field primary key nonclustered (b_id)
);

create clustered index cx_s_log_technical_occur
    on dbo.s_log_technical (b_occurdatetime, b_id);

create clustered index cx_s_log_login_occur
    on dbo.s_log_login (b_occurdatetime, b_id);

create clustered index cx_s_log_audit_occur
    on dbo.s_log_audit (b_occurdatetime, b_id);

create clustered index cx_s_log_audit_field_event
    on dbo.s_log_audit_field (b_event_id, b_id);

create index ix_s_log_technical_trace
    on dbo.s_log_technical (b_trace_id, b_occurdatetime);

create index ix_s_log_login_user
    on dbo.s_log_login (b_user_id, b_occurdatetime);

create index ix_s_log_audit_module
    on dbo.s_log_audit (b_module_id, b_data_id, b_occurdatetime);

11. 业务表主键约定

-- 业务表示例
create table dbo.b_example (
    b_id bigint not null primary key, -- 主键(应用层雪花 ID)
    b_inputuser_id varchar(50) null,  -- 录入用户ID
    b_inputdatetime datetime2 default sysutcdatetime() -- 录入时间
);