create database jqjy;

use jqjy;

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     int      default 0                 not null comment '用户角色 0 - 普通用户 1 - 管理员 2 - 商家 3 - 美甲师',
    index idx_openId (openId)
) comment '用户' collate = utf8mb4_unicode_ci;

-- 商品表
create table if not exists commodities
(
    id                 bigint auto_increment comment 'id' primary key,
    businessId         bigint                                not null comment '商家id',
    commoditiesGroupId bigint                                not null comment '商品分组id',
    commoditiesName    varchar(128)                          not null comment '商品名称',
    commoditiesImage   varchar(1024)                         null comment '商品图片',
    commoditiesPrice   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 commodities_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 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 specifications_commodities
(
    id               bigint auto_increment comment 'id' primary key,
    commoditiesId    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 (commoditiesId),
    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 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 '更新时间',
    commoditiesId   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 if not exists manicurist
(
    id               BIGINT AUTO_INCREMENT COMMENT '美甲师唯一标识(主键,自增)' PRIMARY KEY,
    userId           BIGINT                             not null comment '用户ID(关联用户表)',
    businessId       bigint                             not null comment '商家id',
    manicuristName   VARCHAR(100)                       not null comment '美甲师姓名',
    gender           tinyint                            null comment '性别',
    manicuristAvatar VARCHAR(255) COMMENT '美甲师头像文件的存储路径或链接',
    phone            VARCHAR(20) COMMENT '美甲师联系电话',
    email            VARCHAR(100) COMMENT '美甲师电子邮件',
    employment_date  DATE COMMENT '美甲师入职日期',
    specialties      VARCHAR(255) comment '美甲师的专长(如法式美甲、彩绘等)',
    rating           DECIMAL(3, 2) comment '美甲师的服务评分',
    salary           DECIMAL(10, 2) comment '美甲师的基本薪资',
    manStatus        int      DEFAULT 0 comment '美甲师状态 0 表示正常 1 表示禁用',
    isDelete         TINYINT  DEFAULT 0 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_userId (userId)
) comment ='美甲师表' COLLATE = utf8mb4_unicode_ci;

create table if not exists manicurist_auth
(
    id                   BIGINT AUTO_INCREMENT COMMENT '认证唯一标识(主键,自增)' PRIMARY KEY,
    artistId             BIGINT                             not null comment '美甲师ID(关联美甲师表)',
    certification_number VARCHAR(100) COMMENT '认证编号',
    issuing_authority    VARCHAR(100) COMMENT '发证机构',
    certificate_path     VARCHAR(255) 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 comment '逻辑删除标志,0 表示未删除,1 表示已删除',
    INDEX idx_artistId (artistId)
) comment ='美甲师认证表' COLLATE = utf8mb4_unicode_ci;


-- 预约表
CREATE TABLE IF NOT EXISTS appointments
(
    id              bigint auto_increment comment '预约ID'primary key,
    userId          bigint                             not null comment '用户ID(关联用户表)',
    userName        varchar(100)                       not null comment '用户姓名',
    phone           varchar(64)                        not null comment '手机号',
    businessId      bigint                             not null comment '商家ID(关联商家表)',
    businessName    varchar(512)                       null comment '商家名称',
    manicuristId    bigint                             null comment '美甲师ID, 如果为空到店分配',
    manicuristName  varchar(100)                       null comment '美甲师姓名',
    appointmentTime datetime                           not null comment '预约时间',
    serviceMode     tinyint  default 1                 not null comment '服务方式(0 - 线上, 1 - 到店)',
    notes           varchar(128)                       null comment '备注',
    status          tinyint  default 0                 not null comment '预约状态(0 - 已确认, 1 - 已完成, 2 - 已取消)',
    createTime      datetime default CURRENT_TIMESTAMP not null comment '创建时间',
    updateTime      datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP comment '更新时间',
    isDelete        tinyint  default 0                 not null comment '是否删除',
    orderId         bigint                             null comment '订单ID(关联订单表)',
    INDEX idx_userId (userId),
    INDEX idx_businessId (businessId),
    INDEX idx_manicuristId (manicuristId),
    INDEX idx_orderId (orderId)
) COMMENT '预约表' COLLATE = utf8mb4_unicode_ci;


-- 订单表
CREATE TABLE IF NOT EXISTS orders
(
    id            BIGINT AUTO_INCREMENT COMMENT '订单ID' PRIMARY KEY,
    orderNumber   VARCHAR(50)                        NOT NULL UNIQUE COMMENT '订单号',
    userId        BIGINT                             NOT NULL COMMENT '用户ID(关联用户表)',
    businessId    bigint                             not null comment '商家id',
    userName      VARCHAR(100)                       NOT NULL COMMENT '用户姓名',
    phone         varchar(64)                        not null comment '手机号',
    payMethod     TINYINT                            NOT NULL COMMENT '支付方式:0微信支付',
    appointmentId BIGINT                             NULL COMMENT '预约ID(关联预约表)',
    totalPrice    DECIMAL(10, 2)                     NOT NULL COMMENT '订单总金额',
    paymentStatus TINYINT  DEFAULT 0                 NOT NULL COMMENT '支付状态(0 - 未支付, 1 - 已支付, 2 - 退款中, 3 - 已退款)',
    claimStatus   TINYINT  DEFAULT 0                 NOT NULL COMMENT '抢单状态(0 - 未抢单, 1 - 已抢单)',
    notes         varchar(128)                       null comment '备注',
    createTime    DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
    updateTime    DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    isDelete      TINYINT  DEFAULT 0                 NOT NULL COMMENT '是否删除',
    INDEX idx_userId (userId),
    INDEX idx_appointmentId (appointmentId)
) COMMENT '订单表' COLLATE = utf8mb4_unicode_ci;


-- 订单详情表
CREATE TABLE IF NOT EXISTS order_items
(
    id             BIGINT AUTO_INCREMENT COMMENT '详细订单ID' PRIMARY KEY,
    orderId        BIGINT                             NOT NULL COMMENT '订单ID(关联订单表)',
    commoditiesId  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 ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    isDelete       TINYINT  DEFAULT 0                 NOT NULL COMMENT '是否删除',
    INDEX idx_orderId (orderId),
    INDEX idx_commoditiesId (commoditiesId)
) COMMENT '详细订单表' COLLATE = utf8mb4_unicode_ci;

-- 抢单记录表
CREATE TABLE IF NOT EXISTS order_claim
(
    id           BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '抢单记录ID',
    orderId      BIGINT             NOT NULL COMMENT '订单ID(关联订单表)',
    manicuristId BIGINT             NOT NULL COMMENT '美甲师ID(关联美甲师表)',
    claimStatus  TINYINT  DEFAULT 0 NOT NULL COMMENT '抢单状态(0 - 待抢单, 1 - 已抢单, 2 - 已分配, 3 - 放弃)',
    claimTime    DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '抢单时间',
    isDelete      TINYINT  DEFAULT 0                 NOT NULL COMMENT '是否删除',
    INDEX idx_orderId (orderId),
    INDEX idx_manicuristId (manicuristId)
) COMMENT '抢单记录表' COLLATE = utf8mb4_unicode_ci;