```
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

@@ -98,6 +98,11 @@ func (c *Controller) Delete(ctx context.Context, req *DeleteReq) (res *BaseRes,
if err != nil {
return Fail(err.Error()), err
}
t, _ := data.RowsAffected()
if t == 0 {
return Fail("not found"), err
}
c.Service.ModifyAfter(ctx, "Delete", g.RequestFromCtx(ctx).GetMap())
return Ok(data), err
}
@@ -115,6 +120,10 @@ func (c *Controller) Update(ctx context.Context, req *UpdateReq) (res *BaseRes,
if err != nil {
return Fail(err.Error()), err
}
t, _ := data.RowsAffected()
if t == 0 {
return Fail("not found"), err
}
c.Service.ModifyAfter(ctx, "Update", g.RequestFromCtx(ctx).GetMap())
return Ok(data), err
}

View File

@@ -2,6 +2,7 @@ package cool
import (
"context"
"database/sql"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/database/gdb"
@@ -11,20 +12,21 @@ import (
)
type IService interface {
ServiceAdd(ctx context.Context, req *AddReq) (data interface{}, err error) // 新增
ServiceDelete(ctx context.Context, req *DeleteReq) (data interface{}, err error) // 删除
ServiceUpdate(ctx context.Context, req *UpdateReq) (data interface{}, err error) // 修改
ServiceInfo(ctx context.Context, req *InfoReq) (data interface{}, err error) // 详情
ServiceList(ctx context.Context, req *ListReq) (data interface{}, err error) // 列表
ServicePage(ctx context.Context, req *PageReq) (data interface{}, err error) // 分页
ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改前的操作
ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改后的操作
GetModel() IModel // 获取model
ServiceAdd(ctx context.Context, req *AddReq) (data interface{}, err error) // 新增
ServiceDelete(ctx context.Context, req *DeleteReq) (data sql.Result, err error) // 删除
ServiceUpdate(ctx context.Context, req *UpdateReq) (data sql.Result, err error) // 修改
ServiceInfo(ctx context.Context, req *InfoReq) (data interface{}, err error) // 详情
ServiceList(ctx context.Context, req *ListReq) (data interface{}, err error) // 列表
ServicePage(ctx context.Context, req *PageReq) (data interface{}, err error) // 分页
ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改前的操作
ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) // 新增|删除|修改后的操作
GetModel() IModel // 获取model
}
type Service struct {
Model IModel
ListQueryOp *QueryOp
PageQueryOp *QueryOp
Where func(ctx context.Context) []g.Array // 删除修改定义条件
InsertParam func(ctx context.Context) g.MapStrAny // Add时插入参数
Before func(ctx context.Context) (err error) // CRUD前的操作
InfoIgnoreProperty string // Info时忽略的字段,多个字段用逗号隔开
@@ -104,16 +106,31 @@ func (s *Service) ServiceAdd(ctx context.Context, req *AddReq) (data interface{}
}
// ServiceDelete 删除
func (s *Service) ServiceDelete(ctx context.Context, req *DeleteReq) (data interface{}, err error) {
func (s *Service) ServiceDelete(ctx context.Context, req *DeleteReq) (data sql.Result, err error) {
ids := g.RequestFromCtx(ctx).Get("ids").Slice()
m := g.DB(s.Model.GroupName()).Model(s.Model.TableName())
if s.Where != nil {
where := s.Where(ctx)
if len(where) > 0 {
for _, v := range where {
if len(v) == 3 {
if gconv.Bool(v[2]) {
m.Where(v[0], v[1])
}
}
if len(v) == 2 {
m.Where(v[0], v[1])
}
}
}
}
data, err = m.WhereIn("id", ids).Delete()
return
}
// ServiceUpdate 修改
func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data interface{}, err error) {
func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data sql.Result, err error) {
r := g.RequestFromCtx(ctx)
rmap := r.GetMap()
if rmap["id"] == nil {
@@ -136,6 +153,21 @@ func (s *Service) ServiceUpdate(ctx context.Context, req *UpdateReq) (data inter
}
m := DBM(s.Model)
rmap["updateTime"] = nil
if s.Where != nil {
where := s.Where(ctx)
if len(where) > 0 {
for _, v := range where {
if len(v) == 3 {
if gconv.Bool(v[2]) {
m.Where(v[0], v[1])
}
}
if len(v) == 2 {
m.Where(v[0], v[1])
}
}
}
}
//rmap["id"] = nil
_, err = m.Data(rmap).Where("id", rmap["id"]).Update()

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"},
},