Files
smart_grid_iot/database/db_seed.sql
T
andyesfly 1c80a24d1d feat: 新增客户管理全模块功能
实现客户信息、客户部门、客户账号三个子模块的完整CRUD,包含:
1. 数据库实体、DTO、控制器、服务层代码
2. RBAC权限映射与菜单种子数据扩展
3. 前端路由与API封装
4. 数据权限隔离与鉴权逻辑
5. 配套工具脚本与测试验证清单
2026-08-23 19:15:54 +08:00

178 lines
9.9 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, 1, '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;
-- 初始菜单(客户管理子菜单,parent_id=1,显式指定 id:12-23)
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(12, 1, '客户信息', '/customer/info', 'customer/info/index', 'UserOutlined', 1, 2, 'customer:list', 1),
(13, 1, '客户部门', '/customer/department', 'customer/department/index', 'TeamOutlined', 2, 2, 'customer:department', 1),
(14, 1, '客户账号', '/customer/account', 'customer/account/index', 'SafetyOutlined', 3, 2, 'customer:account', 1),
(15, 1, '新增客户', NULL, NULL, NULL, 1, 3, 'customer:create', 1),
(16, 1, '编辑客户', NULL, NULL, NULL, 2, 3, 'customer:update', 1),
(17, 1, '删除客户', NULL, NULL, NULL, 3, 3, 'customer:delete', 1),
(18, 1, '新增部门', NULL, NULL, NULL, 4, 3, 'department:create', 1),
(19, 1, '编辑部门', NULL, NULL, NULL, 5, 3, 'department:update', 1),
(20, 1, '删除部门', NULL, NULL, NULL, 6, 3, 'department:delete', 1),
(21, 1, '新增账号', NULL, NULL, NULL, 7, 3, 'account:create', 1),
(22, 1, '编辑账号', NULL, NULL, NULL, 8, 3, 'account:update', 1),
(23, 1, '删除账号', NULL, NULL, NULL, 9, 3, 'account:delete', 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', 23, 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 拥有全部 23 条菜单,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),
(1, 12), (1, 13), (1, 14), (1, 15), (1, 16), (1, 17), (1, 18), (1, 19), (1, 20), (1, 21), (1, 22), (1, 23),
(2, 1), (2, 2), (2, 5),
(2, 12), (2, 13), (2, 14), (2, 15), (2, 16), (2, 17), (2, 18), (2, 19), (2, 20), (2, 21), (2, 22), (2, 23)
ON CONFLICT (role_id, menu_id) DO NOTHING;
COMMIT;
-- ============================================================
-- 完 · 7 张系统表种子数据(4 角色 + 23 菜单 + 20 字典 + 1 管理员 + 绑定关系)
-- ============================================================