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

219 wiersze
10 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. "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: utils.Float64ToStr(utils.StrToFloat64(yesterdayTotalAgent[0]["media_revenue"]) / 100)},
  47. {Name: "七日收益(佣金)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotalAgent, withinSevenDaysTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(withinSevenDaysTotalAgent[0]["media_revenue"]) / 100)},
  48. {Name: "本月收益(佣金)", Type: "current_month", Bili: commBili(c, lastMonthTotalAgent, currentMonthTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(currentMonthTotalAgent[0]["media_revenue"]) / 100)},
  49. {Name: "上月收益(佣金)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotalAgent, lastMonthTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(lastMonthTotalAgent[0]["media_revenue"]) / 100)},
  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"]) / 100 / utils.StrToFloat64(v["count"]))},
  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. COUNT(*) as count,
  189. app_id as app_id,
  190. SUM(exposure_count) as exposure_count,
  191. SUM(click_count) as click_count,
  192. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  193. SUM(ecpm) as ecpm,
  194. SUM(media_revenue) as media_revenue
  195. FROM generate_wx_ad_data
  196. where %s group by app_id order by %s,id asc limit 20
  197. `
  198. mediumId := GetAgentMediumId(c)
  199. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + mediumId + ")"
  200. if req.StartDate != "" {
  201. where += " and date>='" + req.StartDate + "'"
  202. }
  203. if req.EndDate != "" {
  204. where += " and date<='" + req.EndDate + "'"
  205. }
  206. if len(appId) > 0 {
  207. where += " and app_id in(" + strings.Join(appId, ",") + ")"
  208. }
  209. if req.Sort == "" {
  210. req.Sort = "media_revenue desc"
  211. }
  212. sql = fmt.Sprintf(sql, where, req.Sort)
  213. nativeString, _ := db.QueryNativeString(db.Db, sql)
  214. return nativeString
  215. }