Files
biz-api-cloud-zax/db/20260722_add_hard_appt_cycle_rule.sql
T
2026-07-21 21:04:18 +08:00

100 lines
4.8 KiB
SQL

-- 可配置周期预约规则。上一版 reserve_mode 已进入测试环境,因此本脚本只做增量变更。
CREATE TABLE hard_appt_cycle_rule
(
id BIGINT(19) auto_increment comment '编号'
primary key,
rule_name VARCHAR(64) not null comment '规则名称',
mch_id BIGINT(19) not null comment '商户ID',
open_weekday TINYINT not null comment '开放星期(1周一至7周日)',
open_time TIME not null comment '开放时间',
close_weekday TINYINT not null comment '截止星期(1周一至7周日)',
close_time TIME not null comment '截止时间',
target_start_offset_days SMALLINT not null comment '截止日期后第几天开始可预约',
target_day_count SMALLINT not null comment '连续可预约天数',
status VARCHAR(32) not null comment '状态',
sort INT default 0 not null comment '排序',
remark VARCHAR(256) null comment '备注',
create_by VARCHAR(64) null comment '创建人',
create_time DATETIME null comment '创建时间',
update_by VARCHAR(64) null comment '修改人',
update_time DATETIME null comment '修改时间',
del_flag CHAR default '0' null comment '删除标记',
tenant_id BIGINT(19) null comment '租户ID',
dept_id BIGINT(19) null comment '部门ID',
index idx_hard_appt_cycle_rule_mch_status (tenant_id, mch_id, status, del_flag)
) comment '周期预约规则';
CREATE TABLE hard_appt_cycle_rule_item
(
id BIGINT(19) auto_increment comment '编号'
primary key,
rule_id BIGINT(19) not null comment '周期预约规则ID',
mch_id BIGINT(19) not null comment '商户ID',
target_day_offset SMALLINT not null comment '相对目标起始日偏移(从0开始)',
hour_type VARCHAR(32) not null comment '餐端字典值(daily_menu)',
create_by VARCHAR(64) null comment '创建人',
create_time DATETIME null comment '创建时间',
update_by VARCHAR(64) null comment '修改人',
update_time DATETIME null comment '修改时间',
del_flag CHAR default '0' null comment '删除标记',
tenant_id BIGINT(19) null comment '租户ID',
index idx_hard_appt_cycle_item_rule (tenant_id, rule_id, del_flag),
index idx_hard_appt_cycle_item_hour (tenant_id, mch_id, hour_type, del_flag)
) comment '周期预约目标日餐段明细';
ALTER TABLE hard_order
ADD COLUMN appt_rule_id BIGINT(19) NULL COMMENT '周期预约规则ID' AFTER appt_id,
ADD INDEX idx_hard_order_appt_rule (tenant_id, appt_rule_id, del_flag);
-- 将测试环境中上一版固定周期规则转换为每个商户一条默认可配置规则。
INSERT INTO hard_appt_cycle_rule
(rule_name, mch_id, open_weekday, open_time, close_weekday, close_time,
target_start_offset_days, target_day_count, status, sort, remark,
create_time, del_flag, tenant_id, dept_id)
SELECT '默认周期预约规则',
a.mch_id,
5,
'17:00:00',
7,
'22:00:00',
2,
7,
CASE WHEN SUM(CASE WHEN a.status = '1' THEN 1 ELSE 0 END) > 0 THEN '1' ELSE '0' END,
0,
'由固定周期预约规则自动迁移',
NOW(),
'0',
a.tenant_id,
MAX(a.dept_id)
FROM hard_appt a
WHERE a.reserve_mode = 'WEEKLY_CYCLE'
AND a.del_flag = '0'
GROUP BY a.tenant_id, a.mch_id;
INSERT INTO hard_appt_cycle_rule_item
(rule_id, mch_id, target_day_offset, hour_type, create_time, del_flag, tenant_id)
SELECT r.id,
a.mch_id,
offsets.target_day_offset,
a.hour_type,
NOW(),
'0',
a.tenant_id
FROM hard_appt_cycle_rule r
INNER JOIN hard_appt a
ON r.mch_id = a.mch_id
AND r.tenant_id <=> a.tenant_id
AND a.reserve_mode = 'WEEKLY_CYCLE'
AND a.hour_type IS NOT NULL
AND a.del_flag = '0'
CROSS JOIN (SELECT 0 AS target_day_offset
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6) offsets
WHERE r.rule_name = '默认周期预约规则'
AND r.remark = '由固定周期预约规则自动迁移'
AND r.del_flag = '0';