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

feat(service): 宠物添加功能增加销售计数参数并优化价格更新逻辑

- 修改PetAdd方法签名,增加salecount参数用于追踪宠物销售次数
- 在多个控制器中统一调用PetAdd方法时传入0作为初始销售次数
- 临时禁用寒流枪活动中的宠物发放功能
- 优化UPdatePrice方法,添加错误处理和价格范围验证逻辑
- 调整宠物购买逻辑,使用免费金币系统并计算递增购买
This commit is contained in:
昔念
2026-03-11 12:19:13 +08:00
parent 46bc05ab29
commit a29a8ddec2
17 changed files with 95 additions and 35 deletions

View File

@@ -168,3 +168,22 @@ func (c *BaseSysUserController) GoldAdd(ctx context.Context, req *UserGoldAddReq
res = cool.Ok(nil)
return
}
type DuihuanGoldAddReq struct {
g.Meta `path:"/duihuan" method:"POST"`
Authorization string `json:"Authorization" in:"header"`
Gold float32 `json:"gold"`
}
func (c *BaseSysUserController) DuihuanGold(ctx context.Context, req *DuihuanGoldAddReq) (res *cool.BaseRes, err error) {
t := cool.GetAdmin(ctx)
if service.NewBaseSysUserService().GetGold(t.UserId) < int64(req.Gold*100) {
res = cool.Fail("余额不足")
return
}
service.NewBaseSysUserService().DuihuanFreeGold(uint32(t.UserId), int64(req.Gold*100))
res = cool.Ok(nil)
return
}

View File

@@ -20,7 +20,8 @@ type BaseSysUser struct {
Email *string `gorm:"column:email;type:varchar(255)" json:"email"` // 邮箱
Status *int32 `gorm:"column:status;not null;default:1" json:"status"` // 状态 0:禁用 1启用
GoldBean int64 `gorm:"column:goldbean;type:bigint;default:0" json:"goldbean"`
Remark *string `gorm:"column:remark;type:varchar(255)" json:"remark"` // 备注
FreeGold int64 `gorm:"column:free_gold;type:bigint;default:0" json:"free_gold"` //集市金豆
Remark *string `gorm:"column:remark;type:varchar(255)" json:"remark"` // 备注
//Debug int32 `gorm:"column:debug;type:int;not null;default:0" json:"debug"` // 是否可以进入2服 测试服
Maxts uint32 `gorm:"column:max_ts;type:int;not null;default:0" json:"max_ts"` //最后生成的时间记录表
}

View File

@@ -9,7 +9,6 @@ import (
"blazing/modules/base/model"
"github.com/alpacahq/alpacadecimal"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/crypto/gmd5"
@@ -52,6 +51,20 @@ func (s *BaseSysUserService) SetdepartmentId(userId, departmentId uint32) (res *
return
}
func (s *BaseSysUserService) DuihuanFreeGold(userId uint32, free int64) {
m := cool.DBM(s.Model).Where("id", userId)
m.Data(g.Map{
"goldbean": gdb.Raw("goldbean-" + gconv.String(free)),
"free_gold": gdb.Raw("free_gold+" + gconv.String(free)),
}).Update()
}
func (s *BaseSysUserService) UpdateFreeGold(userId uint32, gold int64) {
m := cool.DBM(s.Model).Where("id", userId)
m.Increment("free_gold", gold)
}
// 单位是分
func (s *BaseSysUserService) UpdateGold(userId uint32, gold int64) {
@@ -67,10 +80,16 @@ func (s *BaseSysUserService) UpdateGold(userId uint32, gold int64) {
func (s *BaseSysUserService) GetGold(userId uint) (res int64) {
var res1 model.BaseSysUser
m := cool.DBM(s.Model)
m.Where("id", userId).FieldsEx("password").Scan(&res1)
m.Where("id", userId).Fields("goldbean").Scan(&res1)
r1 := alpacadecimal.NewFromInt(res1.GoldBean)
return r1.IntPart()
return res1.GoldBean
}
func (s *BaseSysUserService) GetFreeGold(userId uint) (res int64) {
var res1 model.BaseSysUser
m := cool.DBM(s.Model)
m.Where("id", userId).Fields("free_gold").Scan(&res1)
return res1.GoldBean
}
func (s *BaseSysUserService) GetEamil(userId string) (res *model.BaseSysUser) {
m := cool.DBM(s.Model)

View File

@@ -49,7 +49,7 @@ func (c *PetBagController) GetSession(ctx context.Context, req *PetGetReq) (res
req.PetTypeId, req.IndividualValue, req.NatureId, req.AbilityTypeEnum, req.Level, shiny, -1)
t.CatchRect = 1 //代表这是人工合成的
service.NewUserService(uint32(req.UserID)).Pet.PetAdd(t)
service.NewUserService(uint32(req.UserID)).Pet.PetAdd(t, 0)
return
}
@@ -98,7 +98,7 @@ func (c *PetBagController) ModPrise(ctx context.Context, req *PriseReq) (res *co
if req.Price < 5 {
req.Price = 5
}
service.NewPetService(uint32(admin.UserId)).UPdatePrice(req.Ctime, req.Price, req.IsSale)
err = service.NewPetService(uint32(admin.UserId)).UPdatePrice(req.Ctime, req.Price, req.IsSale)
return

View File

@@ -30,6 +30,7 @@ type Pet struct {
CatchTime uint32 `gorm:"not null;comment:'捕捉时间'" json:"catch_time"` //唯一键
IsSale int `gorm:"not null;default:0;comment:'是否出售'" json:"is_sale"`
SalePrice uint32 `gorm:"not null;default:0;comment:'出售价格'" json:"sale_price"`
SaleCount uint32 `gorm:"not null;default:0;comment:'出售次数'" json:"sale_count"`
// Owner uint32 `struc:"skip"` //仅作为存储
// FreedTime uint32 `struc:"skip"` //放生时间
//是否可交易这里应该定义在精灵ID里

View File

@@ -19,10 +19,7 @@ func (s *ItemService) Exist(itemid uint32) bool {
func (s *ItemService) Get(min, max uint32) []model.Item {
var ttt []model.Item
s.dbm(s.Model).Where(g.Map{
"item_id <=": max,
"item_id >=": min,
}).Where("item_cnt >", 0).Scan(&ttt)
s.dbm(s.Model).WhereBetween("item_id", min, max).Where("item_cnt >", 0).Scan(&ttt)
return ttt

View File

@@ -7,6 +7,7 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
@@ -46,9 +47,30 @@ func (s *PetService) UPdateFree(ctime uint32, free uint32) {
).Update()
}
func (s *PetService) UPdatePrice(ctime uint32, Price uint32, is_sale uint32) {
s.dbm(s.Model).Where("catch_time", ctime).Data("sale_price", Price, "is_sale", is_sale).Update()
func (s *PetService) UPdatePrice(ctime uint32, Price uint32, is_sale uint32) error {
res0, err := s.dbm(s.Model).
Where("catch_time", ctime). // 限定 ctime避免全表更新
Where("sale_price = ?", 0). // 只筛选 sale_price=0 的记录
Data(g.Map{
"sale_price": Price,
"is_sale": is_sale,
}).Update()
if err != nil {
return fmt.Errorf("修改 sale_price=0 的记录失败:%w", err)
}
affected0, _ := res0.RowsAffected()
if affected0 == 0 {
priceUpper := Price * 110 / 100 // 上限 = Price * 1.1(整数运算避免浮点误差)
priceLower := Price * 90 / 100 // 下限 = Price * 0.9
res, _ := s.dbm(s.Model).Where("catch_time", ctime).WhereBetween("sale_price", priceLower, priceUpper).Data("sale_price", Price, "is_sale", is_sale).Update()
t, _ := res.RowsAffected()
if t < 0 {
return fmt.Errorf("修改失败")
}
}
return nil
}
func (s *PetService) BuyPet(pid uint32) error {
@@ -72,15 +94,16 @@ func (s *PetService) BuyPet(pid uint32) error {
if !tt.UpdateTime.AddDate(0, 0, 1).Before(gtime.Now()) {
return fmt.Errorf("数据异常")
}
useglod := int64(tt.SalePrice)*102 + int64(tt.SaleCount)*5
if service.NewBaseSysUserService().GetGold(uint(s.userid)) < int64(tt.SalePrice)*102 {
if service.NewBaseSysUserService().GetFreeGold(uint(s.userid)) < useglod {
return fmt.Errorf("余额不足")
}
service.NewBaseSysUserService().UpdateGold(s.userid, -int64(tt.SalePrice)*102)
service.NewBaseSysUserService().UpdateFreeGold(s.userid, -useglod)
NewPetService(tt.PlayerID).Pet_del(tt.CatchTime)
service.NewBaseSysUserService().UpdateGold(tt.PlayerID, int64(tt.SalePrice)*98)
s.PetAdd(&tt.Data)
service.NewBaseSysUserService().UpdateFreeGold(tt.PlayerID, int64(tt.SalePrice)*98)
s.PetAdd(&tt.Data, tt.SaleCount+1)
return nil
@@ -160,7 +183,7 @@ func (s *PetService) Pet_LEVEL_all() []model.Pet {
}
// 精灵真正添加后的捕捉时间才是真正的时间
func (s *PetService) PetAdd(y *model.PetInfo) uint32 {
func (s *PetService) PetAdd(y *model.PetInfo, salecount uint32) uint32 {
if y == nil {
return 0
}
@@ -184,6 +207,7 @@ RETURNING max_ts;
player.Data = *y
player.CatchTime = y.CatchTime
player.Free = 0
player.SaleCount = salecount
player.IsVip = cool.Config.ServerInfo.IsVip
_, err := m1.Insert(player)