# 数据库 Schema

## 主表 workflows

```sql
CREATE TABLE workflows (
    id              VARCHAR(64) PRIMARY KEY,  -- 格式: {module}-{timestamp}-{creator}
    version         INT NOT NULL DEFAULT 1,   -- 乐观锁版本号

    -- 工单类型与来源
    module_code     VARCHAR(32) NOT NULL,     -- rmdc-user-auth / rmdc-project-management / deliver-update
    workflow_type   VARCHAR(32) NOT NULL,     -- user_registration / user_management / project_detail / microservice_update
    priority        INT DEFAULT 3,            -- 1-5 优先级

    -- 状态与流程
    status          VARCHAR(32) NOT NULL,
    current_step    INT DEFAULT 1,
    total_steps     INT DEFAULT 1,

    -- 人员关联
    creator_id      BIGINT NOT NULL,
    creator_name    VARCHAR(64),
    assignee_id     BIGINT,
    assignee_name   VARCHAR(64),
    delegated_by    BIGINT,                   -- 委派来源

    -- 时间管理
    deadline        DATETIME,
    started_at      DATETIME,
    completed_at    DATETIME,

    -- 业务数据
    title           VARCHAR(256),
    description     TEXT,
    business_payload JSON,                    -- 通用业务数据
    result_payload  JSON,                     -- 处理结果数据

    -- 审计
    created_at      DATETIME NOT NULL,
    updated_at      DATETIME NOT NULL,

    INDEX idx_module_code (module_code),
    INDEX idx_workflow_type (workflow_type),
    INDEX idx_status (status),
    INDEX idx_creator (creator_id),
    INDEX idx_assignee (assignee_id)
);
```

## 步骤表 workflow_steps

```sql
CREATE TABLE workflow_steps (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id     VARCHAR(64) NOT NULL,
    step_order      INT NOT NULL,
    step_name       VARCHAR(64),
    step_type       VARCHAR(32),              -- submit / approve / execute / monitor

    assignee_id     BIGINT,
    assignee_name   VARCHAR(64),
    status          VARCHAR(32),
    result          VARCHAR(32),
    remark          TEXT,
    input_data      JSON,
    output_data     JSON,

    started_at      DATETIME,
    completed_at    DATETIME,
    created_at      DATETIME NOT NULL,

    INDEX idx_workflow (workflow_id),
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
```

## 追踪历史表 workflow_track_history

```sql
CREATE TABLE workflow_track_history (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id     VARCHAR(64) NOT NULL,

    from_status     VARCHAR(32),
    to_status       VARCHAR(32),
    event           VARCHAR(32),              -- submit / approve / reject / return / revoke / close

    operator_id     BIGINT,
    operator_type   VARCHAR(16),              -- user / system / timer
    operator_ip     VARCHAR(45),

    change_details  JSON,
    remark          TEXT,

    created_at      DATETIME NOT NULL,

    INDEX idx_workflow (workflow_id),
    INDEX idx_operator (operator_id)
);
```

## 扩展表

### user_registration_ext

用户注册工单扩展表

```sql
CREATE TABLE user_registration_ext (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id     VARCHAR(64) NOT NULL UNIQUE,
    target_user_id  BIGINT NOT NULL,          -- 被注册用户ID
    original_status VARCHAR(32),              -- 原用户状态（备份）
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
```

### user_management_ext

用户管理工单扩展表

```sql
CREATE TABLE user_management_ext (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id     VARCHAR(64) NOT NULL UNIQUE,
    target_user_id  BIGINT NOT NULL,          -- 被管理用户ID
    action_type     VARCHAR(32) NOT NULL,     -- modify / delete / enable / disable
    original_data   JSON,                     -- 变更前数据快照
    modified_data   JSON,                     -- 期望变更数据
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
```

**original_data / modified_data 示例：**

```json
{
  "username": "zhangsan",
  "chinese_name": "张三",
  "email": "zhangsan@example.com",
  "phone": "13800138000",
  "group_name": "产品与解决方案组",
  "dev_role": "backend",
  "rmdc_role": "normal"
}
```

### project_detail_ext

项目详情工单扩展表

```sql
CREATE TABLE project_detail_ext (
    id               BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id      VARCHAR(64) NOT NULL UNIQUE,
    project_id       VARCHAR(64) NOT NULL,    -- 关联项目ID
    detail_filler_id BIGINT,                  -- 填写人ID
    detail_filler_name VARCHAR(64),           -- 填写人姓名
    draft_data       JSON,                    -- 草稿数据
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
```

**draft_data 示例：**

```json
{
  "deploy_business": {
    "deployer_name": "张三",
    "deployer_phone": "13800138000",
    "deploy_start_time": "2026-01-01",
    "system_version": "v3.2.0"
  },
  "deploy_env": {
    "network_type": "internal",
    "main_public_ip": "10.0.0.1",
    "host_count": 3
  },
  "deploy_middleware": {
    "mysql": {
      "internal_ip": "10.0.0.10",
      "internal_port": 3306
    }
  },
  "last_saved_at": "2026-01-08T10:30:00Z"
}
```

### microservice_update_ext

微服务更新工单扩展表

```sql
CREATE TABLE microservice_update_ext (
    id               BIGINT AUTO_INCREMENT PRIMARY KEY,
    workflow_id      VARCHAR(64) NOT NULL UNIQUE,
    project_id       VARCHAR(64) NOT NULL,    -- 目标项目ID
    namespace        VARCHAR(64) NOT NULL,    -- K8S命名空间
    service_name     VARCHAR(128) NOT NULL,   -- 微服务名称
    build_id         VARCHAR(128),            -- 构建物ID
    current_version  VARCHAR(64),             -- 当前版本
    target_version   VARCHAR(64),             -- 目标版本
    scheduled_at     DATETIME,                -- 计划执行时间
    execute_result   VARCHAR(32),             -- success / failed / rollbacked
    rollback_info    JSON,                    -- 回滚信息
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);
```

**rollback_info 示例：**

```json
{
  "rollback_reason": "Pod健康检查失败",
  "rollback_time": "2026-01-08T15:30:00Z",
  "rollback_version": "v3.1.5",
  "error_details": {
    "pod_name": "service-abc-7d8f9c6b5d-xyz",
    "error_message": "Readiness probe failed: connection refused",
    "failed_checks": 3
  }
}
```

## Migration 回滚脚本

```sql
-- DOWN migration
DROP TABLE IF EXISTS microservice_update_ext;
DROP TABLE IF EXISTS project_detail_ext;
DROP TABLE IF EXISTS user_management_ext;
DROP TABLE IF EXISTS user_registration_ext;
DROP TABLE IF EXISTS workflow_track_history;
DROP TABLE IF EXISTS workflow_steps;
DROP TABLE IF EXISTS workflows;
```
