From b2582ee0aac48819451f6c4a92c533d8e408f41e Mon Sep 17 00:00:00 2001 From: hujiebin Date: Thu, 16 Mar 2023 12:10:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E5=A4=A9?= =?UTF-8?q?=E4=B8=8A=E9=BA=A6=E6=97=B6=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/mic_m/mic.go | 85 +++++++++++++++++++++++++ domain/service/group_mic_s/group_mic.go | 5 ++ route/mic_r/inner.go | 37 +++++++++++ route/router.go | 5 ++ 4 files changed, 132 insertions(+) create mode 100644 domain/model/mic_m/mic.go create mode 100644 route/mic_r/inner.go diff --git a/domain/model/mic_m/mic.go b/domain/model/mic_m/mic.go new file mode 100644 index 0000000..9289e98 --- /dev/null +++ b/domain/model/mic_m/mic.go @@ -0,0 +1,85 @@ +package mic_m + +import ( + "git.hilo.cn/hilo-common/domain" + "git.hilo.cn/hilo-common/resource/mysql" + "git.hilo.cn/hilo-common/utils" + "gorm.io/gorm" + "gorm.io/gorm/clause" + "time" +) + +// 用户上麦 +type UserOnMic struct { + Day string + UserId mysql.ID + Seconds mysql.Num + LastCalTs int64 + CreatedTime time.Time `gorm:"->"` + UpdatedTime time.Time `gorm:"->"` +} + +// 获取用户当日上麦 +// 允许返回gorm.ErrRecordNotFound +func GetUserOnMic(model *domain.Model, userId mysql.ID) (*UserOnMic, error) { + res := new(UserOnMic) + day := time.Now().Format("2006-01-02") + if err := model.DB().Where("day = ? AND user_id = ?", day, userId).First(res).Error; err != nil { + return nil, err + } + return res, nil +} + +// 增加用户上麦时长 +// 事务操作 +func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64) error { + return model.Transaction(func(model *domain.Model) error { + omMic, err := GetUserOnMic(model, userId) + if err != nil && err != gorm.ErrRecordNotFound { + return err + } + curTs := joinMicTimestamp + nowTs := time.Now().Unix() + day0Ts := utils.GetZeroTime(time.Now()).Unix() + if omMic != nil && joinMicTimestamp < omMic.LastCalTs { // 加入的时间比上次计算时间小 + curTs = omMic.LastCalTs + } + // 跨天 + if curTs < day0Ts { + curTs = day0Ts + } + score := nowTs - curTs + day := time.Now().Format("2006-01-02") + onMicNew := &UserOnMic{ + Day: day, + UserId: userId, + Seconds: mysql.Num(score), + } + if err := model.DB().Model(UserOnMic{}).Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "day"}, {Name: "user_id"}}, + DoUpdates: clause.Assignments(map[string]interface{}{ + "seconds": gorm.Expr("seconds + ?", onMicNew.Seconds), + "last_cal_ts": nowTs, + }), + }).Create(onMicNew).Error; err != nil { + model.Log.Errorf("IncrUserOnMic fail:%v", err) + return err + } + return nil + }) +} + +// 批量获取用户上麦时长 +// @return userId->seconds +func MGetUserOnMicSeconds(model *domain.Model, day string, userIds []uint64) (map[mysql.ID]mysql.Num, error) { + var rows []UserOnMic + res := make(map[mysql.ID]mysql.Num) + if err := model.DB().Model(UserOnMic{}).Where("`day`= ? AND user_id in ?", day, userIds).Find(&rows).Error; err != nil { + model.Log.Errorf("MGetUserOnMic fail:%v", err) + return res, err + } + for _, r := range rows { + res[r.UserId] = r.Seconds + } + return res, nil +} diff --git a/domain/service/group_mic_s/group_mic.go b/domain/service/group_mic_s/group_mic.go index 0b8f25c..44d043f 100644 --- a/domain/service/group_mic_s/group_mic.go +++ b/domain/service/group_mic_s/group_mic.go @@ -12,6 +12,7 @@ import ( "hilo-group/domain/event/group_ev" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/group_m" + "hilo-group/domain/model/mic_m" "hilo-group/domain/service/signal_s" "hilo-group/myerr" "hilo-group/myerr/bizerr" @@ -322,5 +323,9 @@ func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId u if err := groupPower_m.IncrGroupPowerStarOnMic(model, groupPowerId, userId, joinMicTimestamp); err != nil { model.Log.Errorf("IncrGroupPowerOnMicTime fail:%v", err) } + // 增加用户天上麦时长 + if err := mic_m.IncrUserOnMic(model, userId, joinMicTimestamp); err != nil { + model.Log.Errorf("IncrUserOnMic fail:%v", err) + } return nil } diff --git a/route/mic_r/inner.go b/route/mic_r/inner.go new file mode 100644 index 0000000..d212ae9 --- /dev/null +++ b/route/mic_r/inner.go @@ -0,0 +1,37 @@ +package mic_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-group/domain/model/mic_m" + "hilo-group/resp" +) + +type MGetUserOnMicSecondsReq struct { + Day string `form:"day" binding:"required"` + Ids []mysql.ID `form:"ids" binding:"required"` +} + +// @Tags 麦位-内部 +// @Summary 批量获取用户天上麦时长 +// @Param day query string true "天 格式2006-001-02" +// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3" +// @Success 200 {object} map[uint64]uint32 +// @Router /inner/mic/onMicSeconds [get] +func MGetUserOnMicSeconds(c *gin.Context) (*mycontext.MyContext, error) { + myCtx := mycontext.CreateMyContext(c.Keys) + var model = domain.CreateModelContext(myCtx) + var req MGetUserOnMicSecondsReq + if err := c.ShouldBindQuery(&req); err != nil { + return myCtx, err + } + onMic, err := mic_m.MGetUserOnMicSeconds(model, req.Day, req.Ids) + if err != nil { + return myCtx, err + } + response := onMic + resp.ResponseOk(c, response) + return myCtx, nil +} diff --git a/route/router.go b/route/router.go index fc9b61e..e1b8cab 100644 --- a/route/router.go +++ b/route/router.go @@ -11,6 +11,7 @@ import ( "hilo-group/domain/model/mgr_m" "hilo-group/route/group_power_r" "hilo-group/route/group_r" + "hilo-group/route/mic_r" ) func InitRouter() *gin.Engine { @@ -139,6 +140,10 @@ func InitRouter() *gin.Engine { { innerGroupPower.GET("/infos", wrapper(group_power_r.MGetGroupPowers)) } + innerMic := inner.Group("/mic") + { + innerMic.GET("/onMicSeconds", wrapper(mic_r.MGetUserOnMicSeconds)) + } r.GET("/test", wrapper(group_power_r.Test)) return r } -- 2.22.0