-- ====================================================
-- v9: 权限数据完整重置
-- 清空旧权限 + 插入新权限 + 重新授权超级管理员
-- 权限存储在 t_role.permissions 字段（管道分隔字符串）
-- 执行前提：确认当前数据库中的旧权限关联可以清除
-- ====================================================

-- ----------------------------
-- 1. 清理旧权限数据
-- ----------------------------
DELETE FROM `t_permission`;

-- 重置自增 ID，确保新权限从 1 开始
ALTER TABLE `t_permission` AUTO_INCREMENT = 1;

-- ----------------------------
-- 2. 插入完整权限（与后端 @SaCheckPermission 严格一一对应）
-- ----------------------------
INSERT INTO `t_permission` (`id`, `company_id`, `name`, `value`, `pid`, `weight`) VALUES
-- ====== 站点管理 ======
(1, NULL, '站点管理', 'washStation', 0, 10),
(2, NULL, '站点列表', 'washStation.list', 1, 1),
(3, NULL, '新增站点', 'washStation.add', 1, 2),
(4, NULL, '修改站点', 'washStation.modify', 1, 3),
(5, NULL, '删除站点', 'washStation.remove', 1, 4),

-- ====== 设备管理 ======
(6, NULL, '设备管理', 'washDevice', 0, 20),
(7, NULL, '设备列表', 'washDevice.list', 6, 1),
(8, NULL, '新增设备', 'washDevice.add', 6, 2),
(9, NULL, '修改设备', 'washDevice.modify', 6, 3),
(10, NULL, '删除设备', 'washDevice.remove', 6, 4),

-- ====== 订单管理 ======
(11, NULL, '订单管理', 'washOrder', 0, 30),
(12, NULL, '订单列表', 'washOrder.list', 11, 1),
(13, NULL, '修改订单', 'washOrder.modify', 11, 2),

-- ====== 用户管理（运维用户） ======
(14, NULL, '用户管理', 'user', 0, 40),
(15, NULL, '用户列表', 'user.list', 14, 1),
(16, NULL, '新增用户', 'user.add', 14, 2),
(17, NULL, '修改用户', 'user.modify', 14, 3),

-- ====== 账户管理（C端用户） ======
(18, NULL, '账户管理', 'account', 0, 50),
(19, NULL, '账户列表', 'account.list', 18, 1),

-- ====== 投资人管理 ======
(20, NULL, '投资人管理', 'investor', 0, 55),
(21, NULL, '新增投资人', 'investor.add', 20, 1),

-- ====== 角色管理 ======
(22, NULL, '角色管理', 'role', 0, 60),
(23, NULL, '角色列表', 'role.list', 22, 1),
(24, NULL, '新增角色', 'role.add', 22, 2),
(25, NULL, '修改角色', 'role.modify', 22, 3),
(26, NULL, '删除角色', 'role.delete', 22, 4),

-- ====== 字典管理 ======
(27, NULL, '字典管理', 'dict', 0, 70),
(28, NULL, '字典列表', 'dict.list', 27, 1),
(29, NULL, '新增字典', 'dict.add', 27, 2),
(30, NULL, '修改字典', 'dict.modify', 27, 3),

-- ====== 充值配置 ======
(31, NULL, '充值配置', 'rechargeConfig', 0, 80),
(32, NULL, '配置列表', 'rechargeConfig.list', 31, 1),
(33, NULL, '新增配置', 'rechargeConfig.add', 31, 2),
(34, NULL, '修改配置', 'rechargeConfig.modify', 31, 3),
(35, NULL, '删除配置', 'rechargeConfig.remove', 31, 4),

-- ====== 线下充值 ======
(36, NULL, '线下充值', 'offlineRecharge', 0, 90),

-- ====== 优惠活动 ======
(37, NULL, '优惠活动', 'promotion', 0, 100),
(38, NULL, '活动列表', 'promotion.list', 37, 1),
(39, NULL, '新增活动', 'promotion.add', 37, 2),
(40, NULL, '修改活动', 'promotion.modify', 37, 3),
(41, NULL, '删除活动', 'promotion.remove', 37, 4),

-- ====== 故障管理 ======
(42, NULL, '故障管理', 'faultSubscriber', 0, 110),
(43, NULL, '订阅列表', 'faultSubscriber.list', 42, 1),
(44, NULL, '解绑订阅', 'faultSubscriber.delete', 42, 2),
(45, NULL, '生成二维码', 'faultSubscriber.qrcode', 42, 3),
(46, NULL, '故障记录', 'faultRecord.list', 42, 4),

-- ====== FAQ 管理 ======
(47, NULL, 'FAQ管理', 'faq', 0, 120),
(48, NULL, 'FAQ列表', 'faq.list', 47, 1),
(49, NULL, '新增FAQ', 'faq.add', 47, 2),
(50, NULL, '修改FAQ', 'faq.modify', 47, 3),
(51, NULL, '删除FAQ', 'faq.remove', 47, 4),

-- ====== 反馈管理 ======
(52, NULL, '反馈管理', 'feedback', 0, 130),
(53, NULL, '反馈列表', 'feedback.list', 52, 1),
(54, NULL, '新增反馈', 'feedback.add', 52, 2),
(55, NULL, '修改反馈', 'feedback.modify', 52, 3),
(56, NULL, '删除反馈', 'feedback.remove', 52, 4),

-- ====== 设备配置 ======
(57, NULL, '设备配置', 'deviceConfig', 0, 140),
(58, NULL, '新增配置', 'deviceConfig.add', 57, 1),
(59, NULL, '修改配置', 'deviceConfig.modify', 57, 2),

-- ====== 设备关联 ======
(60, NULL, '设备关联', 'deviceRelation', 0, 150),
(61, NULL, '关联列表', 'deviceRelation.list', 60, 1),
(62, NULL, '新增关联', 'deviceRelation.add', 60, 2),
(63, NULL, '删除关联', 'deviceRelation.remove', 60, 3),

-- ====== 财务管理（前端菜单权限） ======
(64, NULL, '财务管理', 'finance', 0, 85),
(65, NULL, '充值记录', 'recharge.list', 64, 1),
(66, NULL, '退款清单', 'refundLog.list', 64, 2),
(67, NULL, '退款处理', 'refundLog.modify', 64, 3),
(68, NULL, '提现记录', 'withdrawnRecord.list', 64, 4),
(69, NULL, '提现审核', 'withdrawnRecord.modify', 64, 5),
(70, NULL, '结算记录', 'settlement.list', 64, 6),

-- ====== 横幅广告 ======
(71, NULL, '横幅广告', 'banner', 0, 95),
(72, NULL, '广告列表', 'banner.list', 71, 1),
(73, NULL, '新增广告', 'banner.add', 71, 2),
(74, NULL, '修改广告', 'banner.modify', 71, 3),
(75, NULL, '删除广告', 'banner.delete', 71, 4),

-- ====== 平台费率 ======
(76, NULL, '平台费率', 'platformFeeRate', 0, 105),
(77, NULL, '费率列表', 'platformFeeRate.list', 76, 1),
(78, NULL, '新增费率', 'platformFeeRate.add', 76, 2),
(79, NULL, '修改费率', 'platformFeeRate.modify', 76, 3),

-- ====== 补充已有模块缺失的子权限 ======
(80, NULL, '删除用户', 'user.delete', 14, 4),
(81, NULL, '删除字典', 'dict.delete', 27, 4),
(82, NULL, '投资人列表', 'investor.list', 20, 2),
(83, NULL, '修改投资人', 'investor.modify', 20, 3),
(84, NULL, '删除投资人', 'investor.delete', 20, 4),
(85, NULL, '删除设备配置', 'deviceConfig.remove', 57, 3),

-- ====== 数据统计 ======
(86, NULL, '数据统计', 'statistics', 0, 6),
(87, NULL, '日统计列表', 'dailyStat.list', 86, 1),
(88, NULL, '日统计同步', 'dailyStat.sync', 86, 2),
(89, NULL, '月统计列表', 'monthStat.list', 86, 3),
(90, NULL, '月统计同步', 'monthStat.sync', 86, 4),

-- ====== 停车券管理 ======
(91, NULL, '停车券管理', 'parkingCouponRecord', 0, 105),
(92, NULL, '发放记录', 'parkingCouponRecord.list', 91, 1);

-- ----------------------------
-- 3. 将全部权限写入超级管理员角色（role_id = 1）
--    permissions 字段为管道分隔的权限值字符串
--    注意：92 个权限值总长超过 GROUP_CONCAT 默认 1024，需先调大
-- ----------------------------
SET SESSION group_concat_max_len = 10000;
UPDATE `t_role` SET `permissions` = (
    SELECT GROUP_CONCAT(`value` SEPARATOR '|') FROM `t_permission` ORDER BY `id`
) WHERE `id` = 1;

-- ====================================================
-- 验证
-- SELECT COUNT(*) AS perm_count FROM t_permission;          -- 应为 92
-- SELECT permissions FROM t_role WHERE id = 1;              -- 应为92个值以|分隔
-- ====================================================
