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

svc_index.go 13 KiB

2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. where += " and medium_id in(" + mediumId + ")"
  239. }
  240. if len(appId) > 0 {
  241. where += " and app_id in('" + strings.Join(appId, ",") + "')"
  242. }
  243. if req.Sort == "" {
  244. req.Sort = "media_revenue desc"
  245. }
  246. sql = fmt.Sprintf(sql, where, req.Sort)
  247. nativeString, _ := db.QueryNativeString(db.Db, sql)
  248. return nativeString
  249. }
  250. func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string {
  251. sql := `
  252. SELECT
  253. app_id as app_id,
  254. date as date,
  255. SUM(exposure_count) as exposure_count,
  256. SUM(click_count) as click_count,
  257. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  258. SUM(ecpm) as ecpm,
  259. SUM(media_revenue) as media_revenue
  260. FROM generate_wx_ad_data
  261. where %s group by date,app_id
  262. `
  263. where := "uuid=" + c.GetString("mid")
  264. if startDate != "" {
  265. where += " and date>='" + startDate + "'"
  266. }
  267. if endDate != "" {
  268. where += " and date<='" + endDate + "'"
  269. }
  270. if len(appId) > 0 {
  271. where += " and app_id in('" + strings.Join(appId, ",") + "')"
  272. }
  273. sql = fmt.Sprintf(sql, where)
  274. nativeString, err := db.QueryNativeString(db.Db, sql)
  275. fmt.Println(err)
  276. return nativeString
  277. }