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',
    errandId     bigint                             null comment '跑腿id',
    location     varchar(256)                       null comment '配送地址',
    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已取消 4已出餐 5已完成',
    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 comment 'id' 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 '消息内容',
    from_user_deleted tinyint default false comment '发消息者是否删除',
    to_user_deleted   tinyint default false comment '接收消息者是否删除'
)
    comment '聊天记录' collate = utf8mb4_unicode_ci;

# 收藏表
create table collect
(
    id         bigint auto_increment comment 'id' primary key,
    userId     bigint not null comment '用户id',
    businessId bigint not null comment '商家id'
) comment '收藏';

-- 用户评分表
create table user_rating
(
    id             bigint auto_increment comment 'id' primary key,
    ratedEntityId  bigint                             not null comment '评分对象的ID：商家 跑腿',
    userId         bigint                             not null comment '用户id',
    orderId        bigint                             not null comment '订单id',
    rating         tinyint                            not null comment '评分',
    review         varchar(512)                       null comment '评论',
    businessReview varchar(512)                       null comment '商家回复',
    createTime     datetime default CURRENT_TIMESTAMP not null comment '创建时间'
) comment '用户评分' collate = utf8mb4_unicode_ci;

-- 商家等级表
create table business_level
(
    id           bigint auto_increment primary key comment 'id',
    businessId   bigint        not null comment '商家id',
    averageScore DECIMAL(3, 2) not null default 0 comment '综合评分',
    level        tinyint       not null comment '等级',
    createTime   DATETIME               default CURRENT_TIMESTAMP comment '创建时间',
    updateTime   DATETIME               default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '更新时间'
);

-- 系统信息表
create table if not exists businessInfo
(
    id         bigint auto_increment comment 'id' primary key,
    businessId bigint                             not null comment '商家id',
    content    varchar(256)                       not null comment '功能内容',
    createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间'
) comment '系统信息' collate = utf8mb4_unicode_ci;

-- 跑腿表
create table if not exists errand
(
    id                bigint auto_increment comment 'id' primary key,
    userId            bigint                     not null comment '用户id',
    errandName        varchar(256)               not null comment '跑腿姓名',
    errandAvatarUrl   varchar(256)  default 'http://154.8.193.216:9494/system/0/MRiFcIbr-R.jpg' comment '跑腿头像',
    gender            tinyint                    not null comment '性别',
    phone             varchar(128)               not null comment '手机号',
    distributionScope tinyint                    not null comment '配送范围',
    totalOrders       tinyint       default 0    not null comment '接单量',
    maxOrders         tinyint       default 15   not null comment '最大接单量',
    totalPrice        decimal(10, 2) default 0.00 not null comment '总金额',
    state             tinyint       default 0    not null comment '审核状态 0-未审核 1-审核通过',
    createTime        DATETIME      default CURRENT_TIMESTAMP comment '创建时间',
    updateTime        DATETIME      default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete          tinyint       default 0    not null comment '是否删除'
) comment '跑腿' collate = utf8mb4_unicode_ci;

-- 跑腿认证表
create table if not exists errand_auth
(
    id                   bigint auto_increment primary key comment 'id',
    errandId             bigint                             not null comment '跑腿id',
    bankCard             varchar(64)                        not null comment '银行卡号',
    frontIdCard          varchar(1024)                      not null comment '身份证正面',
    backIdCard           varchar(1024)                      not null comment '身份证反面',
    healthCertificate    varchar(1024)                      null comment '健康证',
    certificateStartTime datetime                           null comment '健康证开始时间',
    certificateEndTime   datetime                           null comment '健康证结束时间',
    createTime           datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    isDelete             tinyint  default 0                 not null comment '逻辑删除'
) comment '跑腿认证' collate = utf8mb4_unicode_ci;

-- 跑腿等级表
create table if not exists errand_level
(
    id           bigint auto_increment primary key comment 'id',
    errandId     bigint        not null comment '跑腿Id',
    averageScore DECIMAL(3, 2) not null default 0 comment '综合评分',
    level        tinyint       not null comment '等级',
    createTime   DATETIME               default CURRENT_TIMESTAMP comment '创建时间',
    updateTime   DATETIME               default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP comment '更新时间'
) comment '跑腿等级' collate = utf8mb4_unicode_ci;

create table if not exists errand_income
(
    id         bigint auto_increment primary key comment 'id',
    errandId   bigint                  not null comment '跑腿id',
    orderId    bigint                  not null comment '订单id',
    income     decimal(10, 2) default 0 comment '综合评分',
    state      tinyint       default 0 not null comment '0未结算 1已结算 2已提现',
    createTime datetime      default CURRENT_TIMESTAMP comment '创建时间'
) comment '跑腿收入' collate = utf8mb4_unicode_ci;

create table if not exists errand_order
(
    id          bigint auto_increment primary key comment 'id',
    orderId     bigint                             not null comment '订单ID',
    errandState tinyint  default 0                 null comment '跑腿状态: 0无跑腿 1待抢单 2待取货 3待送达 4已送达 5再次抢单',
    startTime   datetime                           null comment '送餐开始时间',
    endTime     datetime                           null comment '送餐结束时间',
    errandId    bigint                             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 '是否删除'
) comment '跑腿任务表' collate = utf8mb4_unicode_ci;

create table if not exists order_image
(
    orderId  bigint auto_increment primary key comment 'id',
    imageAddress varchar(256) not null comment '外卖地址'
) comment '外卖图片' collate = utf8mb4_unicode_ci;

create table if not exists errand_bill
(
    id bigint auto_increment primary key comment 'id',
    orderId bigint not null comment '订单Id',
    userPhone varchar(128) not null comment '用户手机号',
    income     decimal(3, 2) default 0 comment '综合评分',
    errandState tinyint  default 0 comment '收入状态：0-待结算 1-已结算',
    orderStartTime Date not null comment '订单创建时间'
) comment '跑腿账单表' collate = utf8mb4_unicode_ci;

create table order_refunds (
     id            bigint auto_increment primary key comment '退款申请ID',
     orderId      bigint not null unique comment '订单号，唯一标识',
     userId       bigint not null comment '用户ID',
     reason        varchar(255) default null comment '退款原因',
     amount        decimal(10,2) not null comment '退款金额',
     status        tinyint not null default 0 comment '退款状态：0-待审核, 1-已同意, 2-已拒绝',
     createTime  timestamp default current_timestamp comment '申请时间',
     updateTime  timestamp default current_timestamp on update current_timestamp comment '更新时间',
     index idx_order_id (orderId),
     index idx_user_id (userId)
) comment='退款申请表' collate = utf8mb4_unicode_ci;
