...
 
Commits (3)
package mic_m package mic_m
import ( import (
"git.hilo.cn/hilo-common/_const/enum/timezone_e"
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/utils" "git.hilo.cn/hilo-common/utils"
...@@ -17,14 +18,15 @@ type UserOnMic struct { ...@@ -17,14 +18,15 @@ type UserOnMic struct {
LastCalTs int64 LastCalTs int64
CreatedTime time.Time `gorm:"->"` CreatedTime time.Time `gorm:"->"`
UpdatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"`
Tz timezone_e.Timezone
} }
// 获取用户当日上麦 // 获取用户当日上麦
// 允许返回gorm.ErrRecordNotFound // 允许返回gorm.ErrRecordNotFound
func GetUserOnMic(model *domain.Model, userId mysql.ID) (*UserOnMic, error) { func GetUserOnMic(model *domain.Model, userId mysql.ID, tz timezone_e.Timezone) (*UserOnMic, error) {
res := new(UserOnMic) res := new(UserOnMic)
day := time.Now().Format("2006-01-02") day := time.Now().Format("2006-01-02")
if err := model.DB().Where("date = ? AND user_id = ?", day, userId).First(res).Error; err != nil { if err := model.DB().Where("date = ? AND user_id = ? AND tz = ?", day, userId, tz).First(res).Error; err != nil {
return nil, err return nil, err
} }
return res, nil return res, nil
...@@ -34,13 +36,14 @@ func GetUserOnMic(model *domain.Model, userId mysql.ID) (*UserOnMic, error) { ...@@ -34,13 +36,14 @@ func GetUserOnMic(model *domain.Model, userId mysql.ID) (*UserOnMic, error) {
// 事务操作 // 事务操作
func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64) error { func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64) error {
return model.Transaction(func(model *domain.Model) error { return model.Transaction(func(model *domain.Model) error {
omMic, err := GetUserOnMic(model, userId) for _, tz := range timezone_e.Timezones {
omMic, err := GetUserOnMic(model, userId, tz)
if err != nil && err != gorm.ErrRecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
return err return err
} }
curTs := joinMicTimestamp curTs := joinMicTimestamp
nowTs := time.Now().Unix() nowTs := time.Now().Unix()
day0Ts := utils.GetZeroTime(time.Now()).Unix() day0Ts := utils.GetZeroTime(time.Now().In(timezone_e.TimezoneLocMap[tz])).Unix()
if omMic != nil && joinMicTimestamp < omMic.LastCalTs { // 加入的时间比上次计算时间小 if omMic != nil && joinMicTimestamp < omMic.LastCalTs { // 加入的时间比上次计算时间小
curTs = omMic.LastCalTs curTs = omMic.LastCalTs
} }
...@@ -49,15 +52,16 @@ func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64) ...@@ -49,15 +52,16 @@ func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64)
curTs = day0Ts curTs = day0Ts
} }
score := nowTs - curTs score := nowTs - curTs
day := time.Now().Format("2006-01-02") day := time.Now().In(timezone_e.TimezoneLocMap[tz]).Format("2006-01-02")
onMicNew := &UserOnMic{ onMicNew := &UserOnMic{
Date: day, Date: day,
UserId: userId, UserId: userId,
Seconds: mysql.Num(score), Seconds: mysql.Num(score),
LastCalTs: nowTs, LastCalTs: nowTs,
Tz: tz,
} }
if err := model.DB().Model(UserOnMic{}).Clauses(clause.OnConflict{ if err := model.DB().Model(UserOnMic{}).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "date"}, {Name: "user_id"}}, Columns: []clause.Column{{Name: "date"}, {Name: "user_id"}, {Name: "tz"}},
DoUpdates: clause.Assignments(map[string]interface{}{ DoUpdates: clause.Assignments(map[string]interface{}{
"seconds": gorm.Expr("seconds + ?", onMicNew.Seconds), "seconds": gorm.Expr("seconds + ?", onMicNew.Seconds),
"last_cal_ts": nowTs, "last_cal_ts": nowTs,
...@@ -66,16 +70,32 @@ func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64) ...@@ -66,16 +70,32 @@ func IncrUserOnMic(model *domain.Model, userId mysql.ID, joinMicTimestamp int64)
model.Log.Errorf("IncrUserOnMic fail:%v", err) model.Log.Errorf("IncrUserOnMic fail:%v", err)
return err return err
} }
}
return nil return nil
}) })
} }
// 批量获取用户上麦时长 // 批量获取用户上麦时长
// @return userId->seconds // @return userId->seconds
func MGetUserOnMicSeconds(model *domain.Model, day string, userIds []uint64) (map[mysql.ID]mysql.Num, error) { func MGetUserOnMicSeconds(model *domain.Model, day string, tz timezone_e.Timezone, 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("`date`= ? AND tz = ? AND user_id in ?", day, tz, 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
}
// 批量获取用户上麦时长
// @return userId->seconds
func MGetUserOnMicSecondsRange(model *domain.Model, beginDate, endDate string, tz timezone_e.Timezone, userIds []uint64) (map[mysql.ID]mysql.Num, error) {
var rows []UserOnMic var rows []UserOnMic
res := make(map[mysql.ID]mysql.Num) res := make(map[mysql.ID]mysql.Num)
if err := model.DB().Model(UserOnMic{}).Where("`date`= ? AND user_id in ?", day, userIds).Find(&rows).Error; err != nil { if err := model.DB().Model(UserOnMic{}).Where("`date` >= ? AND `date` <= ? AND tz = ? AND user_id in ?", beginDate, endDate, tz, userIds).Find(&rows).Error; err != nil {
model.Log.Errorf("MGetUserOnMic fail:%v", err) model.Log.Errorf("MGetUserOnMic fail:%v", err)
return res, err return res, err
} }
......
package mic_r package mic_r
import ( import (
"git.hilo.cn/hilo-common/_const/enum/timezone_e"
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/resource/mysql" "git.hilo.cn/hilo-common/resource/mysql"
...@@ -12,12 +13,14 @@ import ( ...@@ -12,12 +13,14 @@ import (
type MGetUserOnMicSecondsReq struct { type MGetUserOnMicSecondsReq struct {
Day string `form:"day" binding:"required"` Day string `form:"day" binding:"required"`
Ids []mysql.ID `form:"ids" binding:"required"` Ids []mysql.ID `form:"ids" binding:"required"`
Tz timezone_e.Timezone `form:"tz"`
} }
// @Tags 麦位-内部 // @Tags 麦位-内部
// @Summary 批量获取用户天上麦时长 // @Summary 批量获取用户天上麦时长
// @Param day query string true "天 格式2006-001-02" // @Param day query string true "天 格式2006-001-02"
// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3" // @Param ids query string true "用户id,如:ids=1&ids=2&ids=3"
// @Param tz query string false "时区 0:北京时间(默认) 1:沙特时间"
// @Success 200 {object} map[uint64]uint32 // @Success 200 {object} map[uint64]uint32
// @Router /inner/mic/onMicSeconds [get] // @Router /inner/mic/onMicSeconds [get]
func MGetUserOnMicSeconds(c *gin.Context) (*mycontext.MyContext, error) { func MGetUserOnMicSeconds(c *gin.Context) (*mycontext.MyContext, error) {
...@@ -27,7 +30,38 @@ func MGetUserOnMicSeconds(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -27,7 +30,38 @@ func MGetUserOnMicSeconds(c *gin.Context) (*mycontext.MyContext, error) {
if err := c.ShouldBindQuery(&req); err != nil { if err := c.ShouldBindQuery(&req); err != nil {
return myCtx, err return myCtx, err
} }
onMic, err := mic_m.MGetUserOnMicSeconds(model, req.Day, req.Ids) onMic, err := mic_m.MGetUserOnMicSeconds(model, req.Day, req.Tz, req.Ids)
if err != nil {
return myCtx, err
}
response := onMic
resp.ResponseOk(c, response)
return myCtx, nil
}
type MGetUserOnMicSecondsRangeReq struct {
BeginDate string `form:"beginDate" binding:"required"`
EndDate string `form:"endDate" binding:"required"`
Ids []mysql.ID `form:"ids" binding:"required"`
Tz timezone_e.Timezone `form:"tz"`
}
// @Tags 麦位-内部
// @Summary 批量获取用户范围上麦时长
// @Param beginDate query string true "天 格式2006-001-02"
// @Param endDate query string true "天 格式2006-001-02"
// @Param ids query string true "用户id,如:ids=1&ids=2&ids=3"
// @Param tz query string false "时区 0:北京时间(默认) 1:沙特时间"
// @Success 200 {object} map[uint64]uint32
// @Router /inner/mic/onMicSeconds/range [get]
func MGetUserOnMicSecondsRange(c *gin.Context) (*mycontext.MyContext, error) {
myCtx := mycontext.CreateMyContext(c.Keys)
var model = domain.CreateModelContext(myCtx)
var req MGetUserOnMicSecondsRangeReq
if err := c.ShouldBindQuery(&req); err != nil {
return myCtx, err
}
onMic, err := mic_m.MGetUserOnMicSecondsRange(model, req.BeginDate, req.EndDate, req.Tz, req.Ids)
if err != nil { if err != nil {
return myCtx, err return myCtx, err
} }
......
...@@ -161,6 +161,7 @@ func InitRouter() *gin.Engine { ...@@ -161,6 +161,7 @@ func InitRouter() *gin.Engine {
innerMic := inner.Group("/mic") innerMic := inner.Group("/mic")
{ {
innerMic.GET("/onMicSeconds", wrapper(mic_r.MGetUserOnMicSeconds)) innerMic.GET("/onMicSeconds", wrapper(mic_r.MGetUserOnMicSeconds))
innerMic.GET("/onMicSeconds/range", wrapper(mic_r.MGetUserOnMicSecondsRange))
} }
r.GET("/test", wrapper(group_power_r.Test)) r.GET("/test", wrapper(group_power_r.Test))
return r return r
......