广告平台(站长使用)
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

316 satır
13 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. yesterdayTotalAgent := commTotalAgent(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. beforeYesterDayTotalAgent := commTotalAgent(c, time.Unix(yesterday["start"]-86400, 0).Format("2006-01-02"), time.Unix(yesterday["start"]-3600, 0).Format("2006-01-02"))
  20. //7天
  21. withinSevenDays := utils.GetTimeRange("new_within_seven_days")
  22. withinSevenDaysTotal := commTotal(c, time.Unix(withinSevenDays["start"], 0).Format("2006-01-02"), time.Unix(withinSevenDays["end"]-3600, 0).Format("2006-01-02"))
  23. withinSevenDaysTotalAgent := commTotalAgent(c, time.Unix(withinSevenDays["start"], 0).Format("2006-01-02"), time.Unix(withinSevenDays["end"]-3600, 0).Format("2006-01-02"))
  24. //7天前的 7天
  25. 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"))
  26. 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"))
  27. //本月
  28. currentMonth := utils.GetTimeRange("current_month")
  29. currentMonthTotal := commTotal(c, time.Unix(currentMonth["start"], 0).Format("2006-01-02"), time.Unix(currentMonth["end"]-3600, 0).Format("2006-01-02"))
  30. currentMonthTotalAgent := commTotalAgent(c, time.Unix(currentMonth["start"], 0).Format("2006-01-02"), time.Unix(currentMonth["end"]-3600, 0).Format("2006-01-02"))
  31. //上月
  32. lastMonth := utils.GetTimeRange("last_month")
  33. lastMonthTotal := commTotal(c, time.Unix(lastMonth["start"], 0).Format("2006-01-02"), time.Unix(lastMonth["end"]-3600, 0).Format("2006-01-02"))
  34. lastMonthTotalAgent := commTotalAgent(c, time.Unix(lastMonth["start"], 0).Format("2006-01-02"), time.Unix(lastMonth["end"]-3600, 0).Format("2006-01-02"))
  35. //上上月
  36. beforeLastMonth := time.Unix(lastMonth["start"]-86400, 0)
  37. beforeLastMonthStr := time.Date(beforeLastMonth.Year(), beforeLastMonth.Month(), 1, 0, 0, 0, 0, beforeLastMonth.Location())
  38. beforeLastMonthTotal := commTotal(c, beforeLastMonthStr.Format("2006-01-02"), time.Unix(lastMonth["start"]-3600, 0).Format("2006-01-02"))
  39. beforeLastMonthTotalAgent := commTotalAgent(c, beforeLastMonthStr.Format("2006-01-02"), time.Unix(lastMonth["start"]-3600, 0).Format("2006-01-02"))
  40. res := []md.IndexAppListDataList{
  41. {Name: "昨日收益(媒体)", Type: "yesterday", Bili: commBili(c, beforeYesterDayTotal, yesterdayTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(yesterdayTotal[0]["media_revenue"]) / 100)},
  42. {Name: "七日收益(媒体)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotal, withinSevenDaysTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(withinSevenDaysTotal[0]["media_revenue"]) / 100)},
  43. {Name: "本月收益(媒体)", Type: "current_month", Bili: commBili(c, lastMonthTotal, currentMonthTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(currentMonthTotal[0]["media_revenue"]) / 100)},
  44. {Name: "上月收益(媒体)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotal, lastMonthTotal, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(lastMonthTotal[0]["media_revenue"]) / 100)},
  45. {Name: "昨日曝光量(媒体)", Type: "yesterday", Bili: commBili(c, beforeYesterDayTotal, yesterdayTotal, "exposure_count"), Value: yesterdayTotal[0]["exposure_count"]},
  46. {Name: "七日曝光量(媒体)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotal, withinSevenDaysTotal, "exposure_count"), Value: withinSevenDaysTotal[0]["exposure_count"]},
  47. {Name: "本月曝光量(媒体)", Type: "current_month", Bili: commBili(c, lastMonthTotal, currentMonthTotal, "exposure_count"), Value: currentMonthTotal[0]["exposure_count"]},
  48. {Name: "上月曝光量(媒体)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotal, lastMonthTotal, "exposure_count"), Value: lastMonthTotal[0]["exposure_count"]},
  49. {Name: "昨日收益(渠道)", Type: "yesterday", Bili: commBili(c, beforeYesterDayTotalAgent, yesterdayTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(yesterdayTotalAgent[0]["media_revenue"]) / 100)},
  50. {Name: "七日收益(渠道)", Type: "within_seven_days", Bili: commBili(c, beforeWithinSevenDaysTotalAgent, withinSevenDaysTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(withinSevenDaysTotalAgent[0]["media_revenue"]) / 100)},
  51. {Name: "本月收益(渠道)", Type: "current_month", Bili: commBili(c, lastMonthTotalAgent, currentMonthTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(currentMonthTotalAgent[0]["media_revenue"]) / 100)},
  52. {Name: "上月收益(渠道)", Type: "last_month", Bili: commBili(c, beforeLastMonthTotalAgent, lastMonthTotalAgent, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(lastMonthTotalAgent[0]["media_revenue"]) / 100)},
  53. }
  54. e.OutSuc(c, res, nil)
  55. return
  56. }
  57. func commBili(c *gin.Context, before, after []map[string]string, types string) string {
  58. bili := "-%"
  59. if utils.StrToFloat64(before[0][types]) > 0 {
  60. tmpDiff := utils.StrToFloat64(after[0][types]) - utils.StrToFloat64(before[0][types])
  61. types1 := "+"
  62. if tmpDiff < 0 {
  63. types1 = ""
  64. }
  65. bili = types1 + utils.Float64ToStr(tmpDiff/utils.StrToFloat64(before[0][types])*100) + "%"
  66. }
  67. return bili
  68. }
  69. func commTotalAgent(c *gin.Context, startDate, endDate string) []map[string]string {
  70. sql := `
  71. SELECT
  72. SUM(agent_revenue+extra_revenue) as media_revenue
  73. FROM generate_wx_ad_data_with_agent_flow
  74. where %s
  75. `
  76. where := "uuid=" + c.GetString("mid")
  77. if startDate != "" {
  78. where += " and date>='" + startDate + "'"
  79. }
  80. if endDate != "" {
  81. where += " and date<='" + endDate + "'"
  82. }
  83. sql = fmt.Sprintf(sql, where)
  84. nativeString, _ := db.QueryNativeString(db.Db, sql)
  85. if len(nativeString) == 0 {
  86. nativeString = []map[string]string{
  87. {
  88. "media_revenue": "0",
  89. },
  90. }
  91. }
  92. return nativeString
  93. }
  94. func BeforeSevenPoint(c *gin.Context, req md.IndexAppListTableReq) map[string][]string {
  95. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  96. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-start)/86400 + 1
  97. first := start - day*86400*6
  98. dayAll := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-first)/86400 + 1
  99. firstTime := time.Unix(first, 0).Format("2006-01-02")
  100. appData := make(map[string][]string)
  101. for _, v := range req.AppId {
  102. appData[v] = make([]string, dayAll/day+1, dayAll/day+1)
  103. }
  104. dateList := make(map[int]string)
  105. j := 1
  106. for i := 0; i < int(dayAll); i++ {
  107. if i >= (j-1)*int(day) && i < j*int(day) {
  108. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  109. continue
  110. }
  111. j++
  112. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  113. }
  114. if len(req.AppId) > 0 {
  115. date := commTotalByDate(c, firstTime, req.EndDate, req.AppId)
  116. for _, v := range date {
  117. for k1, v1 := range dateList {
  118. if strings.Contains(v1, v["date"]) {
  119. appData[v["app_id"]][k1] = utils.Float64ToStr(utils.StrToFloat64(appData[v["app_id"]][k1]) + utils.StrToFloat64(v["media_revenue"])/100)
  120. }
  121. }
  122. }
  123. }
  124. for k, v := range appData {
  125. for k1, v1 := range v {
  126. if v1 == "" {
  127. appData[k][k1] = "0"
  128. }
  129. }
  130. }
  131. return appData
  132. }
  133. func IndexAppList(c *gin.Context, req md.IndexAppListReq) md.IndexAppListRes {
  134. appId := make([]string, 0)
  135. data := commTotalByApp(c, req, appId)
  136. for _, v := range data {
  137. appId = append(appId, v["app_id"])
  138. }
  139. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  140. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - start) / 86400
  141. req.EndDate = time.Unix(start-1, 0).Format("2006-01-02")
  142. req.StartDate = time.Unix(start-1-day*86400, 0).Format("2006-01-02")
  143. dataLast := commTotalByApp(c, req, appId)
  144. dataLastMap := make(map[string][]map[string]string)
  145. for _, v := range dataLast {
  146. dataLastMap[v["app_id"]] = make([]map[string]string, 0)
  147. dataLastMap[v["app_id"]] = append(dataLastMap[v["app_id"]], v)
  148. }
  149. list := make([]md.IndexAppListData, 0)
  150. for _, v := range data {
  151. _, ok := dataLastMap[v["app_id"]]
  152. if ok == false {
  153. dataLastMap[v["app_id"]] = []map[string]string{
  154. {"media_revenue": "0", "ecpm": "0", "exposure_count": "0", "click_count": "0", "click_rate": "0"},
  155. }
  156. }
  157. tmpList := []map[string]string{
  158. v,
  159. }
  160. dataList := []md.IndexAppListDataList{
  161. {Name: "广告预估收益", Type: "media_revenue", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "media_revenue"), Value: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)},
  162. {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"]))},
  163. {Name: "曝光量", Type: "exposure_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "exposure_count"), Value: v["exposure_count"]},
  164. {Name: "点击量", Type: "click_count", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_count"), Value: v["click_count"]},
  165. {Name: "点击率", Type: "click_rate", Bili: commBili(c, dataLastMap[v["app_id"]], tmpList, "click_rate"), Value: utils.GetPrec(v["click_rate"], "2") + "%"},
  166. }
  167. applet := GetAppletInfo(c, v["app_id"])
  168. tmp := md.IndexAppListData{
  169. Name: applet["name"],
  170. Logo: applet["logo"],
  171. List: dataList,
  172. TableList: []string{},
  173. }
  174. list = append(list, tmp)
  175. }
  176. res := md.IndexAppListRes{
  177. List: list,
  178. AppId: appId,
  179. Sort: []md.SelectData{
  180. {Name: "按收益最高", Value: "media_revenue desc"},
  181. {Name: "按ECPM最高", Value: "ecpm desc"},
  182. {Name: "按曝光量最高", Value: "exposure_count desc"},
  183. },
  184. }
  185. return res
  186. }
  187. func commTotal(c *gin.Context, startDate, endDate string) []map[string]string {
  188. sql := `
  189. SELECT
  190. SUM(exposure_count) as exposure_count,
  191. SUM(click_count) as click_count,
  192. SUM(click_rate) as click_rate,
  193. SUM(ecpm) as ecpm,
  194. SUM(media_revenue) as media_revenue
  195. FROM generate_wx_ad_data
  196. where %s
  197. `
  198. where := "uuid=" + c.GetString("mid")
  199. if startDate != "" {
  200. where += " and date>='" + startDate + "'"
  201. }
  202. if endDate != "" {
  203. where += " and date<='" + endDate + "'"
  204. }
  205. sql = fmt.Sprintf(sql, where)
  206. nativeString, _ := db.QueryNativeString(db.Db, sql)
  207. if len(nativeString) == 0 {
  208. nativeString = []map[string]string{
  209. {
  210. "media_revenue": "0",
  211. },
  212. }
  213. }
  214. return nativeString
  215. }
  216. func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []map[string]string {
  217. sql := `
  218. SELECT
  219. COUNT(*) as count,
  220. app_id as app_id,
  221. SUM(exposure_count) as exposure_count,
  222. SUM(click_count) as click_count,
  223. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  224. SUM(ecpm) as ecpm,
  225. SUM(media_revenue) as media_revenue
  226. FROM generate_wx_ad_data
  227. where %s group by app_id order by %s,id asc limit 50
  228. `
  229. where := "uuid=" + c.GetString("mid")
  230. if req.StartDate != "" {
  231. where += " and date>='" + req.StartDate + "'"
  232. }
  233. if req.EndDate != "" {
  234. where += " and date<='" + req.EndDate + "'"
  235. }
  236. if req.Name != "" {
  237. mediumId := GetMediumId(c, req.Name)
  238. ex := strings.Split(mediumId, ",")
  239. str := ""
  240. for _, v := range ex {
  241. if str == "" {
  242. str += "'" + v + "'"
  243. } else {
  244. str += ",'" + v + "'"
  245. }
  246. }
  247. where += " and medium_id in(" + str + ")"
  248. }
  249. if len(appId) > 0 {
  250. str := ""
  251. for _, v := range appId {
  252. if str == "" {
  253. str += "'" + v + "'"
  254. } else {
  255. str += ",'" + v + "'"
  256. }
  257. }
  258. where += " and app_id in(" + str + ")"
  259. }
  260. if req.Sort == "" {
  261. req.Sort = "media_revenue desc"
  262. }
  263. sql = fmt.Sprintf(sql, where, req.Sort)
  264. nativeString, _ := db.QueryNativeString(db.Db, sql)
  265. return nativeString
  266. }
  267. func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string {
  268. sql := `
  269. SELECT
  270. app_id as app_id,
  271. date as date,
  272. SUM(exposure_count) as exposure_count,
  273. SUM(click_count) as click_count,
  274. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  275. SUM(ecpm) as ecpm,
  276. SUM(media_revenue) as media_revenue
  277. FROM generate_wx_ad_data
  278. where %s group by date,app_id
  279. `
  280. where := "uuid=" + c.GetString("mid")
  281. if startDate != "" {
  282. where += " and date>='" + startDate + "'"
  283. }
  284. if endDate != "" {
  285. where += " and date<='" + endDate + "'"
  286. }
  287. if len(appId) > 0 {
  288. str := ""
  289. for _, v := range appId {
  290. if str == "" {
  291. str += "'" + v + "'"
  292. } else {
  293. str += ",'" + v + "'"
  294. }
  295. }
  296. where += " and app_id in(" + str + ")"
  297. }
  298. sql = fmt.Sprintf(sql, where)
  299. nativeString, err := db.QueryNativeString(db.Db, sql)
  300. fmt.Println(err)
  301. return nativeString
  302. }