-- ============================================================
-- 通用审批框架（轻量自建）
-- 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, '默认或签，请在「审批配置」修改审批人');
