SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `tb_approval_flow` (
  `id` bigint NOT NULL COMMENT '主键',
  `name` varchar(128) NOT NULL COMMENT '流程名称',
  `resource_type` varchar(32) NOT NULL COMMENT '资源类型',
  `action_type` varchar(32) NOT NULL COMMENT '动作类型',
  `priority` int NOT NULL DEFAULT 0 COMMENT '优先级',
  `status` varchar(32) NOT NULL COMMENT '流程状态',
  `version` int NOT NULL DEFAULT 1 COMMENT '流程版本',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  KEY `idx_approval_flow_match` (`resource_type`, `action_type`, `status`, `priority`),
  KEY `idx_approval_flow_modified` (`modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批流程';

CREATE TABLE IF NOT EXISTS `tb_approval_flow_scope` (
  `id` bigint NOT NULL COMMENT '主键',
  `flow_id` bigint NOT NULL COMMENT '流程ID',
  `scope_type` varchar(32) NOT NULL COMMENT '范围类型',
  `scope_value` bigint NOT NULL COMMENT '范围值',
  `include_children` tinyint NOT NULL DEFAULT 0 COMMENT '是否包含子节点',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_approval_flow_scope` (`flow_id`, `scope_type`, `scope_value`),
  KEY `idx_approval_flow_scope_type` (`scope_type`, `scope_value`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批流程范围';

CREATE TABLE IF NOT EXISTS `tb_approval_flow_step` (
  `id` bigint NOT NULL COMMENT '主键',
  `flow_id` bigint NOT NULL COMMENT '流程ID',
  `step_no` int NOT NULL COMMENT '步骤序号',
  `step_name` varchar(128) NOT NULL COMMENT '步骤名称',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_approval_flow_step` (`flow_id`, `step_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批流程步骤';

CREATE TABLE IF NOT EXISTS `tb_approval_instance` (
  `id` bigint NOT NULL COMMENT '主键',
  `flow_id` bigint NOT NULL COMMENT '流程ID',
  `flow_version` int NOT NULL COMMENT '流程版本',
  `resource_type` varchar(32) NOT NULL COMMENT '资源类型',
  `resource_id` bigint NOT NULL COMMENT '资源ID',
  `action_type` varchar(32) NOT NULL COMMENT '动作类型',
  `status` varchar(32) NOT NULL COMMENT '实例状态',
  `current_step_no` int DEFAULT NULL COMMENT '当前步骤序号',
  `snapshot_json` json DEFAULT NULL COMMENT '审批快照',
  `summary` varchar(255) DEFAULT NULL COMMENT '审批摘要',
  `applicant_id` bigint NOT NULL COMMENT '申请人ID',
  `submitted_at` datetime DEFAULT NULL COMMENT '提交时间',
  `finished_at` datetime DEFAULT NULL COMMENT '完成时间',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  KEY `idx_approval_instance_flow_status` (`flow_id`, `status`),
  KEY `idx_approval_instance_resource` (`resource_type`, `resource_id`, `action_type`),
  KEY `idx_approval_instance_applicant` (`applicant_id`, `submitted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批实例';

CREATE TABLE IF NOT EXISTS `tb_approval_task` (
  `id` bigint NOT NULL COMMENT '主键',
  `instance_id` bigint NOT NULL COMMENT '实例ID',
  `step_no` int NOT NULL COMMENT '步骤序号',
  `status` varchar(32) NOT NULL COMMENT '任务状态',
  `assignee_role_code` varchar(64) NOT NULL COMMENT '指派角色编码',
  `acted_by` bigint DEFAULT NULL COMMENT '处理人ID',
  `acted_at` datetime DEFAULT NULL COMMENT '处理时间',
  `comment` varchar(500) DEFAULT NULL COMMENT '处理意见',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_approval_task_step` (`instance_id`, `step_no`),
  KEY `idx_approval_task_status` (`status`, `assignee_role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批任务';

CREATE TABLE IF NOT EXISTS `tb_approval_log` (
  `id` bigint NOT NULL COMMENT '主键',
  `instance_id` bigint NOT NULL COMMENT '实例ID',
  `event_type` varchar(32) NOT NULL COMMENT '事件类型',
  `operator_id` bigint DEFAULT NULL COMMENT '操作人ID',
  `payload_json` json DEFAULT NULL COMMENT '事件载荷',
  `created` datetime DEFAULT NULL COMMENT '创建时间',
  `created_by` bigint DEFAULT NULL COMMENT '创建者',
  `modified` datetime DEFAULT NULL COMMENT '修改时间',
  `modified_by` bigint DEFAULT NULL COMMENT '修改者',
  PRIMARY KEY (`id`),
  KEY `idx_approval_log_instance` (`instance_id`, `created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批日志';

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000001, 258052082618335232, 0, 'menus.system.approval', '/sys/approval', '/system/approval/ApprovalManage', 'svg:approval',
  1, '', 750, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批管理菜单'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000001
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000011, 367100000000000001, 1, '查询', '', '', '',
  0, '/api/v1/approvalFlow/query', 1, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批流程-查询'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000011
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000012, 367100000000000001, 1, '保存', '', '', '',
  0, '/api/v1/approvalFlow/save', 2, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批流程-保存'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000012
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000013, 367100000000000001, 1, '删除', '', '', '',
  0, '/api/v1/approvalFlow/remove', 3, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批流程-删除'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000013
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000014, 367100000000000001, 1, '启用', '', '', '',
  0, '/api/v1/approvalFlow/enable', 4, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批流程-启用'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000014
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000015, 367100000000000001, 1, '停用', '', '', '',
  0, '/api/v1/approvalFlow/disable', 5, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批流程-停用'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000015
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000021, 367100000000000001, 1, '实例查询', '', '', '',
  0, '/api/v1/approvalInstance/query', 6, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批实例-查询'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000021
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000022, 367100000000000001, 1, '通过审批', '', '', '',
  0, '/api/v1/approvalInstance/approve', 7, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批实例-通过'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000022
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000023, 367100000000000001, 1, '驳回审批', '', '', '',
  0, '/api/v1/approvalInstance/reject', 8, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批实例-驳回'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000023
);

INSERT INTO `tb_sys_menu` (
  `id`, `parent_id`, `menu_type`, `menu_title`, `menu_url`, `component`, `menu_icon`,
  `is_show`, `permission_tag`, `sort_no`, `status`, `created`, `created_by`, `modified`, `modified_by`, `remark`
)
SELECT
  367100000000000024, 367100000000000001, 1, '撤回审批', '', '', '',
  0, '/api/v1/approvalInstance/revoke', 9, 0, '2026-04-06 10:00:00', 1, '2026-04-06 10:00:00', 1, '审批实例-撤回'
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_menu` WHERE `id` = 367100000000000024
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000101, 1, 367100000000000001
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000101
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000111, 1, 367100000000000011
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000111
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000112, 1, 367100000000000012
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000112
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000113, 1, 367100000000000013
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000113
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000114, 1, 367100000000000014
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000114
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000115, 1, 367100000000000015
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000115
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000121, 1, 367100000000000021
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000121
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000122, 1, 367100000000000022
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000122
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000123, 1, 367100000000000023
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000123
);

INSERT INTO `tb_sys_role_menu` (`id`, `role_id`, `menu_id`)
SELECT 367100000000000124, 1, 367100000000000024
FROM DUAL
WHERE NOT EXISTS (
  SELECT 1 FROM `tb_sys_role_menu` WHERE `id` = 367100000000000124
);
