广告平台(站长下代理使用)
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.

svc_index.go 10 KiB

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