-- 最小工作流（方案3）: 菜单 + 字典 初始化脚本
-- 适用: MySQL
-- 说明:
-- 1) 本脚本会新增一个一级菜单【流程管理】及其子菜单（流程定义/我的待办/我的已办/流程发起）
-- 2) 本脚本会补充字典【流程状态 bpm_status】（如已存在同 code 的字典请按需手动去重）
-- 3) 由于 sys_permission 主键为 id，本脚本使用“按 url/perms/name 清理后插入”的方式尽量保证可重复执行

START TRANSACTION;

-- =========================
-- 1. 字典：流程状态 bpm_status
-- =========================
-- 注意：如果你的库中已存在 code=bpm_status 的字典，这里会先删除再插入（避免重复）。如你不希望删除，可改成只 insert。
DELETE FROM sys_dict_item WHERE dict_id IN (SELECT id FROM sys_dict WHERE dict_code='bpm_status');
DELETE FROM sys_dict WHERE dict_code='bpm_status';

INSERT INTO sys_dict (id, dict_name, dict_code, description, del_flag, create_by, create_time, update_by, update_time, type)
VALUES ('BPM_DICT_STATUS_0001', '流程状态', 'bpm_status', '流程状态', 0, 'admin', NOW(), NULL, NULL, 0);

INSERT INTO sys_dict_item (id, dict_id, item_text, item_value, description, sort_order, status, create_by, create_time, update_by, update_time)
VALUES
('BPM_DICT_STATUS_ITEM_1', 'BPM_DICT_STATUS_0001', '草稿', 'draft', NULL, 1, 1, 'admin', NOW(), NULL, NULL),
('BPM_DICT_STATUS_ITEM_2', 'BPM_DICT_STATUS_0001', '审批中', 'running', NULL, 2, 1, 'admin', NOW(), NULL, NULL),
('BPM_DICT_STATUS_ITEM_3', 'BPM_DICT_STATUS_0001', '已通过', 'passed', NULL, 3, 1, 'admin', NOW(), NULL, NULL),
('BPM_DICT_STATUS_ITEM_4', 'BPM_DICT_STATUS_0001', '已驳回', 'rejected', NULL, 4, 1, 'admin', NOW(), NULL, NULL),
('BPM_DICT_STATUS_ITEM_5', 'BPM_DICT_STATUS_0001', '已撤回', 'revoked', NULL, 5, 1, 'admin', NOW(), NULL, NULL);

-- =========================
-- 2. 菜单：流程管理（一级菜单）
-- =========================
-- 清理：按 url/component/perms/name 清理可能存在的旧记录（避免重复）
DELETE FROM sys_permission WHERE url IN ('/bpm', '/bpm/process-definition', '/bpm/process/design', '/bpm/task/todo', '/bpm/task/done', '/bpm/process/start');
DELETE FROM sys_permission WHERE component IN (
  'layouts/RouteView',
  'modules/bpm/process/ProcessDefinitionList',
  'modules/bpm/process/ProcessDesigner',
  'modules/bpm/task/MyTaskList',
  'modules/bpm/task/MyDoneList',
  'modules/bpm/process/StartProcess'
) AND name IN ('流程管理','流程定义','流程设计','我的待办','我的已办','流程发起');

-- 一级菜单
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_ROOT_0001', '', '流程管理', '/bpm', 'layouts/RouteView', NULL, NULL,
  0, NULL, '1', 50.00, 0, 'profile',
  1, 0, 0, 0, 0, '最小工作流菜单（方案3）',
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

-- 子菜单：流程定义
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_PROC_DEF_0001', 'BPM_MENU_ROOT_0001', '流程定义', '/bpm/process-definition', 'modules/bpm/process/ProcessDefinitionList', NULL, NULL,
  1, NULL, '1', 1.00, 0, 'deployment-unit',
  1, 1, 0, 0, 0, NULL,
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

-- 子菜单：流程设计（画布设计器）
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_DESIGN_0001', 'BPM_MENU_ROOT_0001', '流程设计', '/bpm/process/design', 'modules/bpm/process/ProcessDesigner', NULL, NULL,
  1, NULL, '1', 1.50, 0, 'edit',
  1, 1, 0, 0, 0, '可视化流程设计器',
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

-- 子菜单：我的待办
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_TASK_TODO_0001', 'BPM_MENU_ROOT_0001', '我的待办', '/bpm/task/todo', 'modules/bpm/task/MyTaskList', NULL, NULL,
  1, NULL, '1', 2.00, 0, 'clock-circle',
  1, 1, 0, 0, 0, NULL,
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

-- 子菜单：我的已办
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_TASK_DONE_0001', 'BPM_MENU_ROOT_0001', '我的已办', '/bpm/task/done', 'modules/bpm/task/MyDoneList', NULL, NULL,
  1, NULL, '1', 3.00, 0, 'check-square',
  1, 1, 0, 0, 0, NULL,
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

-- 子菜单：流程发起
INSERT INTO sys_permission (
  id, parent_id, name, url, component, component_name, redirect,
  menu_type, perms, perms_type, sort_no, always_show, icon,
  is_route, is_leaf, keep_alive, hidden, hide_tab, description,
  create_by, create_time, update_by, update_time, del_flag, rule_flag, status, internal_or_external
) VALUES (
  'BPM_MENU_START_0001', 'BPM_MENU_ROOT_0001', '流程发起', '/bpm/process/start', 'modules/bpm/process/StartProcess', NULL, NULL,
  1, NULL, '1', 4.00, 0, 'play-circle',
  1, 1, 0, 0, 0, NULL,
  'admin', NOW(), NULL, NULL, 0, 0, '1', 0
);

COMMIT;

