SET NAMES utf8mb4;
SET
FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `open_app_key_secrets`;
CREATE TABLE `open_app_key_secrets`
(
    `id`            int                                                           NOT NULL AUTO_INCREMENT COMMENT 'id',
    `app_id`        varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci  NOT NULL COMMENT 'appId',
    `customer_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci  NOT NULL COMMENT '客户名字',
    `key`           varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT 'app key',
    `secret`        varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NOT NULL COMMENT 'app secret',
    `deleted`       tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记（0正常 1删除）',
    `create_time`   datetime                                                      NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`   datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `create_by`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建人',
    `update_by`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '修改人',
    `tenant_id`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '租户ID（可选）',
    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_german2_ci ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `open_sn_call_logs`;
CREATE TABLE `open_sn_call_logs`
(
    `id`             bigint                                                        NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `sn_code`        varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'SN码',
    `session_id`     int                                                           NOT NULL,
    `request_type`   varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci  NOT NULL COMMENT '请求类型，如authorize、correct',
    `request_desc`   varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '请求描述，如“授权请求”或“批改接口调用”',
    `key_parameters` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '关键参数（如题目ID、用户ID等）',
    `request`        longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '请求参数JSON',
    `response`       longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '响应数据JSON',
    `status`         tinyint                                                       NOT NULL DEFAULT 0 COMMENT '状态：0成功 1失败',
    `error_message`  longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '错误信息',
    `use_time`       int NULL DEFAULT NULL COMMENT '接口耗时（毫秒）',
    `start_time`     datetime                                                      NOT NULL COMMENT '请求开始时间',
    `end_time`       datetime NULL DEFAULT NULL COMMENT '请求结束时间',
    `deleted`        tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记（0正常 1删除）',
    `create_time`    datetime                                                      NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`    datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `create_by`      varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建人',
    `update_by`      varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '修改人',
    `tenant_id`      varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '租户ID（可选）',
    PRIMARY KEY (`id`) USING BTREE,
    INDEX            `idx_sn_code`(`sn_code` ASC) USING BTREE,
    INDEX            `idx_request_type`(`request_type` ASC) USING BTREE,
    INDEX            `idx_start_time`(`start_time` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 40 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'SN接口调用日志表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `open_sn_codes`;
CREATE TABLE `open_sn_codes`
(
    `id`          bigint                                                       NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `app_id`      int                                                          NOT NULL COMMENT 'app id不可为空',
    `sn_code`     varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'SN码，唯一标识授权方',
    `status`      tinyint                                                      NOT NULL DEFAULT 1 COMMENT '0=禁用，1=启用',
    `used_count`  int                                                          NOT NULL DEFAULT 0 COMMENT '累计调用次数',
    `expire_time` datetime NULL DEFAULT NULL COMMENT '到期时间（可为空表示永久有效）',
    `remark`      varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注信息',
    `deleted`     tinyint                                                      NOT NULL,
    `create_time` datetime                                                     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` datetime                                                     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `create_by`   varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
    `update_by`   varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
    `tenant_id`   varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '租户id',
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE INDEX `uk_sn_code`(`sn_code` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'SN授权表' ROW_FORMAT = Dynamic;

DROP TABLE IF EXISTS `open_sn_daily_stats`;
CREATE TABLE `open_sn_daily_stats`
(
    `id`            bigint                                                       NOT NULL AUTO_INCREMENT,
    `sn_code`       varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'SN码',
    `date`          date                                                         NOT NULL COMMENT '统计日期',
    `total_calls`   bigint                                                       NOT NULL DEFAULT 0 COMMENT '总调用次数',
    `success_calls` bigint                                                       NOT NULL DEFAULT 0 COMMENT '成功次数',
    `fail_calls`    bigint                                                       NOT NULL DEFAULT 0 COMMENT '失败次数',
    `avg_use_time`  bigint NULL DEFAULT 0 COMMENT '平均耗时（毫秒）',
    `deleted`       tinyint(1) NOT NULL DEFAULT 0 COMMENT '逻辑删除标记（0正常 1删除）',
    `create_time`   datetime                                                     NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time`   datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `create_by`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建人',
    `update_by`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '修改人',
    `tenant_id`     varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '租户ID（可选）',
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE INDEX `uk_sn_date`(`sn_code` ASC, `date` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = 'SN每日调用统计表' ROW_FORMAT = Dynamic;

SET
FOREIGN_KEY_CHECKS = 1;
