广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_operator_index.go 10 KiB

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