广告平台(站长使用)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

svc_index.go 7.7 KiB

pirms 2 mēnešiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. db "code.fnuoos.com/zhimeng/model.git/src"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "strings"
  10. "time"
  11. )
  12. func IndexTotal(c *gin.Context) {
  13. //昨天
  14. yesterday := utils.GetTimeRange("yesterday")
  15. yesterdayTotal := commTotal(c, time.Unix(yesterday["start"], 0).Format("2006-01-02"), time.Unix(yesterday["end"]-3600, 0).Format("2006-01-02"))
  16. //前天
  17. beforeYesterDayTotal := commTotal(c, time.Unix(yesterday["start"]-86400, 0).Format("2006-01-02"), time.Unix(yesterday["start"]-3600, 0).Format("2006-01-02"))
  18. //7天
  19. withinSevenDays := utils.GetTimeRange("new_within_seven_days")
  20. withinSevenDaysTotal := commTotal(c, time.Unix(withinSevenDays["start"], 0).Format("2006-01-02"), time.Unix(withinSevenDays["end"]-3600, 0).Format("2006-01-02"))
  21. //7天前的 7天
  22. beforeWithinSevenDaysTotal := commTotal(c, time.Unix(withinSevenDays["start"]-3600-7*86400, 0).Format("2006-01-02"), time.Unix(withinSevenDays["start"]-3600, 0).Format("2006-01-02"))
  23. //本月
  24. currentMonth := utils.GetTimeRange("current_month")
  25. currentMonthTotal := commTotal(c, time.Unix(currentMonth["start"], 0).Format("2006-01-02"), time.Unix(currentMonth["end"]-3600, 0).Format("2006-01-02"))
  26. //上月
  27. lastMonth := utils.GetTimeRange("last_month")
  28. lastMonthTotal := commTotal(c, time.Unix(lastMonth["start"], 0).Format("2006-01-02"), time.Unix(lastMonth["end"]-3600, 0).Format("2006-01-02"))
  29. //上上月
  30. beforeLastMonth := time.Unix(lastMonth["start"]-86400, 0)
  31. beforeLastMonthStr := time.Date(beforeLastMonth.Year(), beforeLastMonth.Month(), 1, 0, 0, 0, 0, beforeLastMonth.Location())
  32. beforeLastMonthTotal := commTotal(c, beforeLastMonthStr.Format("2006-01-02"), time.Unix(lastMonth["start"]-3600, 0).Format("2006-01-02"))
  33. res := []md.IndexAppListDataList{
  34. {Name: "昨日收益(媒体)", Type: "yesterday", Bili: commBili(c, beforeYesterDayTotal, yesterdayTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(yesterdayTotal[0]["media_revenue"]) / 100)},
  35. {Name: "七日收益(媒体)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotal, withinSevenDaysTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(withinSevenDaysTotal[0]["media_revenue"]) / 100)},
  36. {Name: "本月收益(媒体)", Type: "current_month", Bili: commBili(c, lastMonthTotal, currentMonthTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(currentMonthTotal[0]["media_revenue"]) / 100)},
  37. {Name: "上月收益(媒体)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotal, lastMonthTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(lastMonthTotal[0]["media_revenue"]) / 100)},
  38. {Name: "昨日收益(曝光量)", Type: "yesterday", Bili: commBili(c, beforeYesterDayTotal, yesterdayTotal, "exposure_count"), Value: yesterdayTotal[0]["exposure_count"]},
  39. {Name: "七日收益(曝光量)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotal, withinSevenDaysTotal, "exposure_count"), Value: withinSevenDaysTotal[0]["exposure_count"]},
  40. {Name: "本月收益(曝光量)", Type: "current_month", Bili: commBili(c, lastMonthTotal, currentMonthTotal, "exposure_count"), Value: currentMonthTotal[0]["exposure_count"]},
  41. {Name: "上月收益(曝光量)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotal, lastMonthTotal, "exposure_count"), Value: lastMonthTotal[0]["exposure_count"]},
  42. }
  43. e.OutSuc(c, res, nil)
  44. return
  45. }
  46. func commBili(c *gin.Context, before, after []map[string]string, types string) string {
  47. bili := "-%"
  48. if utils.StrToFloat64(before[0][types]) > 0 {
  49. tmpDiff := utils.StrToFloat64(after[0][types]) - utils.StrToFloat64(before[0][types])
  50. types1 := "+"
  51. if tmpDiff < 0 {
  52. types1 = ""
  53. }
  54. bili = types1 + utils.Float64ToStr(tmpDiff/utils.StrToFloat64(before[0][types])*100) + "%"
  55. }
  56. return bili
  57. }
  58. func IndexAppList(c *gin.Context, req md.IndexAppListReq) md.IndexAppListRes {
  59. appId := make([]string, 0)
  60. data := commTotalByApp(c, req, appId)
  61. for _, v := range data {
  62. appId = append(appId, v["app_id"])
  63. }
  64. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  65. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - start) / 86400
  66. req.EndDate = time.Unix(start-1, 0).Format("2006-01-02")
  67. req.StartDate = time.Unix(start-1-day*86400, 0).Format("2006-01-02")
  68. dataLast := commTotalByApp(c, req, appId)
  69. dataLastMap := make(map[string][]map[string]string)
  70. for _, v := range dataLast {
  71. dataLastMap[v["app_id"]] = make([]map[string]string, 0)
  72. dataLastMap[v["app_id"]] = append(dataLastMap[v["app_id"]], v)
  73. }
  74. list := make([]md.IndexAppListData, 0)
  75. for _, v := range data {
  76. _, ok := dataLastMap[v["app_id"]]
  77. if ok == false {
  78. dataLastMap[v["app_id"]] = []map[string]string{
  79. {"media_revenue": "0", "ecpm": "0", "exposure_count": "0", "click_count": "0", "click_rate": "0"},
  80. }
  81. }
  82. tmpList := []map[string]string{
  83. v,
  84. }
  85. dataList := []md.IndexAppListDataList{
  86. {Name: "广告预估收益", Type: "media_revenue", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)},
  87. {Name: "ECPM", Type: "ecpm", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "ecpm"), Value: v["ecpm"]},
  88. {Name: "曝光量", Type: "exposure_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "exposure_count"), Value: v["exposure_count"]},
  89. {Name: "点击量", Type: "click_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_count"), Value: v["click_count"]},
  90. {Name: "点击率", Type: "click_rate", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_rate"), Value: utils.GetPrec(v["click_rate"], "2") + "%"},
  91. }
  92. applet := GetAppletInfo(c, v["app_id"])
  93. tmp := md.IndexAppListData{
  94. Name: applet["name"],
  95. Logo: applet["logo"],
  96. List: dataList,
  97. TableList: []string{},
  98. }
  99. list = append(list, tmp)
  100. }
  101. res := md.IndexAppListRes{
  102. List: list,
  103. Sort: []md.SelectData{
  104. {Name: "按收益最高", Value: "media_revenue desc"},
  105. {Name: "按ECPM最高", Value: "ecpm desc"},
  106. {Name: "按曝光量最高", Value: "exposure_count desc"},
  107. },
  108. }
  109. return res
  110. }
  111. func commTotal(c *gin.Context, startDate, endDate string) []map[string]string {
  112. sql := `
  113. SELECT
  114. SUM(exposure_count) as exposure_count,
  115. SUM(click_count) as click_count,
  116. SUM(click_rate) as click_rate,
  117. SUM(ecpm) as ecpm,
  118. SUM(media_revenue) as media_revenue
  119. FROM generate_wx_ad_data
  120. where %s
  121. `
  122. where := "uuid=" + c.GetString("mid")
  123. if startDate != "" {
  124. where += " and date>='" + startDate + "'"
  125. }
  126. if endDate != "" {
  127. where += " and date<='" + endDate + "'"
  128. }
  129. sql = fmt.Sprintf(sql, where)
  130. nativeString, _ := db.QueryNativeString(db.Db, sql)
  131. if len(nativeString) == 0 {
  132. nativeString = []map[string]string{
  133. {
  134. "media_revenue": "0",
  135. },
  136. }
  137. }
  138. return nativeString
  139. }
  140. func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []map[string]string {
  141. sql := `
  142. SELECT
  143. app_id as app_id,
  144. SUM(exposure_count) as exposure_count,
  145. SUM(click_count) as click_count,
  146. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  147. SUM(ecpm) as ecpm,
  148. SUM(media_revenue) as media_revenue
  149. FROM generate_wx_ad_data
  150. where %s group by app_id order by %s,id asc limit 50
  151. `
  152. where := "uuid=" + c.GetString("mid")
  153. if req.StartDate != "" {
  154. where += " and date>='" + req.StartDate + "'"
  155. }
  156. if req.EndDate != "" {
  157. where += " and date<='" + req.EndDate + "'"
  158. }
  159. if req.Name != "" {
  160. mediumId := GetMediumId(c, req.Name)
  161. where += " and medium_id in(" + mediumId + ")"
  162. }
  163. if len(appId) > 0 {
  164. where += " and app_id in(" + strings.Join(appId, ",") + ")"
  165. }
  166. if req.Sort == "" {
  167. req.Sort = "media_revenue desc"
  168. }
  169. sql = fmt.Sprintf(sql, where, req.Sort)
  170. nativeString, _ := db.QueryNativeString(db.Db, sql)
  171. return nativeString
  172. }