feat(pet): 完善宠物购买功能的验证逻辑 - 添加宠物信息获取和多重验证检查,包括VIP状态、上架状态、存在性等 - 增强数据库事务操作的错误处理机制 - 优化用户余额扣减和宠物删除的事务安全性 - 修复原代码中查询逻辑的位置错误问题 ```
This commit is contained in:
@@ -87,44 +87,46 @@ func (s *PetService) UPdatePrice(ctime uint32, Price uint32, is_sale uint32) err
|
||||
return nil
|
||||
}
|
||||
func (s *PetService) BuyPet(pid uint32) error {
|
||||
tt := NewPetService(0).PetInfo_One_ID(pid)
|
||||
|
||||
if tt == nil {
|
||||
return fmt.Errorf("没有此精灵")
|
||||
}
|
||||
if tt.IsVip != 0 {
|
||||
return fmt.Errorf("不允许交易")
|
||||
}
|
||||
if tt.IsSale == 0 {
|
||||
return fmt.Errorf("未上架")
|
||||
}
|
||||
if tt.Free == 0 {
|
||||
return fmt.Errorf("没有此精灵")
|
||||
}
|
||||
if tt.SalePrice == 0 {
|
||||
return fmt.Errorf("未设置价格")
|
||||
}
|
||||
if !tt.UpdateTime.AddDate(0, 0, 1).Before(gtime.Now()) {
|
||||
return fmt.Errorf("数据异常")
|
||||
}
|
||||
return g.DB().Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
|
||||
|
||||
m := tx.Model(s.Model).Where("is_vip", 0).Where("id", pid)
|
||||
var tt *model.Pet
|
||||
|
||||
m.Scan(&tt)
|
||||
if tt == nil {
|
||||
return fmt.Errorf("没有此精灵")
|
||||
}
|
||||
if tt.IsVip != 0 {
|
||||
return fmt.Errorf("不允许交易")
|
||||
}
|
||||
if tt.IsSale == 0 {
|
||||
return fmt.Errorf("未上架")
|
||||
}
|
||||
if tt.Free == 0 {
|
||||
return fmt.Errorf("没有此精灵")
|
||||
}
|
||||
if tt.SalePrice == 0 {
|
||||
return fmt.Errorf("未设置价格")
|
||||
}
|
||||
if !tt.UpdateTime.AddDate(0, 0, 1).Before(gtime.Now()) {
|
||||
return fmt.Errorf("数据异常")
|
||||
}
|
||||
useglod := int64(tt.SalePrice)*102 + int64(tt.SaleCount)*5
|
||||
var res1 basemodel.BaseSysUser
|
||||
|
||||
tx.Model(basemodel.BaseSysUser{}).Where("id", s.userid).Fields("free_gold").Scan(&res1)
|
||||
if res1.FreeGold < useglod {
|
||||
return fmt.Errorf("余额不足")
|
||||
}
|
||||
_, err := tx.Model(basemodel.BaseSysUser{}).Where("id", s.userid).Increment("free_gold", -useglod)
|
||||
err := tx.Model(basemodel.BaseSysUser{}).Where("id", s.userid).Fields("free_gold").Scan(&res1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res1.FreeGold < useglod {
|
||||
return fmt.Errorf("余额不足")
|
||||
}
|
||||
_, err = tx.Model(basemodel.BaseSysUser{}).Where("id", s.userid).Increment("free_gold", -useglod)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Model(s.Model).Where("catch_time", tt.CatchTime).Delete()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx.Model(s.Model).Where("catch_time", tt.CatchTime).Delete()
|
||||
|
||||
_, err = tx.Model(basemodel.BaseSysUser{}).Where("id", tt.PlayerID).Increment("free_gold", int64(tt.SalePrice)*98)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user