From 9068f71484b73ec296af96e76a1808623fed4ff6 Mon Sep 17 00:00:00 2001 From: JiebinHu <458249864@qq.com> Date: Sun, 27 Aug 2023 14:56:41 +0800 Subject: [PATCH] =?UTF-8?q?todo=20=E4=B8=80=E6=AC=A1=E6=80=A7=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/group_power_r/inner.go | 62 ++++++++++++++++++++++++++++++++++++ route/router.go | 1 + 2 files changed, 63 insertions(+) diff --git a/route/group_power_r/inner.go b/route/group_power_r/inner.go index cfde560..1feda46 100644 --- a/route/group_power_r/inner.go +++ b/route/group_power_r/inner.go @@ -1,18 +1,23 @@ package group_power_r import ( + "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/resource/mysql" "github.com/gin-gonic/gin" + "github.com/jinzhu/now" "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/enum/msg_e" + "hilo-group/_const/redis_key/groupPower_k" "hilo-group/cv/group_power_cv" "hilo-group/domain/model/groupPower_m" "hilo-group/domain/model/msg_m" "hilo-group/domain/model/user_m" "hilo-group/domain/service/group_power_s" + "hilo-group/myerr/bizerr" "hilo-group/resp" + "time" ) type MGetGroupPowerReq struct { @@ -93,3 +98,60 @@ func Test(c *gin.Context) (*mycontext.MyContext, error) { //} return nil, nil } + +type GroupPowerDayStar struct { + Date string + GroupPowerId uint64 + Type groupPower_e.GroupPowerStarType + UserId uint64 + Score uint64 +} + +// @Tags 国家势力-内部 +// @Summary 同步家族之星到redis +// @Success 200 +// @Router /inner/groupPower/sync/star [get] +func SyncStar(c *gin.Context) (*mycontext.MyContext, error) { + myCtx := mycontext.CreateMyContext(c.Keys) + start := c.Query("start") + end := c.Query("end") + if len(start) <= 0 || len(end) <= 0 { + return myCtx, bizerr.InvalidParameter + } + var model = domain.CreateModelContext(myCtx) + var stars []GroupPowerDayStar + if err := model.DB().Table("group_power_day_star"). + Select("`date`,group_power_id,type,user_id,SUM(score) score"). + Where("`date` BETWEEN ? AND ?", start, end). + Group("`date`,group_power_id,type,user_id").Find(&stars).Error; err != nil { + model.Log.Errorf("SyncStar fail:%v", err) + return myCtx, err + } + ttl := map[string]time.Duration{ + "day": time.Hour * 24 * 7, + "week": time.Hour * 24 * 7 * 30, + "month": time.Hour * 24 * 7 * 30 * 2, + } + for _, star := range stars { + date, err := time.Parse("2006-01-02", star.Date) + if err != nil { + model.Log.Errorf("SyncStar date fail:%v", err) + continue + } + for _, period := range []string{"day", "week", "month"} { + var dateStr string + switch period { + case "day": + dateStr = date.Format("2006-01-02") + case "week": + dateStr = now.With(date).BeginningOfWeek().Format("2006-01-02") + case "month": + dateStr = now.With(date).BeginningOfMonth().Format("2006-01-02") + } + key := groupPower_k.GetGroupPowerStarRankKey(star.Type, period, star.GroupPowerId, dateStr) + model.RedisCluster.ZIncrBy(model, key, float64(star.Score), fmt.Sprintf("%d", star.UserId)) + model.RedisCluster.Expire(model, key, ttl[period]) + } + } + return myCtx, nil +} diff --git a/route/router.go b/route/router.go index 406bf10..c9393a3 100644 --- a/route/router.go +++ b/route/router.go @@ -159,6 +159,7 @@ func InitRouter() *gin.Engine { innerGroupPower := inner.Group("/groupPower") { innerGroupPower.GET("/infos", wrapper(group_power_r.MGetGroupPowers)) + innerGroupPower.GET("/sync/star", wrapper(group_power_r.SyncStar)) // todo 一次性脚本 } innerMic := inner.Group("/mic") { -- 2.22.0