1279 lines
57 KiB
SQL
1279 lines
57 KiB
SQL
-- YumiNail 首次部署主 SQL
|
||
-- 说明:
|
||
-- 1. 首次部署空库时,只需要执行本文件即可完成基础建表、结构补丁、权限种子、VIP 规则、admin01 初始化与默认权限分配。
|
||
-- 2. 2026-03-20_user_asset_vip_coupon_permission_patch.sql、2026-03-25_user_shop_binding_and_shop_status_patch.sql、
|
||
-- 2026-03-20_init_admin_permission_template.sql 仍保留用于已有数据库的增量升级,不再作为空库首选入口。
|
||
-- 3. 如果你的数据库名不是 nailart,请先将本文件中的 `use nailart;` 改成实际库名后再执行。
|
||
use nailart;
|
||
|
||
-- auto-generated definition
|
||
create table t_balance
|
||
(
|
||
id bigint unsigned auto_increment comment '余额记录唯一主键ID(雪花ID)'
|
||
primary key comment '余额表主键索引',
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
amount decimal(10, 2) default 0.00 not null comment '当前余额',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '余额账户创建时间',
|
||
last_update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '最近一次更新时间',
|
||
constraint uk_user_id
|
||
unique (user_id) comment '用户余额唯一索引(一个用户对应一条余额记录)'
|
||
)
|
||
comment '用户余额信息表';
|
||
|
||
|
||
-- auto-generated definition
|
||
create table t_balance_change_detail
|
||
(
|
||
id bigint unsigned auto_increment comment '变动记录唯一主键ID(雪花ID)'
|
||
primary key comment '余额变动明细主键索引',
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
amount decimal(10, 2) not null comment '本次变动金额(正数为增加,负数为减少)',
|
||
type varchar(20) not null comment '变动类型(如充值、消费、退款)',
|
||
current_balance decimal(10, 2) not null comment '变动后的当前余额',
|
||
change_time datetime default CURRENT_TIMESTAMP not null comment '变动时间'
|
||
)
|
||
comment '用户余额变动明细表';
|
||
|
||
create index idx_change_time
|
||
on t_balance_change_detail (change_time)
|
||
comment '按时间查询余额变动的索引';
|
||
|
||
create index idx_user_id
|
||
on t_balance_change_detail (user_id)
|
||
comment '按用户查询余额变动的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_cart
|
||
(
|
||
id bigint unsigned auto_increment comment '购物车主键索引'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
product_id bigint unsigned not null comment '关联商品表主键',
|
||
quantity int unsigned default 1 not null comment '商品数量',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_user_product
|
||
unique (user_id, product_id) comment '用户-商品唯一索引'
|
||
)
|
||
comment '购物车表';
|
||
|
||
create index idx_user_id
|
||
on t_cart (user_id)
|
||
comment '按用户查询购物车的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_comment
|
||
(
|
||
id bigint unsigned auto_increment comment '评论唯一主键ID(雪花ID)'
|
||
primary key comment '评论主键索引',
|
||
user_id bigint unsigned not null comment '评论用户ID,关联用户表主键',
|
||
shop_id bigint unsigned default 0 null comment '关联店铺表主键(0表示非店铺评论)',
|
||
product_id bigint unsigned default 0 null comment '关联商品表主键(0表示非商品评论)',
|
||
comment_content text not null comment '评论内容',
|
||
parent_comment_id bigint unsigned default 0 null comment '父级评论ID(0表示为根评论)',
|
||
like_count int unsigned default 0 null comment '评论点赞数',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '评论创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '评论更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除标识:0-未删除,1-已删除'
|
||
)
|
||
comment '用户评论信息表';
|
||
|
||
create index idx_parent_comment_id
|
||
on t_comment (parent_comment_id)
|
||
comment '按父级评论查询回复的索引';
|
||
|
||
create index idx_product_id
|
||
on t_comment (product_id)
|
||
comment '按商品查询评论的索引';
|
||
|
||
create index idx_shop_id
|
||
on t_comment (shop_id)
|
||
comment '按店铺查询评论的索引';
|
||
|
||
create index idx_user_id
|
||
on t_comment (user_id)
|
||
comment '按用户查询评论的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_coupon
|
||
(
|
||
id bigint unsigned auto_increment comment '优惠券主键索引'
|
||
primary key,
|
||
coupon_code varchar(50) not null comment '优惠券编码',
|
||
coupon_name varchar(100) not null comment '优惠券名称',
|
||
coupon_type tinyint not null comment '优惠券类型:1-折扣券,2-满减券',
|
||
discount_value decimal(10, 2) default 0.00 null comment '折扣值(如9.5表示95折)',
|
||
min_amount decimal(10, 2) default 0.00 null comment '满减券最低消费金额',
|
||
reduce_amount decimal(10, 2) default 0.00 null comment '满减券减免金额',
|
||
total_count int unsigned default 100 not null comment '优惠券总数量',
|
||
used_count int unsigned default 0 not null comment '已使用数量',
|
||
start_time datetime not null comment '开始时间',
|
||
end_time datetime not null comment '结束时间',
|
||
status tinyint unsigned default 1 not null comment '状态:1-可用,2-已过期,3-已用完',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
shop_id bigint null comment '所属店铺ID',
|
||
constraint uk_coupon_code
|
||
unique (coupon_code) comment '优惠券编码唯一索引'
|
||
)
|
||
comment '优惠券信息表';
|
||
|
||
create index idx_status
|
||
on t_coupon (status)
|
||
comment '按状态查询优惠券的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_coupon_tag
|
||
(
|
||
id bigint unsigned auto_increment comment '优惠券标签关联主键索引'
|
||
primary key,
|
||
coupon_id bigint unsigned not null comment '关联优惠券表主键',
|
||
tag_id bigint unsigned not null comment '关联标签表主键',
|
||
is_available tinyint unsigned default 1 not null comment '是否可用:1-可用,0-不可用',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_coupon_tag
|
||
unique (coupon_id, tag_id) comment '优惠券-标签唯一索引'
|
||
)
|
||
comment '优惠券标签关联表';
|
||
|
||
create index idx_coupon_id
|
||
on t_coupon_tag (coupon_id)
|
||
comment '按优惠券查询标签的索引';
|
||
|
||
create index idx_tag_id
|
||
on t_coupon_tag (tag_id)
|
||
comment '按标签查询优惠券的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_effect_diagram
|
||
(
|
||
id bigint unsigned auto_increment comment '效果图唯一主键ID(雪花id)'
|
||
primary key comment '效果图主键索引',
|
||
product_id bigint unsigned not null comment '所属商品ID,关联商品表主键',
|
||
category varchar(50) default '' null comment '效果图类别',
|
||
image_file_ids text null comment '图片文件ID集合,多ID以逗号分隔',
|
||
description text null comment '效果图描述',
|
||
price decimal(10, 2) default 0.00 null comment '效果图对应服务价格',
|
||
duration varchar(50) default '' null comment '效果图对应服务耗时',
|
||
tag varchar(50) default '' null comment '效果图标签',
|
||
like_count int unsigned default 0 null comment '效果图点赞数',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删'
|
||
)
|
||
comment '商品效果图信息表';
|
||
|
||
create index idx_product_id
|
||
on t_effect_diagram (product_id)
|
||
comment '按商品查询效果图的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_login_identity
|
||
(
|
||
id bigint unsigned auto_increment comment '登录标识唯一主键ID(雪花id)'
|
||
primary key comment '登录标识主键索引',
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
login_method varchar(50) not null comment '登录方式(0-手机号、1-账号、2-邮箱)',
|
||
input_value varchar(100) not null comment '前端输入的具体值(如手机号、账号、邮箱)',
|
||
input_time datetime not null comment '输入时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
status tinyint unsigned default 1 null comment '状态:1-有效,0-无效'
|
||
)
|
||
comment '用户登录标识信息表';
|
||
|
||
create index idx_login_method
|
||
on t_login_identity (login_method)
|
||
comment '按登录方式查询的索引';
|
||
|
||
create index idx_user_id
|
||
on t_login_identity (user_id)
|
||
comment '按用户查询登录标识的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_nail_artist_info
|
||
(
|
||
id bigint unsigned auto_increment comment '美甲师信息主键索引'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '关联用户表主键(美甲师用户)',
|
||
identity varchar(50) default '' null comment '身份',
|
||
experience int unsigned default 0 null comment '从业年限',
|
||
skills text null comment '技能标签ID集合,多个ID以逗号分隔',
|
||
rating decimal(3, 1) default 0.0 null comment '评分',
|
||
status tinyint unsigned default 1 null comment '状态:1-营业中,0-休息中',
|
||
portfolio text null comment '作品集商品ID集合,多个ID以逗号分隔',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_user_id
|
||
unique (user_id) comment '用户ID唯一索引(一个美甲师对应一条信息记录)'
|
||
)
|
||
comment '美甲师信息表';
|
||
|
||
create index idx_status
|
||
on t_nail_artist_info (status)
|
||
comment '按状态查询美甲师的索引';
|
||
|
||
create index idx_user_id
|
||
on t_nail_artist_info (user_id)
|
||
comment '按用户查询美甲师信息的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_object_storage_file
|
||
(
|
||
id bigint unsigned auto_increment comment '文件记录唯一主键ID(雪花ID)'
|
||
primary key comment '对象存储文件主键索引',
|
||
file_name varchar(100) not null comment '文件名称',
|
||
file_url text not null comment '文件访问链接',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '文件上传时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '文件更新时间',
|
||
status tinyint unsigned default 1 null comment '文件状态:1-可用,0-不可用,2-回收站',
|
||
delete_time datetime null comment '文件删除时间(NULL表示未删除)',
|
||
recycle_time datetime null comment '文件移入回收站时间(NULL表示未移入)'
|
||
)
|
||
comment '对象存储文件信息表';
|
||
|
||
create index idx_status
|
||
on t_object_storage_file (status)
|
||
comment '按文件状态查询的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_order
|
||
(
|
||
id bigint unsigned auto_increment comment '订单主键索引'
|
||
primary key,
|
||
order_no varchar(50) not null comment '订单编号',
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
total_amount decimal(10, 2) not null comment '订单总金额',
|
||
actual_amount decimal(10, 2) not null comment '实际支付金额',
|
||
coupon_id bigint unsigned default 0 null comment '使用的优惠券ID',
|
||
status tinyint unsigned default 1 not null comment '订单状态:1-待使用,2-已完成,3-已取消',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_order_no
|
||
unique (order_no) comment '订单编号唯一索引'
|
||
)
|
||
comment '订单表';
|
||
|
||
create index idx_status
|
||
on t_order (status)
|
||
comment '按状态查询订单的索引';
|
||
|
||
create index idx_user_id
|
||
on t_order (user_id)
|
||
comment '按用户查询订单的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_order_item
|
||
(
|
||
id bigint unsigned auto_increment comment '订单商品主键索引'
|
||
primary key,
|
||
order_id bigint unsigned not null comment '关联订单表主键',
|
||
product_id bigint unsigned not null comment '关联商品表主键',
|
||
quantity int unsigned not null comment '商品数量',
|
||
price decimal(10, 2) not null comment '商品单价',
|
||
artist_id bigint unsigned null comment '关联美甲师ID',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删'
|
||
)
|
||
comment '订单商品表';
|
||
|
||
create index idx_order_id
|
||
on t_order_item (order_id)
|
||
comment '按订单查询商品的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_permission
|
||
(
|
||
id bigint unsigned auto_increment comment '权限唯一主键ID'
|
||
primary key,
|
||
permission_code varchar(100) not null comment '权限编码(唯一标识)',
|
||
permission_name varchar(100) not null comment '权限名称',
|
||
permission_group varchar(50) not null comment '权限分组(商品管理、用户管理等)',
|
||
description varchar(255) null comment '权限描述',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_permission_code
|
||
unique (permission_code) comment '权限编码唯一索引'
|
||
)
|
||
comment '系统权限表';
|
||
|
||
-- auto-generated definition
|
||
create table t_product
|
||
(
|
||
id bigint unsigned auto_increment comment '商品唯一主键ID(雪花id)'
|
||
primary key comment '商品主键索引',
|
||
shop_id bigint unsigned not null comment '所属店铺ID,关联店铺表主键',
|
||
title varchar(100) not null comment '商品标题',
|
||
description text null comment '商品描述',
|
||
image_file_ids text null comment '图片文件ID集合,多个ID以逗号分隔',
|
||
price decimal(10, 2) not null comment '商品价格,保留2位小数',
|
||
tags varchar(512) default '' null comment '标签集合,多个标签以逗号分隔,最多5个',
|
||
like_count int unsigned default 0 null comment '商品点赞数',
|
||
collect_count int unsigned default 0 null comment '商品收藏数',
|
||
approximate_duration varchar(50) default '' null comment '商品对应的服务大约耗时(如“30分钟”)',
|
||
bookable_artists varchar(200) default '' null comment '可预约的美甲师ID集合,多个ID以逗号分隔',
|
||
available_coupons text null comment '可用的优惠券ID集合,多个ID以逗号分隔',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '商品创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '商品信息最后更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除标识:0-未删除,1-已删除'
|
||
)
|
||
comment '商品信息表';
|
||
|
||
create index idx_price
|
||
on t_product (price)
|
||
comment '按价格筛选商品的索引';
|
||
|
||
create index idx_shop_id
|
||
on t_product (shop_id)
|
||
comment '按所属店铺查询商品的索引';
|
||
|
||
create index idx_tags
|
||
on t_product (tags)
|
||
comment '按标签筛选商品的索引';
|
||
|
||
create index idx_title
|
||
on t_product (title)
|
||
comment '商品标题搜索的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_product_like
|
||
(
|
||
id bigint unsigned auto_increment comment '商品点赞主键索引'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
product_id bigint unsigned not null comment '关联商品表主键',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删除,1-已删除',
|
||
constraint uk_user_product_like
|
||
unique (user_id, product_id) comment '用户-商品唯一索引'
|
||
)
|
||
comment '商品点赞记录表';
|
||
|
||
create index idx_product_id_like
|
||
on t_product_like (product_id)
|
||
comment '按商品查询点赞的索引';
|
||
|
||
create index idx_user_id_like
|
||
on t_product_like (user_id)
|
||
comment '按用户查询点赞的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_shop
|
||
(
|
||
id bigint unsigned auto_increment comment '店铺唯一主键ID'
|
||
primary key comment '店铺主键索引',
|
||
shop_name varchar(100) not null comment '店铺名称',
|
||
description text null comment '店铺描述',
|
||
address varchar(200) not null comment '店铺地址',
|
||
business_hours varchar(100) default '' null comment '店铺营业时间',
|
||
cover_image_file_id varchar(50) default '' null comment '店铺封面图文件ID',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
image_file_ids text null comment '图片文件ID集合,多个ID以逗号分隔'
|
||
)
|
||
comment '店铺信息表';
|
||
|
||
create index idx_shop_name
|
||
on t_shop (shop_name)
|
||
comment '按店铺名称查询的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_tag
|
||
(
|
||
id bigint unsigned auto_increment comment '标签唯一主键ID'
|
||
primary key comment '标签主键索引',
|
||
tag_name varchar(50) not null comment '标签名称',
|
||
text_color varchar(20) default '#333333' null comment '标签文字颜色(16进制)',
|
||
bg_color varchar(20) default '#FFFFFF' null comment '标签背景颜色(16进制)',
|
||
border_color varchar(20) default '#E5E5E5' null comment '标签边框颜色(16进制)',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
shop_id bigint null comment '店铺ID',
|
||
constraint uk_tag_shop_name
|
||
unique (shop_id, tag_name) comment '店铺内标签名称唯一索引'
|
||
)
|
||
comment '系统标签信息表';
|
||
|
||
-- auto-generated definition
|
||
create table t_user
|
||
(
|
||
id bigint unsigned auto_increment comment '用户唯一主键ID(雪花ID)'
|
||
primary key comment '用户主键索引',
|
||
shop_id bigint unsigned default 0 null comment '所属店铺ID,关联店铺表主键(0表示无所属店铺)',
|
||
nickname varchar(50) not null comment '用户昵称',
|
||
account varchar(50) not null comment '用户账号(登录用)',
|
||
phone varchar(50) null comment '手机号',
|
||
email varchar(100) null comment '邮箱',
|
||
phone_verified tinyint unsigned default 0 null comment '手机号是否已验证:0-未验证,1-已验证',
|
||
email_verified tinyint unsigned default 0 null comment '邮箱是否已验证:0-未验证,1-已验证',
|
||
password varchar(100) not null comment '用户密码(加密存储)',
|
||
signature varchar(200) default '' null comment '用户签名',
|
||
level varchar(20) default '普通' null comment '用户等级',
|
||
type tinyint not null comment '用户类型,(0-用户,2-店长,3-美甲师,4-管理员)',
|
||
vip_id bigint unsigned default 0 null comment '关联VIP信息表主键(0表示非VIP)',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
status tinyint unsigned default 1 null comment '用户状态:1-正常,0-禁用',
|
||
avatar varchar(512) null comment '头像链接',
|
||
constraint uk_account
|
||
unique (account) comment '用户账号唯一索引'
|
||
)
|
||
comment '系统用户信息表';
|
||
|
||
create index idx_email
|
||
on t_user (email)
|
||
comment '按邮箱查询的索引';
|
||
|
||
create index idx_phone
|
||
on t_user (phone)
|
||
comment '按手机号查询的索引';
|
||
|
||
create index idx_shop_id
|
||
on t_user (shop_id)
|
||
comment '按店铺查询用户的索引';
|
||
|
||
create index idx_type
|
||
on t_user (type)
|
||
comment '按用户类型查询的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_user_coupon
|
||
(
|
||
id bigint unsigned auto_increment comment '用户优惠券关联主键索引'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
coupon_id bigint unsigned not null comment '关联优惠券表主键',
|
||
status tinyint unsigned default 1 not null comment '状态:1-未使用,2-已使用,3-已过期',
|
||
obtain_time datetime default CURRENT_TIMESTAMP not null comment '获取时间',
|
||
use_time datetime null comment '使用时间',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_user_coupon
|
||
unique (user_id, coupon_id) comment '用户-优惠券唯一索引'
|
||
)
|
||
comment '用户优惠券关联表';
|
||
|
||
create index idx_status
|
||
on t_user_coupon (status)
|
||
comment '按状态查询用户优惠券的索引';
|
||
|
||
create index idx_user_id
|
||
on t_user_coupon (user_id)
|
||
comment '按用户查询优惠券的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_user_permission
|
||
(
|
||
id bigint unsigned auto_increment comment '关联ID'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '用户ID',
|
||
permission_id bigint unsigned not null comment '权限ID',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
create_by bigint unsigned null comment '创建人ID',
|
||
update_by bigint unsigned null comment '更新人ID',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删,1-已删',
|
||
constraint uk_user_permission
|
||
unique (user_id, permission_id) comment '用户-权限唯一索引'
|
||
)
|
||
comment '用户权限关联表';
|
||
|
||
create index idx_permission_id
|
||
on t_user_permission (permission_id)
|
||
comment '权限ID索引';
|
||
|
||
create index idx_user_id
|
||
on t_user_permission (user_id)
|
||
comment '用户ID索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_vip_benefit
|
||
(
|
||
id bigint unsigned auto_increment comment 'VIP权益唯一主键ID(雪花ID)'
|
||
primary key comment 'VIP权益主键索引',
|
||
benefit_info text not null comment '权益详情(如折扣、专属服务)',
|
||
required_vip_level varchar(20) not null comment '所需VIP等级',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间'
|
||
)
|
||
comment 'VIP权益信息表';
|
||
|
||
create index idx_required_vip_level
|
||
on t_vip_benefit (required_vip_level)
|
||
comment '按VIP等级查询权益的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_vip_info
|
||
(
|
||
id bigint unsigned auto_increment comment 'VIP信息唯一主键ID(雪花ID)'
|
||
primary key comment 'VIP信息主键索引',
|
||
vip_level varchar(20) not null comment 'VIP等级(如青铜、白银)',
|
||
start_time datetime not null comment 'VIP开通时间',
|
||
expire_time datetime not null comment 'VIP到期时间',
|
||
activation_method varchar(50) not null comment 'VIP开通方式(如购买、赠送)',
|
||
benefit_id bigint unsigned not null comment '关联VIP权益信息表主键',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间'
|
||
)
|
||
comment '用户VIP信息表';
|
||
|
||
create index idx_vip_level
|
||
on t_vip_info (vip_level)
|
||
comment '按VIP等级查询的索引';
|
||
|
||
-- auto-generated definition
|
||
create table t_wishlist
|
||
(
|
||
id bigint unsigned auto_increment comment '商品收藏主键索引'
|
||
primary key,
|
||
user_id bigint unsigned not null comment '关联用户表主键',
|
||
product_id bigint unsigned not null comment '关联商品表主键',
|
||
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
|
||
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
|
||
is_delete tinyint unsigned default 0 null comment '逻辑删除:0-未删除,1-已删除',
|
||
constraint uk_user_product
|
||
unique (user_id, product_id) comment '用户-商品唯一索引'
|
||
)
|
||
comment '商品收藏表';
|
||
|
||
create index idx_product_id
|
||
on t_wishlist (product_id)
|
||
comment '按商品查询收藏的索引';
|
||
|
||
create index idx_user_id
|
||
on t_wishlist (user_id)
|
||
comment '按用户查询收藏的索引';
|
||
|
||
-- ========================================
|
||
-- 首次部署整合区:用户资产 / VIP / 优惠券补丁
|
||
-- 来源:2026-03-20_user_asset_vip_coupon_permission_patch.sql
|
||
-- ========================================
|
||
|
||
SET NAMES utf8mb4;
|
||
|
||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||
|
||
DELIMITER $$
|
||
|
||
CREATE PROCEDURE sp_add_column_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_column_name VARCHAR(64),
|
||
IN p_definition TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.COLUMNS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND COLUMN_NAME = p_column_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ',
|
||
p_definition
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
CREATE PROCEDURE sp_add_index_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_index_name VARCHAR(64),
|
||
IN p_columns TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.STATISTICS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND INDEX_NAME = p_index_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD INDEX `', p_index_name, '` ',
|
||
p_columns
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
CREATE PROCEDURE sp_add_unique_index_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_index_name VARCHAR(64),
|
||
IN p_columns TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.STATISTICS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND INDEX_NAME = p_index_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD UNIQUE INDEX `', p_index_name, '` ',
|
||
p_columns
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
DELIMITER ;
|
||
|
||
CALL sp_add_column_if_not_exists('t_balance', 'total_recharge_amount', 'decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT ''累计充值金额''');
|
||
CALL sp_add_column_if_not_exists('t_balance', 'last_recharge_time', 'datetime NULL COMMENT ''最近一次充值时间''');
|
||
|
||
UPDATE t_balance
|
||
SET total_recharge_amount = COALESCE(total_recharge_amount, 0.00)
|
||
WHERE total_recharge_amount IS NULL;
|
||
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'before_balance', 'decimal(10,2) NULL COMMENT ''变动前余额''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'after_balance', 'decimal(10,2) NULL COMMENT ''变动后余额''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'change_scene', 'varchar(32) NULL COMMENT ''余额变动场景''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'related_order_id', 'bigint unsigned NULL COMMENT ''关联系统订单ID''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'related_order_no', 'varchar(64) NULL COMMENT ''关联系统订单号''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'external_order_no', 'varchar(64) NULL COMMENT ''手工录入的外部订单号''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'operator_id', 'bigint unsigned NULL COMMENT ''操作人ID''');
|
||
CALL sp_add_column_if_not_exists('t_balance_change_detail', 'remark', 'varchar(255) NULL COMMENT ''备注''');
|
||
|
||
UPDATE t_balance_change_detail
|
||
SET before_balance = COALESCE(before_balance, current_balance - amount),
|
||
after_balance = COALESCE(after_balance, current_balance),
|
||
change_scene = COALESCE(NULLIF(change_scene, ''), type)
|
||
WHERE before_balance IS NULL
|
||
OR after_balance IS NULL
|
||
OR change_scene IS NULL
|
||
OR change_scene = '';
|
||
|
||
CALL sp_add_index_if_not_exists('t_balance_change_detail', 'idx_balance_detail_related_order_id', '(`related_order_id`)');
|
||
CALL sp_add_index_if_not_exists('t_balance_change_detail', 'idx_balance_detail_operator_id', '(`operator_id`)');
|
||
|
||
CALL sp_add_column_if_not_exists('t_order', 'shop_id', 'bigint unsigned NULL COMMENT ''所属店铺ID''');
|
||
CALL sp_add_column_if_not_exists('t_order', 'user_coupon_id', 'bigint unsigned NULL COMMENT ''用户券ID''');
|
||
CALL sp_add_column_if_not_exists('t_order', 'coupon_source_type', 'varchar(32) NULL COMMENT ''优惠来源类型''');
|
||
|
||
UPDATE t_order
|
||
SET coupon_id = NULL
|
||
WHERE coupon_id = 0;
|
||
|
||
UPDATE t_order o
|
||
INNER JOIN (
|
||
SELECT oi.order_id, MIN(p.shop_id) AS resolved_shop_id
|
||
FROM t_order_item oi
|
||
INNER JOIN t_product p ON p.id = oi.product_id
|
||
GROUP BY oi.order_id
|
||
) detail_shop ON detail_shop.order_id = o.id
|
||
SET o.shop_id = detail_shop.resolved_shop_id
|
||
WHERE o.shop_id IS NULL;
|
||
|
||
UPDATE t_order
|
||
SET coupon_source_type = 'USER_COUPON'
|
||
WHERE coupon_source_type IS NULL
|
||
AND coupon_id IS NOT NULL;
|
||
|
||
CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_shop_id', '(`shop_id`)');
|
||
CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_coupon_source_type', '(`coupon_source_type`)');
|
||
|
||
CALL sp_add_column_if_not_exists('t_order_item', 'appointment_time', 'datetime NULL COMMENT ''预约开始时间''');
|
||
CALL sp_add_index_if_not_exists('t_order_item', 'idx_t_order_item_artist_appointment_time', '(`artist_id`, `appointment_time`)');
|
||
|
||
SET @drop_old_tag_unique_sql = (
|
||
SELECT IF(
|
||
EXISTS(
|
||
SELECT 1
|
||
FROM information_schema.STATISTICS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = 't_tag'
|
||
AND INDEX_NAME = 'uk_tag_name'
|
||
),
|
||
'ALTER TABLE `t_tag` DROP INDEX `uk_tag_name`',
|
||
'SELECT 1'
|
||
)
|
||
);
|
||
PREPARE stmt FROM @drop_old_tag_unique_sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
|
||
CALL sp_add_unique_index_if_not_exists('t_tag', 'uk_tag_shop_name', '(`shop_id`, `tag_name`)');
|
||
|
||
CALL sp_add_column_if_not_exists('t_coupon', 'coupon_scene', 'varchar(32) NOT NULL DEFAULT ''NORMAL'' COMMENT ''优惠券场景''');
|
||
CALL sp_add_column_if_not_exists('t_coupon', 'required_vip_level', 'varchar(20) NULL COMMENT ''要求的VIP等级''');
|
||
CALL sp_add_column_if_not_exists('t_coupon', 'auto_match', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否自动匹配''');
|
||
|
||
UPDATE t_coupon
|
||
SET coupon_scene = COALESCE(NULLIF(coupon_scene, ''), 'NORMAL'),
|
||
auto_match = COALESCE(auto_match, 0)
|
||
WHERE coupon_scene IS NULL
|
||
OR coupon_scene = ''
|
||
OR auto_match IS NULL;
|
||
|
||
CALL sp_add_index_if_not_exists('t_coupon', 'idx_t_coupon_scene_auto_match', '(`coupon_scene`, `auto_match`)');
|
||
CALL sp_add_index_if_not_exists('t_coupon', 'idx_t_coupon_required_vip_level', '(`required_vip_level`)');
|
||
|
||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'level_rank', 'int NOT NULL DEFAULT 0 COMMENT ''等级序号''');
|
||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'recharge_threshold', 'decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT ''累计充值门槛''');
|
||
CALL sp_add_column_if_not_exists('t_vip_benefit', 'status', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''状态''');
|
||
|
||
UPDATE t_vip_benefit
|
||
SET level_rank = CASE required_vip_level
|
||
WHEN '普通' THEN 0
|
||
WHEN '青铜' THEN 1
|
||
WHEN '白银' THEN 2
|
||
WHEN '黄金' THEN 3
|
||
ELSE level_rank
|
||
END,
|
||
recharge_threshold = CASE required_vip_level
|
||
WHEN '普通' THEN 0.00
|
||
WHEN '青铜' THEN 500.00
|
||
WHEN '白银' THEN 1000.00
|
||
WHEN '黄金' THEN 2000.00
|
||
ELSE recharge_threshold
|
||
END,
|
||
status = COALESCE(status, 1)
|
||
WHERE required_vip_level IN ('普通', '青铜', '白银', '黄金');
|
||
|
||
CALL sp_add_index_if_not_exists('t_vip_benefit', 'idx_t_vip_benefit_status_rank', '(`status`, `level_rank`)');
|
||
|
||
CALL sp_add_column_if_not_exists('t_vip_info', 'user_id', 'bigint unsigned NULL COMMENT ''所属用户ID''');
|
||
CALL sp_add_column_if_not_exists('t_vip_info', 'manual_override', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''是否手工覆盖VIP等级''');
|
||
|
||
ALTER TABLE t_vip_info
|
||
MODIFY COLUMN expire_time datetime NULL COMMENT 'VIP到期时间',
|
||
MODIFY COLUMN benefit_id bigint unsigned NULL COMMENT '关联VIP权益信息表主键';
|
||
|
||
UPDATE t_vip_info vi
|
||
INNER JOIN t_user u ON u.vip_id = vi.id
|
||
SET vi.user_id = u.id
|
||
WHERE (vi.user_id IS NULL OR vi.user_id = 0)
|
||
AND u.vip_id IS NOT NULL
|
||
AND u.vip_id <> 0;
|
||
|
||
UPDATE t_vip_info
|
||
SET start_time = COALESCE(start_time, create_time, NOW()),
|
||
activation_method = COALESCE(NULLIF(activation_method, ''), 'INIT'),
|
||
manual_override = COALESCE(manual_override, 0);
|
||
|
||
CALL sp_add_unique_index_if_not_exists('t_vip_info', 'uk_t_vip_info_user_id', '(`user_id`)');
|
||
CALL sp_add_index_if_not_exists('t_vip_info', 'idx_t_vip_info_manual_override', '(`manual_override`)');
|
||
|
||
UPDATE t_user u
|
||
LEFT JOIN t_vip_info vi ON vi.id = u.vip_id
|
||
SET u.level = CASE
|
||
WHEN u.type = 0 AND u.vip_id IS NOT NULL AND u.vip_id <> 0 AND vi.vip_level IS NOT NULL THEN vi.vip_level
|
||
WHEN u.type = 0 THEN '普通'
|
||
ELSE u.level
|
||
END,
|
||
u.vip_id = CASE
|
||
WHEN u.type = 0 AND u.vip_id IS NULL THEN 0
|
||
ELSE u.vip_id
|
||
END,
|
||
u.update_time = NOW()
|
||
WHERE u.is_delete = 0
|
||
AND u.type = 0
|
||
AND (u.level IS NULL OR u.level = '' OR u.vip_id IS NULL);
|
||
|
||
UPDATE t_vip_benefit
|
||
SET benefit_info = '普通用户,无额外VIP折扣',
|
||
level_rank = 0,
|
||
recharge_threshold = 0.00,
|
||
status = 1,
|
||
update_time = NOW()
|
||
WHERE required_vip_level = '普通';
|
||
|
||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||
SELECT '普通用户,无额外VIP折扣', '普通', 0, 0.00, 1, NOW(), NOW()
|
||
WHERE NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_vip_benefit
|
||
WHERE required_vip_level = '普通'
|
||
);
|
||
|
||
UPDATE t_vip_benefit
|
||
SET benefit_info = '青铜VIP,系统自动匹配9.5折券',
|
||
level_rank = 1,
|
||
recharge_threshold = 500.00,
|
||
status = 1,
|
||
update_time = NOW()
|
||
WHERE required_vip_level = '青铜';
|
||
|
||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||
SELECT '青铜VIP,系统自动匹配9.5折券', '青铜', 1, 500.00, 1, NOW(), NOW()
|
||
WHERE NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_vip_benefit
|
||
WHERE required_vip_level = '青铜'
|
||
);
|
||
|
||
UPDATE t_vip_benefit
|
||
SET benefit_info = '白银VIP,系统自动匹配9.2折券',
|
||
level_rank = 2,
|
||
recharge_threshold = 1000.00,
|
||
status = 1,
|
||
update_time = NOW()
|
||
WHERE required_vip_level = '白银';
|
||
|
||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||
SELECT '白银VIP,系统自动匹配9.2折券', '白银', 2, 1000.00, 1, NOW(), NOW()
|
||
WHERE NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_vip_benefit
|
||
WHERE required_vip_level = '白银'
|
||
);
|
||
|
||
UPDATE t_vip_benefit
|
||
SET benefit_info = '黄金VIP,系统自动匹配9.0折券',
|
||
level_rank = 3,
|
||
recharge_threshold = 2000.00,
|
||
status = 1,
|
||
update_time = NOW()
|
||
WHERE required_vip_level = '黄金';
|
||
|
||
INSERT INTO t_vip_benefit (benefit_info, required_vip_level, level_rank, recharge_threshold, status, create_time, update_time)
|
||
SELECT '黄金VIP,系统自动匹配9.0折券', '黄金', 3, 2000.00, 1, NOW(), NOW()
|
||
WHERE NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_vip_benefit
|
||
WHERE required_vip_level = '黄金'
|
||
);
|
||
|
||
INSERT INTO t_coupon (
|
||
coupon_code,
|
||
coupon_name,
|
||
coupon_type,
|
||
discount_value,
|
||
min_amount,
|
||
reduce_amount,
|
||
total_count,
|
||
used_count,
|
||
start_time,
|
||
end_time,
|
||
status,
|
||
create_time,
|
||
update_time,
|
||
is_delete,
|
||
shop_id,
|
||
coupon_scene,
|
||
required_vip_level,
|
||
auto_match
|
||
)
|
||
VALUES
|
||
('VIP_AUTO_BRONZE_95', '青铜VIP自动95折券', 1, 9.50, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '青铜', 1),
|
||
('VIP_AUTO_SILVER_92', '白银VIP自动92折券', 1, 9.20, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '白银', 1),
|
||
('VIP_AUTO_GOLD_90', '黄金VIP自动90折券', 1, 9.00, 0.00, 0.00, 999999, 0, NOW(), '2099-12-31 23:59:59', 1, NOW(), NOW(), 0, NULL, 'VIP_AUTO', '黄金', 1)
|
||
ON DUPLICATE KEY UPDATE
|
||
coupon_name = VALUES(coupon_name),
|
||
coupon_type = VALUES(coupon_type),
|
||
discount_value = VALUES(discount_value),
|
||
min_amount = VALUES(min_amount),
|
||
reduce_amount = VALUES(reduce_amount),
|
||
total_count = VALUES(total_count),
|
||
status = VALUES(status),
|
||
is_delete = VALUES(is_delete),
|
||
shop_id = VALUES(shop_id),
|
||
coupon_scene = VALUES(coupon_scene),
|
||
required_vip_level = VALUES(required_vip_level),
|
||
auto_match = VALUES(auto_match),
|
||
update_time = NOW();
|
||
|
||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||
|
||
-- ========================================
|
||
-- 首次部署整合区:多店铺登录 / 店铺状态 / 用户店铺绑定补丁
|
||
-- 来源:2026-03-25_user_shop_binding_and_shop_status_patch.sql
|
||
-- ========================================
|
||
|
||
SET NAMES utf8mb4;
|
||
|
||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||
|
||
DELIMITER $$
|
||
|
||
CREATE PROCEDURE sp_add_column_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_column_name VARCHAR(64),
|
||
IN p_definition TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.COLUMNS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND COLUMN_NAME = p_column_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ',
|
||
p_definition
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
CREATE PROCEDURE sp_add_index_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_index_name VARCHAR(64),
|
||
IN p_columns TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.STATISTICS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND INDEX_NAME = p_index_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD INDEX `', p_index_name, '` ',
|
||
p_columns
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
CREATE PROCEDURE sp_add_unique_index_if_not_exists(
|
||
IN p_table_name VARCHAR(64),
|
||
IN p_index_name VARCHAR(64),
|
||
IN p_columns TEXT
|
||
)
|
||
BEGIN
|
||
IF NOT EXISTS (
|
||
SELECT 1
|
||
FROM information_schema.STATISTICS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = p_table_name
|
||
AND INDEX_NAME = p_index_name
|
||
) THEN
|
||
SET @sql = CONCAT(
|
||
'ALTER TABLE `', p_table_name, '` ADD UNIQUE INDEX `', p_index_name, '` ',
|
||
p_columns
|
||
);
|
||
PREPARE stmt FROM @sql;
|
||
EXECUTE stmt;
|
||
DEALLOCATE PREPARE stmt;
|
||
END IF;
|
||
END$$
|
||
|
||
DELIMITER ;
|
||
|
||
CALL sp_add_column_if_not_exists('t_shop', 'status', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''店铺状态:1-启用,0-停用''');
|
||
|
||
UPDATE t_shop
|
||
SET status = 1
|
||
WHERE status IS NULL;
|
||
|
||
CALL sp_add_index_if_not_exists('t_shop', 'idx_t_shop_status', '(`status`)');
|
||
|
||
CREATE TABLE IF NOT EXISTS `t_user_shop_binding` (
|
||
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '用户店铺绑定主键ID',
|
||
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
|
||
`shop_id` bigint unsigned NOT NULL COMMENT '店铺ID',
|
||
`status` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '绑定状态:1-正常,0-禁用',
|
||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||
`is_delete` tinyint unsigned DEFAULT 0 COMMENT '逻辑删除:0-未删,1-已删',
|
||
PRIMARY KEY (`id`)
|
||
) COMMENT='用户与店铺绑定关系表';
|
||
|
||
CALL sp_add_column_if_not_exists('t_user_shop_binding', 'status', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''绑定状态:1-正常,0-禁用''');
|
||
CALL sp_add_column_if_not_exists('t_user_shop_binding', 'create_time', 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间''');
|
||
CALL sp_add_column_if_not_exists('t_user_shop_binding', 'update_time', 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间''');
|
||
CALL sp_add_column_if_not_exists('t_user_shop_binding', 'is_delete', 'tinyint unsigned DEFAULT 0 COMMENT ''逻辑删除:0-未删,1-已删''');
|
||
|
||
CALL sp_add_index_if_not_exists('t_user_shop_binding', 'idx_t_user_shop_binding_user_id', '(`user_id`)');
|
||
CALL sp_add_index_if_not_exists('t_user_shop_binding', 'idx_t_user_shop_binding_shop_id', '(`shop_id`)');
|
||
CALL sp_add_index_if_not_exists('t_user_shop_binding', 'idx_t_user_shop_binding_status', '(`status`)');
|
||
CALL sp_add_unique_index_if_not_exists('t_user_shop_binding', 'uk_t_user_shop_binding_user_shop', '(`user_id`, `shop_id`)');
|
||
|
||
INSERT INTO t_user_shop_binding (
|
||
user_id,
|
||
shop_id,
|
||
status,
|
||
create_time,
|
||
update_time,
|
||
is_delete
|
||
)
|
||
SELECT
|
||
u.id,
|
||
u.shop_id,
|
||
CASE
|
||
WHEN COALESCE(u.status, 1) = 1 THEN 1
|
||
ELSE 0
|
||
END AS binding_status,
|
||
COALESCE(u.create_time, NOW()),
|
||
COALESCE(u.update_time, NOW()),
|
||
0
|
||
FROM t_user u
|
||
INNER JOIN t_shop s ON s.id = u.shop_id AND COALESCE(s.is_delete, 0) = 0
|
||
LEFT JOIN t_user_shop_binding b
|
||
ON b.user_id = u.id
|
||
AND b.shop_id = u.shop_id
|
||
AND COALESCE(b.is_delete, 0) = 0
|
||
WHERE COALESCE(u.is_delete, 0) = 0
|
||
AND u.shop_id IS NOT NULL
|
||
AND u.shop_id > 0
|
||
AND b.id IS NULL;
|
||
|
||
UPDATE t_user_shop_binding b
|
||
INNER JOIN t_user u ON u.id = b.user_id
|
||
SET b.status = CASE
|
||
WHEN b.status IS NULL THEN CASE WHEN COALESCE(u.status, 1) = 1 THEN 1 ELSE 0 END
|
||
ELSE b.status
|
||
END,
|
||
b.is_delete = COALESCE(b.is_delete, 0),
|
||
b.update_time = COALESCE(b.update_time, NOW())
|
||
WHERE b.status IS NULL
|
||
OR b.is_delete IS NULL
|
||
OR b.update_time IS NULL;
|
||
|
||
DROP PROCEDURE IF EXISTS sp_add_column_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_index_if_not_exists;
|
||
DROP PROCEDURE IF EXISTS sp_add_unique_index_if_not_exists;
|
||
|
||
-- ========================================
|
||
-- 首次部署整合区:权限种子 / admin01 初始化 / 默认权限分配
|
||
-- 来源:2026-03-20_init_admin_permission_template.sql
|
||
-- ========================================
|
||
|
||
SET NAMES utf8mb4;
|
||
|
||
INSERT INTO t_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete)
|
||
VALUES
|
||
('home:view', '查看首页', '首页', '查看系统首页入口与概览', NOW(), NOW(), 0),
|
||
('dashboard:view', '查看仪表盘', '仪表盘', '查看后台仪表盘统计', NOW(), NOW(), 0),
|
||
('user:view', '查看用户', '用户管理', '查看用户列表与详情', NOW(), NOW(), 0),
|
||
('user:create', '新增用户', '用户管理', '创建用户账号', NOW(), NOW(), 0),
|
||
('user:edit', '编辑用户', '用户管理', '修改用户基础信息', NOW(), NOW(), 0),
|
||
('user:disable', '启停用户', '用户管理', '启用或禁用用户账号', NOW(), NOW(), 0),
|
||
('user:unbind', '解绑用户', '用户管理', '解绑用户与当前店铺的关系', NOW(), NOW(), 0),
|
||
('user:reset_password', '重置密码', '用户管理', '重置用户密码', NOW(), NOW(), 0),
|
||
('user:delete', '删除用户', '用户管理', '删除用户账号', NOW(), NOW(), 0),
|
||
('user:balance:view', '查看余额资产', '用户资产', '查看用户余额、累计充值与流水', NOW(), NOW(), 0),
|
||
('user:balance:manage', '调整余额', '用户资产', '充值、手工加减余额与关联订单', NOW(), NOW(), 0),
|
||
('user:vip:manage', '管理VIP', '用户资产', '手工设置VIP等级或恢复自动判级', NOW(), NOW(), 0),
|
||
('product:view', '查看商品', '商品管理', '查看商品列表与详情', NOW(), NOW(), 0),
|
||
('product:create', '新增商品', '商品管理', '创建商品', NOW(), NOW(), 0),
|
||
('product:edit', '编辑商品', '商品管理', '编辑商品信息', NOW(), NOW(), 0),
|
||
('product:delete', '删除商品', '商品管理', '删除商品', NOW(), NOW(), 0),
|
||
('product:category:manage', '管理商品分类', '商品管理', '维护商品分类与归类信息', NOW(), NOW(), 0),
|
||
('shop:view', '查看店铺', '店铺管理', '查看店铺列表与详情', NOW(), NOW(), 0),
|
||
('shop:create', '新增店铺', '店铺管理', '创建店铺', NOW(), NOW(), 0),
|
||
('shop:edit', '编辑店铺', '店铺管理', '编辑店铺信息', NOW(), NOW(), 0),
|
||
('shop:delete', '删除店铺', '店铺管理', '删除店铺', NOW(), NOW(), 0),
|
||
('shop:staff:manage', '管理员工', '店铺管理', '管理店铺员工与归属', NOW(), NOW(), 0),
|
||
('permission:view', '查看权限', '权限管理', '查看权限清单', NOW(), NOW(), 0),
|
||
('permission:create', '新增权限', '权限管理', '新增系统权限定义', NOW(), NOW(), 0),
|
||
('permission:edit', '编辑权限', '权限管理', '编辑系统权限定义', NOW(), NOW(), 0),
|
||
('permission:delete', '删除权限', '权限管理', '删除系统权限定义', NOW(), NOW(), 0),
|
||
('permission:assign', '分配权限', '权限管理', '给用户分配后台权限', NOW(), NOW(), 0),
|
||
('tag:view', '查看标签', '标签管理', '查看标签列表与详情', NOW(), NOW(), 0),
|
||
('tag:create', '新增标签', '标签管理', '创建标签', NOW(), NOW(), 0),
|
||
('tag:edit', '编辑标签', '标签管理', '编辑标签', NOW(), NOW(), 0),
|
||
('tag:delete', '删除标签', '标签管理', '删除标签', NOW(), NOW(), 0),
|
||
('tag:assign', '分配标签', '标签管理', '给商品或优惠券分配标签', NOW(), NOW(), 0),
|
||
('coupon:view', '查看优惠券', '优惠券管理', '查看优惠券与发放情况', NOW(), NOW(), 0),
|
||
('coupon:create', '新增优惠券', '优惠券管理', '创建优惠券模板', NOW(), NOW(), 0),
|
||
('coupon:edit', '编辑优惠券', '优惠券管理', '编辑优惠券模板', NOW(), NOW(), 0),
|
||
('coupon:delete', '删除优惠券', '优惠券管理', '删除优惠券模板', NOW(), NOW(), 0),
|
||
('coupon:assign', '发放优惠券', '优惠券管理', '向用户发放普通优惠券', NOW(), NOW(), 0),
|
||
('order:view', '查看订单', '订单管理', '查看订单列表与详情', NOW(), NOW(), 0),
|
||
('order:process', '处理订单', '订单管理', '处理订单状态或由美甲师完结订单', NOW(), NOW(), 0),
|
||
('order:edit', '编辑订单', '订单管理', '编辑订单信息的预留权限', NOW(), NOW(), 0),
|
||
('order:delete', '删除订单', '订单管理', '删除订单的预留权限', NOW(), NOW(), 0),
|
||
('nail_artist:view', '查看美甲师', '美甲师管理', '查看美甲师信息与排班入口', NOW(), NOW(), 0),
|
||
('nail_artist:edit', '编辑美甲师', '美甲师管理', '编辑美甲师信息', NOW(), NOW(), 0)
|
||
ON DUPLICATE KEY UPDATE
|
||
permission_name = VALUES(permission_name),
|
||
permission_group = VALUES(permission_group),
|
||
description = VALUES(description),
|
||
update_time = NOW(),
|
||
is_delete = 0;
|
||
|
||
INSERT INTO t_user (
|
||
shop_id,
|
||
nickname,
|
||
account,
|
||
phone_verified,
|
||
email_verified,
|
||
password,
|
||
signature,
|
||
level,
|
||
type,
|
||
vip_id,
|
||
create_time,
|
||
update_time,
|
||
is_delete,
|
||
status
|
||
)
|
||
SELECT
|
||
0,
|
||
'系统管理员',
|
||
'admin01',
|
||
0,
|
||
0,
|
||
'$2a$10$9v.6PztWy3.vmJiVMaOyKuWeVqAnpuHB3hQ3Dgiuz6L8IwBe7Cn9W',
|
||
'',
|
||
'管理员',
|
||
4,
|
||
0,
|
||
NOW(),
|
||
NOW(),
|
||
0,
|
||
1
|
||
WHERE NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_user
|
||
WHERE account = 'admin01'
|
||
);
|
||
|
||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||
FROM t_user u
|
||
INNER JOIN t_permission p
|
||
ON p.is_delete = 0
|
||
AND p.permission_code IN (
|
||
'home:view',
|
||
'dashboard:view',
|
||
'user:view',
|
||
'user:create',
|
||
'user:edit',
|
||
'user:disable',
|
||
'user:unbind',
|
||
'user:reset_password',
|
||
'user:delete',
|
||
'user:balance:view',
|
||
'product:view',
|
||
'shop:view',
|
||
'shop:create',
|
||
'shop:edit',
|
||
'shop:delete',
|
||
'shop:staff:manage',
|
||
'permission:view',
|
||
'permission:create',
|
||
'permission:edit',
|
||
'permission:delete',
|
||
'permission:assign',
|
||
'tag:view',
|
||
'coupon:view',
|
||
'order:view',
|
||
'nail_artist:view'
|
||
)
|
||
WHERE u.is_delete = 0
|
||
AND u.type = 4
|
||
AND NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_user_permission up
|
||
WHERE up.user_id = u.id
|
||
AND up.permission_id = p.id
|
||
);
|
||
|
||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||
FROM t_user u
|
||
INNER JOIN t_permission p
|
||
ON p.is_delete = 0
|
||
AND p.permission_code IN (
|
||
'home:view',
|
||
'dashboard:view',
|
||
'user:view',
|
||
'user:create',
|
||
'user:edit',
|
||
'user:unbind',
|
||
'user:reset_password',
|
||
'user:balance:view',
|
||
'user:balance:manage',
|
||
'user:vip:manage',
|
||
'product:view',
|
||
'product:create',
|
||
'product:edit',
|
||
'product:delete',
|
||
'product:category:manage',
|
||
'shop:view',
|
||
'shop:edit',
|
||
'shop:staff:manage',
|
||
'permission:view',
|
||
'permission:assign',
|
||
'tag:view',
|
||
'tag:create',
|
||
'tag:edit',
|
||
'tag:delete',
|
||
'tag:assign',
|
||
'coupon:view',
|
||
'coupon:create',
|
||
'coupon:edit',
|
||
'coupon:delete',
|
||
'coupon:assign',
|
||
'order:view',
|
||
'order:process',
|
||
'order:edit',
|
||
'order:delete',
|
||
'nail_artist:view',
|
||
'nail_artist:edit'
|
||
)
|
||
WHERE u.is_delete = 0
|
||
AND u.type = 2
|
||
AND NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_user_permission up
|
||
WHERE up.user_id = u.id
|
||
AND up.permission_id = p.id
|
||
);
|
||
|
||
INSERT INTO t_user_permission (user_id, permission_id, create_time, update_time, create_by, update_by, is_delete)
|
||
SELECT u.id, p.id, NOW(), NOW(), u.id, u.id, 0
|
||
FROM t_user u
|
||
INNER JOIN t_permission p
|
||
ON p.is_delete = 0
|
||
AND p.permission_code IN (
|
||
'home:view',
|
||
'user:view',
|
||
'user:create',
|
||
'user:balance:view',
|
||
'product:view',
|
||
'product:create',
|
||
'shop:view',
|
||
'tag:view',
|
||
'tag:create',
|
||
'coupon:view',
|
||
'order:view',
|
||
'order:process'
|
||
)
|
||
WHERE u.is_delete = 0
|
||
AND u.type = 3
|
||
AND NOT EXISTS (
|
||
SELECT 1
|
||
FROM t_user_permission up
|
||
WHERE up.user_id = u.id
|
||
AND up.permission_id = p.id
|
||
);
|
||
|
||
|
||
|