diff --git a/domain/model/bag_m/user_bag.go b/domain/model/bag_m/user_bag.go index 543c0062c089621deb8c5ff721c6e4095766141f..5e5595558a98f53e2b85a61a11333c4802335408 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 092b91aedadb050013d328bac2037272a541d682..8d0465a404fcc0a551923a8b2d5367ca2beacaa5 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 1ad7bb73d6b445c52e611e83ed23772b6cc056b6..aaad537b61c904bf049788e3ac7837a67aae6e28 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