Files
smart_grid_iot/database/db_seed.sql
T

448 lines
27 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)
-- 2. 初始菜单(顶级 6 + 系统管理子菜单 6 + 运营派单子菜单 13 等,共 50 条)
-- 3. 初始字典(客户/设备/派单/支付等业务字典)
-- 4. 管理员账户(admin / admin123456)
-- 5. 用户-角色绑定 / 角色-菜单绑定
-- ============================================================
BEGIN;
-- ------------------------------------------------------------
-- 1. 初始角色(显式指定 id:1=ADMIN 2=SALESMAN)
-- 后台仅存在系统用户(sys_user)角色;电工(worker 表)与客户(customer_account 表)
-- 各自独立账号体系,不作为后台角色出现。
-- ------------------------------------------------------------
INSERT INTO sys_role (id, role_code, role_name, description, sort_order) VALUES
(1, 'ADMIN', '运营管理员', '全部菜单权限,管理所有客户', 1),
(2, 'SALESMAN', '业务员', '客户/项目/方案/合同/回访', 2)
ON CONFLICT (id) DO UPDATE SET
role_code = EXCLUDED.role_code,
role_name = EXCLUDED.role_name,
description = EXCLUDED.description,
sort_order = EXCLUDED.sort_order;
-- 清理历史遗留的 APP 端角色(电工/客户不参与后台角色体系)
DELETE FROM sys_role WHERE role_code IN ('WORKER', 'CUSTOMER');
-- 重置 sys_role 自增序列
SELECT setval('sys_role_id_seq', 2, 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, 1, 'project:list', 1),
(3, NULL, '运营派单', '/dispatch', NULL, 'ScheduleOutlined', 3, 1, 'dispatch:list', 1),
(4, NULL, '设备监控', '/device', NULL, 'MonitorOutlined', 4, 1, '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', 'xt-xiaochengxu', 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;
-- 初始菜单(项目域子菜单 + 客户扩展子菜单,id:24-31 + 空间拓扑 32)
-- 项目域:客户拜访(customer:visit) / 项目方案(project:plan)
-- 合同管理(project:contract)
-- 项目验收(project:acceptance) / 回访记录(project:visit)
-- 客户域:空间拓扑与用电架构(customer:topology)
-- 注:客户拜访归属“项目管理”,但仍沿用 /customer/visit 路径与 customer:visit 权限码
-- 注:项目勘察(27)/项目安装(33) 已迁至运营派单 parent_id=3(见下方运营派单块注释)
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(24, 2, '客户拜访', '/customer/visit', 'customer/visit/index', 'xt-jihuabaifang', 1, 2, 'customer:visit', 1),
(27, 3, '项目勘察', '/dispatch/survey', 'dispatch/survey/index', 'xt-xiangmukancha', 2, 2, 'project:survey', 1),
(28, 2, '项目方案', '/project/plan', 'project/plan/index', 'xt-xiangmufangan', 3, 2, 'project:plan', 1),
(29, 2, '合同管理', '/project/contract', 'project/contract/index', 'FileDoneOutlined', 4, 2, 'project:contract', 1),
(33, 3, '项目安装', '/dispatch/install', 'dispatch/install/index', 'ToolOutlined', 3, 2, 'project:install', 1),
(30, 2, '项目验收', '/project/acceptance', 'project/acceptance/index', 'ThunderboltOutlined', 6, 2, 'project:acceptance',1),
(31, 2, '回访记录', '/project/visit', 'project/visit/index', 'AuditOutlined', 7, 2, 'project:visit', 1),
(32, 1, '空间拓扑与用电架构', '/customer/topology', 'customer/topology/index', 'xt-topology', 5, 2, 'customer:topology',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=3)
-- 运营派单域统一 id 分配(避免历史上 36/37 被两段 INSERT 复用而互相覆盖):
-- 27 项目勘察 / 33 项目安装(自项目管理 parent_id=2 迁入)
-- 34 运维费定价 / 35 勘察费用(均归属基础数据 parent_id=59) / 36 电工登记管理
-- 37-46 按钮权限
-- 55 报修派单 / 56 维保派单
-- 子菜单排序:36 电工登记管理(1) → 27 项目勘察(2) → 33 项目安装(3) → 55 报修派单(4) → 56 维保派单(5)
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(55, 3, '报修派单', '/dispatch/repair', 'dispatch/repair/index', 'ToolOutlined', 4, 2, 'dispatch:repair', 1),
(56, 3, '维保派单', '/dispatch/maintenance', 'dispatch/maintenance/index', 'ToolOutlined', 5, 2, 'dispatch:maintenance',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=3;其中 34 运维费定价及按钮 37-39、35 勘察费用及按钮 40-42 已归属基础数据 parent_id=59;id:36 为菜单项,43-46 为按钮权限)
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(34, 59, '运维费定价', '/base/price', 'base/price/index', 'MoneyCollectOutlined', 2, 2, 'dispatch:price', 1),
(35, 59, '勘察费用', '/base/cost', 'base/cost/index', 'AccountBookOutlined', 8, 2, 'dispatch:cost', 1),
(36, 3, '电工登记管理', '/dispatch/worker', 'dispatch/worker/index', 'ToolOutlined', 1, 2, 'dispatch:worker', 1),
(37, 34, '新增价目', NULL, NULL, NULL, 1, 3, 'price:create', 1),
(38, 34, '编辑价目', NULL, NULL, NULL, 2, 3, 'price:update', 1),
(39, 34, '删除价目', NULL, NULL, NULL, 3, 3, 'price:delete', 1),
(40, 35, '新增勘察费用', NULL, NULL, NULL, 9, 3, 'cost:create', 1),
(41, 35, '编辑勘察费用', NULL, NULL, NULL, 10, 3, 'cost:update', 1),
(42, 35, '删除勘察费用', NULL, NULL, NULL, 11, 3, 'cost:delete', 1),
(43, 3, '新增电工', NULL, NULL, NULL, 7, 3, 'worker:create', 1),
(44, 3, '编辑电工', NULL, NULL, NULL, 8, 3, 'worker:update', 1),
(45, 3, '删除电工', NULL, NULL, NULL, 9, 3, 'worker:delete', 1),
(46, 3, '审核电工', NULL, NULL, NULL, 10, 3, 'worker:audit', 1),
(57, 3, '重置电工密码', NULL, NULL, NULL, 11, 3, 'worker:reset-password', 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', 50, 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),
('dispatch_status', '已归档', '3', 4),
('dispatch_status', '已取消', '5', 5),
('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),
('om_service_type', '维修', '维修', 1),
('om_service_type', '维保', '维保', 2),
('om_service_type', '报警处理', '报警处理', 3),
('om_service_type', '巡检', '巡检', 4),
('om_service_type', '其他', '其他', 5),
('om_price_mode', '按次', '1', 1),
('om_price_mode', '按工时', '2', 2),
('om_price_mode', '材料', '3', 3),
('om_price_unit', '次', '次', 1),
('om_price_unit', '小时', '小时', 2),
('om_price_unit', '个', '个', 3),
('om_price_unit', '米', '米', 4),
('om_price_unit', '根', '根', 5),
('worker_cert_type', '低压电工证', '低压电工证', 1),
('worker_cert_type', '高压电工证', '高压电工证', 2),
('worker_cert_type', '特种作业证', '特种作业证', 3),
('worker_cert_type', '登高证', '登高证', 4),
('worker_cert_type', '其他', '其他', 5),
('worker_audit_status', '待审核', '0', 1),
('worker_audit_status', '已通过', '1', 2),
('worker_audit_status', '已驳回', '2', 3);
-- ------------------------------------------------------------
-- 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 拥有全部菜单,SALESMAN 拥有客户域+项目+统计报表
-- ------------------------------------------------------------
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),
(1, 24), (1, 27), (1, 28), (1, 29), (1, 30), (1, 31), (1, 32), (1, 33), (1, 55), (1, 56),
(1, 34), (1, 35), (1, 36), (1, 37), (1, 38), (1, 39), (1, 40), (1, 41), (1, 42), (1, 43), (1, 44), (1, 45), (1, 46), (1, 57),
(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),
(2, 24), (2, 27), (2, 28), (2, 29), (2, 30), (2, 31), (2, 32), (2, 33), (2, 55), (2, 56)
ON CONFLICT (role_id, menu_id) DO NOTHING;
-- 移除已废弃的菜单(客户建筑/客户房间,id=25/26)及其角色绑定(幂等清理,确保每次执行都收敛)
DELETE FROM sys_role_menu WHERE menu_id IN (25, 26);
DELETE FROM sys_menu WHERE id IN (25, 26);
-- ------------------------------------------------------------
-- 7. 定时任务域菜单(系统管理 parent_id=6,id:47 为菜单项,48-50 为按钮权限)
-- ------------------------------------------------------------
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(47, 6, '定时任务', '/system/job', 'system/job/index', 'ClockCircleOutlined', 6, 2, 'system:job', 1),
(48, 47, '编辑定时任务', NULL, NULL, NULL, 1, 3, 'system:job:update', 1),
(49, 47, '立即执行', NULL, NULL, NULL, 2, 3, 'system:job:run', 1),
(50, 47, '清理日志', NULL, NULL, NULL, 3, 3, 'system:job:log:clear', 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;
-- 角色-菜单绑定:ADMIN(role_id=1)拥有 47-50
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 47), (1, 48), (1, 49), (1, 50)
ON CONFLICT (role_id, menu_id) DO NOTHING;
-- ------------------------------------------------------------
-- 8. 设备域菜单拆分(设备监控目录 + 设备管理台账子菜单 / 基础数据目录 + 智电设备型号子菜单)
-- 设备监控(id=4)由菜单项改为目录 menu_type=1;台账子菜单挂在设备监控下
-- 基础数据为新一级目录,智电设备型号(基础数据,与客户无关)挂其下
-- 注:id 用 58-60(55-57 已被生产库报修派单/维保派单/重置电工密码占用,避免重跑冲突)
-- ------------------------------------------------------------
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(58, 4, '设备管理台账', '/device/equipment', 'device/equipment/index', 'MonitorOutlined', 1, 2, 'device:list', 1),
(59, NULL, '基础数据', '/base', NULL, 'DatabaseOutlined', 5, 1, 'base:list', 1),
(60, 59, '智电设备型号', '/base/smart-device', 'device/smart-device/index', 'ApartmentOutlined', 1, 2, 'device:model', 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;
-- 角色-菜单绑定:ADMIN(role_id=1)拥有 58-61
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 58), (1, 59), (1, 60), (1, 61)
ON CONFLICT (role_id, menu_id) DO NOTHING;
-- 重置 sys_menu 自增序列(覆盖到 61)
SELECT setval('sys_menu_id_seq', 61, true);
-- ------------------------------------------------------------
-- 8a. 设备监控下的智电改造线路台账菜单(id=61,挂设备监控 parent_id=4)
-- ------------------------------------------------------------
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(61, 4, '智电改造线路台账', '/device/line', 'device/line/index', 'ApartmentOutlined', 2, 2, 'device: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;
-- 角色-菜单绑定:ADMIN(role_id=1)拥有 61
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 61)
ON CONFLICT (role_id, menu_id) DO NOTHING;
-- ------------------------------------------------------------
-- 9. 定时任务种子(sys_job,按 job_key 幂等)
-- ------------------------------------------------------------
INSERT INTO sys_job (job_key, job_name, cron_expression, freq_type, freq_config, params, retry_times, retry_interval, status, remark) VALUES
('demo_heartbeat', '示例心跳', '*/5 * * * *', 'minute', '{"interval":5}'::jsonb, '{"message":"heartbeat"}'::jsonb, 0, 60, 1,
'示例心跳任务:用于验证调度链路,默认每 5 分钟写入一条 info 日志'),
('log_clean', '日志清理', '0 3 * * *', 'day', '{"times":["03:00"]}'::jsonb, '{"keepDays":90}'::jsonb, 2, 60, 1,
'日志清理任务:每日 03:00 清理早于 keepDays 天的操作日志与任务日志,默认保留 90 天')
ON CONFLICT (job_key) DO NOTHING;
-- ------------------------------------------------------------
-- 9. 通知域菜单(系统管理 parent_id=6,id:51 为菜单项,52-54 为按钮权限)
-- ------------------------------------------------------------
INSERT INTO sys_menu (id, parent_id, menu_name, menu_path, component, icon, sort_order, menu_type, permission, visible) VALUES
(51, 6, '通知管理', '/system/notice', 'system/notice/index', 'BellOutlined', 7, 2, 'notice:rule', 1),
(52, 51, '保存规则', NULL, NULL, NULL, 1, 3, 'notice:rule:update', 1),
(53, 51, '删除规则', NULL, NULL, NULL, 2, 3, 'notice:rule:delete', 1),
(54, 51, '群发消息', NULL, NULL, NULL, 3, 3, 'notice:batch', 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;
-- 角色-菜单绑定:ADMIN(role_id=1)拥有 51-54
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 51), (1, 52), (1, 53), (1, 54)
ON CONFLICT (role_id, menu_id) DO NOTHING;
-- 重置 sys_menu 自增序列
SELECT setval('sys_menu_id_seq', 57, true);
-- ------------------------------------------------------------
-- 10. 通知规则扫描任务种子(sys_job,按 job_key 幂等)
-- ------------------------------------------------------------
INSERT INTO sys_job (job_key, job_name, cron_expression, freq_type, freq_config, params, retry_times, retry_interval, status, remark) VALUES
('notice_scan', '通知规则扫描', '0 8 * * *', 'day', '{"times":["08:00"]}'::jsonb, '{}'::jsonb, 1, 300, 1,
'遍历启用的扫描式通知规则,按模板与接收对象生成站内通知(幂等,默认每日 08:00)')
ON CONFLICT (job_key) DO NOTHING;
-- ------------------------------------------------------------
-- 10a. 智电线路同步任务种子(sys_job,按 job_key 幂等;默认每 10 分钟)
-- ------------------------------------------------------------
INSERT INTO sys_job (job_key, job_name, cron_expression, params, retry_times, retry_interval, status, remark) VALUES
('icc_line_sync', '智电线路同步', '*/10 * * * *', '{}'::jsonb, 1, 300, 1,
'从 ICC 平台拉取智电改造线路并按 line_code 幂等 upsert(默认每 10 分钟)')
ON CONFLICT (job_key) DO NOTHING;
-- ------------------------------------------------------------
-- 11. 通知规则种子(sys_notice_rule,按 rule_key 幂等;接收对象可在「通知管理」页调整)
-- ------------------------------------------------------------
INSERT INTO sys_notice_rule (rule_key, rule_name, event_key, trigger_mode, enabled, advance_days, title_tpl, content_tpl, receiver_config, remark) VALUES
('cert_expire', '电工资质证照到期提醒', 'cert_expire', 1, 1, 30,
'电工【{workerName}】的{certType}{expireState}',
'证书编号:{certNo};发证机关:{issueAuthority};有效期至:{expireDate};{remainDaysText},请及时提醒电工办理续期',
'{"users":{"roles":["ADMIN"],"ids":[]},"workers":{"mode":"event","ids":[]}}'::jsonb,
'扫描 worker_cert 中 advance_days 天内到期或已过期的证照,通知 ADMIN 与证照所属电工'),
('worker_audit_result', '电工入驻审核结果通知', 'worker_audit_result', 2, 1, NULL,
'您的入驻审核{auditState}',
'审核{auditState};{auditRemarkText}如资料有误,请修改后重新提交。',
'{"users":{"roles":[],"ids":[]},"workers":{"mode":"event","ids":[]}}'::jsonb,
'审核通过/驳回时实时通知电工本人(业务埋点触发)'),
('dispatch_assign', '工单派单通知', 'dispatch_assign', 2, 1, NULL,
'您有新的{orderTypeName}工单',
'工单编号:{orderNo};客户:{customerName};{detailText}请及时在电工端查看并接单。',
'{"users":{"roles":[],"ids":[]},"workers":{"mode":"event","ids":[]}}'::jsonb,
'勘察/安装/维保/报修派单时实时通知被指派电工(业务埋点触发)'),
('contract_expire', '客户合同到期提醒', 'contract_expire', 1, 1, 30,
'客户【{customerName}】的合同{expireState}',
'合同编号:{contractNo};合同名称:{contractName};到期日:{endDate};{remainDaysText},请及时跟进续签。',
'{"users":{"roles":["ADMIN"],"ids":[]},"workers":{"mode":"none","ids":[]}}'::jsonb,
'扫描 contract 中 advance_days 天内到期或已过期的合同,通知 ADMIN'),
('visit_plan', '客户拜访计划提醒', 'visit_plan', 1, 1, 0,
'客户拜访计划待执行:{customerName}',
'计划拜访日期:{planDate};{remainDaysText}。请及时完成拜访。',
'{"users":{"roles":["ADMIN"],"ids":[]},"workers":{"mode":"none","ids":[]}}'::jsonb,
'扫描 customer_visit 中未执行且计划日期已到/超期的拜访计划,通知 ADMIN'),
('order_timeout', '工单超时未接单提醒', 'order_timeout', 1, 1, 1,
'工单超时未接单:{orderTypeName}',
'工单编号:{orderNo};客户:{customerName};已派单 {staleDays} 天仍未接单,请及时跟进。',
'{"users":{"roles":["ADMIN"],"ids":[]},"workers":{"mode":"none","ids":[]}}'::jsonb,
'扫描派单后超过 advance_days 天仍未接单的工单,通知 ADMIN')
ON CONFLICT (rule_key) DO NOTHING;
COMMIT;
-- ============================================================
-- 完 · 系统表种子数据(4 角色 + 54 菜单 + 43 字典 + 1 管理员 + 3 定时任务 + 6 通知规则 + 绑定关系)
-- ============================================================