-- ============================================================
-- Shiro 权限初始化 —— 抖音商品(同步 / 查询 / 绑定套餐)
-- 数据库: PostgreSQL (park_bike, system_* 系列表)
-- 作者  : rk
-- 日期  : 2026-06-25
-- 说明  :
--   1) 本脚本【幂等】,可重复执行,不产生重复权限或重复授权。
--   2) Shiro 的 @RequiresPermissions 匹配 system_permission.code。
--   3) 抖音【验券】类接口为 web 端使用(JWT 鉴权),不走 Shiro,
--      故不在此处登记权限。
--   4) 默认授权给超管角色 id = 1;若不同请替换。菜单(system_menu)另行配置。
-- ============================================================


-- ------------------------------------------------------------
-- 一、抖音商品权限定义(system_permission)
-- ------------------------------------------------------------

INSERT INTO system_permission (code, name, remark, fixed, deleted, create_time, update_time)
SELECT 'business:douyinProduct:sync', '抖音商品-同步', '从抖音同步团购商品到本地', 0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM system_permission WHERE code = 'business:douyinProduct:sync' AND deleted = 0);

INSERT INTO system_permission (code, name, remark, fixed, deleted, create_time, update_time)
SELECT 'business:douyinProduct:query', '抖音商品-查询', '抖音商品分页/详情/联调测试查询', 0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM system_permission WHERE code = 'business:douyinProduct:query' AND deleted = 0);

INSERT INTO system_permission (code, name, remark, fixed, deleted, create_time, update_time)
SELECT 'business:douyinProduct:bind', '抖音商品-绑定套餐', '将抖音商品 out_id 绑定本地套餐(discount.id),空值解绑', 0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM system_permission WHERE code = 'business:douyinProduct:bind' AND deleted = 0);


-- ------------------------------------------------------------
-- 二、授权给超级管理员角色(system_role_permission,默认 id = 1)
-- ------------------------------------------------------------

INSERT INTO system_role_permission (role_id, permission_id, deleted, create_time, update_time)
SELECT 1 AS role_id, p.id AS permission_id, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM system_permission p
WHERE p.deleted = 0
  AND p.code IN (
       'business:douyinProduct:sync',
       'business:douyinProduct:query',
       'business:douyinProduct:bind'
  )
  AND NOT EXISTS (
       SELECT 1 FROM system_role_permission rp
       WHERE rp.role_id = 1 AND rp.permission_id = p.id AND rp.deleted = 0
  );


-- ============================================================
-- 附:给【其它角色】授权的模板(按需取消注释使用)
-- ------------------------------------------------------------
-- INSERT INTO system_role_permission (role_id, permission_id, deleted, create_time, update_time)
-- SELECT r.id, p.id, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
-- FROM system_permission p
-- CROSS JOIN system_role r
-- WHERE p.deleted = 0
--   AND r.deleted = 0
--   AND r.name LIKE '%运营%'                  -- ← 改成目标角色名关键字
--   AND p.code LIKE 'business:douyinProduct%'
--   AND NOT EXISTS (
--        SELECT 1 FROM system_role_permission rp
--        WHERE rp.role_id = r.id AND rp.permission_id = p.id AND rp.deleted = 0
--   );
-- ============================================================


-- ============================================================
-- 数据报表(概览 / 收入车型分析 / 收入统计)—— 不登记权限点
-- 作者  : rk
-- 日期  : 2026-06-26
-- 说明  :
--   1) 数据报表接口由 web 端(JWT)迁移至 platform 端(/business/report/*)。
--   2) 【配置约定】报表类为只读统计,不做菜单/按钮级权限限制:
--      Controller 上不挂 @RequiresPermissions,仅受 Shiro authc 登录校验保护,
--      任何登录后台的账号均可访问;故不登记 system_permission 权限点,
--      也不写 system_role_permission 授权。
--   3) 如日后需要细粒度权限,再于此追加 business:report:query 定义并授权。
-- ============================================================


-- ============================================================
-- 抖音券核销(管理端:核销记录分页 / 撤销核销)
-- 作者  : rk
-- 日期  : 2026-06-26
-- 说明  :
--   1) 撤销核销由 web 端(/web/douyin/cancel,JWT)迁移至 platform 端(/business/douyinVerify/cancel,Shiro)。
--   2) 核销记录对外分页 /business/douyinVerify/page 亦在 platform 端。
--   3) 幂等登记 query/cancel 两个权限点,默认授权超管 role_id = 1。
-- ============================================================

INSERT INTO system_permission (code, name, remark, fixed, deleted, create_time, update_time)
SELECT 'business:douyinVerify:query', '抖音核销-查询', '抖音券核销记录对外分页查询', 0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM system_permission WHERE code = 'business:douyinVerify:query' AND deleted = 0);

INSERT INTO system_permission (code, name, remark, fixed, deleted, create_time, update_time)
SELECT 'business:douyinVerify:cancel', '抖音核销-撤销', '撤销抖音券核销(管理端,作废本地套餐卡)', 0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM system_permission WHERE code = 'business:douyinVerify:cancel' AND deleted = 0);

INSERT INTO system_role_permission (role_id, permission_id, deleted, create_time, update_time)
SELECT 1 AS role_id, p.id AS permission_id, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM system_permission p
WHERE p.deleted = 0
  AND p.code IN (
       'business:douyinVerify:query',
       'business:douyinVerify:cancel'
  )
  AND NOT EXISTS (
       SELECT 1 FROM system_role_permission rp
       WHERE rp.role_id = 1 AND rp.permission_id = p.id AND rp.deleted = 0
  );
