``fix(pet): 修复宠物添加时捕捉时间冲突问题,添加唯一键约束和自增重试逻辑``

This commit is contained in:
1
2025-10-17 19:23:38 +00:00
parent 7b2a22dde5
commit 6090580560
2 changed files with 16 additions and 9 deletions

View File

@@ -11,8 +11,8 @@ const TableNamePet = "pet"
type Pet struct {
*cool.Model
PlayerID uint32 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
InBag int `gorm:"not null;comment:'是否在背包中'" json:"in_bag"` //"0为放入仓库1为放入背包
CatchTime uint32 `gorm:"not null;comment:'捕捉时间'" json:"catch_time"`
InBag int `gorm:"not null;comment:'是否在背包中'" json:"in_bag"` //"0为放入仓库1为放入背包
CatchTime uint32 `gorm:"not null;unique;comment:'捕捉时间'" json:"catch_time"` //唯一键
// Owner uint32 `struc:"skip"` //仅作为存储
// FreedTime uint32 `struc:"skip"` //放生时间
//是否可交易这里应该定义在精灵ID里

View File

@@ -34,14 +34,21 @@ func (s *UserService) PetInfo_One(cachetime uint32) model.PetEX {
}
func (s *UserService) PetAdd(y model.PetInfo) {
m1 := cool.DBM(s.pet.Model).Where("player_id", s.userid)
var player model.PetEX
player.PlayerID = s.userid
player.Data = y
player.CatchTime = y.CatchTime
player.InBag = 0
for {
m1 := cool.DBM(s.pet.Model).Where("player_id", s.userid)
var player model.PetEX
player.PlayerID = s.userid
player.Data = y
player.CatchTime = y.CatchTime
player.InBag = 0
m1.Insert(player)
_, err := m1.Insert(player)
if err == nil {
break
}
y.CatchTime += 1//自增保持时间排序
}
}