-- ============================================================
-- 通用审批框架（轻量自建）
-- 1. oa_approval_config   业务-审批人配置（可改）
-- 2. oa_approval_instance 审批单实例（一次提交一条）
-- 3. oa_approval_record   审批操作流水（一人一条）
-- ============================================================

DROP TABLE IF EXISTS oa_approval_record;
DROP TABLE IF EXISTS oa_approval_instance;
DROP TABLE IF EXISTS oa_approval_config;

CREATE TABLE oa_approval_config (
  id              BIGINT       NOT NULL AUTO_INCREMENT COMMENT '主键',
  business_type   VARCHAR(64)  NOT NULL COMMENT '业务类型 key（如 purchase_req/contract）',
  business_name   VARCHAR(128) NOT NULL COMMENT '业务名称（展示用）',
  approver_ids    VARCHAR(512) NOT NULL COMMENT '审批人 sys_user.user_id 列表，逗号分隔',
  sign_type       TINYINT      NOT NULL DEFAULT 1 COMMENT '1或签 2会签',
  enabled         TINYINT      NOT NULL DEFAULT 1 COMMENT '是否启用：0停用 1启用',
  remark          VARCHAR(255)          DEFAULT NULL COMMENT '备注',
  create_by       VARCHAR(64)           DEFAULT NULL,
  create_time     DATETIME              DEFAULT CURRENT_TIMESTAMP,
  update_by       VARCHAR(64)           DEFAULT NULL,
  update_time     DATETIME              DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  del_flag        TINYINT(1)   NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  UNIQUE KEY uk_biz_type (business_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='业务审批配置';

CREATE TABLE oa_approval_instance (
  id                BIGINT       NOT NULL AUTO_INCREMENT COMMENT '主键',
  business_type     VARCHAR(64)  NOT NULL COMMENT '业务类型 key',
  business_id       BIGINT       NOT NULL COMMENT '业务表主键',
  business_title    VARCHAR(255)          DEFAULT NULL COMMENT '业务标题（冗余展示）',
  apply_user_id     BIGINT       NOT NULL COMMENT '申请人 user_id',
  apply_user_name   VARCHAR(64)           DEFAULT NULL,
  apply_time        DATETIME     NOT NULL,
  approver_ids      VARCHAR(512) NOT NULL COMMENT '快照：提交时的审批人',
  sign_type         TINYINT      NOT NULL COMMENT '快照：1或签 2会签',
  status            TINYINT      NOT NULL DEFAULT 0 COMMENT '0待审 1通过 2驳回 3撤回',
  finish_time       DATETIME              DEFAULT NULL,
  remark            VARCHAR(255)          DEFAULT NULL,
  create_by         VARCHAR(64)           DEFAULT NULL,
  create_time       DATETIME              DEFAULT CURRENT_TIMESTAMP,
  update_by         VARCHAR(64)           DEFAULT NULL,
  update_time       DATETIME              DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  del_flag          TINYINT(1)   NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  KEY idx_biz (business_type, business_id),
  KEY idx_status (status),
  KEY idx_apply_user (apply_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批单实例';

CREATE TABLE oa_approval_record (
  id              BIGINT       NOT NULL AUTO_INCREMENT COMMENT '主键',
  instance_id     BIGINT       NOT NULL COMMENT '审批单 id',
  approver_id     BIGINT       NOT NULL COMMENT '审批人 user_id',
  approver_name   VARCHAR(64)           DEFAULT NULL,
  action          TINYINT      NOT NULL COMMENT '1通过 2驳回',
  comment         VARCHAR(512)          DEFAULT NULL,
  op_time         DATETIME     NOT NULL,
  create_time     DATETIME              DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_inst (instance_id),
  KEY idx_approver (approver_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批操作流水';

-- 默认业务审批配置（先用 admin=1 占位，部署后到「审批配置」里改成真实审批人）
INSERT INTO oa_approval_config (business_type, business_name, approver_ids, sign_type, enabled, remark) VALUES
  ('purchase_req', '采购需求', '1', 1, 1, '默认或签，请在「审批配置」修改审批人'),
  ('contract',     '合同',     '1', 1, 1, '默认或签，请在「审批配置」修改审批人');

-- ============================================================
-- 菜单：审批中心 / 我的审批 / 我的发起 / 审批配置
-- 注意：id 段 2063900001000001~9 已避开 dev 占用，prod 部署前请确认未冲突
-- ============================================================
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query_param, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES
(2063900001000001, '审批中心',     0,                  50, 'approval',  NULL,                          NULL, 1, 0, 'M', '0', '0', NULL,                       'edit',          'admin', NOW(), '通用审批菜单'),
(2063900001000002, '我的审批',     2063900001000001,    1, 'pending',   'oa/approval/pending/index',   NULL, 1, 0, 'C', '0', '0', 'oa:approval:list',         'checkbox',      'admin', NOW(), '通用审批菜单'),
(2063900001000003, '我的发起',     2063900001000001,    2, 'submitted', 'oa/approval/submitted/index', NULL, 1, 0, 'C', '0', '0', 'oa:approval:submitted',    'documentation', 'admin', NOW(), '通用审批菜单'),
(2063900001000004, '审批配置',     2063900001000001,    3, 'config',    'oa/approval/config/index',    NULL, 1, 0, 'C', '0', '0', 'oa:approval:config',       'edit',          'admin', NOW(), '通用审批菜单'),
(2063900001000005, '审批配置新增', 2063900001000004,    1, '',          NULL,                          NULL, 1, 0, 'F', '0', '0', 'oa:approval:configAdd',    '#',             'admin', NOW(), '通用审批菜单'),
(2063900001000006, '审批配置修改', 2063900001000004,    2, '',          NULL,                          NULL, 1, 0, 'F', '0', '0', 'oa:approval:configEdit',   '#',             'admin', NOW(), '通用审批菜单'),
(2063900001000007, '审批配置删除', 2063900001000004,    3, '',          NULL,                          NULL, 1, 0, 'F', '0', '0', 'oa:approval:configRemove', '#',             'admin', NOW(), '通用审批菜单'),
(2063900001000008, '审批操作',     2063900001000002,    1, '',          NULL,                          NULL, 1, 0, 'F', '0', '0', 'oa:approval:act',          '#',             'admin', NOW(), '通用审批菜单'),
(2063900001000009, '审批撤回',     2063900001000003,    1, '',          NULL,                          NULL, 1, 0, 'F', '0', '0', 'oa:approval:withdraw',     '#',             'admin', NOW(), '通用审批菜单');

-- 「说明」顶级菜单（用户使用说明 / 数据流向）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query_param, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES
(2063900002000001, '说明',   0,                60, 'docs',     NULL,                     NULL, 1, 0, 'M', '0', '0', NULL,               'question',      'admin', NOW(), '说明文档'),
(2063900002000002, '采购流', 2063900002000001,  1, 'purchase', 'oa/docs/purchase/index', NULL, 1, 0, 'C', '0', '0', 'oa:docs:purchase', 'documentation', 'admin', NOW(), '说明文档'),
(2063900002000003, '合同流', 2063900002000001,  2, 'contract', 'oa/docs/contract/index', NULL, 1, 0, 'C', '0', '0', 'oa:docs:contract', 'documentation', 'admin', NOW(), '说明文档');
