```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

feat(pet): 添加宠物交易功能

- 新增ModPrise接口用于修改宠物售价
- 新增BuyPet接口用于购买宠物
- 修改Pet模型中SalePrice字段类型为float32
- 实现宠物购买逻辑,包括价格验证、余额检查和交易处理
- 更新查询条件以支持宠物交易状态筛选
```
This commit is contained in:
昔念
2026-03-10 22:20:36 +08:00
parent 69350bb79e
commit 6792e0e79a
3 changed files with 75 additions and 11 deletions

View File

@@ -84,3 +84,34 @@ type PetLevelRes struct {
//ID
ID int `json:"id"`
}
type PriseReq struct {
g.Meta `path:"/modpirse" method:"POST"`
Ctime uint32 `json:"catch_time"`
Price float32 `json:"sale_price"`
}
func (c *PetBagController) ModPrise(ctx context.Context, req *PriseReq) (res *cool.BaseRes, err error) {
admin := cool.GetAdmin(ctx)
res = &cool.BaseRes{}
service.NewPetService(uint32(admin.UserId)).UPdatePrice(req.Ctime, req.Price)
return
}
type BuyPetReq struct {
g.Meta `path:"/buy" method:"POST"`
Cid uint32 `json:"id"`
}
func (c *PetBagController) BuyPet(ctx context.Context, req *BuyPetReq) (res *cool.BaseRes, err error) {
admin := cool.GetAdmin(ctx)
res = &cool.BaseRes{}
service.NewPetService(uint32(admin.UserId)).BuyPet(req.Cid)
return
}

View File

@@ -25,11 +25,11 @@ const TableNamePet = "player_pet"
// Pet mapped from table <pet>
type Pet struct {
Base
PlayerID uint32 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
Free int `gorm:"not null;default:0;comment:'是否放生'" json:"free"` //"0为放入仓库1为放入背包
CatchTime uint32 `gorm:"not null;comment:'捕捉时间'" json:"catch_time"` //唯一键
IsSale int `gorm:"not null;default:0;comment:'是否出售'" json:"is_sale"`
SalePrice int `gorm:"not null;default:0;comment:'出售价格'" json:"sale_price"`
PlayerID uint32 `gorm:"not null;index:idx_pet_by_player_id;comment:'所属玩家ID'" json:"player_id"`
Free int `gorm:"not null;default:0;comment:'是否放生'" json:"free"` //"0为放入仓库1为放入背包
CatchTime uint32 `gorm:"not null;comment:'捕捉时间'" json:"catch_time"` //唯一键
IsSale int `gorm:"not null;default:0;comment:'是否出售'" json:"is_sale"`
SalePrice float32 `gorm:"not null;default:0;comment:'出售价格'" json:"sale_price"`
// Owner uint32 `struc:"skip"` //仅作为存储
// FreedTime uint32 `struc:"skip"` //放生时间
//是否可交易这里应该定义在精灵ID里

View File

@@ -43,6 +43,39 @@ func (s *PetService) UPdateFree(ctime uint32, free uint32) {
"free", free,
).Update()
}
func (s *PetService) UPdatePrice(ctime uint32, Price float32) {
s.dbm(s.Model).Where("catch_time", ctime).Data("sale_price", Price).Update()
}
func (s *PetService) BuyPet(pid uint32) error {
var tt *model.Pet
s.dbm(s.Model).Where("id", pid).Scan(&tt)
if tt == nil {
return fmt.Errorf("没有此精灵")
}
if tt.IsVip != 0 {
return fmt.Errorf("不允许交易")
}
if tt.IsSale == 0 {
return fmt.Errorf("未上架")
}
if tt.SalePrice == 0 {
return fmt.Errorf("未设置价格")
}
if service.NewBaseSysUserService().GetGold(uint(s.userid)) < int64(tt.SalePrice)*100 {
return fmt.Errorf("余额不足")
}
service.NewBaseSysUserService().UpdateGold(tt.PlayerID, int64(tt.SalePrice)*100)
service.NewBaseSysUserService().UpdateGold(s.userid, -int64(tt.SalePrice)*100)
s.PetAdd(&tt.Data)
s.dbm(s.Model).Where("id", pid).Delete() //删除旧精灵
return nil
}
func (s *PetService) UPdate(t model.PetInfo) {
@@ -168,14 +201,14 @@ func NewPetService(userid uint32) *PetService {
Service: &cool.Service{
Model: model.NewPet(),
PageQueryOp: &cool.QueryOp{
FieldEQ: []string{"player_id", "free"},
FieldEQ: []string{"player_id", "free", "is_sale"},
Where: func(ctx context.Context) [][]interface{} {
var (
admin = cool.GetAdmin(ctx)
userId = admin.UserId
)
// var (
// admin = cool.GetAdmin(ctx)
// // userId = admin.UserId
// )
return [][]interface{}{
{"player_id", userId, true},
// {"player_id", userId, true},
{"free", 1, true},
{"is_vip", 0, true},
}