From b43cea0af6c2db73a8ee9a53d14402d4d96957c8 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 10 Oct 2024 11:25:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/hdl/hdl_index.go | 25 ++++++++++++++++ app/md/md_index.go | 5 ++++ app/router/router.go | 7 +++-- app/svc/svc_index.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) diff --git a/app/hdl/hdl_index.go b/app/hdl/hdl_index.go index 539a462..7a05384 100644 --- a/app/hdl/hdl_index.go +++ b/app/hdl/hdl_index.go @@ -87,3 +87,28 @@ func IndexAppList(c *gin.Context) { e.OutSuc(c, res, nil) return } + +// IndexAppListTable +// @Summary 应用数据-每个应用的折线图 +// @Tags 首页------嘉俊 +// @Description 首页-应用数据-每个应用的折线图 +// @param Authorization header string true "验证参数Bearer和token空格拼接" +// @Accept json +// @Produce json +// @Param args body md.IndexAppListTableReq true "请求参数" +// @Success 200 {string} "具体看返回内容 " +// @Failure 400 {object} md.Response "具体错误" +// @Router /api/index/app/list/table [POST] +func IndexAppListTable(c *gin.Context) { + var req md.IndexAppListTableReq + err := c.ShouldBindJSON(&req) + if err != nil { + err = validate.HandleValidateErr(err) + err1 := err.(e.E) + e.OutErr(c, err1.Code, err1.Error()) + return + } + res := svc.BeforeSevenPoint(c, req) + e.OutSuc(c, res, nil) + return +} diff --git a/app/md/md_index.go b/app/md/md_index.go index b8570b3..26d8c98 100644 --- a/app/md/md_index.go +++ b/app/md/md_index.go @@ -22,3 +22,8 @@ type IndexAppListDataList struct { Type string `json:"type"` Bili string `json:"bili"` } +type IndexAppListTableReq struct { + StartDate string `json:"start_date" example:"2024-08-30"` + EndDate string `json:"end_date" example:"2024-08-30"` + AppId []string `json:"app_id"` +} diff --git a/app/router/router.go b/app/router/router.go index 7d8a3f1..abac72f 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -113,9 +113,10 @@ func rSetCenter(r *gin.RouterGroup) { } func rIndex(r *gin.RouterGroup) { - r.GET("/base", hdl.Base) //首页-基本信息 - r.GET("/total", hdl.IndexTotal) //首页-统计数据 - r.POST("/app/list", hdl.IndexAppList) //首页-应用数据 + r.GET("/base", hdl.Base) //首页-基本信息 + r.GET("/total", hdl.IndexTotal) //首页-统计数据 + r.POST("/app/list", hdl.IndexAppList) //首页-应用数据 + r.POST("/app/list/table", hdl.IndexAppListTable) // 首页-应用数据-每个应用的折线图 } func rDataCenter(r *gin.RouterGroup) { r.GET("/select/data", hdl.DataCenterSelectData) //数据中心-筛选条件 diff --git a/app/svc/svc_index.go b/app/svc/svc_index.go index 6809871..a338050 100644 --- a/app/svc/svc_index.go +++ b/app/svc/svc_index.go @@ -216,3 +216,72 @@ func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []ma nativeString, _ := db.QueryNativeString(db.Db, sql) return nativeString } +func BeforeSevenPoint(c *gin.Context, req md.IndexAppListTableReq) map[string][]string { + + start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00") + day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-start)/86400 + 1 + first := start - day*86400*6 + dayAll := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-first)/86400 + 1 + + firstTime := time.Unix(first, 0).Format("2006-01-02") + appData := make(map[string][]string) + for _, v := range req.AppId { + appData[v] = make([]string, dayAll/day+1, dayAll/day+1) + } + dateList := make(map[int]string) + j := 1 + for i := 0; i < int(dayAll); i++ { + if i >= (j-1)*int(day) && i < j*int(day) { + dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02") + continue + } + j++ + dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02") + } + if len(req.AppId) > 0 { + date := commTotalByDate(c, firstTime, req.EndDate, req.AppId) + for _, v := range date { + for k1, v1 := range dateList { + if strings.Contains(v1, v["date"]) { + appData[v["app_id"]][k1] = utils.Float64ToStr(utils.StrToFloat64(appData[v["app_id"]][k1]) + utils.StrToFloat64(v["media_revenue"])/100) + } + } + } + } + for k, v := range appData { + for k1, v1 := range v { + if v1 == "" { + appData[k][k1] = "0" + } + } + } + return appData +} +func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string { + sql := ` + SELECT + app_id as app_id, + date as date, + SUM(exposure_count) as exposure_count, + SUM(click_count) as click_count, + SUM(click_count)/SUM(exposure_count)*100 as click_rate, + SUM(ecpm) as ecpm, + SUM(media_revenue) as media_revenue + FROM generate_wx_ad_data + where %s group by date,app_id +` + where := "uuid=" + c.GetString("mid") + if startDate != "" { + where += " and date>='" + startDate + "'" + } + if endDate != "" { + where += " and date<='" + endDate + "'" + } + if len(appId) > 0 { + where += " and app_id in('" + strings.Join(appId, ",") + "')" + } + sql = fmt.Sprintf(sql, where) + nativeString, err := db.QueryNativeString(db.Db, sql) + fmt.Println(err) + return nativeString +}