From c1bc2b781f58f636f0a2f8a2362e3662743afd82 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Fri, 17 Feb 2023 18:16:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 +- cv/user_cv/level.go | 11 ++++ domain/model/noble_m/userNoble.go | 95 ------------------------------ domain/model/user_m/matchCharm.go | 60 +++---------------- domain/model/user_m/matchWealth.go | 58 +++--------------- route/router.go | 6 ++ route/user_r/inner.go | 45 ++++++++++++++ 7 files changed, 81 insertions(+), 198 deletions(-) create mode 100644 cv/user_cv/level.go delete mode 100755 domain/model/noble_m/userNoble.go create mode 100644 route/user_r/inner.go diff --git a/Makefile b/Makefile index 9f34b23..d20f3a2 100755 --- a/Makefile +++ b/Makefile @@ -1,4 +1,2 @@ swag: - swag init -proto: - protoc --go_out=./ --go-grpc_out=. ./protocol/*.proto \ No newline at end of file + swag init \ No newline at end of file diff --git a/cv/user_cv/level.go b/cv/user_cv/level.go new file mode 100644 index 0000000..af1001c --- /dev/null +++ b/cv/user_cv/level.go @@ -0,0 +1,11 @@ +package user_cv + +import "git.hilo.cn/hilo-common/resource/mysql" + +type MGetUserLevelData map[mysql.ID]CvUserLevel + +type CvUserLevel struct { + UserId mysql.ID `json:"userId"` // 用户id + WealthUserGrade uint32 `json:"wealthUserGrade"` //财富等级 + CharmUserGrade uint32 `json:"charmUserGrade"` //魅力等级 +} diff --git a/domain/model/noble_m/userNoble.go b/domain/model/noble_m/userNoble.go deleted file mode 100755 index 6956a99..0000000 --- a/domain/model/noble_m/userNoble.go +++ /dev/null @@ -1,95 +0,0 @@ -package noble_m - -import ( - "gorm.io/gorm" - "hilo-user/resource/mysql" - "time" -) - -type UserNoble struct { - mysql.Entity - UserId uint64 - Level uint16 - EndTime time.Time -} - -func (ub *UserNoble) Create(db *gorm.DB) error { - return db.Create(ub).Error -} - -func (ub *UserNoble) UpdateEndTime(db *gorm.DB, endTime time.Time) (int64, error) { - r := db.Model(&UserNoble{}).Where(ub).Update("end_time", endTime) - return r.RowsAffected, r.Error -} - -// 查询用户未过期的贵族 -func (ub *UserNoble) Find(db *gorm.DB) ([]UserNoble, error) { - rows := make([]UserNoble, 0) - if err := db.Where(ub).Where("end_time>=NOW()").Order("level DESC").Find(&rows).Error; err != nil { - return nil, err - } - return rows, nil -} - -// 查询用户所有的贵族(包括已过期) -func (ub *UserNoble) FindAll(db *gorm.DB) ([]UserNoble, error) { - rows := make([]UserNoble, 0) - if err := db.Where(ub).Order("level DESC").Find(&rows).Error; err != nil { - return nil, err - } - return rows, nil -} - -func RemoveNoble(db *gorm.DB, userId uint64, level uint16) error { - ub := UserNoble{ - UserId: userId, - Level: level, - } - return ub.Delete(db) -} - -func (ub *UserNoble) Delete(db *gorm.DB) error { - return db.Where(ub).Delete(&UserNoble{}).Error -} - -// 查询用户未过期的贵族 -func (ub *UserNoble) batchGet(db *gorm.DB, userIds []uint64) (map[uint64][]UserNoble, error) { - rows := make([]UserNoble, 0) - if err := db.Model(ub).Where("end_time>=NOW() AND user_id IN ?", userIds).Order("level DESC").Find(&rows).Error; err != nil { - return nil, err - } - result := make(map[uint64][]UserNoble, 0) - for _, i := range rows { - if _, ok := result[i.UserId]; !ok { - result[i.UserId] = make([]UserNoble, 0) - } - result[i.UserId] = append(result[i.UserId], i) - } - return result, nil -} - -func FindActiveNoble(db *gorm.DB, userId uint64) (*UserNoble, error) { - ub := UserNoble{ - UserId: userId, - } - records, err := ub.Find(db) - if err != nil { - return nil, err - } - if len(records) <= 0 { - return nil, nil - } - return &records[0], nil -} - -func GetNobleLevel(db *gorm.DB, userId uint64) (uint16, error) { - noble, err := FindActiveNoble(db, userId) - if err != nil { - return 0, err - } - if noble == nil { - return 0, nil - } else { - return noble.Level, nil - } -} diff --git a/domain/model/user_m/matchCharm.go b/domain/model/user_m/matchCharm.go index c4789ab..f5b982c 100755 --- a/domain/model/user_m/matchCharm.go +++ b/domain/model/user_m/matchCharm.go @@ -3,9 +3,6 @@ package user_m import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" - "gorm.io/gorm" - "hilo-user/_const/enum/match_e" - "hilo-user/myerr" ) /** @@ -19,54 +16,15 @@ type MatchCharmUserScore struct { Grade mysql.Num } -//获取魅力等级 -func GetCharmGrade(model *domain.Model, userId mysql.ID) (uint32, uint32, error) { - var charmUserScore MatchCharmUserScore - if err := model.Db.Model(&MatchCharmUserScore{}).Where(&MatchCharmUserScore{ - UserId: userId, - }).First(&charmUserScore).Error; err != nil { - if err == gorm.ErrRecordNotFound { - return 0, 0, nil - } else { - return 0, 0, myerr.WrapErr(err) - } +// 批量获取魅力等级 +func MGetCharmGrade(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]mysql.Num, error) { + res := make(map[mysql.ID]mysql.Num) + var charmUserScore []*MatchCharmUserScore + if err := model.Db.Model(&MatchCharmUserScore{}).Where("user_id in ?", userIds).Find(&charmUserScore).Error; err != nil { + return res, err } - return charmUserScore.Grade, charmUserScore.Score, nil -} - -type MatchCharmUserScoreDetail struct { - mysql.Entity - MatchCharmUserScoreId mysql.ID - UserId mysql.ID - BeforeScore mysql.Num - Score mysql.Num - AfterScore mysql.Num - Type match_e.MatchCharmUserScoreDetailType - OrginId mysql.ID -} - -func addMatchCharmUserScoreDetail(model *domain.Model, matchCharmUserScoreId mysql.ID, userId mysql.ID, beforeScore mysql.Num, score mysql.Num, t match_e.MatchCharmUserScoreDetailType, orginId mysql.ID) error { - if err := model.Db.Save(&MatchCharmUserScoreDetail{ - MatchCharmUserScoreId: matchCharmUserScoreId, - UserId: userId, - BeforeScore: beforeScore, - Score: score, - AfterScore: beforeScore + score, - Type: t, - OrginId: orginId, - }).Error; err != nil { - return myerr.WrapErr(err) + for _, r := range charmUserScore { + res[r.UserId] = r.Grade } - return nil -} - -/** - * 获取的分数同等级关系 - **/ -type MatchCharmSetScoreGrade struct { - mysql.Entity - *domain.Model `gorm:"-"` - MinNum mysql.Num - MaxNum mysql.Num - Grade mysql.Num + return res, nil } diff --git a/domain/model/user_m/matchWealth.go b/domain/model/user_m/matchWealth.go index 04a0ed8..4099004 100755 --- a/domain/model/user_m/matchWealth.go +++ b/domain/model/user_m/matchWealth.go @@ -3,9 +3,6 @@ package user_m import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" - "gorm.io/gorm" - "hilo-user/_const/enum/match_e" - "hilo-user/myerr" ) /** @@ -19,52 +16,15 @@ type MatchWealthUserScore struct { Grade mysql.Num } -/** - * 获取的分数同等级关系 - **/ -type MatchWealthSetScoreGrade struct { - mysql.Entity - *domain.Model `gorm:"-"` - MinNum mysql.Num - MaxNum mysql.Num - Grade mysql.Num -} - -type MatchWealthUserScoreDetail struct { - mysql.Entity - MatchWealthUserScoreId mysql.ID - UserId mysql.ID - BeforeScore mysql.Num - Score mysql.Num - AfterScore mysql.Num - Type match_e.MatchWealthUserScoreDetailType - OrginId mysql.ID -} - -func addMatchWealthUserScoreDetail(model *domain.Model, matchWealthUserScoreId mysql.ID, userId mysql.ID, beforeScore mysql.Num, score mysql.Num, t match_e.MatchWealthUserScoreDetailType, orginId mysql.ID) error { - if err := model.Db.Save(&MatchWealthUserScoreDetail{ - MatchWealthUserScoreId: matchWealthUserScoreId, - UserId: userId, - BeforeScore: beforeScore, - Score: score, - AfterScore: beforeScore + score, - Type: t, - OrginId: orginId, - }).Error; err != nil { - return myerr.WrapErr(err) +// 批量获取财富等级 +func MGetWealthGrade(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]mysql.Num, error) { + res := make(map[mysql.ID]mysql.Num) + var wealthUserScore []*MatchWealthUserScore + if err := model.Db.Model(&MatchWealthUserScore{}).Where("user_id in ?", userIds).Find(&wealthUserScore).Error; err != nil { + return res, err } - return nil -} - -//获取财富等级 -func GetWealthGrade(model *domain.Model, userId mysql.ID) (uint32, uint32, error) { - var wealthUserScore MatchWealthUserScore - if err := model.Db.Model(&MatchWealthUserScore{}).Where(&MatchWealthUserScore{UserId: userId}).First(&wealthUserScore).Error; err != nil { - if err == gorm.ErrRecordNotFound { - return 0, 0, nil - } else { - return 0, 0, myerr.WrapErr(err) - } + for _, r := range wealthUserScore { + res[r.UserId] = r.Grade } - return wealthUserScore.Grade, wealthUserScore.Score, nil + return res, nil } diff --git a/route/router.go b/route/router.go index 549d39c..5867d84 100755 --- a/route/router.go +++ b/route/router.go @@ -20,5 +20,11 @@ func InitRouter() *gin.Engine { { user.GET("/nameplate", wrapper(user_r.UserNameplate)) } + inner := r.Group("/inner") + inner.Use(ExceptionHandle, LoggerHandle) + innerUser := inner.Group("/user") + { + innerUser.GET("/levels", wrapper(user_r.MGetUserLevels)) + } return r } diff --git a/route/user_r/inner.go b/route/user_r/inner.go new file mode 100644 index 0000000..42d4de7 --- /dev/null +++ b/route/user_r/inner.go @@ -0,0 +1,45 @@ +package user_r + +import ( + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/mycontext" + "git.hilo.cn/hilo-common/resource/mysql" + "github.com/gin-gonic/gin" + "hilo-user/cv/user_cv" + "hilo-user/domain/model/user_m" + "hilo-user/resp" +) + +type MGetUserLevelReq struct { + Ids []mysql.ID `form:"ids" binding:"required"` +} + +// @Tags 用户-内部 +// @Summary 批量获取用户等级 +// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3" +// @Success 200 {object} user_cv.MGetUserLevelData +// @Router /inner/user/levels [get] +func MGetUserLevels(c *gin.Context) (*mycontext.MyContext, error) { + myCtx := mycontext.CreateMyContext(c.Keys) + var model = domain.CreateModelContext(myCtx) + var req MGetUserLevelReq + if err := c.ShouldBindQuery(&req); err != nil { + return myCtx, err + } + wealthGrade, err := user_m.MGetWealthGrade(model, req.Ids) + if err != nil { + return myCtx, err + } + charmGrade, err := user_m.MGetCharmGrade(model, req.Ids) + response := user_cv.MGetUserLevelData{} + for _, userId := range req.Ids { + response[userId] = user_cv.CvUserLevel{ + UserId: userId, + WealthUserGrade: wealthGrade[userId], + CharmUserGrade: charmGrade[userId], + } + } + + resp.ResponseOk(c, response) + return myCtx, nil +} -- 2.22.0