diff --git a/.gitea/workflows/自动部署prod环境.yml b/.gitea/workflows/自动部署prod环境.yml index 2b6c4c51..ab68d1b9 100644 --- a/.gitea/workflows/自动部署prod环境.yml +++ b/.gitea/workflows/自动部署prod环境.yml @@ -48,7 +48,7 @@ jobs: - name: 校验 PROD Nacos 发布记录 shell: bash run: node .gitea/scripts/assert-nacos-release.mjs - - name: 构建并固化 PROD 发布包 + - name: 构建并校验含 Flyway 迁移的 PROD 发布包 shell: pwsh env: VITE_API_BASE_URL: https://salon.yunail.art/api diff --git a/.gitea/workflows/自动部署sit环境.yml b/.gitea/workflows/自动部署sit环境.yml index be7c88ca..68101fc5 100644 --- a/.gitea/workflows/自动部署sit环境.yml +++ b/.gitea/workflows/自动部署sit环境.yml @@ -45,7 +45,7 @@ jobs: - name: 校验 SIT Nacos 发布记录 shell: bash run: node .gitea/scripts/assert-nacos-release.mjs - - name: 构建 SIT 发布包 + - name: 构建并校验含 Flyway 迁移的 SIT 发布包 shell: pwsh env: VITE_API_BASE_URL: https://sit.yunail.art/api diff --git a/JC-Salon-Core/pom.xml b/JC-Salon-Core/pom.xml index 5e00698b..1e6901cd 100644 --- a/JC-Salon-Core/pom.xml +++ b/JC-Salon-Core/pom.xml @@ -110,6 +110,15 @@ mysql-connector-j runtime + + + org.flywaydb + flyway-core + + + org.flywaydb + flyway-mysql + com.baomidou mybatis-plus-boot-starter diff --git a/JC-Salon-Core/src/main/resources/db/migration/V1__Salon空库初始化.sql b/JC-Salon-Core/src/main/resources/db/migration/V1__Salon空库初始化.sql new file mode 100644 index 00000000..d21e068e --- /dev/null +++ b/JC-Salon-Core/src/main/resources/db/migration/V1__Salon空库初始化.sql @@ -0,0 +1,2479 @@ +-- Flyway V1 baseline for JayuCloud Salon. +-- Derived from the frozen empty-database initialization SQL; applied once at Core startup. +-- The jayucloud_salon database must exist before the application starts. +-- 默认平台管理员账号:xiaowang,默认密码沿用当前 bcrypt 值。 +-- Linux MySQL 环境库名大小写敏感,数据源必须指向 jayucloud_salon。 +-- 2026-10-10 已吸收 docs/scripts/sql 下全部 Salon 版本 SQL 的最终状态: +-- 工作台注册审核、短信供应商全局切换、短信模板编码映射调整、短信模板编码统一修复。 + +-- 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 '当前余额', + total_recharge_amount decimal(10, 2) default 0.00 not null comment '累计充值金额', + last_recharge_time datetime 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 '最近一次更新时间', + 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 '变动后的当前余额', + before_balance decimal(10, 2) null comment '变动前余额', + after_balance decimal(10, 2) null comment '变动后余额', + change_scene varchar(32) null comment '余额变动场景', + related_order_id bigint unsigned null comment '关联系统订单ID', + related_order_no varchar(64) null comment '关联系统订单号', + external_order_no varchar(64) null comment '手工录入的外部订单号', + operator_id bigint unsigned null comment '操作人ID', + remark varchar(255) 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表示为根评论)', + rating_score tinyint unsigned default 5 null comment '评论评分:1-5星', + image_file_ids text null comment '评论图片文件ID集合,多个ID以逗号分隔', + moderation_status varchar(20) default 'APPROVED' not null comment '本地模拟审核状态:PENDING/APPROVED/REJECTED/RETRY', + 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', + coupon_scene varchar(32) default 'NORMAL' not null comment '优惠券场景', + required_vip_level varchar(20) null comment '要求的VIP等级', + auto_match tinyint unsigned default 0 not null comment '是否自动匹配', + 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 '输入时间', + 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-无效' +) + 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_wechat_mini_binding +( + id bigint unsigned auto_increment comment '微信小程序绑定主键ID(雪花ID)' + primary key, + user_id bigint unsigned not null comment '关联用户ID', + app_id varchar(64) not null comment '微信小程序appId', + openid varchar(100) not null comment '微信小程序openid', + unionid varchar(100) null comment '微信开放平台unionid', + status tinyint unsigned default 1 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 '更新时间', + constraint uk_wechat_mini_app_openid + unique (app_id, openid), + constraint uk_wechat_mini_app_user + unique (app_id, user_id) +) + comment '微信小程序账号绑定表'; + +create index idx_wechat_mini_unionid + on t_wechat_mini_binding (unionid) + comment '按unionid查询的索引'; + +-- 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 4.5 null comment '评分', + status tinyint unsigned default 1 null comment '状态:1-营业中,0-休息中', + participate_production tinyint unsigned default 1 not null comment '是否参与美甲制作:1-参与,0-不参与', + work_start_time time null comment '工作开始时间', + work_end_time time null comment '工作结束时间', + 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 '按用户查询美甲师信息的索引'; + +create index idx_t_nail_artist_info_participate + on t_nail_artist_info (participate_production, status); + +-- auto-generated definition +create table t_object_storage_file +( + id bigint unsigned auto_increment comment '????ID' + primary key comment '????????', + file_name varchar(100) not null comment '????', + file_url text not null comment '??????', + object_name varchar(512) null comment '????Key', + primary_provider varchar(64) null comment '??????', + primary_url text null comment '???????', + backup_provider varchar(64) null comment '???????', + backup_url text null comment '????????', + content_type varchar(128) null comment '??Content-Type', + file_size bigint unsigned null comment '?????????', + primary_upload_status tinyint unsigned default 0 not null comment '????????0????1???2??', + backup_upload_status tinyint unsigned default 0 not null comment '?????????0????1???2??', + backup_retry_count int unsigned default 0 not null comment '??????', + backup_last_error varchar(500) null comment '??????????', + backup_uploaded_at datetime null comment '??????', + backup_last_retry_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 '??????', + status tinyint unsigned default 1 null comment '?????1???0????2???', + delete_time datetime null comment '??????', + recycle_time datetime null comment '?????????' +) + comment '?????????'; + +create index idx_status + on t_object_storage_file (status) + comment '??????'; + +create index idx_object_storage_file_backup_retry + on t_object_storage_file (backup_upload_status, primary_upload_status, status) + comment '????????'; + +-- auto-generated definition +CREATE TABLE IF NOT EXISTS `t_object_storage_file_replica` +( + `id` bigint unsigned NOT NULL COMMENT '副本ID', + `file_id` bigint unsigned NOT NULL COMMENT '文件ID', + `provider` varchar(64) NOT NULL COMMENT '对象存储提供方', + `object_name` varchar(512) NOT NULL COMMENT '对象Key', + `object_url` text NULL COMMENT '对象访问地址', + `upload_status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '上传状态:0-待上传,1-成功,2-失败', + `retry_count` int unsigned NOT NULL DEFAULT 0 COMMENT '重试次数', + `last_error` varchar(500) NULL COMMENT '最近一次错误', + `uploaded_at` datetime NULL COMMENT '上传成功时间', + `last_retry_time` datetime NULL COMMENT '最近一次重试时间', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`id`), + UNIQUE KEY `uk_object_storage_file_replica_file_provider` (`file_id`, `provider`), + KEY `idx_object_storage_file_replica_retry` (`upload_status`, `provider`, `file_id`), + KEY `idx_object_storage_file_replica_file_status` (`file_id`, `upload_status`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_unicode_ci + 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 null comment '关联用户表主键,历史补单可为空', + shop_id bigint unsigned null comment '所属店铺ID', + 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', + user_coupon_id bigint unsigned null comment '用户券ID', + coupon_source_type varchar(32) null comment '优惠来源类型', + customer_remark varchar(500) null comment '用户询价预约备注', + customer_name_snapshot varchar(100) null comment '补单客户名称快照', + customer_phone_snapshot varchar(128) null comment '加密补单客户手机号快照', + customer_phone_snapshot_hash char(64) null comment '补单客户手机号HMAC索引', + order_source varchar(32) default 'NORMAL_APPOINTMENT' not null comment '订单来源:NORMAL_APPOINTMENT/HISTORICAL_BACKFILL', + status tinyint unsigned default 1 not null comment '订单状态:0-价格确认中,1-待服务,2-已完成,3-已取消,4-服务中', + 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 '按用户查询订单的索引'; + +create index idx_order_source_status_delete + on t_order (order_source, status, is_delete) + 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', + appointment_time datetime null comment '预约开始时间', + service_date date 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_order_id + on t_order_item (order_id) + comment '按订单查询商品的索引'; + +create index idx_order_item_artist_service_date + on t_order_item (artist_id, service_date, is_delete) + comment '按员工和业务服务日期查询的索引'; + +CREATE TABLE IF NOT EXISTS t_order_artist_review +( + id bigint unsigned auto_increment comment '评价任务ID' + primary key, + order_id bigint unsigned not null comment '订单ID', + order_item_id bigint unsigned not null comment '订单项ID', + customer_id bigint unsigned not null comment '下单客户ID', + shop_id bigint unsigned null comment '店铺ID', + artist_id bigint unsigned not null comment '被评价美甲师ID', + product_id bigint unsigned null comment '商品ID', + rating_score decimal(2, 1) null comment '评分,提交后必填0.5-5', + content varchar(1000) null comment '评价文字', + image_file_ids text null comment '评价图片文件ID集合', + status tinyint unsigned default 0 not null comment '状态:0-待评价,1-已评价', + reminder_message_id bigint unsigned null comment '提醒消息ID', + submitted_at 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-已删' +) + comment '订单美甲师评价任务表'; + +create unique index uk_order_artist_review_item + on t_order_artist_review (order_item_id, is_delete); + +create index idx_order_artist_review_customer + on t_order_artist_review (customer_id, status, create_time); + +create index idx_order_artist_review_artist + on t_order_artist_review (artist_id, status); + +CREATE TABLE IF NOT EXISTS t_order_artist_change_request +( + id bigint unsigned auto_increment comment '改派申请ID' + primary key, + order_id bigint unsigned not null comment '订单ID', + order_item_id bigint unsigned not null comment '订单项ID', + customer_id bigint unsigned not null comment '下单客户ID', + shop_id bigint unsigned null comment '店铺ID', + product_id bigint unsigned null comment '商品ID', + from_artist_id bigint unsigned not null comment '原美甲师ID', + to_artist_id bigint unsigned not null comment '新美甲师ID', + requester_id bigint unsigned not null comment '申请人ID', + status varchar(20) default 'PENDING' not null comment '状态:PENDING/APPROVED/REJECTED', + reason varchar(500) null comment '申请原因', + reject_reason varchar(500) null comment '拒绝原因', + message_id bigint unsigned null comment '提醒消息ID', + pending_key varchar(80) null comment '待处理唯一键,PENDING时为订单项维度', + approved_time datetime null comment '同意时间', + rejected_time datetime null comment '拒绝时间', + expire_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-已删' +) + comment '订单美甲师改派申请表'; + +create index idx_order_artist_change_customer + on t_order_artist_change_request (customer_id, status, create_time); + +create index idx_order_artist_change_artist + on t_order_artist_change_request (from_artist_id, to_artist_id, status); + +create index idx_order_artist_change_shop + on t_order_artist_change_request (shop_id, status, create_time); + +create unique index uk_order_artist_change_pending_key + on t_order_artist_change_request (pending_key); + +-- 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 '商品描述', + target_audience text null comment '适合人群说明', + precautions 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以逗号分隔', + front_visible tinyint unsigned default 1 not null comment '前台是否展示:0-隐藏,1-展示', + source_type varchar(64) default 'BACKEND' not null comment '来源类型:BACKEND-后台维护,MANAGER_APPOINTMENT-管理师端手动预约', + 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 '商品标题搜索的索引'; + +create index idx_t_product_front_visible + on t_product (shop_id, front_visible, source_type) + 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 '店铺地址', + map_provider varchar(20) default 'TENCENT' not null comment '地图服务商:TENCENT/AMAP', + longitude decimal(12, 8) null comment '店铺经度', + latitude decimal(12, 8) null comment '店铺纬度', + business_hours varchar(100) default '' null comment '店铺营业时间', + cover_image_file_id varchar(50) default '' null comment '店铺封面图文件ID', + theme_primary_color varchar(20) null comment '前台主题主色', + theme_secondary_color varchar(20) null comment '前台主题辅色', + theme_accent_color varchar(20) null comment '前台主题强调色', + theme_page_bg_color varchar(20) null comment '前台主题页面背景色', + theme_card_bg_color varchar(20) null comment '前台主题卡片背景色', + theme_background_image_file_id varchar(50) null comment '前台主题背景图文件ID', + status tinyint unsigned default 1 not null comment '店铺状态:1-启用,0-停用', + authorization_start_date date null comment '店铺授权起始日', + authorization_days int null comment '店铺授权有效天数', + authorization_expire_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-已删', + 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进制)', + scene_group varchar(50) null comment '前台场景筛选分组', + display_name varchar(80) null comment '前台展示名称', + sort_order int default 100 null comment '前台筛选排序', + front_visible tinyint unsigned default 1 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-已删', + 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(128) null comment '用户账号(手机号账号加密存储)', + account_hash char(64) null comment '账号HMAC索引', + phone varchar(128) null comment '手机号AES-GCM密文', + phone_hash char(64) null comment '手机号HMAC索引', + contact_phone varchar(128) null comment '联系电话AES-GCM密文', + contact_phone_hash char(64) null comment '联系电话HMAC索引', + birthday date null comment '生日', + wechat_id varchar(100) null comment '微信号', + wechat_qr_code varchar(512) 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) 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_user_phone_hash + unique (phone_hash) comment '用户手机号HMAC唯一索引(允许多行NULL)', + constraint uk_user_account_hash + unique (account_hash) comment '手机号账号HMAC唯一索引(允许多行NULL)' +) + comment '系统用户信息表'; + +create index idx_email + on t_user (email) + comment '按邮箱查询的索引'; + +create index idx_shop_id + on t_user (shop_id) + comment '按店铺查询用户的索引'; + +create index idx_type + on t_user (type) + comment '按用户类型查询的索引'; + +-- 工作台注册审核申请 +CREATE TABLE IF NOT EXISTS `t_workbench_registration_application` +( + `id` bigint NOT NULL COMMENT '申请ID', + `application_no` varchar(40) NOT NULL COMMENT '申请编号', + `user_id` bigint NOT NULL COMMENT '申请用户ID', + `role_type` varchar(20) NOT NULL COMMENT '申请身份:MANAGER/STAFF', + `shop_id` bigint NULL COMMENT '目标店铺ID', + `shop_name` varchar(120) NULL COMMENT '店铺名称快照', + `shop_address` varchar(255) NULL COMMENT '店铺地址快照', + `status` varchar(30) NOT NULL COMMENT '状态:PENDING_ADMIN/PENDING_MANAGER/APPROVED/REJECTED', + `reviewer_id` bigint NULL COMMENT '审核人ID', + `reviewed_at` datetime NULL COMMENT '审核时间', + `reject_reason` varchar(500) NULL COMMENT '驳回原因', + `is_delete` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '逻辑删除:0-未删,1-已删', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`id`), + UNIQUE KEY `uk_workbench_registration_application_no` (`application_no`), + KEY `idx_workbench_registration_user` (`user_id`), + KEY `idx_workbench_registration_shop_status` (`shop_id`, `status`), + KEY `idx_workbench_registration_status` (`status`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Salon店务工作台注册审核申请'; + +-- 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索引'; + +create table if not exists t_system_message +( + id bigint unsigned auto_increment comment '站内消息主键' + primary key, + title varchar(100) not null comment '消息标题', + content text not null comment '消息内容', + image_file_id bigint unsigned null comment '消息图片文件ID', + publisher_id bigint unsigned not null comment '发布人ID', + publisher_name varchar(100) default '' null comment '发布人名称', + publisher_type tinyint not null comment '发布人类型:2-店长,4-管理员', + target_type varchar(30) not null comment '目标范围:PLATFORM_ALL/CURRENT_SHOP_ALL/SHOP_ALL/SELECTED_SHOPS/SELECTED_USERS', + target_ids text null comment '目标ID集合,多个ID以逗号分隔', + business_type varchar(64) null comment '业务类型', + business_id bigint unsigned null comment '业务ID', + action_path varchar(255) null comment '业务跳转路径', + idempotency_key varchar(160) null comment '业务消息幂等键', + popup_enabled tinyint unsigned default 1 not null comment '是否弹窗:1-弹窗,0-不弹窗', + status tinyint unsigned default 1 not null comment '状态:1-已发布,0-已撤回', + publish_time datetime default CURRENT_TIMESTAMP 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-已删' +) + comment '站内消息表'; + +create index idx_t_system_message_publish_time + on t_system_message (publish_time); + +create index idx_t_system_message_target + on t_system_message (target_type, status); + +create unique index uk_t_system_message_idempotency + on t_system_message (idempotency_key); + +create table if not exists t_system_message_user_state +( + id bigint unsigned auto_increment comment '用户消息状态主键' + primary key, + message_id bigint unsigned not null comment '消息ID', + user_id bigint unsigned not null comment '用户ID', + popup_shown tinyint unsigned default 0 not null comment '是否已弹窗:1-是,0-否', + read_status tinyint unsigned default 0 not null comment '是否已读:1-是,0-否', + popup_time datetime null comment '首次弹窗时间', + read_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_t_system_message_user + unique (message_id, user_id) +) + comment '用户站内消息状态表'; + +create index idx_t_system_message_user_state_user + on t_system_message_user_state (user_id, read_status, popup_shown); + +create table if not exists t_announcement +( + id bigint unsigned auto_increment comment '公告主键' + primary key, + title varchar(100) not null comment '公告标题', + content text not null comment '公告内容', + publisher_id bigint unsigned not null comment '发布人ID', + publisher_name varchar(100) default '' null comment '发布人名称', + publisher_type tinyint not null comment '发布人类型:2-店长,4-管理员', + target_type varchar(30) not null comment '目标范围', + target_ids text null comment '目标ID集合,多个ID以逗号分隔', + status tinyint unsigned default 1 not null comment '状态:1-已发布,0-已撤回', + sort_order int default 0 not null comment '排序值', + publish_time datetime default CURRENT_TIMESTAMP 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-已删' +) + comment '首页滚动公告表'; + +create index idx_t_announcement_target + on t_announcement (target_type, status); + +create index idx_t_announcement_sort_time + on t_announcement (sort_order, publish_time); + +create table if not exists t_vip_recharge_plan +( + id bigint unsigned auto_increment comment 'VIP充值档位主键' + primary key, + shop_id bigint unsigned not null comment '所属店铺ID', + title varchar(100) not null comment '档位标题', + recharge_amount decimal(10, 2) not null comment '充值金额', + gift_amount decimal(10, 2) default 0.00 not null comment '赠送金额', + vip_level varchar(20) not null comment '对应VIP等级', + description varchar(255) null comment '展示说明', + sort_order int default 0 not null comment '排序值', + status 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-已删' +) + comment 'VIP充值展示档位表'; + +create index idx_t_vip_recharge_plan_shop_status + on t_vip_recharge_plan (shop_id, status, sort_order); + +-- 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等级', + level_rank int default 0 not null comment '等级序号', + recharge_threshold decimal(10, 2) default 0.00 not null comment '累计充值门槛', + status tinyint 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 '更新时间' +) + 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信息主键索引', + user_id bigint unsigned null comment '所属用户ID', + vip_level varchar(20) not null comment 'VIP等级(如青铜、白银)', + start_time datetime not null comment 'VIP开通时间', + expire_time datetime null comment 'VIP到期时间', + activation_method varchar(50) not null comment 'VIP开通方式(如购买、赠送)', + benefit_id bigint unsigned null comment '关联VIP权益信息表主键', + manual_override tinyint unsigned default 0 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_group +( + id bigint unsigned auto_increment comment '收藏分组主键' + primary key, + user_id bigint unsigned not null comment '关联用户表主键', + name varchar(50) not null comment '分组名称,默认预设:最近想做、婚礼款、夏天款', + sort_order int default 100 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-已删除' +) + comment '商品收藏分组表'; + +create index idx_t_wishlist_group_sort + on t_wishlist_group (user_id, sort_order, create_time) + comment '按用户和排序查询收藏分组的索引'; + +create index idx_t_wishlist_group_user + on t_wishlist_group (user_id) + comment '按用户查询收藏分组的索引'; + +-- 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 '关联商品表主键', + group_id bigint unsigned null comment '收藏分组ID', + note varchar(500) null comment '收藏灵感备注', + sort_order int default 100 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 '按用户查询收藏的索引'; + +create index idx_t_wishlist_user_group_sort + on t_wishlist (user_id, group_id, sort_order, create_time) + 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; + +CREATE TABLE IF NOT EXISTS t_account_closure_request ( + id bigint unsigned NOT NULL COMMENT '账号注销申请ID', + user_id bigint unsigned NOT NULL COMMENT '申请人用户ID', + status varchar(20) NOT NULL DEFAULT 'PENDING' COMMENT '申请状态:PENDING/APPROVED/REJECTED/WITHDRAWN', + requested_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '申请时间', + processed_at datetime NULL COMMENT '处理时间', + reviewer_id bigint unsigned NULL COMMENT '处理人用户ID', + reviewer_note varchar(500) NULL COMMENT '处理说明', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id), + KEY idx_account_closure_user_status (user_id, status), + KEY idx_account_closure_status_time (status, requested_at) +) COMMENT='账号注销申请记录'; + +CREATE TABLE IF NOT EXISTS t_content_moderation_record ( + id bigint unsigned NOT NULL COMMENT '内容审核记录ID', + content_type varchar(40) NOT NULL COMMENT '内容类型:USER_PROFILE/USER_AVATAR/PRODUCT/COMMENT', + business_id bigint unsigned NULL COMMENT '业务记录ID', + user_id bigint unsigned NULL COMMENT '内容提交人', + status varchar(20) NOT NULL COMMENT '审核状态:PENDING/APPROVED/REJECTED/RETRY', + provider varchar(30) NOT NULL DEFAULT 'LOCAL_MOCK' COMMENT '审核Provider标识', + result_code varchar(40) NULL COMMENT 'Provider结果代码', + reason varchar(500) NULL COMMENT '审核或复核说明', + payload_json mediumtext NULL COMMENT '隔离内容载荷,仅待审核记录可访问', + reviewed_by bigint unsigned NULL COMMENT '复核人用户ID', + reviewed_at datetime NULL COMMENT '复核时间', + attempt_count int NOT NULL DEFAULT 0 COMMENT '处理尝试次数', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id), + KEY idx_content_moderation_status_time (status, create_time), + KEY idx_content_moderation_business (content_type, business_id) +) COMMENT='本地内容审核隔离与复核记录'; + +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 ''最近一次充值时间'''); +CALL sp_add_column_if_not_exists('t_balance', 'update_time', 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间'''); + +CALL sp_add_column_if_not_exists('t_object_storage_file', 'object_name', 'varchar(512) NULL COMMENT ''????Key'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'primary_provider', 'varchar(64) NULL COMMENT ''??????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'primary_url', 'text NULL COMMENT ''???????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_provider', 'varchar(64) NULL COMMENT ''???????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_url', 'text NULL COMMENT ''????????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'content_type', 'varchar(128) NULL COMMENT ''??Content-Type'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'file_size', 'bigint unsigned NULL COMMENT ''?????????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'primary_upload_status', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''????????0????1???2??'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_upload_status', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''?????????0????1???2??'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_retry_count', 'int unsigned NOT NULL DEFAULT 0 COMMENT ''??????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_last_error', 'varchar(500) NULL COMMENT ''??????????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_uploaded_at', 'datetime NULL COMMENT ''??????'''); +CALL sp_add_column_if_not_exists('t_object_storage_file', 'backup_last_retry_time', 'datetime NULL COMMENT ''??????????'''); +UPDATE t_object_storage_file +SET primary_provider = COALESCE(primary_provider, 'QINIU_KODO'), + primary_url = COALESCE(primary_url, file_url), + backup_provider = COALESCE(backup_provider, 'ALIYUN_OSS'), + primary_upload_status = CASE WHEN primary_upload_status IS NULL OR primary_upload_status = 0 THEN 1 ELSE primary_upload_status END, + backup_upload_status = COALESCE(backup_upload_status, 0), + backup_retry_count = COALESCE(backup_retry_count, 0) +WHERE status = 1; +CALL sp_add_index_if_not_exists('t_object_storage_file', 'idx_object_storage_file_backup_retry', '(`backup_upload_status`, `primary_upload_status`, `status`)'); +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 ''备注'''); +CALL sp_add_column_if_not_exists('t_login_identity', 'create_time', 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间'''); + +CALL sp_add_column_if_not_exists('t_user', 'contact_phone', 'varchar(50) NULL COMMENT ''联系电话'''); +CALL sp_add_column_if_not_exists('t_user', 'birthday', 'date NULL COMMENT ''生日'''); +CALL sp_add_column_if_not_exists('t_user', 'wechat_id', 'varchar(100) NULL COMMENT ''微信号'''); +CALL sp_add_column_if_not_exists('t_user', 'wechat_qr_code', 'varchar(512) NULL COMMENT ''微信二维码'''); +CALL sp_add_column_if_not_exists('t_user', 'account_hash', 'char(64) NULL COMMENT ''账号HMAC索引'''); +CALL sp_add_column_if_not_exists('t_user', 'phone_hash', 'char(64) NULL COMMENT ''手机号HMAC索引'''); +CALL sp_add_column_if_not_exists('t_user', 'contact_phone_hash', 'char(64) NULL COMMENT ''联系电话HMAC索引'''); +CALL sp_add_column_if_not_exists('t_comment', 'moderation_status', 'varchar(20) NOT NULL DEFAULT ''APPROVED'' COMMENT ''本地模拟审核状态:PENDING/APPROVED/REJECTED/RETRY'''); +ALTER TABLE t_user MODIFY COLUMN account varchar(128) NULL COMMENT '手机号账号AES-GCM密文'; +ALTER TABLE t_user MODIFY COLUMN phone varchar(128) NULL COMMENT '手机号AES-GCM密文'; +ALTER TABLE t_user MODIFY COLUMN contact_phone varchar(128) NULL COMMENT '联系电话AES-GCM密文'; +SET @drop_legacy_account_index_sql = IF( + EXISTS(SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't_user' AND INDEX_NAME = 'uk_account'), + 'ALTER TABLE t_user DROP INDEX uk_account', 'SELECT 1' +); +PREPARE stmt_drop_legacy_account_index FROM @drop_legacy_account_index_sql; +EXECUTE stmt_drop_legacy_account_index; +DEALLOCATE PREPARE stmt_drop_legacy_account_index; +SET @drop_legacy_phone_index_sql = IF( + EXISTS(SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't_user' AND INDEX_NAME = 'uk_user_phone'), + 'ALTER TABLE t_user DROP INDEX uk_user_phone', 'SELECT 1' +); +PREPARE stmt_drop_legacy_phone_index FROM @drop_legacy_phone_index_sql; +EXECUTE stmt_drop_legacy_phone_index; +DEALLOCATE PREPARE stmt_drop_legacy_phone_index; +CALL sp_add_unique_index_if_not_exists('t_user', 'uk_user_phone_hash', '(`phone_hash`)'); +CALL sp_add_unique_index_if_not_exists('t_user', 'uk_user_account_hash', '(`account_hash`)'); +CALL sp_add_index_if_not_exists('t_user', 'idx_user_contact_phone_hash', '(`contact_phone_hash`)'); +CALL sp_add_column_if_not_exists('t_order', 'customer_phone_snapshot_hash', 'char(64) NULL COMMENT ''补单客户手机号HMAC索引'''); +ALTER TABLE t_order MODIFY COLUMN customer_phone_snapshot varchar(128) NULL COMMENT '加密补单客户手机号快照'; +CALL sp_add_index_if_not_exists('t_order', 'idx_order_customer_phone_snapshot_hash', '(`customer_phone_snapshot_hash`)'); +SET @drop_consent_device_id_sql = IF( + EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't_user_agreement_consent' AND COLUMN_NAME = 'device_id'), + 'ALTER TABLE t_user_agreement_consent DROP COLUMN device_id', 'SELECT 1' +); +PREPARE stmt_drop_consent_device_id FROM @drop_consent_device_id_sql; +EXECUTE stmt_drop_consent_device_id; +DEALLOCATE PREPARE stmt_drop_consent_device_id; +SET @drop_audit_device_id_sql = IF( + EXISTS(SELECT 1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 't_security_audit_log' AND COLUMN_NAME = 'device_id'), + 'ALTER TABLE t_security_audit_log DROP COLUMN device_id', 'SELECT 1' +); +PREPARE stmt_drop_audit_device_id FROM @drop_audit_device_id_sql; +EXECUTE stmt_drop_audit_device_id; +DEALLOCATE PREPARE stmt_drop_audit_device_id; +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'''); + +SET @sql = (SELECT IF(COUNT(*) = 0, + 'ALTER TABLE t_order MODIFY COLUMN user_id BIGINT UNSIGNED NULL COMMENT ''关联用户表主键,历史补单可为空''', + 'SELECT 1') FROM information_schema.columns + WHERE table_schema = DATABASE() AND table_name = 't_order' AND column_name = 'user_id' AND is_nullable = 'YES'); +PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; + +CALL sp_add_column_if_not_exists('t_order', 'customer_remark', 'varchar(500) NULL COMMENT ''用户询价预约备注'''); +CALL sp_add_column_if_not_exists('t_order', 'customer_name_snapshot', 'varchar(100) NULL COMMENT ''补单客户名称快照'''); +CALL sp_add_column_if_not_exists('t_order', 'customer_phone_snapshot', 'varchar(32) NULL COMMENT ''补单客户手机号快照'''); +CALL sp_add_column_if_not_exists('t_order', 'order_source', 'varchar(32) NOT NULL DEFAULT ''NORMAL_APPOINTMENT'' COMMENT ''订单来源:NORMAL_APPOINTMENT/HISTORICAL_BACKFILL'''); + +CALL sp_add_column_if_not_exists('t_order_item', 'appointment_time', 'datetime NULL COMMENT ''预约开始时间'''); +CALL sp_add_column_if_not_exists('t_order_item', 'service_date', 'date NULL COMMENT ''业务服务日期,历史补单无具体时间时使用'''); +CALL sp_add_index_if_not_exists('t_order_item', 'idx_t_order_item_artist_appointment_time', '(`artist_id`, `appointment_time`)'); +CALL sp_add_index_if_not_exists('t_order', 'idx_order_source_status_delete', '(`order_source`, `status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_order_item', 'idx_order_item_artist_service_date', '(`artist_id`, `service_date`, `is_delete`)'); + +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_tag', 'scene_group', 'varchar(50) NULL COMMENT ''前台场景筛选分组'''); +CALL sp_add_column_if_not_exists('t_tag', 'display_name', 'varchar(80) NULL COMMENT ''前台展示名称'''); +CALL sp_add_column_if_not_exists('t_tag', 'sort_order', 'int NULL DEFAULT 100 COMMENT ''前台筛选排序'''); +CALL sp_add_column_if_not_exists('t_tag', 'front_visible', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''前台筛选是否可见:1-可见,0-隐藏'''); +UPDATE t_tag +SET front_visible = 1 +WHERE front_visible IS NULL; +CALL sp_add_index_if_not_exists('t_tag', 'idx_t_tag_front_filter', '(`shop_id`, `front_visible`, `scene_group`, `sort_order`, `is_delete`)'); + +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 = '黄金' +); + +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-停用'''); +CALL sp_add_column_if_not_exists('t_shop', 'authorization_start_date', 'date NULL COMMENT ''店铺授权起始日'''); +CALL sp_add_column_if_not_exists('t_shop', 'authorization_days', 'int NULL COMMENT ''店铺授权有效天数'''); +CALL sp_add_column_if_not_exists('t_shop', 'authorization_expire_time', 'datetime NULL COMMENT ''店铺授权有效期,空表示长期有效'''); +CALL sp_add_column_if_not_exists('t_shop', 'map_provider', 'varchar(20) NOT NULL DEFAULT ''TENCENT'' COMMENT ''地图服务商:TENCENT/AMAP'''); +CALL sp_add_column_if_not_exists('t_shop', 'longitude', 'decimal(12,8) NULL COMMENT ''店铺经度'''); +CALL sp_add_column_if_not_exists('t_shop', 'latitude', 'decimal(12,8) NULL COMMENT ''店铺纬度'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_primary_color', 'varchar(20) NULL COMMENT ''前台主题主色'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_secondary_color', 'varchar(20) NULL COMMENT ''前台主题辅色'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_accent_color', 'varchar(20) NULL COMMENT ''前台主题强调色'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_page_bg_color', 'varchar(20) NULL COMMENT ''前台主题页面背景色'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_card_bg_color', 'varchar(20) NULL COMMENT ''前台主题卡片背景色'''); +CALL sp_add_column_if_not_exists('t_shop', 'theme_background_image_file_id', 'varchar(50) NULL COMMENT ''前台主题背景图文件ID'''); + +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; + +CALL sp_add_column_if_not_exists('t_nail_artist_info', 'participate_production', 'tinyint unsigned NOT NULL DEFAULT 1 COMMENT ''是否参与美甲制作:1-参与,0-不参与'''); +CALL sp_add_column_if_not_exists('t_nail_artist_info', 'work_start_time', 'time NULL COMMENT ''工作开始时间'''); +CALL sp_add_column_if_not_exists('t_nail_artist_info', 'work_end_time', 'time NULL COMMENT ''工作结束时间'''); +CALL sp_add_index_if_not_exists('t_nail_artist_info', 'idx_t_nail_artist_info_participate', '(`participate_production`, `status`)'); +CALL sp_add_column_if_not_exists('t_system_message', 'image_file_id', 'bigint unsigned NULL COMMENT ''消息图片文件ID'''); + +CALL sp_add_column_if_not_exists('t_system_message', 'business_type', 'varchar(64) NULL COMMENT ''业务类型'''); +CALL sp_add_column_if_not_exists('t_system_message', 'business_id', 'bigint unsigned NULL COMMENT ''业务ID'''); +CALL sp_add_column_if_not_exists('t_system_message', 'action_path', 'varchar(255) NULL COMMENT ''业务跳转路径'''); +CALL sp_add_column_if_not_exists('t_system_message', 'idempotency_key', 'varchar(160) NULL COMMENT ''业务消息幂等键'''); + +CREATE TABLE IF NOT EXISTS t_order_artist_review +( + id bigint unsigned auto_increment comment '评价任务ID' + primary key, + order_id bigint unsigned not null comment '订单ID', + order_item_id bigint unsigned not null comment '订单项ID', + customer_id bigint unsigned not null comment '下单客户ID', + shop_id bigint unsigned null comment '店铺ID', + artist_id bigint unsigned not null comment '被评价美甲师ID', + product_id bigint unsigned null comment '商品ID', + rating_score decimal(2, 1) null comment '评分,提交后必填0.5-5', + content varchar(1000) null comment '评价文字', + image_file_ids text null comment '评价图片文件ID集合', + status tinyint unsigned default 0 not null comment '状态:0-待评价,1-已评价', + reminder_message_id bigint unsigned null comment '提醒消息ID', + submitted_at 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-已删' +) + comment '订单美甲师评价任务表'; + +CREATE TABLE IF NOT EXISTS t_order_artist_change_request +( + id bigint unsigned auto_increment comment '改派申请ID' + primary key, + order_id bigint unsigned not null comment '订单ID', + order_item_id bigint unsigned not null comment '订单项ID', + customer_id bigint unsigned not null comment '下单客户ID', + shop_id bigint unsigned null comment '店铺ID', + product_id bigint unsigned null comment '商品ID', + from_artist_id bigint unsigned not null comment '原美甲师ID', + to_artist_id bigint unsigned not null comment '新美甲师ID', + requester_id bigint unsigned not null comment '申请人ID', + status varchar(20) default 'PENDING' not null comment '状态:PENDING/APPROVED/REJECTED', + reason varchar(500) null comment '申请原因', + reject_reason varchar(500) null comment '拒绝原因', + message_id bigint unsigned null comment '提醒消息ID', + pending_key varchar(80) null comment '待处理唯一键,PENDING时为订单项维度', + approved_time datetime null comment '同意时间', + rejected_time datetime null comment '拒绝时间', + expire_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-已删' +) + comment '订单美甲师改派申请表'; + +CALL sp_add_unique_index_if_not_exists('t_order_artist_review', 'uk_order_artist_review_item', '(`order_item_id`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_order_artist_review', 'idx_order_artist_review_customer', '(`customer_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_order_artist_review', 'idx_order_artist_review_artist', '(`artist_id`, `status`)'); +CALL sp_add_index_if_not_exists('t_order_artist_review', 'idx_order_artist_review_order', '(`order_id`)'); +CALL sp_add_unique_index_if_not_exists('t_system_message', 'uk_t_system_message_idempotency', '(`idempotency_key`)'); +CALL sp_add_column_if_not_exists('t_order_artist_change_request', 'pending_key', 'varchar(80) NULL COMMENT ''待处理唯一键,PENDING时为订单项维度'''); +CALL sp_add_unique_index_if_not_exists('t_order_artist_change_request', 'uk_order_artist_change_pending_key', '(`pending_key`)'); +CALL sp_add_index_if_not_exists('t_order_artist_change_request', 'idx_order_artist_change_item_status', '(`order_item_id`, `status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_order_artist_change_request', 'idx_order_artist_change_customer', '(`customer_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_order_artist_change_request', 'idx_order_artist_change_artist', '(`from_artist_id`, `to_artist_id`, `status`)'); +CALL sp_add_index_if_not_exists('t_order_artist_change_request', 'idx_order_artist_change_shop', '(`shop_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_order_artist_change_request', 'idx_order_artist_change_expire', '(`status`, `expire_time`, `is_delete`)'); + +CREATE TABLE IF NOT EXISTS t_business_event_outbox +( + id bigint unsigned auto_increment comment '事件ID' + primary key, + event_key varchar(160) not null comment '事件幂等键', + event_type varchar(64) not null comment '事件类型', + business_type varchar(64) not null comment '业务类型', + business_id bigint unsigned not null comment '业务ID', + payload_json text null comment '事件载荷', + status varchar(20) default 'PENDING' not null comment 'PENDING/PROCESSING/SUCCEEDED/FAILED', + retry_count int unsigned default 0 not null comment '重试次数', + next_retry_time datetime null comment '下次重试时间', + last_error varchar(500) null comment '最近错误', + claimed_at datetime null comment '认领时间', + processed_at 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 '逻辑删除' +) + comment '业务事件Outbox表'; + +CALL sp_add_unique_index_if_not_exists('t_business_event_outbox', 'uk_business_event_outbox_event_key', '(`event_key`)'); +CALL sp_add_index_if_not_exists('t_business_event_outbox', 'idx_business_event_outbox_due', '(`status`, `next_retry_time`, `create_time`)'); + +-- Artist change requests can be expired asynchronously; expired rows release pending_key for a new request. +-- EXPIRED + +-- ======================================== +-- vNext operations schema: schedule / reservation / payment / after sale / inventory / campaign / growth +-- ======================================== + +CALL sp_add_column_if_not_exists('t_order', 'payment_status', 'varchar(20) NOT NULL DEFAULT ''UNPAID'' COMMENT ''支付状态:UNPAID/PAYING/PAID/REFUNDED'''); +CALL sp_add_column_if_not_exists('t_order', 'pay_amount', 'decimal(10,2) NULL COMMENT ''应支付金额快照'''); +CALL sp_add_column_if_not_exists('t_order', 'paid_amount', 'decimal(10,2) NULL COMMENT ''实付金额快照'''); +CALL sp_add_column_if_not_exists('t_order', 'pay_time', 'datetime NULL COMMENT ''支付完成时间'''); +CALL sp_add_column_if_not_exists('t_order', 'payment_method', 'varchar(32) NULL COMMENT ''支付方式/渠道'''); +CALL sp_add_column_if_not_exists('t_order', 'payment_order_no', 'varchar(64) NULL COMMENT ''最近一次支付单号'''); +CALL sp_add_column_if_not_exists('t_order', 'refund_status', 'varchar(20) NOT NULL DEFAULT ''NONE'' COMMENT ''退款状态:NONE/REFUNDED'''); +CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_payment_status', '(`payment_status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_payment_order_no', '(`payment_order_no`)'); +CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_refund_status', '(`refund_status`, `is_delete`)'); + +CREATE TABLE IF NOT EXISTS t_artist_schedule +( + id bigint unsigned NOT NULL COMMENT '排班ID', + shop_id bigint unsigned NOT NULL COMMENT '店铺ID', + artist_id bigint unsigned NOT NULL COMMENT '美甲师用户ID', + work_date date NOT NULL COMMENT '工作日期', + start_time time NOT NULL COMMENT '班次开始时间', + end_time time NOT NULL COMMENT '班次结束时间', + slot_minutes int NOT NULL DEFAULT 30 COMMENT '预约粒度分钟数', + capacity int NOT NULL DEFAULT 1 COMMENT '同一时段可预约容量', + status tinyint NOT NULL DEFAULT 1 COMMENT '状态:1-可预约,0-停用', + remark varchar(255) NULL COMMENT '排班备注', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '美甲师排班表'; + +CREATE TABLE IF NOT EXISTS t_appointment_reservation +( + id bigint unsigned NOT NULL COMMENT '预约占用ID', + order_id bigint unsigned NOT NULL COMMENT '订单ID', + order_item_id bigint unsigned NOT NULL COMMENT '订单项ID', + shop_id bigint unsigned NOT NULL COMMENT '店铺ID', + artist_id bigint unsigned NOT NULL COMMENT '美甲师用户ID', + product_title varchar(200) NULL COMMENT '预约作品标题快照', + appointment_start_time datetime NOT NULL COMMENT '预约开始时间', + appointment_end_time datetime NOT NULL COMMENT '预约结束时间', + status varchar(20) NOT NULL DEFAULT 'LOCKED' COMMENT '状态:LOCKED/PENDING_QUOTE/CONFIRMED/RELEASED/CANCELLED/EXPIRED', + client_request_id varchar(80) NULL COMMENT '客户端幂等请求ID', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '订单预约时段占用表'; + +CREATE TABLE IF NOT EXISTS t_payment_order +( + id bigint unsigned NOT NULL COMMENT '支付单ID', + pay_no varchar(64) NOT NULL COMMENT '支付单号', + order_id bigint unsigned NOT NULL COMMENT '订单ID', + order_no varchar(64) NOT NULL COMMENT '订单号快照', + user_id bigint unsigned NOT NULL COMMENT '支付用户ID', + shop_id bigint unsigned NULL COMMENT '店铺ID', + channel varchar(32) NOT NULL DEFAULT 'MANUAL' COMMENT '支付渠道', + status varchar(20) NOT NULL DEFAULT 'PAYING' COMMENT '状态:PAYING/PAID/CLOSED', + pay_amount decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '应支付金额', + paid_amount decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '实付金额', + pay_time datetime NULL COMMENT '支付完成时间', + expire_time datetime NULL COMMENT '支付单过期时间', + operator_id bigint unsigned NULL COMMENT '手工确认操作人ID', + remark varchar(255) NULL COMMENT '支付备注', + idempotency_key varchar(160) NULL COMMENT '支付幂等键', + raw_payload text NULL COMMENT '支付渠道原始载荷', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '订单支付单表'; + +CREATE TABLE IF NOT EXISTS t_refund_order +( + id bigint unsigned NOT NULL COMMENT '退款单ID', + refund_no varchar(64) NOT NULL COMMENT '退款单号', + after_sale_id bigint unsigned NULL COMMENT '售后单ID', + payment_order_id bigint unsigned NOT NULL COMMENT '支付单ID', + order_id bigint unsigned NOT NULL COMMENT '订单ID', + status varchar(20) NOT NULL DEFAULT 'REFUNDED' COMMENT '状态:PROCESSING/REFUNDED/FAILED', + refund_amount decimal(10, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', + refund_time datetime NULL COMMENT '退款完成时间', + channel varchar(32) NULL COMMENT '退款渠道', + external_refund_no varchar(64) NULL COMMENT '外部退款单号', + fail_reason varchar(255) NULL COMMENT '失败原因或备注', + idempotency_key varchar(160) NULL COMMENT '退款幂等键', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '订单退款单表'; + +CREATE TABLE IF NOT EXISTS t_after_sale +( + id bigint unsigned NOT NULL COMMENT '售后单ID', + after_sale_no varchar(64) NOT NULL COMMENT '售后单号', + order_id bigint unsigned NOT NULL COMMENT '订单ID', + order_item_id bigint unsigned NULL COMMENT '订单项ID', + user_id bigint unsigned NOT NULL COMMENT '申请用户ID', + shop_id bigint unsigned NULL COMMENT '店铺ID', + type varchar(32) NOT NULL COMMENT '售后类型', + reason varchar(1000) NULL COMMENT '申请原因', + refund_amount decimal(10, 2) NULL COMMENT '申请退款金额', + status varchar(20) NOT NULL DEFAULT 'PENDING' COMMENT '状态:PENDING/APPROVED/REJECTED', + audit_user_id bigint unsigned NULL COMMENT '审核人ID', + audit_reason varchar(500) NULL COMMENT '审核说明', + audit_time datetime NULL COMMENT '审核时间', + refund_order_id bigint unsigned NULL COMMENT '关联退款单ID', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '订单售后申请表'; + +CREATE TABLE IF NOT EXISTS t_after_sale_log +( + id bigint unsigned NOT NULL COMMENT '售后日志ID', + after_sale_id bigint unsigned NOT NULL COMMENT '售后单ID', + action varchar(32) NOT NULL COMMENT '动作:CREATE/APPROVE/REJECT', + from_status varchar(20) NULL COMMENT '变更前状态', + to_status varchar(20) NULL COMMENT '变更后状态', + operator_id bigint unsigned NULL COMMENT '操作人ID', + operator_type varchar(20) NULL COMMENT '操作人类型:USER/ADMIN', + remark varchar(500) NULL COMMENT '备注', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id) +) + COMMENT '订单售后流转日志表'; + +CREATE TABLE IF NOT EXISTS t_inventory_item +( + id bigint unsigned NOT NULL COMMENT '库存物料ID', + shop_id bigint unsigned NOT NULL COMMENT '店铺ID', + category varchar(50) NULL COMMENT '物料分类', + item_name varchar(100) NOT NULL COMMENT '物料名称', + unit varchar(20) NOT NULL DEFAULT '件' COMMENT '计量单位', + stock_quantity decimal(12, 3) NOT NULL DEFAULT 0.000 COMMENT '当前库存数量', + warning_threshold decimal(12, 3) NOT NULL DEFAULT 0.000 COMMENT '库存预警阈值', + status tinyint 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '门店库存物料表'; + +CREATE TABLE IF NOT EXISTS t_inventory_record +( + id bigint unsigned NOT NULL COMMENT '库存流水ID', + inventory_item_id bigint unsigned NOT NULL COMMENT '库存物料ID', + shop_id bigint unsigned NOT NULL COMMENT '店铺ID', + change_type varchar(32) NOT NULL COMMENT '变动类型:IN/OUT/ADJUST', + change_quantity decimal(12, 3) NOT NULL COMMENT '变动数量', + before_quantity decimal(12, 3) NOT NULL COMMENT '变动前数量', + after_quantity decimal(12, 3) NOT NULL COMMENT '变动后数量', + operator_id bigint unsigned NULL COMMENT '操作人ID', + remark varchar(255) NULL COMMENT '备注', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id) +) + COMMENT '门店库存变动流水表'; + +CREATE TABLE IF NOT EXISTS t_campaign +( + id bigint unsigned NOT NULL COMMENT '营销活动ID', + shop_id bigint unsigned NULL COMMENT '店铺ID,空表示平台活动', + title varchar(120) NOT NULL COMMENT '活动标题', + description varchar(1000) NULL COMMENT '活动说明', + scene varchar(32) NOT NULL DEFAULT 'HOME' COMMENT '活动场景', + product_ids text NULL COMMENT '关联作品ID集合', + coupon_ids text NULL COMMENT '关联优惠券ID集合', + status tinyint NOT NULL DEFAULT 0 COMMENT '状态:0-草稿,1-启用,2-停用', + start_time datetime NULL COMMENT '开始时间', + end_time datetime NULL COMMENT '结束时间', + sort_order int NOT NULL DEFAULT 100 COMMENT '排序', + 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 NULL COMMENT '逻辑删除:0-未删,1-已删', + PRIMARY KEY (id) +) + COMMENT '营销活动配置表'; + +CREATE TABLE IF NOT EXISTS t_user_growth_record +( + id bigint unsigned NOT NULL COMMENT '成长值记录ID', + user_id bigint unsigned NOT NULL COMMENT '用户ID', + source_type varchar(32) NOT NULL COMMENT '成长来源:REVIEW/REBOOK/ORDER', + source_id bigint unsigned NULL COMMENT '来源业务ID', + growth_value int NOT NULL DEFAULT 0 COMMENT '成长值变化', + remark varchar(255) NULL COMMENT '备注', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id) +) + COMMENT '用户成长值流水表'; + +CALL sp_add_unique_index_if_not_exists('t_artist_schedule', 'uk_t_artist_schedule_slot', '(`shop_id`, `artist_id`, `work_date`, `start_time`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_artist_schedule', 'idx_t_artist_schedule_artist_date', '(`artist_id`, `work_date`, `status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_artist_schedule', 'idx_t_artist_schedule_shop_date', '(`shop_id`, `work_date`, `status`, `is_delete`)'); +CALL sp_add_column_if_not_exists('t_appointment_reservation', 'client_request_id', 'varchar(80) NULL COMMENT ''客户端幂等请求ID'''); +CALL sp_add_unique_index_if_not_exists('t_appointment_reservation', 'uk_t_appointment_reservation_item', '(`order_item_id`, `is_delete`)'); +CALL sp_add_unique_index_if_not_exists('t_appointment_reservation', 'uk_t_appointment_reservation_client_request', '(`shop_id`, `artist_id`, `client_request_id`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_appointment_reservation', 'idx_t_appointment_reservation_artist_time', '(`artist_id`, `status`, `appointment_start_time`, `appointment_end_time`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_appointment_reservation', 'idx_t_appointment_reservation_order', '(`order_id`, `status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_appointment_reservation', 'idx_t_appointment_reservation_shop_time', '(`shop_id`, `appointment_start_time`, `status`, `is_delete`)'); +CALL sp_add_unique_index_if_not_exists('t_payment_order', 'uk_t_payment_order_pay_no', '(`pay_no`)'); +CALL sp_add_unique_index_if_not_exists('t_payment_order', 'uk_t_payment_order_idempotency', '(`idempotency_key`)'); +CALL sp_add_index_if_not_exists('t_payment_order', 'idx_t_payment_order_order_latest', '(`order_id`, `is_delete`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_payment_order', 'idx_t_payment_order_user_status', '(`user_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_payment_order', 'idx_t_payment_order_shop_status', '(`shop_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_payment_order', 'idx_t_payment_order_expire', '(`status`, `expire_time`, `is_delete`)'); +CALL sp_add_unique_index_if_not_exists('t_refund_order', 'uk_t_refund_order_refund_no', '(`refund_no`)'); +CALL sp_add_unique_index_if_not_exists('t_refund_order', 'uk_t_refund_order_idempotency', '(`idempotency_key`)'); +CALL sp_add_index_if_not_exists('t_refund_order', 'idx_t_refund_order_after_sale', '(`after_sale_id`)'); +CALL sp_add_index_if_not_exists('t_refund_order', 'idx_t_refund_order_order_status', '(`order_id`, `status`, `create_time`)'); +CALL sp_add_unique_index_if_not_exists('t_after_sale', 'uk_t_after_sale_no', '(`after_sale_no`)'); +CALL sp_add_index_if_not_exists('t_after_sale', 'idx_t_after_sale_user_status', '(`user_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_after_sale', 'idx_t_after_sale_shop_status', '(`shop_id`, `status`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_after_sale', 'idx_t_after_sale_order', '(`order_id`, `order_item_id`)'); +CALL sp_add_index_if_not_exists('t_after_sale_log', 'idx_t_after_sale_log_after_sale', '(`after_sale_id`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_after_sale_log', 'idx_t_after_sale_log_operator', '(`operator_id`, `create_time`)'); +CALL sp_add_unique_index_if_not_exists('t_inventory_item', 'uk_t_inventory_item_shop_name', '(`shop_id`, `item_name`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_inventory_item', 'idx_t_inventory_item_shop_status', '(`shop_id`, `status`, `is_delete`)'); +CALL sp_add_index_if_not_exists('t_inventory_item', 'idx_t_inventory_item_warning', '(`shop_id`, `stock_quantity`, `warning_threshold`)'); +CALL sp_add_index_if_not_exists('t_inventory_record', 'idx_t_inventory_record_item_time', '(`inventory_item_id`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_inventory_record', 'idx_t_inventory_record_shop_time', '(`shop_id`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_campaign', 'idx_t_campaign_shop_status', '(`shop_id`, `status`, `is_delete`, `sort_order`)'); +CALL sp_add_index_if_not_exists('t_campaign', 'idx_t_campaign_scene_time', '(`scene`, `status`, `start_time`, `end_time`)'); +CALL sp_add_index_if_not_exists('t_user_growth_record', 'idx_t_user_growth_user_time', '(`user_id`, `create_time`)'); +CALL sp_add_index_if_not_exists('t_user_growth_record', 'idx_t_user_growth_source', '(`source_type`, `source_id`)'); +CALL sp_add_unique_index_if_not_exists('t_user_growth_record', 'uk_t_user_growth_source_once', '(`user_id`, `source_type`, `source_id`)'); + +INSERT INTO t_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (313931273555136610, 'schedule:view', '查看排班', '排班管理', '查看美甲师排班和可约时段', NOW(), NOW(), 0), + (313931273555136611, 'schedule:edit', '编辑排班', '排班管理', '维护美甲师班次、休息日和预约容量', NOW(), NOW(), 0), + (313931273555136612, 'payment:view', '查看支付', '支付退款', '查看订单支付状态和支付单', NOW(), NOW(), 0), + (313931273555136613, 'payment:manage', '确认支付', '支付退款', '手工确认支付或处理支付异常', NOW(), NOW(), 0), + (313931273555136614, 'refund:view', '查看退款', '支付退款', '查看退款申请和退款单', NOW(), NOW(), 0), + (313931273555136615, 'refund:manage', '处理退款', '支付退款', '手工确认退款或处理退款异常', NOW(), NOW(), 0), + (313931273555136616, 'after_sale:view', '查看售后', '售后管理', '查看订单售后和客户反馈', NOW(), NOW(), 0), + (313931273555136617, 'after_sale:audit', '处理售后', '售后管理', '审批或驳回售后申请', NOW(), NOW(), 0), + (313931273555136618, 'inventory:view', '查看库存', '库存管理', '查看门店耗材库存和预警', NOW(), NOW(), 0), + (313931273555136619, 'inventory:manage', '调整库存', '库存管理', '维护耗材、入库和库存调整', NOW(), NOW(), 0), + (313931273555136620, 'campaign:view', '查看活动', '活动管理', '查看营销活动和推荐位', NOW(), NOW(), 0), + (313931273555136621, 'campaign:manage', '管理活动', '活动管理', '创建、编辑和发布营销活动', NOW(), NOW(), 0), + (313931273555136622, 'growth:view', '查看成长', '用户成长', '查看用户成长值和会员权益进度', NOW(), NOW(), 0), + (313931273555136623, 'analytics:view', '查看经营分析', '经营分析', '查看订单漏斗和经营指标', NOW(), NOW(), 0), + (313931273555136624, 'performance:view', '查看绩效', '绩效统计', '查看门店和美甲师服务绩效', NOW(), NOW(), 0), + (313931273555136625, 'workbench:view', '查看工作台', '门店工作台', '查看今日订单、售后待办、排班和绩效摘要', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + permission_name = VALUES(permission_name), + permission_group = VALUES(permission_group), + description = VALUES(description), + is_delete = 0, + update_time = NOW(); + + +CALL sp_add_column_if_not_exists('t_order', 'customer_remark', 'varchar(500) NULL COMMENT ''用户询价预约备注'''); +CALL sp_add_column_if_not_exists('t_order', 'quote_status', 'varchar(32) NULL COMMENT ''报价状态:CONFIRMING/CONFIRMED/EXPIRED'''); +CALL sp_add_column_if_not_exists('t_order', 'quote_user_confirmed', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''用户是否确认当前报价'''); +CALL sp_add_column_if_not_exists('t_order', 'quote_shop_confirmed', 'tinyint unsigned NOT NULL DEFAULT 0 COMMENT ''店长是否确认当前报价'''); +CALL sp_add_column_if_not_exists('t_order', 'quote_expire_time', 'datetime NULL COMMENT ''询价报价过期时间'''); +CALL sp_add_column_if_not_exists('t_order', 'quote_confirmed_time', 'datetime NULL COMMENT ''双方确认报价时间'''); +CALL sp_add_index_if_not_exists('t_order', 'idx_t_order_quote_status_expire', '(`quote_status`, `quote_expire_time`)'); + +CALL sp_add_column_if_not_exists('t_order_item', 'reference_price', 'decimal(10,2) NULL COMMENT ''作品参考价快照'''); +CALL sp_add_column_if_not_exists('t_order_item', 'quoted_price', 'decimal(10,2) NULL COMMENT ''当前报价'''); +CALL sp_add_column_if_not_exists('t_order_item', 'confirmed_price', 'decimal(10,2) NULL COMMENT ''最终确认价'''); + +CREATE TABLE IF NOT EXISTS t_order_quote_message +( + id bigint unsigned auto_increment comment '报价交互消息主键' + primary key, + order_id bigint unsigned not null comment '订单ID', + actor_id bigint unsigned null comment '操作人ID', + actor_type varchar(32) not null comment '操作人类型:USER/SHOP/SYSTEM', + action varchar(32) not null comment '动作:CREATE/SHOP_QUOTE/USER_REPLY/USER_CONFIRM/SHOP_CONFIRM/SYSTEM_CONFIRM/SYSTEM_EXPIRE', + remark varchar(500) null comment '备注', + items_snapshot text null comment '订单项报价快照JSON', + 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 '订单询价报价交互消息表'; + +CALL sp_add_index_if_not_exists('t_order_quote_message', 'idx_order_quote_message_order_id', '(`order_id`, `is_delete`, `create_time`)'); + +CREATE TABLE IF NOT EXISTS `t_messagehub_push_config` ( + `id` bigint unsigned NOT NULL COMMENT '主键ID', + `scene_code` varchar(80) NOT NULL COMMENT '业务场景编码', + `scene_name` varchar(100) NOT NULL COMMENT '业务场景名称', + `platform` varchar(20) NOT NULL DEFAULT 'SMS' COMMENT 'MessageHub 平台类型:SMS/EMAIL/QQ', + `msg_type` varchar(30) NOT NULL DEFAULT 'VERIFICATION' COMMENT 'MessageHub 消息类型', + `queue_name` varchar(100) NOT NULL DEFAULT 'smsQueue' COMMENT '投递到 MessageHub 的队列名', + `source_system` varchar(80) NOT NULL DEFAULT 'jayucloud-salon' COMMENT 'MessageHub 来源系统编码', + `template_code` varchar(100) NOT NULL COMMENT 'MessageHub 模板编码', + `channel_id` bigint unsigned NULL COMMENT 'MessageHub 通道ID,不填则由 MessageHub 自动选择', + `send_method` varchar(30) NULL COMMENT '兼容旧短信 DTO 的发送方式,如 SPUG/OTHER', + `enabled` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '是否启用:1-启用,0-停用', + `remark` varchar(255) NULL COMMENT '备注', + `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 NOT NULL DEFAULT 0 COMMENT '逻辑删除:0-未删除,1-已删除', + PRIMARY KEY (`id`), + UNIQUE KEY `uk_messagehub_push_config_scene` (`scene_code`), + KEY `idx_messagehub_push_config_enabled` (`enabled`, `is_delete`) +) COMMENT='JayuCloud Salon 按业务场景投递 MessageHub 的配置'; + +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; + +-- ======================================== +-- 首次部署整合区:权限种子 / xiaowang 初始化 / 默认权限分配 +-- 来源: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), + ('comment:view', '查看评论', '评论管理', '查看作品评论与回复', NOW(), NOW(), 0), + ('comment:delete', '删除评论', '评论管理', '删除不合规作品评论', 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), + ('message:view', '查看消息', '消息管理', '查看站内消息发布与通知', NOW(), NOW(), 0), + ('message:create', '发布消息', '消息管理', '发布站内消息', NOW(), NOW(), 0), + ('message:delete', '撤回消息', '消息管理', '撤回已发布站内消息', NOW(), NOW(), 0) + , + ('announcement:view', '查看公告', '公告管理', '查看公告列表与详情', NOW(), NOW(), 0), + ('announcement:create', '发布公告', '公告管理', '发布公告', NOW(), NOW(), 0), + ('announcement:delete', '撤回公告', '公告管理', '撤回已发布公告', 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_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (910000000008001, 'message:view', '查看消息', '消息管理', '查看站内消息发布与通知', NOW(), NOW(), 0), + (910000000008002, 'message:create', '发布消息', '消息管理', '发布站内消息', NOW(), NOW(), 0), + (910000000008003, 'message:delete', '撤回消息', '消息管理', '撤回已发布站内消息', 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_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (910000000008011, 'announcement:view', '查看公告', '公告管理', '查看公告列表与详情', NOW(), NOW(), 0), + (910000000008012, 'announcement:create', '发布公告', '公告管理', '发布公告', NOW(), NOW(), 0), + (910000000008013, 'announcement:delete', '撤回公告', '公告管理', '撤回已发布公告', 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_permission (permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES ('order:artist-change', '处理改派申请', '订单管理', '处理订单美甲师改派申请', 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, + '系统管理员', + 'xiaowang', + 0, + 0, + '$2a$10$3ZC5HHVK6EYIfkjcU9IHZulGXwNZhy.6LU85HRJC9A.3LHm0IEdMO', + '', + '管理员', + 4, + 0, + NOW(), + NOW(), + 0, + 1 +WHERE NOT EXISTS ( + SELECT 1 + FROM t_user + WHERE account = 'xiaowang' +); + +-- ======================================== +-- 雪花 ID 口径收口区:去掉运行时主键表的 AUTO_INCREMENT +-- 说明: +-- 1. 前面的建表与基础配置种子继续沿用旧链路,保证空库初始化过程不受影响 +-- 2. 本段将运行时已切到应用层雪花 ID 的主键表统一收口 +-- 3. 后续基础配置插入均使用显式主键,与 2026-03-30_snowflake_id_alignment_patch.sql 保持同口径 +-- ======================================== + +DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists; + +DELIMITER $$ + +CREATE PROCEDURE sp_drop_auto_increment_on_id_if_exists(IN p_table_name VARCHAR(64)) +BEGIN + DECLARE v_column_type VARCHAR(128) DEFAULT NULL; + DECLARE v_column_comment VARCHAR(255) DEFAULT NULL; + DECLARE v_extra VARCHAR(128) DEFAULT NULL; + + SELECT COLUMN_TYPE, COLUMN_COMMENT, EXTRA + INTO v_column_type, v_column_comment, v_extra + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = p_table_name + AND COLUMN_NAME = 'id' + LIMIT 1; + + IF v_column_type IS NOT NULL AND v_extra LIKE '%auto_increment%' THEN + SET @alter_sql = CONCAT( + 'ALTER TABLE `', p_table_name, '` MODIFY COLUMN `id` ', + v_column_type, + ' NOT NULL' + ); + + IF v_column_comment IS NOT NULL AND v_column_comment <> '' THEN + SET @alter_sql = CONCAT(@alter_sql, ' COMMENT ', QUOTE(v_column_comment)); + END IF; + + PREPARE stmt FROM @alter_sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + END IF; +END$$ + +DELIMITER ; + +CALL sp_drop_auto_increment_on_id_if_exists('t_balance'); +CALL sp_drop_auto_increment_on_id_if_exists('t_balance_change_detail'); +CALL sp_drop_auto_increment_on_id_if_exists('t_cart'); +CALL sp_drop_auto_increment_on_id_if_exists('t_comment'); +CALL sp_drop_auto_increment_on_id_if_exists('t_coupon'); +CALL sp_drop_auto_increment_on_id_if_exists('t_coupon_tag'); +CALL sp_drop_auto_increment_on_id_if_exists('t_effect_diagram'); +CALL sp_drop_auto_increment_on_id_if_exists('t_login_identity'); +CALL sp_drop_auto_increment_on_id_if_exists('t_wechat_mini_binding'); +CALL sp_drop_auto_increment_on_id_if_exists('t_nail_artist_info'); +CALL sp_drop_auto_increment_on_id_if_exists('t_object_storage_file'); +CALL sp_drop_auto_increment_on_id_if_exists('t_order'); +CALL sp_drop_auto_increment_on_id_if_exists('t_order_item'); +CALL sp_drop_auto_increment_on_id_if_exists('t_permission'); +CALL sp_drop_auto_increment_on_id_if_exists('t_product'); +CALL sp_drop_auto_increment_on_id_if_exists('t_product_like'); +CALL sp_drop_auto_increment_on_id_if_exists('t_shop'); +CALL sp_drop_auto_increment_on_id_if_exists('t_tag'); +CALL sp_drop_auto_increment_on_id_if_exists('t_user'); +CALL sp_drop_auto_increment_on_id_if_exists('t_user_coupon'); +CALL sp_drop_auto_increment_on_id_if_exists('t_user_permission'); +CALL sp_drop_auto_increment_on_id_if_exists('t_vip_benefit'); +CALL sp_drop_auto_increment_on_id_if_exists('t_vip_info'); +CALL sp_drop_auto_increment_on_id_if_exists('t_wishlist_group'); +CALL sp_drop_auto_increment_on_id_if_exists('t_wishlist'); +CALL sp_drop_auto_increment_on_id_if_exists('t_user_shop_binding'); +CALL sp_drop_auto_increment_on_id_if_exists('t_system_message'); +CALL sp_drop_auto_increment_on_id_if_exists('t_system_message_user_state'); +CALL sp_drop_auto_increment_on_id_if_exists('t_announcement'); +CALL sp_drop_auto_increment_on_id_if_exists('t_vip_recharge_plan'); +CALL sp_drop_auto_increment_on_id_if_exists('t_artist_schedule'); +CALL sp_drop_auto_increment_on_id_if_exists('t_appointment_reservation'); +CALL sp_drop_auto_increment_on_id_if_exists('t_payment_order'); +CALL sp_drop_auto_increment_on_id_if_exists('t_refund_order'); +CALL sp_drop_auto_increment_on_id_if_exists('t_after_sale'); +CALL sp_drop_auto_increment_on_id_if_exists('t_after_sale_log'); +CALL sp_drop_auto_increment_on_id_if_exists('t_inventory_item'); +CALL sp_drop_auto_increment_on_id_if_exists('t_inventory_record'); +CALL sp_drop_auto_increment_on_id_if_exists('t_campaign'); +CALL sp_drop_auto_increment_on_id_if_exists('t_user_growth_record'); + +DROP PROCEDURE IF EXISTS sp_drop_auto_increment_on_id_if_exists; + +CREATE TABLE IF NOT EXISTS t_shop_role ( + id BIGINT UNSIGNED NOT NULL COMMENT '店铺角色ID' PRIMARY KEY, + shop_id BIGINT UNSIGNED NOT NULL COMMENT '店铺ID', + role_name VARCHAR(100) NOT NULL COMMENT '角色名称', + creator_id BIGINT UNSIGNED NULL COMMENT '创建人', + status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态', + remark VARCHAR(255) NULL COMMENT '备注', + sort_order INT NOT NULL DEFAULT 0 COMMENT '排序', + create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP, + is_delete TINYINT UNSIGNED DEFAULT 0 COMMENT '逻辑删除' +) COMMENT='店铺自定义角色表'; + +CREATE TABLE IF NOT EXISTS t_shop_role_permission ( + id BIGINT UNSIGNED NOT NULL COMMENT '主键' PRIMARY KEY, + role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID', + permission_id BIGINT UNSIGNED NOT NULL COMMENT '权限ID', + create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP, + is_delete TINYINT UNSIGNED DEFAULT 0 +) COMMENT='店铺角色权限关联表'; + +CREATE TABLE IF NOT EXISTS t_user_shop_role ( + id BIGINT UNSIGNED NOT NULL COMMENT '主键' PRIMARY KEY, + user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID', + shop_id BIGINT UNSIGNED NOT NULL COMMENT '店铺ID', + role_id BIGINT UNSIGNED NOT NULL COMMENT '角色ID', + status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态', + create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP, + is_delete TINYINT UNSIGNED DEFAULT 0 +) COMMENT='用户店铺角色绑定表'; + +CREATE TABLE IF NOT EXISTS t_user_relation ( + id BIGINT UNSIGNED NOT NULL COMMENT '主键' PRIMARY KEY, + user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID', + shop_id BIGINT UNSIGNED NULL COMMENT '店铺ID', + parent_user_id BIGINT UNSIGNED NULL COMMENT '直属上级用户ID', + parent_role_type INT NULL COMMENT '上级身份类型', + source_type VARCHAR(32) NULL COMMENT '来源类型', + creator_id BIGINT UNSIGNED NULL COMMENT '创建人', + create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP, + is_delete TINYINT UNSIGNED DEFAULT 0 +) COMMENT='用户归属关系表'; + +CREATE TABLE IF NOT EXISTS t_user_invite_code ( + id BIGINT UNSIGNED NOT NULL COMMENT '主键' PRIMARY KEY, + invite_code VARCHAR(64) NOT NULL COMMENT '邀请码', + owner_user_id BIGINT UNSIGNED NOT NULL COMMENT '所属人', + shop_id BIGINT UNSIGNED NOT NULL COMMENT '店铺ID', + status TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态', + use_count INT NOT NULL DEFAULT 0 COMMENT '使用次数', + expire_time DATETIME NULL COMMENT '过期时间', + create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, + update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP, + is_delete TINYINT UNSIGNED DEFAULT 0, + UNIQUE KEY uk_invite_code (invite_code) +) COMMENT='邀请码表'; + + +INSERT INTO t_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (313931273555136490, 'messagehub:config:manage', '管理消息参数', '参数管理', '管理地图 Key、短信 MessageHub 等平台参数', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + permission_name = VALUES(permission_name), + permission_group = VALUES(permission_group), + description = VALUES(description), + is_delete = 0, + update_time = NOW(); + +INSERT INTO t_messagehub_push_config ( + id, + scene_code, + scene_name, + platform, + msg_type, + queue_name, + source_system, + template_code, + channel_id, + send_method, + enabled, + remark, + create_time, + update_time, + is_delete +) +VALUES + (313931273555137001, 'USER_REGISTER_SMS_CODE', '用户注册验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '注册功能发送验证码时使用', NOW(), NOW(), 0), + (313931273555137002, 'USER_LOGIN_SMS_CODE', '手机号登录验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '手机号验证码登录时使用', NOW(), NOW(), 0), + (313931273555137003, 'USER_RESET_PASSWORD_SMS_CODE', '找回密码验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '找回密码时使用', NOW(), NOW(), 0), + (313931273555137004, 'USER_BIND_PHONE_SMS_CODE', '绑定手机号验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '绑定手机号时使用', NOW(), NOW(), 0), + (313931273555137005, 'USER_CHANGE_PHONE_SMS_CODE', '修改手机号验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '修改手机号时使用', NOW(), NOW(), 0), + (313931273555137006, 'USER_CHANGE_PASSWORD_SMS_CODE', '修改密码验证码', 'SMS', 'VERIFICATION', 'smsQueue', 'jayucloud-salon', 'SPUG_SMS_VERIFY_1', NULL, 'SPUG', 1, '修改密码时使用', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + scene_name = VALUES(scene_name), + platform = VALUES(platform), + msg_type = VALUES(msg_type), + queue_name = VALUES(queue_name), + source_system = VALUES(source_system), + template_code = VALUES(template_code), + channel_id = VALUES(channel_id), + send_method = VALUES(send_method), + enabled = VALUES(enabled), + remark = VALUES(remark), + is_delete = 0, + update_time = NOW(); + +INSERT INTO t_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (313931273555136501, 'role:view', '查看店铺角色', '角色管理', '查看店铺自定义角色', NOW(), NOW(), 0), + (313931273555136502, 'role:create', '新增店铺角色', '角色管理', '创建店铺自定义角色', NOW(), NOW(), 0), + (313931273555136503, 'role:edit', '编辑店铺角色', '角色管理', '编辑角色权限和绑定员工', NOW(), NOW(), 0), + (313931273555136504, 'role:delete', '删除店铺角色', '角色管理', '删除店铺自定义角色', NOW(), NOW(), 0), + (313931273555136505, 'user:relation:view', '查看用户所属', '用户管理', '查看用户归属关系', NOW(), NOW(), 0), + (313931273555136506, 'user:relation:edit', '编辑用户所属', '用户管理', '调整用户店铺和直属上级', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + id = VALUES(id), + permission_name = VALUES(permission_name), + permission_group = VALUES(permission_group), + description = VALUES(description), + is_delete = 0, + update_time = NOW(); + +-- 2026-05-18 协议管理、用户协议同意记录与安全审计日志 +CREATE TABLE IF NOT EXISTS t_legal_agreement ( + id bigint NOT NULL COMMENT '协议ID', + agreement_type varchar(32) NOT NULL COMMENT '协议类型:USER_TERMS/PRIVACY_POLICY', + title varchar(100) NOT NULL COMMENT '协议标题', + version varchar(32) NOT NULL COMMENT '协议版本号', + content longtext NOT NULL COMMENT '协议正文', + status tinyint NOT NULL DEFAULT 0 COMMENT '状态:0草稿/停用,1已发布启用', + effective_time datetime NULL COMMENT '生效时间', + 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 NOT NULL DEFAULT 0 COMMENT '逻辑删除:0否,1是', + PRIMARY KEY (id), + UNIQUE KEY uk_legal_agreement_type_version (agreement_type, version, is_delete), + KEY idx_legal_agreement_current (agreement_type, status, effective_time, is_delete) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户协议与隐私政策'; + +CREATE TABLE IF NOT EXISTS t_user_agreement_consent ( + id bigint NOT NULL COMMENT '同意记录ID', + user_id bigint NULL COMMENT '用户ID;注销后解除身份关联,保留最小同意凭证', + agreement_type varchar(32) NOT NULL COMMENT '协议类型', + version varchar(32) NOT NULL COMMENT '同意版本号', + entry varchar(64) NULL COMMENT '同意入口:login/register/phone-login/wechat-mini', + client_type varchar(32) NULL COMMENT '客户端类型:WEB/WECHAT_MINI', + ip_address varchar(64) NULL COMMENT '同意IP', + consent_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '同意时间', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id), + UNIQUE KEY uk_user_agreement_consent (user_id, agreement_type, version), + KEY idx_user_agreement_user (user_id, agreement_type) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户协议同意记录'; + +ALTER TABLE t_user_agreement_consent MODIFY COLUMN user_id bigint NULL COMMENT '用户ID;注销后解除身份关联,保留最小同意凭证'; + +CREATE TABLE IF NOT EXISTS t_security_audit_log ( + id bigint NOT NULL COMMENT '审计日志ID', + user_id bigint NULL COMMENT '操作用户ID', + account varchar(100) NULL COMMENT '操作账号', + action varchar(64) NOT NULL COMMENT '安全动作', + result varchar(32) NOT NULL COMMENT '结果:SUCCESS/FAIL', + reason varchar(255) NULL COMMENT '失败或补充原因', + ip_address varchar(64) NULL COMMENT '请求IP', + operate_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间', + create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (id), + KEY idx_security_audit_action_time (action, operate_time), + KEY idx_security_audit_account_time (account, operate_time) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='安全审计日志'; + +CREATE TABLE IF NOT EXISTS t_system_parameter_config ( + id bigint NOT NULL COMMENT '配置ID', + config_group varchar(50) NOT NULL COMMENT '配置分组', + config_key varchar(100) NOT NULL COMMENT '配置键', + config_name varchar(100) NOT NULL COMMENT '配置名称', + config_value varchar(1000) NULL COMMENT '配置值', + enabled tinyint NOT NULL DEFAULT 1 COMMENT '是否启用:1启用,0停用', + remark varchar(255) NULL COMMENT '备注', + 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 NOT NULL DEFAULT 0 COMMENT '逻辑删除:0否,1是', + PRIMARY KEY (id), + UNIQUE KEY uk_system_parameter_group_key (config_group, config_key, is_delete) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统参数配置'; + +SET @system_parameter_enabled_missing := ( + SELECT COUNT(1) = 0 + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 't_system_parameter_config' + AND COLUMN_NAME = 'enabled' +); +SET @system_parameter_enabled_sql := IF( + @system_parameter_enabled_missing, + 'ALTER TABLE t_system_parameter_config ADD COLUMN enabled tinyint NOT NULL DEFAULT 1 COMMENT ''是否启用:1启用,0停用'' AFTER config_value', + 'SELECT 1' +); +PREPARE stmt_system_parameter_enabled FROM @system_parameter_enabled_sql; +EXECUTE stmt_system_parameter_enabled; +DEALLOCATE PREPARE stmt_system_parameter_enabled; + +INSERT INTO t_legal_agreement (id, agreement_type, title, version, content, status, effective_time, create_time, update_time, is_delete) +VALUES + (313931273555138001, 'USER_TERMS', '用户协议', '2026.10.21', '欢迎使用甲屿手纪 Salon 门店工作台。本服务供经门店授权的店长和员工管理门店日程、预约、员工和经营记录使用。登录前须阅读并明确同意本协议与隐私政策;不同意时不会交换微信登录凭证,也不能继续使用。微信 OpenID 用于识别账号,昵称和头像为可选资料,手机号绑定为可选,不绑定不影响基础工作台使用。页面中的预约金额仅为门店经营记录,不代表本平台已经收款;当前不提供充值、订阅或平台虚拟权益购买。请仅提交有权处理的业务信息,并确保上传的文字、图片具备合法来源。可在账号设置申请注销;管理员确认门店移交或归档后清理账号资料和登录身份。依法及履行必要业务所需的订单、预约和经营记录可能以匿名化形式保留,目标保留期为五年。', 1, NOW(), NOW(), NOW(), 0), + (313931273555138002, 'PRIVACY_POLICY', '隐私政策', '2026.10.21', '我们处理信息仅用于提供门店工作台:登录时由用户主动同意后调用微信登录并接收 OpenID;昵称、头像仅在用户主动补充或更新时处理;手机号仅在用户主动绑定或更换时获取,用于账号找回和必要的业务联系,不用于营销。店长和员工可能为预约及订单录入客户姓名、可选手机号和服务信息,这些是门店业务资料,不是客户小程序账号。我们不主动采集设备标识、IMEI、MAC、定位或麦克风信息;选择图片只在用户主动操作后调用。账号、客户和订单快照中的手机号使用 AES-GCM 加密保存,并用独立 HMAC 索引完成查重;常规界面、日志和导出对手机号脱敏。服务运行可能使用微信登录、短信服务商、消息通道和按环境配置的对象存储服务。用户可在账号设置申请注销或解绑手机号;注销申请经审核后撤销会话并在目标十五个工作日内删除或匿名化个人资料,订单、预约及经营凭证按产品目标保留五年并解除账号身份关联。头像、作品、评论等内容目前由本地模拟审核流程处理,结果仅用于开发验证,不代表已接入微信内容安全审核,正式上线前将另行完成实际审核平台和隐私指引配置。隐私内容上线前须由运营方核实数据处理方、时限和适用法律要求。', 1, NOW(), NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + title = VALUES(title), + content = VALUES(content), + status = VALUES(status), + effective_time = VALUES(effective_time), + is_delete = 0, + update_time = NOW(); + +INSERT INTO t_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (313931273555136601, 'legal_agreement:manage', '管理协议', '协议管理', '管理用户协议和隐私政策版本', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + permission_name = VALUES(permission_name), + permission_group = VALUES(permission_group), + description = VALUES(description), + is_delete = 0, + update_time = NOW(); + +INSERT INTO t_system_parameter_config ( + id, config_group, config_key, config_name, config_value, enabled, remark, create_time, update_time, is_delete +) +VALUES + (313931273555139000, 'MAP', 'MAP_PROVIDER', '当前地图渠道', 'TENCENT', 1, 'TENCENT 或 AMAP;决定后台店铺选点默认使用哪个地图渠道', NOW(), NOW(), 0), + (313931273555139001, 'MAP', 'TENCENT_MAP_KEY', '腾讯 Web Key', '', 0, '用于后台腾讯地图选点和 Web 展示;请在后台参数管理中填入真实 Key 后启用', NOW(), NOW(), 0), + (313931273555139002, 'MAP', 'AMAP_WEB_KEY', '高德 Web Key', '', 0, '用于后台高德地图选点和 Web 展示;Web 端 Key 与小程序 Key 分开维护', NOW(), NOW(), 0), + (313931273555139003, 'MAP', 'AMAP_MINI_KEY', '高德小程序 Key', '', 0, '用于后续小程序端高德 SDK/API 调用;不要与 Web Key 混用', NOW(), NOW(), 0), + (313931273555139004, 'USER', 'DEFAULT_PASSWORD', '后台新增用户默认密码', 'Fingertip@123', 1, '后台新增用户不填写密码时使用;需满足 8-32 位且至少包含数字、小写字母、大写字母、特殊符号中的 3 类', NOW(), NOW(), 0), + (313931273555139010, 'SMS', 'SMS_PROVIDER', '短信供应商', 'SPUG', 1, '全局切换短信供应商时同步验证码模板和 MessageHub 通道', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + config_name = VALUES(config_name), + remark = VALUES(remark), + config_value = IF(t_system_parameter_config.config_key = 'MAP_PROVIDER' AND (t_system_parameter_config.config_value IS NULL OR t_system_parameter_config.config_value = ''), VALUES(config_value), t_system_parameter_config.config_value), + is_delete = 0, + update_time = NOW(); + +-- 最小运行初始化:确保 xiaowang 拥有当前权限目录中的全部权限。 +INSERT INTO t_user_permission (id, user_id, permission_id, create_time, update_time, create_by, update_by, is_delete) +SELECT + 313931273556000000 + p.id, + 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 +WHERE u.account = 'xiaowang' + AND u.is_delete = 0 + AND u.type = 4 +ON DUPLICATE KEY UPDATE + is_delete = 0, + update_by = VALUES(update_by), + update_time = NOW(); + +INSERT INTO t_permission (id, permission_code, permission_name, permission_group, description, create_time, update_time, is_delete) +VALUES + (313931273555136602, 'security:audit:view', '查看安全审计', '安全审计', '查看高危操作和登录安全审计日志', NOW(), NOW(), 0) +ON DUPLICATE KEY UPDATE + permission_name = VALUES(permission_name), + permission_group = VALUES(permission_group), + description = VALUES(description), + is_delete = 0, + update_time = NOW(); + +-- 最终权限兜底:确保平台管理员拥有当前权限目录中的全部基础权限。 +INSERT INTO t_user_permission (id, user_id, permission_id, create_time, update_time, create_by, update_by, is_delete) +SELECT + (CAST(u.id AS UNSIGNED) << 20) + (CAST(p.id AS UNSIGNED) & 1048575), + u.id, + p.id, + NOW(), + NOW(), + 0, + 0, + 0 +FROM t_user u +INNER JOIN t_permission p + ON p.is_delete = 0 +WHERE u.account = 'xiaowang' + AND u.is_delete = 0 + AND u.type = 4 +ON DUPLICATE KEY UPDATE + is_delete = 0, + update_by = VALUES(update_by), + update_time = NOW(); + +-- 排班规则与例外 +CREATE TABLE IF NOT EXISTS t_artist_schedule_rule ( + id bigint unsigned NOT NULL COMMENT '排班规则ID', shop_id bigint unsigned NOT NULL COMMENT '店铺ID', artist_id bigint unsigned NOT NULL COMMENT '美甲师用户ID', preset_type varchar(32) NOT NULL COMMENT '预设类型', work_days varchar(32) NOT NULL COMMENT '工作星期,1-7逗号分隔', start_time time NOT NULL COMMENT '常规开始时间', end_time time NOT NULL COMMENT '常规结束时间', slot_minutes int NOT NULL DEFAULT 60 COMMENT '预约间隔分钟', capacity int NOT NULL DEFAULT 1 COMMENT '同一时间段容量', status tinyint NOT NULL DEFAULT 1 COMMENT '状态:1启用,0停用', remark varchar(255) NULL COMMENT '备注', 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 NOT NULL DEFAULT 0 COMMENT '逻辑删除', PRIMARY KEY (id), UNIQUE KEY uk_artist_schedule_rule_artist (shop_id, artist_id, is_delete), KEY idx_artist_schedule_rule_shop_status (shop_id, status, is_delete) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='美甲师常规排班规则表'; +CREATE TABLE IF NOT EXISTS t_artist_schedule_exception ( + id bigint unsigned NOT NULL COMMENT '排班例外ID', shop_id bigint unsigned NOT NULL COMMENT '店铺ID', artist_id bigint unsigned NOT NULL COMMENT '美甲师用户ID', work_date date NOT NULL COMMENT '例外日期', exception_type varchar(32) NOT NULL COMMENT '例外类型:REST/EXTRA_WORK/CHANGE_SHIFT', start_time time NULL COMMENT '例外开始时间', end_time time NULL COMMENT '例外结束时间', slot_minutes int NOT NULL DEFAULT 60 COMMENT '预约间隔分钟', capacity int NOT NULL DEFAULT 1 COMMENT '同一时间段容量', remark varchar(255) NULL COMMENT '备注', 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 NOT NULL DEFAULT 0 COMMENT '逻辑删除', PRIMARY KEY (id), UNIQUE KEY uk_artist_schedule_exception_date (shop_id, artist_id, work_date, is_delete), KEY idx_artist_schedule_exception_artist_date (artist_id, work_date, is_delete) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='美甲师排班例外表'; + +-- 店长员工审核提醒参数。 +INSERT INTO t_system_parameter_config ( + id, config_group, config_key, config_name, config_value, enabled, remark, + create_time, update_time, is_delete +) +VALUES ( + 313931273555139020, 'WORKBENCH', 'MANAGER_REGISTRATION_REVIEW_NOTICE', + '店长员工审核提醒', 'true', 1, + '控制店长登录小程序后是否显式提示当前门店的待审核员工申请', + NOW(), NOW(), 0 +) +ON DUPLICATE KEY UPDATE + config_name = VALUES(config_name), + remark = VALUES(remark), + is_delete = 0, + update_time = NOW(); + +-- 历史命名兼容修正:整合已部署库迁移所需的数据修正,空库执行时无副作用。 +START TRANSACTION; +UPDATE t_messagehub_push_config +SET source_system = 'jayucloud-salon' +WHERE source_system IN ('fingertipdesigner', 'fingertip-designer', 'FingertipDesigner'); +ALTER TABLE t_messagehub_push_config +COMMENT = 'JayuCloud Salon 按业务场景投递 MessageHub 的配置'; +COMMIT; +SET FOREIGN_KEY_CHECKS = 0; +SET FOREIGN_KEY_CHECKS = 1; diff --git a/docs/architecture/backend-design.md b/docs/architecture/backend-design.md index 90d918eb..9158f25e 100644 --- a/docs/architecture/backend-design.md +++ b/docs/architecture/backend-design.md @@ -68,6 +68,6 @@ vNext 经营能力在数据库层补齐独立运行时表,并与订单、权 ## 数据库脚本 -初始化 SQL 只放在 `docs/scripts/sql/init/`:店务端使用 `jayucloud-salon.sql`,设计师端使用 `jayucloud-artlist.sql`。 +空库 SQL 基线放在 `docs/scripts/sql/init/`:ArtList 仍使用 `jayucloud-artlist.sql`;Salon 后续环境结构由 `JC-Salon-Core/src/main/resources/db/migration/` 中的 Flyway 版本迁移管理,初始 V1 由 `jayucloud-salon.sql` 派生,不能对同一空库重复执行两种入口。 -经营表结构、索引、权限种子和雪花 ID 主键口径已经并入对应端侧初始化 SQL。空库初始化只执行目标端对应脚本。 +ArtList 空库初始化继续执行对应初始化 SQL。Salon 可选择人工执行初始化 SQL,或由 Core 通过 Flyway 应用版本迁移;同一数据库不得混用或重复执行两种入口。 diff --git a/docs/scripts/Flyway版本管理与自动迁移说明_20261022.md b/docs/scripts/Flyway版本管理与自动迁移说明_20261022.md new file mode 100644 index 00000000..51cf4e3d --- /dev/null +++ b/docs/scripts/Flyway版本管理与自动迁移说明_20261022.md @@ -0,0 +1,36 @@ +# Salon Flyway 版本管理与自动迁移说明 + +更新时间:2026-10-22 + +## 范围 + +Flyway Core 与 MySQL 数据库适配器仅接入 Salon Core,不影响 ArtList。数据库结构迁移由 Core 启动时使用当前 Nacos 数据源执行,Gitea 工作流不自行连接数据库或运行 SQL 客户端。 + +空库基线位于 `JC-Salon-Core/src/main/resources/db/migration/V1__Salon空库初始化.sql`,由冻结的 `docs/scripts/sql/init/jayucloud-salon.sql` 派生,仅移除 `CREATE DATABASE` 和 `USE`。初始化 SQL 本身不变。V1 包含完整空库结构及运行必需种子数据;不要对已经由初始化 SQL 建好的同一数据库再次运行 Flyway V1。 + +## 启动行为 + +DEV、SIT、PROD 的 Salon Nacos `core.yaml` 均启用 Flyway,并使用 `classpath:db/migration`。目标 schema 固定为 `jayucloud_salon`,关闭 Flyway 自动建库。迁移前执行校验;禁用 `clean`、自动 baseline 和乱序迁移。JDBC URL 指向的 schema 必须预先存在且为空;若 schema 非空但没有 `flyway_schema_history`,Flyway 会拒绝启动,不会自动将现有库标记为已迁移。 + +Flyway 使用 Core 的数据库账号执行 V1。该账号须具备创建/修改表、索引、初始化种子数据及创建/调用/删除初始化脚本中条件迁移存储过程所需的权限。迁移失败会令 Core 启动失败;SIT/生产部署脚本等待当前节点健康检查,因此失败时不会继续发布后续 Core 节点。 + +## Gitea 发布链路 + +- DEV 推送会自动发布 DEV Nacos 配置;当前仓库没有 DEV Core 自动部署目标,因此 V1 会在 DEV Core 下次启动时执行。 +- SIT 分支推送会先自动发布并校验 SIT Nacos,再调度 SIT 应用部署工作流。 +- PROD 分支推送会先自动发布并校验 PROD Nacos,再调度 PROD 应用部署工作流。 +- SIT/生产构建脚本验证 Core JAR 中存在 Flyway Core、MySQL 适配器和 V1 迁移资源,随后将该 JAR 随发布包部署。 +- Core 部署按服务器逐台推进:第一台 Core 启动并完成 Flyway 后通过健康检查,才启动下一台;Gateway 和 Web 在 Core 部署组完成后继续。 +- 现有自动部署工作流使用 `-SkipTests`,这是仓库已有的 Core 测试限制下的发布约定。本次不改变此门禁;本地仍应单独运行测试和编译。 + +## 后续迁移规则 + +1. 每个数据库变更新增一个递增版本文件,文件名使用中文描述,例如 `V2__账号手机号索引调整.sql`。 +2. 任何已在环境中成功执行的迁移不可编辑或删除;校验发现 checksum 不一致时应修复迁移发布流程,不得通过关闭校验掩盖问题。 +3. 迁移采用向前兼容方式。应用回退不会回滚数据库;需要数据库修正时新增后续版本迁移。 +4. 评估 SQL 锁表时间、索引构建方式、权限和多节点兼容后再晋级环境。 +5. 如果环境已含有业务表或数据,但没有 Flyway 历史记录,应停止自动启动并制定逐库核对/基线方案;不得启用自动 baseline,也不得清库或直接执行 V1。 + +## 验证边界 + +本次只修改仓库代码、配置样例和文档;没有连接 DEV、SIT、PROD 数据库或 Nacos。首次应用迁移前,维护人员需确认目标 schema 为空、已创建且数据库账号权限符合要求。 diff --git a/docs/scripts/deploy-jayucloud-salon-production.ps1 b/docs/scripts/deploy-jayucloud-salon-production.ps1 index 31fbf3f2..e9d3d3db 100644 --- a/docs/scripts/deploy-jayucloud-salon-production.ps1 +++ b/docs/scripts/deploy-jayucloud-salon-production.ps1 @@ -159,6 +159,29 @@ function Get-RelationshipId { return "" } +function Assert-FlywayMigrationPackaged { + param([string]$Path) + + if (-not (Test-Path -LiteralPath $Path)) { + throw "Salon Core 发布包不存在:$Path" + } + + Add-Type -AssemblyName System.IO.Compression.FileSystem + $zip = [System.IO.Compression.ZipFile]::OpenRead($Path) + try { + $migrationEntries = @($zip.Entries | Where-Object { $_.FullName -match '^BOOT-INF/classes/db/migration/V[0-9]+__[^/]+\.sql$' }) + $baselineEntry = $zip.GetEntry("BOOT-INF/classes/db/migration/V1__Salon空库初始化.sql") + $flywayLibrary = @($zip.Entries | Where-Object { $_.FullName -match '^BOOT-INF/lib/flyway-core-[^/]+\.jar$' }) + $mysqlLibrary = @($zip.Entries | Where-Object { $_.FullName -match '^BOOT-INF/lib/flyway-mysql-[^/]+\.jar$' }) + if (-not $baselineEntry -or $migrationEntries.Count -eq 0 -or $flywayLibrary.Count -eq 0 -or $mysqlLibrary.Count -eq 0) { + throw "Salon Core JAR 未包含 Flyway Core、MySQL 适配器和 V1 数据库基线:$Path" + } + } + finally { + $zip.Dispose() + } +} + function Get-ColumnIndex { param([string]$CellRef) @@ -594,6 +617,7 @@ function Build-ReleasePackage { throw "缺少发布产物:$requiredPath" } } + Assert-FlywayMigrationPackaged $coreJar $coreArtifact = Join-Path $backendDir "JC-Salon-Core.jar" $gatewayArtifact = Join-Path $backendDir "JC-Salon-Gateway.jar" @@ -919,6 +943,7 @@ function Get-ExistingReleasePackage { throw "发布包缺少文件:$requiredPath" } } + Assert-FlywayMigrationPackaged $coreArtifact $hashes = foreach ($artifact in @($coreArtifact, $gatewayArtifact, $frontendArtifact, $sqlArtifact)) { $hash = Get-FileHash -LiteralPath $artifact -Algorithm SHA256 [pscustomobject]@{ Path = $artifact.Replace($releaseDir, "").TrimStart("\", "/"); Sha256 = $hash.Hash } diff --git a/docs/scripts/运行与发布配置总说明.md b/docs/scripts/运行与发布配置总说明.md index 8c9ebc53..12bb085f 100644 --- a/docs/scripts/运行与发布配置总说明.md +++ b/docs/scripts/运行与发布配置总说明.md @@ -1,6 +1,6 @@ # JayuCloud 运行与发布配置总说明 -更新时间:2026-10-21 +更新时间:2026-10-22 本文是 `docs/scripts/` 下运行、配置、发布和回退规则的唯一说明入口。可执行脚本和初始化 SQL 保持独立文件,不在本文中重复粘贴。 @@ -8,6 +8,7 @@ - `sql/init/jayucloud-artlist.sql`:ArtList 空库初始化入口。 - `sql/init/jayucloud-salon.sql`:Salon 空库初始化入口,已包含历史版本脚本中的表、索引和种子数据。 +- `JC-Salon-Core/src/main/resources/db/migration/`:Salon Core Flyway 版本迁移,`V1` 是冻结初始化 SQL 的空库基线。 - `deploy-jayucloud-artlist-production.ps1`:ArtList 生产部署脚本。 - `deploy-jayucloud-salon-production.ps1`:Salon 生产部署脚本。 - `package-release.ps1`:发布包构建与归档脚本。 @@ -65,7 +66,11 @@ Nacos 发布流程应先保存目标环境快照,再发布全部 YAML,并从 缺少该挂载时,Prepare、Deploy 和 Rollback 任务之间无法共享发布记录、配置快照和应用包。 -SIT 和生产部署前确认 Java 17、systemd、Nacos、数据库、Redis、RabbitMQ、MinIO 和 Nginx 可达;SQL 只归档,不由发布工作流自动执行。回退必须使用上一成功版本的应用包和对应配置快照。 +Salon DEV/SIT/PROD 的 Nacos `core.yaml` 启用 Flyway,并固定目标 schema 为 `jayucloud_salon`、关闭自动建库。Gitea 现有 SIT/生产自动部署会构建并校验 Core JAR 中包含 V1 迁移;首个 Core 节点启动时由 Flyway 在已存在的空库中执行迁移,健康检查通过后才继续发布下一个 Core 节点。工作流不直接连接数据库或执行 SQL;迁移使用应用连接的 Nacos 数据源凭据,因此该账号必须具备建表、索引、数据初始化及初始化脚本所需存储过程的权限。Flyway 禁用自动 baseline、禁用 clean,并校验已应用迁移;schema 必须预先存在且为空,非空或迁移失败时 Core 启动失败并停止后续节点。应用回退不会回滚数据库,后续修复只能追加新版本迁移。 + +Salon 冻结初始化 SQL 继续作为人工空库初始化参考,不与 Flyway V1 同时执行。ArtList 数据库仍由原有流程管理,本次不启用 Flyway。 + +回退必须使用上一成功版本的应用包和对应配置快照;Flyway 迁移是前向兼容部署,应用回退不会撤销已执行的数据库迁移。 ## 6. MessageHub 与对象存储 diff --git a/docs/项目变更记录.md b/docs/项目变更记录.md index 13fc1490..eda0a408 100644 --- a/docs/项目变更记录.md +++ b/docs/项目变更记录.md @@ -275,3 +275,11 @@ - 新增 Salon 静态用户协议/隐私政策页面及隐私合规后台入口;手机号回填和注销审核参数、字段、索引并入 Salon 初始化 SQL。 - 新增配置说明 `docs/scripts/Salon合规整改配置说明_20261021.md`,更新 `docs/scripts/运行与发布配置总说明.md`,明确 DEV mock、SIT/PROD fail-closed 和密钥回填边界。 - Core 单元测试 25 项、小程序全部现有测试及 Salon 前端单元测试 83 项通过;Salon 前端类型检查和生产构建通过。内容审核目前仅为 mock,微信后台配置、真实 Nacos 发布和数据库执行均未进行。 + +## 2026-10-22 Salon Core Flyway 与 Gitea 自动迁移 + +- Salon Core 引入 Spring Boot 管理版本的 Flyway Core 与 MySQL 适配器,新增 V1 空库基线,派生自冻结初始化 SQL并移除建库/切库语句;原初始化 SQL保持不变。 +- DEV、SIT、PROD Salon Nacos `core.yaml` 显式启用 Flyway 校验,关闭自动 baseline、clean 和乱序迁移。 +- SIT/生产 Gitea 自动部署构建阶段校验 Core JAR 包含 V1;部署脚本逐个启动 Core 并等待健康检查,首节点完成 Flyway 迁移后才继续。 +- 工作流不直接执行 SQL或连接数据库;目标 schema 必须预建且为空,数据源账号需有 Flyway DDL 和初始化权限。应用回退不回滚数据库,只允许追加迁移前向修复。 +- 新增 `docs/scripts/Flyway版本管理与自动迁移说明_20261022.md`。本次仅更新仓库代码与配置,不连接实际数据库或 Nacos。 diff --git a/nacos/namespaces/dev/jayucloud-salon/DEFAULT_GROUP/core.yaml b/nacos/namespaces/dev/jayucloud-salon/DEFAULT_GROUP/core.yaml index e49b2d6a..9136efc5 100644 --- a/nacos/namespaces/dev/jayucloud-salon/DEFAULT_GROUP/core.yaml +++ b/nacos/namespaces/dev/jayucloud-salon/DEFAULT_GROUP/core.yaml @@ -1,6 +1,17 @@ spring: datasource: url: jdbc:mysql://192.168.5.15:3306/jayucloud_salon?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai + flyway: + enabled: true + locations: classpath:db/migration + fail-on-missing-locations: true + schemas: [jayucloud_salon] + default-schema: jayucloud_salon + create-schemas: false + baseline-on-migrate: false + validate-on-migrate: true + clean-disabled: true + out-of-order: false servlet: multipart: max-file-size: 5MB diff --git a/nacos/namespaces/prod/jayucloud-salon/DEFAULT_GROUP/core.yaml b/nacos/namespaces/prod/jayucloud-salon/DEFAULT_GROUP/core.yaml index fc3044df..d141130b 100644 --- a/nacos/namespaces/prod/jayucloud-salon/DEFAULT_GROUP/core.yaml +++ b/nacos/namespaces/prod/jayucloud-salon/DEFAULT_GROUP/core.yaml @@ -1,6 +1,17 @@ spring: datasource: url: jdbc:mysql://192.168.10.200:6033/jayucloud_salon?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai + flyway: + enabled: true + locations: classpath:db/migration + fail-on-missing-locations: true + schemas: [jayucloud_salon] + default-schema: jayucloud_salon + create-schemas: false + baseline-on-migrate: false + validate-on-migrate: true + clean-disabled: true + out-of-order: false servlet: multipart: max-file-size: 5MB diff --git a/nacos/namespaces/sit/jayucloud-salon/DEFAULT_GROUP/core.yaml b/nacos/namespaces/sit/jayucloud-salon/DEFAULT_GROUP/core.yaml index 9c09b18b..91803d1b 100644 --- a/nacos/namespaces/sit/jayucloud-salon/DEFAULT_GROUP/core.yaml +++ b/nacos/namespaces/sit/jayucloud-salon/DEFAULT_GROUP/core.yaml @@ -1,6 +1,17 @@ spring: datasource: url: jdbc:mysql://192.168.10.11:3306/jayucloud_salon?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai + flyway: + enabled: true + locations: classpath:db/migration + fail-on-missing-locations: true + schemas: [jayucloud_salon] + default-schema: jayucloud_salon + create-schemas: false + baseline-on-migrate: false + validate-on-migrate: true + clean-disabled: true + out-of-order: false servlet: multipart: max-file-size: 5MB