CREATE TABLE IF NOT EXISTS coin_seller_sub_applications (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  application_id BIGINT NOT NULL PRIMARY KEY COMMENT '子币商申请 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '提交申请幂等命令 ID',
  parent_user_id BIGINT NOT NULL COMMENT '父币商用户 ID',
  target_user_id BIGINT NOT NULL COMMENT '目标子币商用户 ID',
  status VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '审核状态 pending/approved/rejected',
  relation_id BIGINT NOT NULL DEFAULT 0 COMMENT '审核通过后创建的直属关系 ID',
  reviewed_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '审核管理员 ID',
  review_reason VARCHAR(255) NOT NULL DEFAULT '' COMMENT '审核备注',
  reviewed_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '审核时间，UTC epoch ms',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  pending_target_user_id BIGINT GENERATED ALWAYS AS (
    CASE WHEN status = 'pending' THEN target_user_id ELSE NULL END
  ) STORED COMMENT '当前待审目标用户 ID',
  UNIQUE KEY uk_coin_seller_sub_application_command (app_code, command_id),
  UNIQUE KEY uk_coin_seller_sub_application_pending_target (app_code, pending_target_user_id),
  KEY idx_coin_seller_sub_application_parent (app_code, parent_user_id, status, created_at_ms),
  KEY idx_coin_seller_sub_application_target (app_code, target_user_id, status, created_at_ms),
  KEY idx_coin_seller_sub_application_status (app_code, status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='币商子币商邀请审核申请表';
