admin/sql/20260730_customer_price_tier_key.sql
zzh 206dc3ef3c feat: ??????????????????????????
?????????????????????????????????? SQL?

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-30 15:16:55 +08:00

47 lines
1.9 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 价区键迁到客户:与会员等级解耦
-- 新注册默认价区键/会员等级均为「普通会员」;计价时空键回落「普通客户」
-- 1) 客户表增加价区键
ALTER TABLE `t_customer`
ADD COLUMN `PriceTierKey` VARCHAR(64) NULL COMMENT '价区键(卖价 CustomerGroupValues JSON 键)' AFTER `MemberLevelId`;
-- 2) 从原会员等级价区键回填(依赖本脚本执行前 t_member_level 仍有 PriceTierKey)
UPDATE `t_customer` c
INNER JOIN `t_member_level` ml ON ml.`Id` = c.`MemberLevelId` AND ml.`IsDeleted` = 0
SET c.`PriceTierKey` = NULLIF(TRIM(ml.`PriceTierKey`), '')
WHERE c.`IsDeleted` = 0
AND (c.`PriceTierKey` IS NULL OR TRIM(c.`PriceTierKey`) = '')
AND ml.`PriceTierKey` IS NOT NULL
AND TRIM(ml.`PriceTierKey`) <> '';
-- 3) 仍为空的主客户:默认「普通会员」(与新注册一致)
UPDATE `t_customer`
SET `PriceTierKey` = '普通会员'
WHERE `IsDeleted` = 0
AND (`ParentCustomerId` IS NULL OR `ParentCustomerId` = 0)
AND (`PriceTierKey` IS NULL OR TRIM(`PriceTierKey`) = '');
-- 4) 子客户跟随主客户价区
UPDATE `t_customer` sub
INNER JOIN `t_customer` main ON main.`Id` = sub.`ParentCustomerId` AND main.`IsDeleted` = 0
SET sub.`PriceTierKey` = main.`PriceTierKey`
WHERE sub.`IsDeleted` = 0
AND sub.`ParentCustomerId` IS NOT NULL
AND sub.`ParentCustomerId` <> 0;
-- 5) 删除会员等级上的价区键列(须在插入默认等级前执行,避免 NOT NULL 约束)
ALTER TABLE `t_member_level` DROP COLUMN `PriceTierKey`;
-- 6) 确保存在启用的「普通会员」等级(供新注册默认挂靠)
INSERT INTO `t_member_level` (
`LevelCode`, `LevelName`, `MinPoints`, `ExtraDiscountRate`, `Sort`,
`Status`, `IsDeleted`, `CreateTime`
)
SELECT '普通会员', '普通会员', 0, 0, 0, 2, 0, NOW()
FROM DUAL
WHERE NOT EXISTS (
SELECT 1 FROM `t_member_level`
WHERE `IsDeleted` = 0
AND (`LevelName` = '普通会员' OR `LevelCode` = '普通会员')
);