Files
smart_grid_iot/database/db_seed.sql
T
andyesfly 0a8103f33d refactor: 系统权限模块重构,迁移至PostgreSQL并对齐新设计
- 切换数据库为PostgreSQL,更新依赖与配置
- 重构权限体系,使用sys_user/sys_role/sys_menu等新实体替代旧模型
- 移除旧的用户角色/权限/系统设置相关代码与实体
- 新增字典、菜单管理模块与基础CRUD接口
- 优化鉴权逻辑,基于菜单权限标识进行接口校验
- 前端同步更新API、路由与权限控制逻辑
- 删除冗余的v-permission指令与相关代码
2026-08-19 14:11:34 +08:00

151 lines
7.7 KiB
PL/PgSQL
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.
-- ============================================================
-- 鑫潼智电系统 PostgreSQL 种子数据脚本 V1.0
--
-- 依赖: db_init.sql(须先执行,确保表结构已创建)
--
-- 用法:
-- psql -U postgres -d xintong -f db_seed.sql
--
-- 特性:
-- - 独立事务(BEGIN/COMMIT)
-- - 幂等执行:
-- sys_role / sys_menu / sys_user / sys_user_role / sys_role_menu
-- 使用 ON CONFLICT (id / 联合主键) DO UPDATE | DO NOTHING
-- sys_dict 无业务唯一键,使用 TRUNCATE RESTART IDENTITY 清空重建
-- sys_user 可能被业务表外键引用,禁用 TRUNCATE CASCADE
-- - 显式重置序列,确保 id 不漂移
--
-- 内容:
-- 1. 初始角色(ADMIN / SALESMAN / WORKER / CUSTOMER)
-- 2. 初始菜单(顶级 6 + 系统管理子菜单 5)
-- 3. 初始字典(客户/设备/派单/支付等业务字典)
-- 4. 管理员账户(admin / admin123456)
-- 5. 用户-角色绑定 / 角色-菜单绑定
-- ============================================================
BEGIN;
-- ------------------------------------------------------------
-- 1. 初始角色(显式指定 id:1=ADMIN 2=SALESMAN 3=WORKER 4=CUSTOMER)
-- ------------------------------------------------------------
INSERT INTO sys_role (id, role_code, role_name, description, sort_order) VALUES
(1, 'ADMIN', '运营管理员', '全部菜单权限,管理所有客户', 1),
(2, 'SALESMAN', '业务员', '客户/项目/方案/合同/回访', 2),
(3, 'WORKER', '共享电工', 'APP 接单处理,无后台菜单', 3),
(4, 'CUSTOMER', '客户', '小程序查看用电/报警/报修', 4)
ON CONFLICT (id) DO UPDATE SET
role_code = EXCLUDED.role_code,
role_name = EXCLUDED.role_name,
description = EXCLUDED.description,
sort_order = EXCLUDED.sort_order;
-- 重置 sys_role 自增序列
SELECT setval('sys_role_id_seq', 4, true);
-- ------------------------------------------------------------
-- 2. 初始菜单(顶级菜单,显式指定 id:1-6,系统管理为目录类型 menu_type=1,其余为 menu_type=2)
-- ------------------------------------------------------------
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(1, NULL, '客户管理', '/customer', NULL, 'TeamOutlined', 1, 2, 'customer:list', 1),
(2, NULL, '项目管理', '/project', NULL, 'ProjectOutlined', 2, 2, 'project:list', 1),
(3, NULL, '运营派单', '/dispatch', NULL, 'ScheduleOutlined', 3, 2, 'dispatch:list', 1),
(4, NULL, '设备监控', '/device', NULL, 'MonitorOutlined', 4, 2, 'device:list', 1),
(5, NULL, '统计报表', '/report', NULL, 'BarChartOutlined', 5, 2, 'report:list', 1),
(6, NULL, '系统管理', '/system', NULL, 'SettingOutlined', 6, 1, 'system:list', 1)
ON CONFLICT (id) DO UPDATE SET
parent_id = EXCLUDED.parent_id,
menu_name = EXCLUDED.menu_name,
menu_path = EXCLUDED.menu_path,
component = EXCLUDED.component,
icon = EXCLUDED.icon,
sort_order = EXCLUDED.sort_order,
menu_type = EXCLUDED.menu_type,
permission = EXCLUDED.permission,
visible = EXCLUDED.visible;
-- 初始菜单(系统管理子菜单,parent_id=6,显式指定 id:7-11)
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(7, 6, '用户管理', '/system/user', 'system/user/index', 'UserOutlined', 1, 2, 'system:user', 1),
(8, 6, '角色管理', '/system/role', 'system/role/index', 'SafetyCertificateOutlined', 2, 2, 'system:role', 1),
(9, 6, '菜单管理', '/system/menu', 'system/menu/index', 'MenuOutlined', 3, 2, 'system:menu', 1),
(10, 6, '数据字典', '/system/dict', 'system/dict/index', 'BookOutlined', 4, 2, 'system:dict', 1),
(11, 6, '操作日志', '/log/operation', 'log/operation/index', 'FileTextOutlined', 5, 2, 'system:log', 1)
ON CONFLICT (id) DO UPDATE SET
parent_id = EXCLUDED.parent_id,
menu_name = EXCLUDED.menu_name,
menu_path = EXCLUDED.menu_path,
component = EXCLUDED.component,
icon = EXCLUDED.icon,
sort_order = EXCLUDED.sort_order,
menu_type = EXCLUDED.menu_type,
permission = EXCLUDED.permission,
visible = EXCLUDED.visible;
-- 重置 sys_menu 自增序列
SELECT setval('sys_menu_id_seq', 11, true);
-- ------------------------------------------------------------
-- 3. 初始字典(sys_dict 无业务唯一键,清空重建以支持幂等执行)
-- ------------------------------------------------------------
TRUNCATE sys_dict RESTART IDENTITY;
INSERT INTO sys_dict (dict_type, dict_label, dict_value, sort_order) VALUES
('customer_status', '意向', '1', 1),
('customer_status', '签约', '2', 2),
('customer_status', '运维中', '3', 3),
('customer_status', '终止', '4', 4),
('equip_type', '配电间', '配电间', 1),
('equip_type', '配电井', '配电井', 2),
('equip_type', '配电箱', '配电箱', 3),
('device_type', '网关', '网关', 1),
('device_type', '探测器', '探测器', 2),
('device_type', '采集器', '采集器', 3),
('dispatch_status', '已派发', '0', 1),
('dispatch_status', '已接单', '1', 2),
('dispatch_status', '已完成', '2', 3),
('pay_cycle', '按月支付', '按月', 1),
('pay_cycle', '按年支付', '按年', 2),
('dispatch_type', '勘察派单', 'survey', 1),
('dispatch_type', '安装派单', 'install', 2),
('dispatch_type', '报警派单', 'alarm', 3),
('dispatch_type', '维修派单', 'repair', 4),
('dispatch_type', '维保派单', 'maintenance', 5);
-- ------------------------------------------------------------
-- 4. 管理员账户(显式指定 id=1,密码哈希为 bcrypt('admin123456'))
-- sys_user 可能被业务表外键引用,使用 ON CONFLICT 而非 TRUNCATE,避免业务数据被级联清空
-- ------------------------------------------------------------
INSERT INTO sys_user (id, username, password_hash, real_name, phone, email, status) VALUES
(1, 'admin', '$2a$10$GC/0CaPf9Ti/yeNVSZ8L3OKvCS1MrAY.aohg9UerB6heyp78Fo17.', '运营管理员', NULL, NULL, 1)
ON CONFLICT (id) DO UPDATE SET
username = EXCLUDED.username,
password_hash = EXCLUDED.password_hash,
real_name = EXCLUDED.real_name,
phone = EXCLUDED.phone,
email = EXCLUDED.email,
status = EXCLUDED.status;
-- 重置 sys_user 自增序列
SELECT setval('sys_user_id_seq', 1, true);
-- ------------------------------------------------------------
-- 5. 用户-角色绑定:admin → ADMIN
-- ------------------------------------------------------------
INSERT INTO sys_user_role (user_id, role_id) VALUES (1, 1)
ON CONFLICT (user_id, role_id) DO NOTHING;
-- ------------------------------------------------------------
-- 6. 角色-菜单绑定:ADMIN 拥有全部 11 条菜单,SALESMAN 拥有业务菜单(客户/项目/统计报表)
-- WORKER、CUSTOMER 不绑定任何菜单
-- ------------------------------------------------------------
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11),
(2, 1), (2, 2), (2, 5)
ON CONFLICT (role_id, menu_id) DO NOTHING;
COMMIT;
-- ============================================================
-- 完 · 7 张系统表种子数据(4 角色 + 11 菜单 + 20 字典 + 1 管理员 + 绑定关系)
-- ============================================================