feat(player): 优化宠物购买功能的数据查询逻辑

- 修改BuyPet方法中的数据库查询方式,使用事务内的模型查询替代直接服务调用
- 添加了更安全的数据库事务处理机制,确保购买操作的数据一致性
- 重构了宠物信息验证逻辑,提升代码可读性和维护性
```
This commit is contained in:
昔念
2026-03-13 22:20:54 +08:00
parent 499a0ba5ab
commit 001c86b724

View File

@@ -82,29 +82,31 @@ 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