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

feat(cool): 添加删除和更新操作的数据影响行数检查

- 在Controller的Delete方法中添加RowsAffected检查,当影响行数为0时返回"not found"错误
- 在Controller的Update方法中添加RowsAffected检查,当影响行数为0时返回"not found"错误
- 修改Service接口定义,将ServiceDelete和ServiceUpdate方法的返回值类型从interface{}改为sql
This commit is contained in:
昔念
2026-03-19 17:18:32 +08:00
parent 24bc74fc87
commit 9cc29eec35
6 changed files with 118 additions and 18 deletions

View File

@@ -184,7 +184,7 @@ func (c *BaseSysUserController) DuihuanGold(ctx context.Context, req *DuihuanGol
return
}
service.NewBaseSysUserService().DuihuanFreeGold(uint32(t.UserId), int64(req.Gold*100))
service.NewBaseSysUserService().DuihuanFreeGold(uint32(t.UserId), int64(req.Gold*100), int64(req.Gold*100))
res = cool.Ok(nil)
return
}

View File

@@ -2,6 +2,7 @@ package service
import (
"context"
"database/sql"
"fmt"
"strings"
@@ -51,14 +52,15 @@ func (s *BaseSysUserService) SetdepartmentId(userId, departmentId uint32) (res *
return
}
func (s *BaseSysUserService) DuihuanFreeGold(userId uint32, free int64) {
func (s *BaseSysUserService) DuihuanFreeGold(userId uint32, gold, free int64) {
m := cool.DBM(s.Model).Where("id", userId)
m.Data(g.Map{
"goldbean": gdb.Raw("goldbean-" + gconv.String(free)),
"goldbean": gdb.Raw("goldbean-" + gconv.String(gold)),
"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)
@@ -235,7 +237,7 @@ func (s *BaseSysUserService) ServiceInfo(ctx g.Ctx, req *cool.InfoReq) (data int
}
// ServiceUpdate 方法 更新用户信息
func (s *BaseSysUserService) ServiceUpdate(ctx context.Context, req *cool.UpdateReq) (data interface{}, err error) {
func (s *BaseSysUserService) ServiceUpdate(ctx context.Context, req *cool.UpdateReq) (data sql.Result, err error) {
var (
admin = cool.GetAdmin(ctx)
m = cool.DBM(s.Model)

View File

@@ -2,7 +2,14 @@ package admin
import (
"blazing/cool"
base "blazing/modules/base/service"
"blazing/modules/player/service"
"context"
"fmt"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
type GoldListController struct {
@@ -20,3 +27,33 @@ func init() {
// 注册路由
cool.RegisterController(task_info_controller)
}
type DuihuanGoldAddReq struct {
g.Meta `path:"/duihuan" method:"POST"`
Authorization string `json:"Authorization" in:"header"`
ID uint32 `json:"id"`
}
func (c *GoldListController) DuihuanGold(ctx context.Context, req *DuihuanGoldAddReq) (res *cool.BaseRes, err error) {
t := cool.GetAdmin(ctx)
r, err := service.NewGoldListService(0).Done(req.ID)
if err != nil {
return
}
all := r.Rate * float64(r.ExchangeNum) * 100
if base.NewBaseSysUserService().GetFreeGold(t.UserId) < int64(all*105) {
err = fmt.Errorf("余额不足")
return
}
if !r.UpdateTime.Add(1 * time.Hour).Before(gtime.Now()) {
err = fmt.Errorf("未到购买时间")
return
}
base.NewBaseSysUserService().UpdateGold(uint32(r.PlayerID), int64(all*95))
base.NewBaseSysUserService().UpdateFreeGold(uint32(t.UserId), -int64(all*105))
res = cool.Ok(nil)
return
}

View File

@@ -16,10 +16,11 @@ type GoldListService struct {
func (s *GoldListService) ModifyBefore(ctx context.Context, method string, param map[string]interface{}) (err error) {
admin := cool.GetAdmin(ctx)
userId := admin.UserId
s.userid = uint32(userId)
param["player_id"] = userId
g.RequestFromCtx(ctx).SetParam("player_id", userId)
if method == "Add" {
t, _ := s.dbm_fix(s.Model).Count()
t, _ := s.dbm_fix(s.Model).Where("player_id", userId).Where("status", 0).Count()
if t > 0 {
return fmt.Errorf("不允许多挂单")
}
@@ -27,6 +28,19 @@ func (s *GoldListService) ModifyBefore(ctx context.Context, method string, param
return
}
func (s *GoldListService) Done(listid uint32) (*model.GoldBeanOrder, error) {
var rr model.GoldBeanOrder
s.dbm_fix(s.Model).Where("id", listid).Scan(&rr)
r, err := s.dbm_fix(s.Model).Data("status", 1).Where("status", 0).Where("id", listid).Update()
if err != nil {
return nil, err
}
is, _ := r.RowsAffected()
if is == 0 {
return nil, fmt.Errorf("重复订单")
}
return &rr, nil
}
func NewGoldListService(id uint32) *GoldListService {
return &GoldListService{
@@ -34,6 +48,12 @@ func NewGoldListService(id uint32) *GoldListService {
BaseService: BaseService{userid: id,
Service: &cool.Service{Model: model.NewGoldBeanOrder(),
Where: func(ctx context.Context) []g.Array {
admin := cool.GetAdmin(ctx)
return [][]interface{}{
{"player_id", admin.UserId, true},
}
},
ListQueryOp: &cool.QueryOp{
AddOrderby: g.MapStrStr{"updateTime": "asc"},
},