This commit is contained in:
zhengchiliu
2026-09-15 00:53:04 +08:00
parent 32a0b87e9e
commit 112c5f8e46
42 changed files with 1121 additions and 304 deletions
+20
View File
@@ -6,6 +6,7 @@ import (
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/global"
"git.lzcwy.cn/zhengchiliu/zt-closet-backend/internal/mysqlmodel"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// UserDao 用户数据访问
@@ -56,6 +57,25 @@ func (d *UserDao) VIPGrantIdempotent(ctx context.Context, userID int64, now int6
defer span.End()
return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var before mysqlmodel.User
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", userID).First(&before).Error; err != nil {
return err
}
// 购买开通/续费使用稳定的订单级幂等判断,不让重试后的 change_type 变化绕过唯一键。
if flow.ChangeType == 1 || flow.ChangeType == 2 {
var count int64
if err := tx.Model(&mysqlmodel.VIPFlowLog{}).Where("order_no = ? AND change_type IN ?", flow.OrderNo, []int8{1, 2}).Count(&count).Error; err != nil {
return err
}
if count > 0 {
return nil
}
flow.ChangeType = 1
if before.VIPType != 1 && before.VIPExpire > now {
flow.ChangeType = 2
}
flow.OldVIPType, flow.OldVIPExpire = before.VIPType, before.VIPExpire
}
sql := `UPDATE users SET
vip_start = CASE WHEN vip_type = 1 OR vip_expire <= ? THEN ? ELSE vip_start END,
vip_expire = (CASE WHEN vip_expire > ? THEN vip_expire ELSE ? END) + ?