create table goods_list
(
    gid        varchar(20) not null,
    stock      int         not null,
    start_time datetime    not null,
    end_time   datetime    not null
);

create table host
(
    id     smallint auto_increment
        primary key,
    name   varchar(64)               not null,
    alias  varchar(32)  default ''   not null,
    port   int          default 5921 not null,
    remark varchar(100) default ''   not null
)
    charset = utf8mb3;

create table login_log
(
    id       int auto_increment
        primary key,
    username varchar(32) not null,
    ip       varchar(15) not null,
    created  datetime    not null
)
    charset = utf8mb3;

create table oders_list
(
    order_id   varchar(30) not null,
    trade_time datetime    not null
);

create table setting
(
    id    int auto_increment
        primary key,
    code  varchar(32)              not null,
    `key` varchar(64)              not null,
    value varchar(4096) default '' not null
)
    charset = utf8mb3;

create table task
(
    id                 int auto_increment
        primary key,
    name               varchar(32)             not null,
    level              tinyint      default 1  not null,
    dependency_task_id varchar(64)  default '' not null,
    dependency_status  tinyint      default 1  not null,
    spec               varchar(64)             not null,
    protocol           tinyint                 not null,
    command            varchar(256)            not null,
    http_method        tinyint      default 1  not null,
    timeout            mediumint    default 0  not null,
    multi              tinyint      default 1  not null,
    retry_times        tinyint      default 0  not null,
    retry_interval     smallint     default 0  not null,
    notify_status      tinyint      default 1  not null,
    notify_type        tinyint      default 0  not null,
    notify_receiver_id varchar(256) default '' not null,
    notify_keyword     varchar(128) default '' not null,
    tag                varchar(32)  default '' not null,
    remark             varchar(100) default '' not null,
    status             tinyint      default 0  not null,
    created            datetime                not null,
    deleted            datetime                null
)
    charset = utf8mb3;

create index IDX_task_level
    on task (level);

create index IDX_task_protocol
    on task (protocol);

create index IDX_task_status
    on task (status);

create table task_host
(
    id      int auto_increment
        primary key,
    task_id int      not null,
    host_id smallint not null
)
    charset = utf8mb3;

create index IDX_task_host_host_id
    on task_host (host_id);

create index IDX_task_host_task_id
    on task_host (task_id);

create table task_log
(
    id          bigint auto_increment
        primary key,
    task_id     int          default 0  not null,
    name        varchar(32)             not null,
    spec        varchar(64)             not null,
    protocol    tinyint                 not null,
    command     varchar(256)            not null,
    timeout     mediumint    default 0  not null,
    retry_times tinyint      default 0  not null,
    hostname    varchar(128) default '' not null,
    start_time  datetime                null,
    end_time    datetime                null,
    status      tinyint      default 1  not null,
    result      mediumtext              not null
)
    charset = utf8mb3;

create index IDX_task_log_protocol
    on task_log (protocol);

create index IDX_task_log_status
    on task_log (status);

create index IDX_task_log_task_id
    on task_log (task_id);

create table user
(
    id       int auto_increment
        primary key,
    name     varchar(32)            not null,
    password char(32)               not null,
    salt     char(6)                not null,
    email    varchar(50) default '' not null,
    created  datetime               not null,
    updated  datetime               null,
    is_admin tinyint     default 0  not null,
    status   tinyint     default 1  not null,
    constraint UQE_user_email
        unique (email),
    constraint UQE_user_name
        unique (name)
)
    charset = utf8mb3;

create table user_info
(
    uid              varchar(40)  not null
        primary key,
    telephone        varchar(255) not null,
    password         varchar(30)  null,
    avatar_url       varchar(255) null,
    gender           int          not null,
    `birthdate-date` datetime     null,
    createdtime      datetime     not null,
    updatedtime      datetime     not null,
    bio              varchar(255) null,
    username         varchar(30)  not null
);

create table article_list
(
    articleId        bigint auto_increment comment '文评投票唯一 ID（主键）'
        primary key,
    title            varchar(255) not null comment '文评投票标题',
    vote_type        varchar(50)  not null comment '投票类型',
    total_voters_num int          not null comment '投票总人数',
    end_time         datetime     not null comment '投票结束时间',
    is_ended         tinyint(1)   not null comment '是否已结束（1 = 结束，0 = 未结束）',
    publish_user_id  varchar(40)  not null comment '发表用户 ID（关联用户表）',
    create_time      datetime     not null comment '投票创建时间',
    constraint article_list_user_info_uid_fk
        foreign key (publish_user_id) references user_info (uid)
);

create table article_options
(
    id               bigint auto_increment comment '选项唯一 ID'
        primary key,
    vote_article_id  bigint       not null comment '关联文评投票主表 ID',
    option_content   varchar(255) not null comment '选项内容',
    option_votes_num int          not null comment '该选项得票数',
    sort_order       int          null comment '选项的排序',
    constraint article_options_article_list_articleId_fk
        foreign key (vote_article_id) references article_list (articleId)
);

create table user_msg
(
    sender_id    varchar(30) not null,
    receiver_id  varchar(30) not null,
    message_type varchar(10) not null,
    status       tinyint     not null,
    sequence     varchar(50) not null,
    created_at   time        not null,
    content      text        not null
);

create table user_votes
(
    id              bigint auto_increment comment '投票记录唯一 ID（主键）'
        primary key,
    vote_article_id bigint      not null comment '关联文评投票主表 ID',
    user_id         varchar(40) null comment '投票用户 ID（关联用户表）',
    option_id       bigint      not null comment '关联投票选项表 ID（选了哪个选项）	',
    vote_time       datetime    not null comment '投票时间',
    constraint user_votes_pk
        unique (user_id, option_id, vote_article_id),
    constraint user_votes_article_list_articleId_fk
        foreign key (vote_article_id) references article_list (articleId)
);

create index user_vote_article_vote_id_fk
    on user_votes (option_id);

create table vote_article
(
    id              bigint auto_increment comment '文章ID'
        primary key,
    title           varchar(255)                         not null comment '文章标题',
    content         text                                 null comment '文章内容',
    cover_image     varchar(512)                         null comment '封面图片URL',
    author_id       bigint                               not null comment '作者ID',
    status          tinyint    default 0                 not null comment '状态（0：草稿；1：待审核；2：已发布；3：已结束；4：已下架）',
    vote_start_time datetime                             not null comment '投票开始时间',
    vote_end_time   datetime                             not null comment '投票结束时间',
    total_votes     int        default 0                 not null comment '总投票数',
    total_voters    int        default 0                 not null comment '参与人数',
    is_anonymous    tinyint(1) default 0                 not null comment '是否匿名投票（0：否；1：是）',
    is_multiple     tinyint(1) default 0                 not null comment '是否允许多选（0：单选；1：多选）',
    max_choices     int        default 1                 not null comment '最大可选数量',
    view_count      int        default 0                 not null comment '浏览次数',
    created_at      datetime   default CURRENT_TIMESTAMP not null comment '创建时间',
    updated_at      datetime                             null on update CURRENT_TIMESTAMP comment '更新时间',
    deleted_at      datetime                             null comment '软删除时间'
)
    comment '投票文章表';

create index idx_author_id
    on vote_article (author_id);

create index idx_created_at
    on vote_article (created_at desc);

create index idx_status_time
    on vote_article (status, vote_start_time, vote_end_time);

