-- 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

DELETE FROM sys_dictionary_details d
USING sys_dictionaries dict
WHERE d.sys_dictionary_id = dict.id
  AND dict.type = 'book_author_status';

DELETE FROM sys_dictionaries
WHERE type = 'book_author_status';

WITH dict_seed(name, type, status, description) AS (
  VALUES
    ('书籍评论状态', '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_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_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_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
);

-- 作者启停字段统一为 is_enabled bool / book_author, sys_dictionaries, sys_dictionary_details / 2026-04-27

DO $$
BEGIN
  IF to_regclass('public.book_author') IS NOT NULL THEN
    ALTER TABLE book_author
      ADD COLUMN IF NOT EXISTS is_enabled boolean;

    IF EXISTS (
      SELECT 1
      FROM information_schema.columns
      WHERE table_schema = 'public'
        AND table_name = 'book_author'
        AND column_name = 'author_status'
    ) THEN
      UPDATE book_author
      SET is_enabled = CASE WHEN author_status = 'disabled' THEN false ELSE true END
      WHERE is_enabled IS NULL;
    ELSE
      UPDATE book_author
      SET is_enabled = true
      WHERE is_enabled IS NULL;
    END IF;

    ALTER TABLE book_author
      ALTER COLUMN is_enabled SET DEFAULT true;

    ALTER TABLE book_author
      ALTER COLUMN is_enabled SET NOT NULL;

    COMMENT ON COLUMN book_author.is_enabled IS '作者是否启用';

    DROP INDEX IF EXISTS idx_book_author_author_status;
    CREATE INDEX IF NOT EXISTS idx_book_author_is_enabled ON book_author (is_enabled);

    ALTER TABLE book_author
      DROP COLUMN IF EXISTS author_status;
  END IF;
END $$;

UPDATE sys_dictionary_details d
SET status = false,
    updated_at = NOW()
FROM sys_dictionaries dict
WHERE d.sys_dictionary_id = dict.id
  AND dict.type = 'common_enabled_status';

UPDATE sys_dictionaries
SET status = false,
    updated_at = NOW()
WHERE type = 'common_enabled_status';
