From 94e970a020e1e0b5d0f4565fe98b827053e6ae85 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Thu, 23 Feb 2023 16:56:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=B0=83=E6=95=B4=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/bag_m/user_bag.go | 62 +++++++++++++++++++++++++++++++++- route/router.go | 3 +- route/user_r/inner.go | 4 +-- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/domain/model/bag_m/user_bag.go b/domain/model/bag_m/user_bag.go index 543c006..5e55955 100644 --- a/domain/model/bag_m/user_bag.go +++ b/domain/model/bag_m/user_bag.go @@ -3,6 +3,7 @@ package bag_m import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" + "gorm.io/gorm/clause" "hilo-user/_const/enum/res_e" "time" ) @@ -10,12 +11,25 @@ import ( type UserBag struct { mysql.Entity UserId mysql.ID - ResType mysql.Type + ResType mysql.Type // 资源类型 1:礼物 ResId mysql.ID Count mysql.Num EndTime time.Time } +type UserBagDetail struct { + mysql.Entity + UserId mysql.ID + BagId mysql.ID + ResType mysql.Type // 资源类型 1:礼物 + ResId mysql.ID + Count mysql.Num + AddReduce mysql.AddReduce + BefNum mysql.Num + AftNum mysql.Num + Remark mysql.Str +} + // 获取用户有效的背包 // param userId 用户id // param resType 背包类型 1:礼物 @@ -44,3 +58,49 @@ func GetUserBag(model *domain.Model, bagId mysql.ID) (*UserBag, error) { } return res, nil } + +// 增加用户背包 +// param userId 用户id +// param resType 道具类型 +// param resId 道具id +// param count 增加数量 +// param day 增加天数 +// condition: +// 0.事务操作 +// 1.背包表 +// 2.明细表 +func AddUserBag(model *domain.Model, userId mysql.ID, resType mysql.Type, resId mysql.ID, count mysql.Num, day int, reason string) error { + return model.Transaction(func(model *domain.Model) error { + // 1.背包表 + endTime := time.Now().AddDate(0, 0, day) + userBag := &UserBag{ + UserId: userId, + ResType: resType, + ResId: resId, + Count: count, + EndTime: endTime, + } + if err := model.DB().Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "user_id"}, {Name: "res_type"}, {Name: "res_id"}, {Name: "end_time"}}, + DoUpdates: clause.AssignmentColumns([]string{"count"}), + }).Create(userBag).Error; err != nil { + return err + } + // 2.明细表 + userBagDetail := &UserBagDetail{ + UserId: userId, + BagId: userBag.ID, + ResType: resType, + ResId: resId, + Count: count, + AddReduce: mysql.ADD, + BefNum: 0, // 加背包的统一为0 + AftNum: count, // 加背包统一为count,因为每次加几乎不可能是一样的 + Remark: reason, + } + if err := model.DB().Create(userBagDetail).Error; err != nil { + return err + } + return nil + }) +} diff --git a/route/router.go b/route/router.go index 092b91a..8d0465a 100755 --- a/route/router.go +++ b/route/router.go @@ -26,7 +26,8 @@ func InitRouter() *gin.Engine { innerUser := inner.Group("/user") { innerUser.GET("/levels", wrapper(user_r.MGetUserLevels)) - innerUser.GET("/bag", wrapper(user_r.GetUserBag)) + innerUser.GET("/bag/id", wrapper(user_r.GetUserBagId)) + //innerUser.POST("/bag/send", wrapper(user_r.SendUserBag)) } return r } diff --git a/route/user_r/inner.go b/route/user_r/inner.go index 1ad7bb7..aaad537 100644 --- a/route/user_r/inner.go +++ b/route/user_r/inner.go @@ -55,8 +55,8 @@ type GetUserBagReq struct { // @Summary 获取单个背包 // @Param bagId query int true "背包id" // @Success 200 {object} user_cv.UserBag -// @Router /inner/user/bag [get] -func GetUserBag(c *gin.Context) (*mycontext.MyContext, error) { +// @Router /inner/user/bag/id [get] +func GetUserBagId(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) var model = domain.CreateModelContext(myCtx) var req GetUserBagReq -- 2.22.0