-- API 权限分配接口初始化补齐 / sys_apis, casbin_rule / 2026-04-25

INSERT INTO sys_apis (created_at, updated_at, path, description, api_group, method)
SELECT NOW(), NOW(), '/api/getApiRoles', '获取API关联角色ID列表', 'api', 'GET'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_apis WHERE path = '/api/getApiRoles' AND method = 'GET'
);

INSERT INTO sys_apis (created_at, updated_at, path, description, api_group, method)
SELECT NOW(), NOW(), '/api/setApiRoles', '全量覆盖API关联角色', 'api', 'POST'
WHERE NOT EXISTS (
  SELECT 1 FROM sys_apis WHERE path = '/api/setApiRoles' AND method = 'POST'
);

INSERT INTO casbin_rule (ptype, v0, v1, v2)
SELECT 'p', '888', '/api/getApiRoles', 'GET'
WHERE NOT EXISTS (
  SELECT 1 FROM casbin_rule
  WHERE ptype = 'p' AND v0 = '888' AND v1 = '/api/getApiRoles' AND v2 = 'GET'
);

INSERT INTO casbin_rule (ptype, v0, v1, v2)
SELECT 'p', '888', '/api/setApiRoles', 'POST'
WHERE NOT EXISTS (
  SELECT 1 FROM casbin_rule
  WHERE ptype = 'p' AND v0 = '888' AND v1 = '/api/setApiRoles' AND v2 = 'POST'
);

-- book 业务字典初始化补齐 / sys_dictionaries, sys_dictionary_details / 2026-04-26

WITH dict_seed(name, type, status, description) AS (
  VALUES
    ('作者状态', 'book_author_status', true, '作者状态字典'),
    ('书籍评论状态', 'book_comment_status', true, '书籍评论状态字典'),
    ('书籍完结状态', 'book_completion_status', true, '书籍完结状态字典'),
    ('书籍时代标签', 'book_era_tag', true, '书籍时代标签字典'),
    ('书籍上下架状态', 'book_publish_status', true, '书籍上下架状态字典'),
    ('书籍类型', 'book_type', true, '书籍类型动态字典')
)
UPDATE sys_dictionaries d
SET name = s.name,
    status = s.status,
    "desc" = s.description,
    updated_at = NOW(),
    deleted_at = NULL
FROM dict_seed s
WHERE d.type = s.type;

WITH dict_seed(name, type, status, description) AS (
  VALUES
    ('作者状态', 'book_author_status', true, '作者状态字典'),
    ('书籍评论状态', 'book_comment_status', true, '书籍评论状态字典'),
    ('书籍完结状态', 'book_completion_status', true, '书籍完结状态字典'),
    ('书籍时代标签', 'book_era_tag', true, '书籍时代标签字典'),
    ('书籍上下架状态', 'book_publish_status', true, '书籍上下架状态字典'),
    ('书籍类型', 'book_type', true, '书籍类型动态字典')
)
INSERT INTO sys_dictionaries (created_at, updated_at, name, type, status, "desc")
SELECT NOW(), NOW(), s.name, s.type, s.status, s.description
FROM dict_seed s
WHERE NOT EXISTS (
  SELECT 1 FROM sys_dictionaries d WHERE d.type = s.type
);

WITH detail_seed(dict_type, label, value, sort, status) AS (
  VALUES
    ('book_author_status', '启用', 'enabled', 10, true),
    ('book_author_status', '禁用', 'disabled', 20, true),
    ('book_comment_status', '正常', 'normal', 10, true),
    ('book_comment_status', '隐藏', 'hidden', 20, true),
    ('book_completion_status', '完结', 'completed', 10, true),
    ('book_completion_status', '连载', 'serializing', 20, true),
    ('book_era_tag', '未知时代', 'unknown', 10, true),
    ('book_era_tag', '远古', 'ancient', 20, true),
    ('book_era_tag', '汉', 'han', 30, true),
    ('book_era_tag', '唐', 'tang', 40, true),
    ('book_era_tag', '宋', 'song', 50, true),
    ('book_era_tag', '元', 'yuan', 60, true),
    ('book_era_tag', '明', 'ming', 70, true),
    ('book_era_tag', '清', 'qing', 80, true),
    ('book_era_tag', '近代', 'modern', 90, true),
    ('book_era_tag', '现代', 'contemporary', 100, true),
    ('book_publish_status', '草稿', 'draft', 10, true),
    ('book_publish_status', '下架', 'off_shelf', 20, true),
    ('book_publish_status', '上架', 'on_shelf', 30, true)
)
UPDATE sys_dictionary_details d
SET label = s.label,
    sort = s.sort,
    status = s.status,
    extend = '',
    level = 0,
    path = '',
    updated_at = NOW(),
    deleted_at = NULL
FROM detail_seed s
JOIN sys_dictionaries dict ON dict.type = s.dict_type
WHERE d.sys_dictionary_id = dict.id
  AND d.value = s.value;

WITH detail_seed(dict_type, label, value, sort, status) AS (
  VALUES
    ('book_author_status', '启用', 'enabled', 10, true),
    ('book_author_status', '禁用', 'disabled', 20, true),
    ('book_comment_status', '正常', 'normal', 10, true),
    ('book_comment_status', '隐藏', 'hidden', 20, true),
    ('book_completion_status', '完结', 'completed', 10, true),
    ('book_completion_status', '连载', 'serializing', 20, true),
    ('book_era_tag', '未知时代', 'unknown', 10, true),
    ('book_era_tag', '远古', 'ancient', 20, true),
    ('book_era_tag', '汉', 'han', 30, true),
    ('book_era_tag', '唐', 'tang', 40, true),
    ('book_era_tag', '宋', 'song', 50, true),
    ('book_era_tag', '元', 'yuan', 60, true),
    ('book_era_tag', '明', 'ming', 70, true),
    ('book_era_tag', '清', 'qing', 80, true),
    ('book_era_tag', '近代', 'modern', 90, true),
    ('book_era_tag', '现代', 'contemporary', 100, true),
    ('book_publish_status', '草稿', 'draft', 10, true),
    ('book_publish_status', '下架', 'off_shelf', 20, true),
    ('book_publish_status', '上架', 'on_shelf', 30, true)
)
INSERT INTO sys_dictionary_details (created_at, updated_at, label, value, extend, status, sort, sys_dictionary_id, level, path)
SELECT NOW(), NOW(), s.label, s.value, '', s.status, s.sort, dict.id, 0, ''
FROM detail_seed s
JOIN sys_dictionaries dict ON dict.type = s.dict_type
WHERE NOT EXISTS (
  SELECT 1
  FROM sys_dictionary_details d
  WHERE d.sys_dictionary_id = dict.id
    AND d.value = s.value
);
