广告平台(站长下代理使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_index.go 8.2 KiB

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