广告平台(站长使用)
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

215 wiersze
9.2 KiB

  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 beforeSevenPoint(c *gin.Context, req md.IndexAppListTableReq) {
  59. //start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  60. //day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - start) / 86400
  61. //firstTime := time.Unix(start-1-day*86400*7, 0).Format("2006-01-02")
  62. //list := make([]map[string]interface{}, 0)
  63. //if len(req.AppId) > 0 {
  64. // commTotalByDate(c, firstTime, req.EndDate, req.AppId)
  65. //}
  66. //{Name: "广告预估收益", Type: "media_revenue", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)},
  67. }
  68. func IndexAppList(c *gin.Context, req md.IndexAppListReq) md.IndexAppListRes {
  69. appId := make([]string, 0)
  70. data := commTotalByApp(c, req, appId)
  71. for _, v := range data {
  72. appId = append(appId, v["app_id"])
  73. }
  74. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  75. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - start) / 86400
  76. req.EndDate = time.Unix(start-1, 0).Format("2006-01-02")
  77. req.StartDate = time.Unix(start-1-day*86400, 0).Format("2006-01-02")
  78. dataLast := commTotalByApp(c, req, appId)
  79. dataLastMap := make(map[string][]map[string]string)
  80. for _, v := range dataLast {
  81. dataLastMap[v["app_id"]] = make([]map[string]string, 0)
  82. dataLastMap[v["app_id"]] = append(dataLastMap[v["app_id"]], v)
  83. }
  84. list := make([]md.IndexAppListData, 0)
  85. for _, v := range data {
  86. _, ok := dataLastMap[v["app_id"]]
  87. if ok == false {
  88. dataLastMap[v["app_id"]] = []map[string]string{
  89. {"media_revenue": "0", "ecpm": "0", "exposure_count": "0", "click_count": "0", "click_rate": "0"},
  90. }
  91. }
  92. tmpList := []map[string]string{
  93. v,
  94. }
  95. dataList := []md.IndexAppListDataList{
  96. {Name: "广告预估收益", Type: "media_revenue", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)},
  97. {Name: "ECPM", Type: "ecpm", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "ecpm"), Value: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / float64(day))},
  98. {Name: "曝光量", Type: "exposure_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "exposure_count"), Value: v["exposure_count"]},
  99. {Name: "点击量", Type: "click_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_count"), Value: v["click_count"]},
  100. {Name: "点击率", Type: "click_rate", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_rate"), Value: utils.GetPrec(v["click_rate"], "2") + "%"},
  101. }
  102. applet := GetAppletInfo(c, v["app_id"])
  103. tmp := md.IndexAppListData{
  104. Name: applet["name"],
  105. Logo: applet["logo"],
  106. List: dataList,
  107. TableList: []string{},
  108. }
  109. list = append(list, tmp)
  110. }
  111. res := md.IndexAppListRes{
  112. List: list,
  113. AppId: appId,
  114. Sort: []md.SelectData{
  115. {Name: "按收益最高", Value: "media_revenue desc"},
  116. {Name: "按ECPM最高", Value: "ecpm desc"},
  117. {Name: "按曝光量最高", Value: "exposure_count desc"},
  118. },
  119. }
  120. return res
  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. where := "uuid=" + c.GetString("mid")
  134. if startDate != "" {
  135. where += " and date>='" + startDate + "'"
  136. }
  137. if endDate != "" {
  138. where += " and date<='" + endDate + "'"
  139. }
  140. sql = fmt.Sprintf(sql, where)
  141. nativeString, _ := db.QueryNativeString(db.Db, sql)
  142. if len(nativeString) == 0 {
  143. nativeString = []map[string]string{
  144. {
  145. "media_revenue": "0",
  146. },
  147. }
  148. }
  149. return nativeString
  150. }
  151. func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []map[string]string {
  152. sql := `
  153. SELECT
  154. app_id as app_id,
  155. SUM(exposure_count) as exposure_count,
  156. SUM(click_count) as click_count,
  157. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  158. SUM(ecpm) as ecpm,
  159. SUM(media_revenue) as media_revenue
  160. FROM generate_wx_ad_data
  161. where %s group by app_id order by %s,id asc limit 50
  162. `
  163. where := "uuid=" + c.GetString("mid")
  164. if req.StartDate != "" {
  165. where += " and date>='" + req.StartDate + "'"
  166. }
  167. if req.EndDate != "" {
  168. where += " and date<='" + req.EndDate + "'"
  169. }
  170. if req.Name != "" {
  171. mediumId := GetMediumId(c, req.Name)
  172. where += " and medium_id in(" + mediumId + ")"
  173. }
  174. if len(appId) > 0 {
  175. where += " and app_id in(" + strings.Join(appId, ",") + ")"
  176. }
  177. if req.Sort == "" {
  178. req.Sort = "media_revenue desc"
  179. }
  180. sql = fmt.Sprintf(sql, where, req.Sort)
  181. nativeString, _ := db.QueryNativeString(db.Db, sql)
  182. return nativeString
  183. }
  184. func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string {
  185. sql := `
  186. SELECT
  187. app_id as app_id,
  188. date as date,
  189. SUM(exposure_count) as exposure_count,
  190. SUM(click_count) as click_count,
  191. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  192. SUM(ecpm) as ecpm,
  193. SUM(media_revenue) as media_revenue
  194. FROM generate_wx_ad_data
  195. where %s group by date,app_id
  196. `
  197. where := "uuid=" + c.GetString("mid")
  198. if startDate != "" {
  199. where += " and date>='" + startDate + "'"
  200. }
  201. if endDate != "" {
  202. where += " and date<='" + endDate + "'"
  203. }
  204. if len(appId) > 0 {
  205. where += " and app_id in(" + strings.Join(appId, ",") + ")"
  206. }
  207. sql = fmt.Sprintf(sql, where)
  208. nativeString, _ := db.QueryNativeString(db.Db, sql)
  209. return nativeString
  210. }