-- Create syntax for TABLE 'user'
CREATE TABLE IF NOT EXISTS `user`
(
    `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `login_name`    varchar(64)         NOT NULL,
    `name`          varchar(64)         NOT NULL,
    `password`      varchar(64)         NOT NULL,
    `role`          varchar(64)         NOT NULL,
    `enable`        tinyint(1)          NOT NULL,
    `export_secret` varchar(64)         NOT NULL,
    `access_token`  varchar(64) DEFAULT NULL,
    `create_time`   bigint(20)          NOT NULL,
    `update_time`   bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `login_name` (`login_name`),
    UNIQUE KEY `export_secret` (`export_secret`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'user_permission'
CREATE TABLE IF NOT EXISTS `user_permission`
(
    `user_id`    bigint(20) unsigned NOT NULL,
    `permission` varchar(64)         NOT NULL,
    `scope`      text                NOT NULL,
    PRIMARY KEY (`user_id`, `permission`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'app'
CREATE TABLE IF NOT EXISTS `app`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `code`        varchar(64)         NOT NULL,
    `name`        varchar(64)         NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`code`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'property_item'
CREATE TABLE IF NOT EXISTS `property_item`
(
    `version_id`  bigint(20) unsigned NOT NULL,
    `module_id`   bigint(20) unsigned NOT NULL,
    `env_id`      bigint(20) unsigned NOT NULL,
    `key`         varchar(64)         NOT NULL,
    `value`       text                NOT NULL,
    `comment`     varchar(128) DEFAULT NULL,
    `mode`        varchar(16)         NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`version_id`, `module_id`, `env_id`, `key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'env'
CREATE TABLE IF NOT EXISTS `env`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `app_id`      bigint(20) unsigned NOT NULL,
    `code`        varchar(64)         NOT NULL,
    `name`        varchar(64)         NOT NULL,
    `type`        varchar(16)         NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`app_id`, `code`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'module'
CREATE TABLE IF NOT EXISTS `module`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `app_id`      bigint(20) unsigned NOT NULL,
    `code`        varchar(64)         NOT NULL,
    `name`        varchar(64)         NOT NULL,
    `type`        varchar(16)         NOT NULL,
    `enable`      tinyint(1)          NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`app_id`, `code`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

-- Create syntax for TABLE 'version'
CREATE TABLE IF NOT EXISTS `version`
(
    `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `app_id`        bigint(20) unsigned NOT NULL,
    `name`          varchar(64)         NOT NULL,
    `main_number`   int(11)             NOT NULL,
    `middle_number` int(11)             NOT NULL,
    `sub_number`    int(11)             NOT NULL,
    `archived`      tinyint(1)          NOT NULL,
    `create_time`   bigint(20)          NOT NULL,
    `update_time`   bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `number` (`app_id`, `main_number`, `middle_number`, `sub_number`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `nginx_config`
(
    `app_id`      bigint(20) unsigned NOT NULL,
    `module_id`   bigint(20) unsigned NOT NULL,
    `env_id`      bigint(20) unsigned NOT NULL,
    `content`     text                NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`app_id`, `module_id`, `env_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `deploy`
(
    `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `app_id`        bigint(20) unsigned NOT NULL,
    `name`          varchar(64)         NOT NULL,
    `mode`          varchar(16)         NOT NULL,
    `control`       text DEFAULT NULL,
    `ip_allow`      text DEFAULT NULL,
    `access_key`    varchar(128)        NOT NULL,
    `access_secret` varchar(128)        NOT NULL,
    `create_time`   bigint(20)          NOT NULL,
    `update_time`   bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `access_key` (`access_key`),
    KEY `app_id` (`app_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `deploy_device`
(
    `deploy_id`   bigint(20) unsigned NOT NULL,
    `device_id`   varchar(128)        NOT NULL,
    `remark`      text DEFAULT NULL,
    `create_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`deploy_id`, `device_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `deploy_org`
(
    `deploy_id` bigint(20) unsigned NOT NULL,
    `org_id`    bigint(20) unsigned NOT NULL,
    PRIMARY KEY (`deploy_id`, `org_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `org`
(
    `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `code`          varchar(16)         NOT NULL,
    `name`          varchar(64)         NOT NULL,
    `type`          varchar(16)         NOT NULL,
    `sub_types`     varchar(128) DEFAULT NULL,
    `logo`          varchar(128) DEFAULT NULL,
    `enable`        tinyint(1)          NOT NULL,
    `access_key`    varchar(64)         NOT NULL,
    `access_secret` varchar(64)         NOT NULL,
    `create_time`   bigint(20)          NOT NULL,
    `update_time`   bigint(20)          NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`code`),
    UNIQUE KEY `access_key` (`access_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `wxapp`
(
    `id`           varchar(128) NOT NULL,
    `name`         varchar(64)  NOT NULL,
    `secret`       varchar(512) NOT NULL,
    `access_token` varchar(128) DEFAULT NULL,
    `expire_time`  bigint(20)   DEFAULT NULL,
    `create_time`  bigint(20)   NOT NULL,
    `update_time`  bigint(20)   NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `control_param`
(
    `id`     bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `app_id` bigint(20) unsigned NOT NULL,
    `key`    varchar(64)         NOT NULL,
    `name`   varchar(64)         NOT NULL,
    `type`   varchar(16)         NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `app_key` (`app_id`, `key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `llm_supplier`
(
    `id`                bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name`              varchar(32)         NOT NULL,
    `url`               varchar(128)        NOT NULL,
    `secret`            varchar(128)        NOT NULL,
    `chat_client_class` varchar(128)        NOT NULL,
    `create_time`       bigint(20)          NOT NULL,
    `update_time`       bigint(20)          NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `llm_model`
(
    `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `supplier_id` bigint(20) unsigned NOT NULL,
    `name`        varchar(32)         NOT NULL,
    `qpm`         int(11)             NOT NULL,
    `tpm`         int(11)             NOT NULL,
    `create_time` bigint(20)          NOT NULL,
    `update_time` bigint(20)          NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `llm_org_config`
(
    `org_id`       bigint(20) unsigned NOT NULL,
    `app_type`     varchar(32)         NOT NULL,
    `model_id`     bigint(20) unsigned NOT NULL,
    `prompt_id`    bigint(20) unsigned NOT NULL,
    `permit_count` int(11)             NOT NULL,
    `left_count`   int(11)             NOT NULL,
    PRIMARY KEY (`org_id`, `app_type`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `llm_prompt_template`
(
    `id`       bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `model_id` bigint(20) unsigned NOT NULL,
    `app_type` varchar(32)         NOT NULL,
    `system`   text                NOT NULL,
    `user`     text                NOT NULL,
    `remark`   varchar(32)         NOT NULL,
    PRIMARY KEY (`id`),
    KEY `model_app_type` (`model_id`, `app_type`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;


CREATE TABLE IF NOT EXISTS `ocr_supplier`
(
    `id`           bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name`         varchar(32)         NOT NULL,
    `url`          varchar(128)        NOT NULL,
    `key`          varchar(128)        NOT NULL,
    `secret`       varchar(128)        NOT NULL,
    `client_class` varchar(128)        NOT NULL,
    `qps`          int(11)             NOT NULL,
    `prior`        tinyint(1)          NOT NULL,
    `create_time`  bigint(20)          NOT NULL,
    `update_time`  bigint(20)          NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;