对接
This commit is contained in:
@@ -228,23 +228,13 @@ func (r *repo) Send(ctx context.Context, userID, roomID, giftID uint, quantity i
|
||||
if err := tx.Where("id = ? AND enabled = ?", giftID, true).First(&gift).Error; err != nil {
|
||||
return bizgift.ErrSendNotAllowed
|
||||
}
|
||||
allowedQuantity := false
|
||||
for _, tier := range decode(gift.Tiers) {
|
||||
if tier == quantity {
|
||||
allowedQuantity = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !allowedQuantity {
|
||||
return bizgift.ErrSendNotAllowed
|
||||
}
|
||||
wallet := ledger.WalletPO{UserID: userID}
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("user_id = ?", userID).FirstOrCreate(&wallet).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
total := gift.Price * int64(quantity)
|
||||
if wallet.DiamondBalance < total {
|
||||
return bizgift.ErrInsufficientBalance
|
||||
return &bizgift.InsufficientBalanceError{Required: total, Balance: wallet.DiamondBalance, Missing: total - wallet.DiamondBalance}
|
||||
}
|
||||
rate := int64(10)
|
||||
var setting ledger.SettingPO
|
||||
@@ -292,18 +282,18 @@ func mapError(err error) error {
|
||||
return err
|
||||
}
|
||||
func giftOrder(v string) string {
|
||||
m := map[string]string{"sort_desc": "sort DESC", "name_asc": "name ASC", "name_desc": "name DESC", "price_asc": "price ASC", "price_desc": "price DESC", "updated_at_asc": "updated_at ASC", "updated_at_desc": "updated_at DESC"}
|
||||
m := map[string]string{"sort_asc": "sort ASC", "sort_desc": "sort DESC", "name_asc": "name ASC", "name_desc": "name DESC", "price_asc": "price ASC", "price_desc": "price DESC", "updated_at_asc": "updated_at ASC", "updated_at_desc": "updated_at DESC", "created_at_asc": "created_at ASC", "created_at_desc": "created_at DESC"}
|
||||
if x := m[strings.ToLower(v)]; x != "" {
|
||||
return x
|
||||
}
|
||||
return "sort ASC,created_at DESC"
|
||||
return "sort ASC,created_at DESC,id ASC"
|
||||
}
|
||||
func sendOrder(v string) string {
|
||||
m := map[string]string{"created_at_asc": "created_at ASC", "price_asc": "gift_price ASC", "price_desc": "gift_price DESC", "quantity_asc": "quantity ASC", "quantity_desc": "quantity DESC", "total_asc": "diamond_total ASC", "total_desc": "diamond_total DESC"}
|
||||
m := map[string]string{"created_at_asc": "live_gift_sends.created_at ASC", "created_at_desc": "live_gift_sends.created_at DESC", "price_asc": "live_gift_sends.gift_price ASC", "price_desc": "live_gift_sends.gift_price DESC", "quantity_asc": "live_gift_sends.quantity ASC", "quantity_desc": "live_gift_sends.quantity DESC", "total_asc": "live_gift_sends.diamond_total ASC", "total_desc": "live_gift_sends.diamond_total DESC"}
|
||||
if x := m[strings.ToLower(v)]; x != "" {
|
||||
return x
|
||||
}
|
||||
return "created_at DESC"
|
||||
return "live_gift_sends.created_at DESC,live_gift_sends.id DESC"
|
||||
}
|
||||
func newNumber(prefix string) string {
|
||||
return fmt.Sprintf("%s%s%06d", prefix, time.Now().Format("20060102150405"), time.Now().UnixNano()%1000000)
|
||||
|
||||
@@ -67,12 +67,12 @@ func TestGiftRepoSendUsesTierAndDebitsAtomically(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = repo.Send(context.Background(), 1, 7, item.ID, 2); !errors.Is(err, bizgift.ErrSendNotAllowed) {
|
||||
t.Fatalf("unsupported quantity error = %v", err)
|
||||
if _, err = repo.Send(context.Background(), 1, 7, item.ID, 2); err != nil {
|
||||
t.Fatalf("custom quantity should be accepted: %v", err)
|
||||
}
|
||||
var before ledger.WalletPO
|
||||
if err = db.Where("user_id = ?", 1).First(&before).Error; err != nil || before.DiamondBalance != 100 {
|
||||
t.Fatalf("wallet changed after rejected send: wallet=%+v err=%v", before, err)
|
||||
if err = db.Where("user_id = ?", 1).First(&before).Error; err != nil || before.DiamondBalance != 90 {
|
||||
t.Fatalf("wallet did not debit custom quantity: wallet=%+v err=%v", before, err)
|
||||
}
|
||||
if _, err = repo.Send(context.Background(), 1, 7, item.ID, 10); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -81,7 +81,38 @@ func TestGiftRepoSendUsesTierAndDebitsAtomically(t *testing.T) {
|
||||
var sends int64
|
||||
db.Where("user_id = ?", 1).First(&after)
|
||||
db.Model(&ledger.GiftSendPO{}).Count(&sends)
|
||||
if after.DiamondBalance != 50 || after.TotalSpentDiamonds != 50 || sends != 1 {
|
||||
if after.DiamondBalance != 40 || after.TotalSpentDiamonds != 60 || sends != 2 {
|
||||
t.Fatalf("atomic send result wallet=%+v sends=%d", after, sends)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedDefaultCatalogAddsMissingGiftsWithoutOverwriting(t *testing.T) {
|
||||
db := openGiftTestDB(t)
|
||||
custom := giftPO{Name: "玫瑰", Icon: "custom", Price: 99, Description: "admin configured", Tiers: "[1]", Sort: 88, Enabled: false}
|
||||
if err := db.Create(&custom).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Model(&giftPO{}).Where("id = ?", custom.ID).Update("enabled", false).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := seedDefaultCatalog(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := seedDefaultCatalog(db); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&giftPO{}).Count(&count).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 24 {
|
||||
t.Fatalf("gift count = %d, want 24", count)
|
||||
}
|
||||
var rose giftPO
|
||||
if err := db.Where("name = ?", "玫瑰").First(&rose).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rose.Icon != "custom" || rose.Price != 99 || rose.Sort != 88 || rose.Enabled {
|
||||
t.Fatalf("existing gift was overwritten: %+v", rose)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,5 +19,59 @@ func Migrations() []migration.Step {
|
||||
return nil
|
||||
}
|
||||
return db.Create(&[]giftPO{{Name: "玫瑰", Icon: "🌹", Price: 1, Description: "一朵玫瑰,为主播送上心意", Tiers: "[1,10,66,1314]", Sort: 1, Enabled: true}, {Name: "星星", Icon: "⭐", Price: 10, Description: "点亮一颗星,助力主播上榜", Tiers: "[1,10,66,1314]", Sort: 2, Enabled: true}, {Name: "跑车", Icon: "🚗", Price: 66, Description: "豪车进场,全场焦点", Tiers: "[1,10,66,1314]", Sort: 3, Enabled: true}, {Name: "火箭", Icon: "🚀", Price: 520, Description: "火箭升空,点燃直播间", Tiers: "[1,10,66,520]", Sort: 4, Enabled: true}}).Error
|
||||
}}}
|
||||
}}, {ID: "202609220001_live_gift_catalog_seed", Migrate: seedDefaultCatalog}}
|
||||
}
|
||||
|
||||
// seedDefaultCatalog is intentionally additive. Existing installations may
|
||||
// have edited names, prices, icons, tiers, sort, or enabled state in the
|
||||
// administration UI; migration must never overwrite those choices. We only
|
||||
// insert a missing named gift so the client can rely on the six quick gifts
|
||||
// and the 24-item "more gifts" grid being available after an upgrade.
|
||||
func seedDefaultCatalog(db *gorm.DB) error {
|
||||
defaults := []giftPO{
|
||||
{Name: "玫瑰", Icon: "🌹", Price: 1, Description: "一朵玫瑰,为主播送上心意", Tiers: "[1,10,66,1314]", Sort: 1, Enabled: true},
|
||||
{Name: "星星", Icon: "⭐", Price: 10, Description: "点亮一颗星,助力主播上榜", Tiers: "[1,10,66,1314]", Sort: 2, Enabled: true},
|
||||
{Name: "跑车", Icon: "🚗", Price: 66, Description: "豪车进场,全场焦点", Tiers: "[1,10,66,1314]", Sort: 3, Enabled: true},
|
||||
{Name: "火箭", Icon: "🚀", Price: 520, Description: "火箭升空,点燃直播间", Tiers: "[1,10,66,520]", Sort: 4, Enabled: true},
|
||||
{Name: "城堡", Icon: "🏰", Price: 1314, Description: "城堡降临,点亮直播间", Tiers: "[1,10,66,1314]", Sort: 5, Enabled: true},
|
||||
{Name: "皇冠", Icon: "👑", Price: 5200, Description: "皇冠加冕,彰显特别支持", Tiers: "[1,10,66,1314]", Sort: 6, Enabled: true},
|
||||
{Name: "爱心", Icon: "❤️", Price: 5, Description: "把喜欢送到主播身边", Tiers: "[1,10,66,1314]", Sort: 7, Enabled: true},
|
||||
{Name: "棒棒糖", Icon: "🍭", Price: 8, Description: "甜甜的支持送给你", Tiers: "[1,10,66,1314]", Sort: 8, Enabled: true},
|
||||
{Name: "咖啡", Icon: "☕", Price: 15, Description: "为主播续上一杯咖啡", Tiers: "[1,10,66,1314]", Sort: 9, Enabled: true},
|
||||
{Name: "蛋糕", Icon: "🍰", Price: 20, Description: "分享一份甜蜜祝福", Tiers: "[1,10,66,1314]", Sort: 10, Enabled: true},
|
||||
{Name: "气球", Icon: "🎈", Price: 30, Description: "让快乐在直播间起飞", Tiers: "[1,10,66,1314]", Sort: 11, Enabled: true},
|
||||
{Name: "钻戒", Icon: "💍", Price: 88, Description: "闪耀的心意送给你", Tiers: "[1,10,66,1314]", Sort: 12, Enabled: true},
|
||||
{Name: "麦克风", Icon: "🎤", Price: 99, Description: "为精彩歌声喝彩", Tiers: "[1,10,66,1314]", Sort: 13, Enabled: true},
|
||||
{Name: "吉他", Icon: "🎸", Price: 120, Description: "伴着旋律一起摇摆", Tiers: "[1,10,66,1314]", Sort: 14, Enabled: true},
|
||||
{Name: "花束", Icon: "💐", Price: 166, Description: "把鲜花和掌声送给主播", Tiers: "[1,10,66,1314]", Sort: 15, Enabled: true},
|
||||
{Name: "甜甜圈", Icon: "🍩", Price: 188, Description: "甜甜圈住每一份快乐", Tiers: "[1,10,66,1314]", Sort: 16, Enabled: true},
|
||||
{Name: "烟花", Icon: "🎆", Price: 233, Description: "烟花为精彩时刻喝彩", Tiers: "[1,10,66,1314]", Sort: 17, Enabled: true},
|
||||
{Name: "豪华游艇", Icon: "🛥️", Price: 520, Description: "乘风破浪驶入直播间", Tiers: "[1,10,66,520]", Sort: 18, Enabled: true},
|
||||
{Name: "直升机", Icon: "🚁", Price: 666, Description: "高调登场,气氛拉满", Tiers: "[1,10,66,666]", Sort: 19, Enabled: true},
|
||||
{Name: "热气球", Icon: "🎈", Price: 888, Description: "带着祝福飞向天空", Tiers: "[1,10,66,888]", Sort: 20, Enabled: true},
|
||||
{Name: "月亮", Icon: "🌙", Price: 999, Description: "月光陪伴每一个夜晚", Tiers: "[1,10,66,999]", Sort: 21, Enabled: true},
|
||||
{Name: "太阳", Icon: "☀️", Price: 1024, Description: "把热烈的能量送给主播", Tiers: "[1,10,66,1024]", Sort: 22, Enabled: true},
|
||||
{Name: "彩虹", Icon: "🌈", Price: 1888, Description: "愿每一次相遇都如彩虹", Tiers: "[1,10,66,1314]", Sort: 23, Enabled: true},
|
||||
{Name: "宝箱", Icon: "🎁", Price: 2888, Description: "打开宝箱收获惊喜", Tiers: "[1,10,66,1314]", Sort: 24, Enabled: true},
|
||||
}
|
||||
for _, item := range defaults {
|
||||
var existing giftPO
|
||||
err := db.Unscoped().Where("name = ?", item.Name).First(&existing).Error
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
if err := db.Create(&item).Error; err != nil {
|
||||
// A concurrent migration/admin write may win the unique race. The
|
||||
// named gift is present then, so leave its configuration untouched.
|
||||
var raced giftPO
|
||||
if db.Unscoped().Where("name = ?", item.Name).First(&raced).Error == nil {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user