CREATE TABLE IF NOT EXISTS tenant_onboarding_applications
(
    id                   BIGINT       NOT NULL,
    tenant_id            BIGINT       DEFAULT NULL COMMENT '审核通过后激活的租户',
    owner_user_id        BIGINT       DEFAULT NULL COMMENT '企业 Owner 账号',
    company_name         VARCHAR(160) NOT NULL COMMENT '企业名称',
    credit_code          VARCHAR(32)  NOT NULL COMMENT '统一社会信用代码',
    legal_person_name    VARCHAR(80)  NOT NULL COMMENT '法人姓名',
    agent_name           VARCHAR(80)  DEFAULT NULL COMMENT '经办人',
    contact_name         VARCHAR(80)  NOT NULL COMMENT '联系人',
    contact_phone        VARCHAR(40)  DEFAULT NULL COMMENT '联系电话',
    contact_email        VARCHAR(160) DEFAULT NULL COMMENT '联系邮箱',
    owner_username       VARCHAR(64)  NOT NULL COMMENT 'Owner 登录名',
    owner_email          VARCHAR(160) NOT NULL COMMENT 'Owner 登录邮箱',
    industry             VARCHAR(80)  DEFAULT NULL COMMENT '所属行业',
    company_scale        VARCHAR(40)  DEFAULT NULL COMMENT '企业规模',
    business_scope       TEXT         DEFAULT NULL COMMENT '经营范围',
    trial_plan           VARCHAR(40)  NOT NULL DEFAULT 'PRO_TRIAL' COMMENT '拟开通试用套餐',
    license_file         VARCHAR(500) DEFAULT NULL COMMENT '营业执照附件',
    identity_file        VARCHAR(500) DEFAULT NULL COMMENT '法人身份核验附件',
    authorization_file   VARCHAR(500) DEFAULT NULL COMMENT '经办人授权书附件',
    license_status       VARCHAR(40)  NOT NULL DEFAULT '待验真',
    identity_status      VARCHAR(40)  NOT NULL DEFAULT '待核身',
    authorization_status VARCHAR(40)  NOT NULL DEFAULT '待确认',
    risk_level           VARCHAR(40)  NOT NULL DEFAULT '需人工确认',
    risk_reason          VARCHAR(255) DEFAULT NULL,
    status               VARCHAR(40)  NOT NULL DEFAULT '资料待审',
    review_note          TEXT         DEFAULT NULL,
    reviewed_by          BIGINT       DEFAULT NULL,
    reviewed_at          DATETIME(3)  DEFAULT NULL,
    source               VARCHAR(40)  NOT NULL DEFAULT '客户端入驻',
    created_at           DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at           DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    UNIQUE KEY uk_onboarding_credit_code (credit_code),
    UNIQUE KEY uk_onboarding_owner_email (owner_email),
    KEY idx_onboarding_status_created (status, created_at),
    KEY idx_onboarding_tenant (tenant_id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci COMMENT '企业入驻审核申请';
