广告平台(媒体使用)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_index.go 9.4 KiB

3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
1ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. }
  39. e.OutSuc(c, res, nil)
  40. return
  41. }
  42. func commBili(c *gin.Context, before, after []map[string]string, types string) string {
  43. bili := "-%"
  44. if utils.StrToFloat64(before[0][types]) > 0 {
  45. tmpDiff := utils.StrToFloat64(after[0][types]) - utils.StrToFloat64(before[0][types])
  46. types1 := "+"
  47. if tmpDiff < 0 {
  48. types1 = ""
  49. }
  50. bili = types1 + utils.Float64ToStr(tmpDiff/utils.StrToFloat64(before[0][types])*100) + "%"
  51. }
  52. return bili
  53. }
  54. func IndexAppList(c *gin.Context, req md.IndexAppListReq) md.IndexAppListRes {
  55. appId := make([]string, 0)
  56. data := commTotalByApp(c, req, appId)
  57. for _, v := range data {
  58. appId = append(appId, v["app_id"])
  59. }
  60. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  61. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - start) / 86400
  62. req.EndDate = time.Unix(start-1, 0).Format("2006-01-02")
  63. req.StartDate = time.Unix(start-1-day*86400, 0).Format("2006-01-02")
  64. dataLast := commTotalByApp(c, req, appId)
  65. dataLastMap := make(map[string][]map[string]string)
  66. for _, v := range dataLast {
  67. dataLastMap[v["app_id"]] = make([]map[string]string, 0)
  68. dataLastMap[v["app_id"]] = append(dataLastMap[v["app_id"]], v)
  69. }
  70. list := make([]md.IndexAppListData, 0)
  71. for _, v := range data {
  72. _, ok := dataLastMap[v["app_id"]]
  73. if ok == false {
  74. dataLastMap[v["app_id"]] = []map[string]string{
  75. {"media_revenue": "0", "ecpm": "0", "exposure_count": "0", "click_count": "0", "click_rate": "0"},
  76. }
  77. }
  78. tmpList := []map[string]string{
  79. v,
  80. }
  81. dataList := []md.IndexAppListDataList{
  82. {Name: "广告预估收益", Type: "media_revenue", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)},
  83. {Name: "ECPM", Type: "ecpm", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "ecpm"), Value: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100 / utils.StrToFloat64(v["count"]))},
  84. {Name: "曝光量", Type: "exposure_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "exposure_count"), Value: v["exposure_count"]},
  85. {Name: "点击量", Type: "click_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_count"), Value: v["click_count"]},
  86. {Name: "点击率", Type: "click_rate", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_rate"), Value: utils.GetPrec(v["click_rate"], "2") + "%"},
  87. }
  88. applet := GetAppletInfo(c, v["app_id"])
  89. tmp := md.IndexAppListData{
  90. Name: applet["name"],
  91. Logo: applet["logo"],
  92. List: dataList,
  93. TableList: []string{},
  94. }
  95. list = append(list, tmp)
  96. }
  97. res := md.IndexAppListRes{
  98. List: list,
  99. Sort: []md.SelectData{
  100. {Name: "按收益最高", Value: "media_revenue desc"},
  101. {Name: "按ECPM最高", Value: "ecpm desc"},
  102. {Name: "按曝光量最高", Value: "exposure_count desc"},
  103. },
  104. }
  105. return res
  106. }
  107. func commTotal(c *gin.Context, startDate, endDate string) []map[string]string {
  108. sql := `
  109. SELECT
  110. SUM(exposure_count) as exposure_count,
  111. SUM(click_count) as click_count,
  112. SUM(click_rate) as click_rate,
  113. SUM(ecpm) as ecpm,
  114. SUM(media_revenue) as media_revenue
  115. FROM generate_wx_ad_data
  116. where %s
  117. `
  118. user := GetUser(c)
  119. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId)
  120. if startDate != "" {
  121. where += " and date>='" + startDate + "'"
  122. }
  123. if endDate != "" {
  124. where += " and date<='" + endDate + "'"
  125. }
  126. sql = fmt.Sprintf(sql, where)
  127. nativeString, _ := db.QueryNativeString(db.Db, sql)
  128. if len(nativeString) == 0 {
  129. nativeString = []map[string]string{
  130. {
  131. "media_revenue": "0",
  132. },
  133. }
  134. }
  135. return nativeString
  136. }
  137. func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []map[string]string {
  138. sql := `
  139. SELECT
  140. COUNT(*) as count,
  141. app_id as app_id,
  142. SUM(exposure_count) as exposure_count,
  143. SUM(click_count) as click_count,
  144. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  145. SUM(ecpm) as ecpm,
  146. SUM(media_revenue) as media_revenue
  147. FROM generate_wx_ad_data
  148. where %s group by app_id order by %s,id asc limit 10
  149. `
  150. user := GetUser(c)
  151. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId)
  152. if req.StartDate != "" {
  153. where += " and date>='" + req.StartDate + "'"
  154. }
  155. if req.EndDate != "" {
  156. where += " and date<='" + req.EndDate + "'"
  157. }
  158. if len(appId) > 0 {
  159. str := ""
  160. for _, v := range appId {
  161. if str == "" {
  162. str += "'" + v + "'"
  163. } else {
  164. str += ",'" + v + "'"
  165. }
  166. }
  167. where += " and app_id in(" + str + ")"
  168. }
  169. if req.Sort == "" {
  170. req.Sort = "media_revenue desc"
  171. }
  172. sql = fmt.Sprintf(sql, where, req.Sort)
  173. nativeString, _ := db.QueryNativeString(db.Db, sql)
  174. return nativeString
  175. }
  176. func BeforeSevenPoint(c *gin.Context, req md.IndexAppListTableReq) map[string][]string {
  177. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  178. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-start)/86400 + 1
  179. first := start - day*86400*6
  180. dayAll := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-first)/86400 + 1
  181. firstTime := time.Unix(first, 0).Format("2006-01-02")
  182. appData := make(map[string][]string)
  183. for _, v := range req.AppId {
  184. appData[v] = make([]string, dayAll/day+1, dayAll/day+1)
  185. }
  186. dateList := make(map[int]string)
  187. j := 1
  188. for i := 0; i < int(dayAll); i++ {
  189. if i >= (j-1)*int(day) && i < j*int(day) {
  190. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  191. continue
  192. }
  193. j++
  194. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  195. }
  196. if len(req.AppId) > 0 {
  197. date := commTotalByDate(c, firstTime, req.EndDate, req.AppId)
  198. for _, v := range date {
  199. for k1, v1 := range dateList {
  200. if strings.Contains(v1, v["date"]) {
  201. appData[v["app_id"]][k1] = utils.Float64ToStr(utils.StrToFloat64(appData[v["app_id"]][k1]) + utils.StrToFloat64(v["media_revenue"])/100)
  202. }
  203. }
  204. }
  205. }
  206. for k, v := range appData {
  207. for k1, v1 := range v {
  208. if v1 == "" {
  209. appData[k][k1] = "0"
  210. }
  211. }
  212. }
  213. return appData
  214. }
  215. func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string {
  216. sql := `
  217. SELECT
  218. app_id as app_id,
  219. date as date,
  220. SUM(exposure_count) as exposure_count,
  221. SUM(click_count) as click_count,
  222. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  223. SUM(ecpm) as ecpm,
  224. SUM(media_revenue) as media_revenue
  225. FROM generate_wx_ad_data
  226. where %s group by date,app_id
  227. `
  228. where := "uuid=" + c.GetString("mid")
  229. if startDate != "" {
  230. where += " and date>='" + startDate + "'"
  231. }
  232. if endDate != "" {
  233. where += " and date<='" + endDate + "'"
  234. }
  235. if len(appId) > 0 {
  236. str := ""
  237. for _, v := range appId {
  238. if str == "" {
  239. str += "'" + v + "'"
  240. } else {
  241. str += ",'" + v + "'"
  242. }
  243. }
  244. where += " and app_id in(" + str + ")"
  245. }
  246. sql = fmt.Sprintf(sql, where)
  247. nativeString, err := db.QueryNativeString(db.Db, sql)
  248. fmt.Println(err)
  249. return nativeString
  250. }