create database school_send;

use school_send;

create table user
(
    username     varchar(256)                       null comment '用户昵称',
    id           bigint auto_increment comment 'id'
        primary key,
    unionId      varchar(256)                           null comment '支付宝开放平台id',
    openId       varchar(256)                           null comment 'openId',
    userAccount  varchar(256)                       null comment '账号',
    avatarUrl    varchar(256)                       null comment '用户头像',
    gender       tinyint                            null comment '性别',
    userPassword varchar(512)                       not null comment '密码',
    phone        varchar(128)                       null comment '电话',
    email        varchar(512)                       null comment '邮箱',
    userStatus   int      default 0                 not null comment '状态 0 -正常',
    createTime   datetime default CURRENT_TIMESTAMP null comment '创建时间',
    updateTime   datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
    isDelete     tinyint  default 0                 not null comment '是否删除',
    userRole     varchar(256)      default 'user'                 not null comment 'user-普通用户，business-商家，admin-管理员',
    index idx_openId (openId)
) comment '用户' collate = utf8mb4_unicode_ci;

-- 商家表
create table if not exists business
(
    id              bigint auto_increment comment 'id' primary key,
    userId          bigint                             not null comment '用户id',
    businessName    varchar(512)                       not null comment '门店名称',
    businessAvatar  varchar(1024)                      not null comment '门店头像',
    businessPhone   varchar(64)                        not null comment '门店手机号',
    address         varchar(512)                       not null comment '店铺详细地址',
    businessProfile varchar(512)                       null comment '门店简介',
    businessImages  varchar(1024)                      null comment '商家相册',
    categoryId      bigint                             null comment '分类id',
    startBusiness   varchar(64)                        not null comment '开始营业时间',
    endBusiness     varchar(64)                        not null comment '结束营业时间',
    state           tinyint  default 0                 not null comment '状态:0审核中,1启用,2禁用',
    storeStatus     tinyint  default 0                 not null comment '店铺状态:0休业,1营业',
    createTime      datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime      datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete        tinyint  default 0                 not null comment '是否删除',
    index idx_businessId (id),
    index idx_userId (userId)
) comment '商家' collate = utf8mb4_unicode_ci;

-- 商家认证表
create table if not exists business_auth
(
    id          bigint auto_increment comment 'id' primary key,
    businessId  bigint                             not null comment '店铺id',
    shopkeeper  varchar(64)                        not null comment '店主名',
    license     varchar(1024)                      not null comment '营业执照',
    frontIdCard varchar(1024)                      not null comment '身份证正面',
    backIdCard  varchar(1024)                      not null comment '身份证反面',
    bankCard    varchar(64)                        not null comment '银行卡号',
    createTime  datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime  datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete    tinyint  default 0                 not null comment '是否删除',
    index idx_businessId (businessId)
) comment '商家认证' collate = utf8mb4_unicode_ci;

-- 系统
-- 分类表
create table if not exists category
(
    id         bigint auto_increment comment 'id' primary key,
    name       varchar(256)                       not null comment '分类名',
    image      varchar(512)                       not null comment '分类图片',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    index idx_categoryId (id)
) comment '分类表' collate = utf8mb4_unicode_ci;

-- 系统信息表
create table if not exists systemInfo
(
    id         bigint auto_increment comment 'id' primary key,
    type       tinyint                            not null comment '类型:0公告,1轮播图',
    content    varchar(256)                       not null comment '功能内容',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间'
) comment '系统信息' collate = utf8mb4_unicode_ci;

-- 操作日志表
create table if not exists systemLog
(
    id         bigint auto_increment comment 'id' primary key,
    userId     bigint                             not null comment '用户id',
    content    varchar(256)                       not null comment '操作内容',
    ip         varchar(64)                        not null comment 'ip地址',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    index idx_businessId (userId)
) comment '系统信息' collate = utf8mb4_unicode_ci;

-- 菜品
-- 菜品分组表
create table if not exists dishes_group
(
    id         bigint auto_increment comment 'id' primary key,
    businessId bigint                             not null comment '商家id',
    groupName  varchar(128)                       not null comment '菜品分组名称',
    isTopping  tinyint  default 0                 not null comment '是否置顶:0不置顶,1置顶',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete   tinyint  default 0                 not null comment '是否删除',
    index idx_businessId (businessId)
) comment '菜品分组表' collate = utf8mb4_unicode_ci;

-- 菜品表
create table if not exists dishes
(
    id              bigint auto_increment comment 'id' primary key,
    businessId      bigint                                not null comment '商家id',
    dishesGroupId   bigint                                not null comment '菜品分组id',
    dishesName      varchar(128)                          not null comment '菜品名称',
    dishesImage     varchar(1024)                         null comment '菜品图片',
    dishesPrice     double                                not null comment '菜品价格',
    packPrice       double                                not null comment '打包费',
    inventoryStatus int                                   not null comment '库存数量',
    status          varchar(20) default '上架'              not null comment '菜品状态：上架，下架',
    createTime      datetime    default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime      datetime    default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete        tinyint     default 0                 not null comment '是否删除',
    index idx_businessId (businessId)
) comment '菜品表' collate = utf8mb4_unicode_ci;

-- 菜品和规格的中间表
create table if not exists specifications_dishes
(
    id               bigint auto_increment comment 'id' primary key,
    dishesId         bigint                             not null comment '菜品id',
    specificationsId bigint                             not null comment '规格id',
    createTime       datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime       datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete         tinyint  default 0                 not null comment '是否删除',
    index idx_dishesId (dishesId),
    index idx_specificationsId (specificationsId)
) comment '菜品和规格的中间表' collate = utf8mb4_unicode_ci;

-- 规格表
create table if not exists specifications
(
    id                 bigint auto_increment comment 'id' primary key,
    businessId         bigint                             not null comment '商家id',
    specificationsName varchar(128)                       not null comment '规格名称',
    createTime         datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime         datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete           tinyint  default 0                 not null comment '是否删除',
    index idx_businessId (businessId)
) comment '规格表' collate = utf8mb4_unicode_ci;

-- 属性表
create table if not exists attribute
(
    id               bigint auto_increment comment 'id' primary key,
    businessId       bigint                             not null comment '商家id',
    specificationsId bigint                             not null comment '规格id',
    attributeName    varchar(128)                       not null comment '属性名称',
    attributeStatus  tinyint  default 0                 not null comment '属性状态:0在售,1停售',
    createTime       datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime       datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    index idx_businessId (businessId)
) comment '属性表' collate = utf8mb4_unicode_ci;

-- 订单
-- 订单表
create table if not exists orders
(
    id           bigint auto_increment comment 'id' primary key,
    pickupCode   varchar(64)                        null comment '取餐码',
    userName     varchar(256)                       not null comment '姓名',
    phone        varchar(64)                        not null comment '手机号',
    userId       bigint                             not null comment '下单用户id',
    businessId   bigint                             not null comment '商家id',
    totalPrice   decimal(10, 2)                     not null comment '订单实际总价',
    pickupMethod tinyint                            not null comment '取餐方式(0堂食 1自提)',
    payMethod    tinyint                            not null comment '支付方式：0微信支付',
    pickupTime   datetime                           null comment '取餐时间',
    notes        varchar(128)                       null comment '备注',
    state        tinyint  default 0                 not null comment '订单状态:0未支付 1已完成 2已退款 3已取消',
    createTime   datetime default CURRENT_TIMESTAMP not null comment '下单时间',
    updateTime   datetime default CURRENT_TIMESTAMP not null comment '支付时间',
    isDelete     tinyint  default 0                 not null comment '是否删除',
    index idx_businessId (businessId),
    index idx_stateId (state)
) comment '订单表' collate = utf8mb4_unicode_ci;

-- 订单详情表
create table if not exists order_details
(
    id             bigint auto_increment comment 'id' primary key,
    orderId        bigint                             not null comment '关联的订单id',
    dishesId       bigint                             not null comment '关联的菜品id',
    quantity       int                                not null comment '购买数量',
    price          decimal(10, 2)                     not null comment '单价',
    subtotal       decimal(10, 2)                     not null comment '小计（单价 * 数量）',
    attributeNames varchar(512)                       null comment '规格属性列表',
    createTime     datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime     datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete       tinyint  default 0                 not null comment '是否删除',
    index idx_orderId (orderId)
) comment '订单详情表' collate = utf8mb4_unicode_ci;
-- 购物车表
create table if not exists cart
(
    id              bigint auto_increment comment 'id' primary key,
    userId          bigint                                 not null comment '用户id',
    businessId      bigint                                 not null comment '商家id',
    createTime      datetime     default CURRENT_TIMESTAMP not null comment '加入购物车时间',
    updateTime      datetime     default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
    dishesId        bigint                                 null comment '菜品id',
    quantity        int          default 1                 not null comment '商品数量',
    price           decimal(10, 2)                         not null comment '当前选择规格的价格',
    subtotal        decimal(10, 2) as ((`price` * `quantity`)) stored comment '小计（单价 * 数量）',
    selectedOptions varchar(512) default ''                not null comment '已选规格属性列表',
    isDelete        tinyint      default 0                 not null comment '是否删除',
    index idx_userId (userId),
    index idx_businessId (businessId)
) comment '购物车表' collate = utf8mb4_unicode_ci;


create table demo(
    id  int auto_increment primary key ,
    name varchar(30) comment '姓名',
    detail varchar(256) comment '详情'
);

-- 系统信息表
create table if not exists systemInfo
(
    id         bigint auto_increment comment 'id' primary key,
    type       tinyint                            not null comment '类型:0公告,1轮播图',
    content    varchar(256)                       not null comment '功能内容',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间'
) comment '系统信息' collate = utf8mb4_unicode_ci;

-- 聊天记录表
create table private_message
(
    id               bigint auto_increment primary key,
    from_userAccount varchar(255) not null comment '发消息者',
    to_userAccount   varchar(255) not null comment '接收消息者',
    orderId          BIGINT       null comment '订单id',
    message          varchar(255) null comment '消息内容'
)comment '聊天记录' collate = utf8mb4_unicode_ci;