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

2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 week geleden
2 maanden geleden
1 maand geleden
1 maand geleden
1 maand geleden
2 maanden geleden
1 maand geleden
2 maanden geleden
1 week geleden
2 maanden geleden
1 week geleden
2 maanden geleden
1 maand geleden
1 week geleden
1 maand geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ex := strings.Split(mediumId, ",")
  142. str := ""
  143. for _, v := range ex {
  144. if str == "" {
  145. str += "'" + v + "'"
  146. } else {
  147. str += ",'" + v + "'"
  148. }
  149. }
  150. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + str + ")"
  151. if startDate != "" {
  152. where += " and date>='" + startDate + "'"
  153. }
  154. if endDate != "" {
  155. where += " and date<='" + endDate + "'"
  156. }
  157. sql = fmt.Sprintf(sql, where)
  158. nativeString, _ := db.QueryNativeString(db.Db, sql)
  159. if len(nativeString) == 0 {
  160. nativeString = []map[string]string{
  161. {
  162. "media_revenue": "0",
  163. },
  164. }
  165. }
  166. return nativeString
  167. }
  168. func commTotalAgent(c *gin.Context, startDate, endDate string) []map[string]string {
  169. sql := `
  170. SELECT
  171. SUM(agent_revenue+extra_revenue) as media_revenue
  172. FROM generate_wx_ad_data_with_agent_flow
  173. where %s
  174. `
  175. user := GetUser(c)
  176. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and agent_id =" + utils.IntToStr(user.AgentId)
  177. if startDate != "" {
  178. where += " and date>='" + startDate + "'"
  179. }
  180. if endDate != "" {
  181. where += " and date<='" + endDate + "'"
  182. }
  183. sql = fmt.Sprintf(sql, where)
  184. nativeString, _ := db.QueryNativeString(db.Db, sql)
  185. if len(nativeString) == 0 {
  186. nativeString = []map[string]string{
  187. {
  188. "media_revenue": "0",
  189. },
  190. }
  191. }
  192. return nativeString
  193. }
  194. func commTotalByApp(c *gin.Context, req md.IndexAppListReq, appId []string) []map[string]string {
  195. sql := `
  196. SELECT
  197. COUNT(*) as count,
  198. app_id as app_id,
  199. SUM(exposure_count) as exposure_count,
  200. SUM(click_count) as click_count,
  201. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  202. SUM(ecpm) as ecpm,
  203. SUM(media_revenue) as media_revenue
  204. FROM generate_wx_ad_data
  205. where %s group by app_id order by %s,id asc limit 20
  206. `
  207. mediumId := GetAgentMediumId(c)
  208. ex := strings.Split(mediumId, ",")
  209. str := ""
  210. for _, v := range ex {
  211. if str == "" {
  212. str += "'" + v + "'"
  213. } else {
  214. str += ",'" + v + "'"
  215. }
  216. }
  217. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + str + ")"
  218. if req.StartDate != "" {
  219. where += " and date>='" + req.StartDate + "'"
  220. }
  221. if req.EndDate != "" {
  222. where += " and date<='" + req.EndDate + "'"
  223. }
  224. if len(appId) > 0 {
  225. str1 := ""
  226. for _, v := range appId {
  227. if str1 == "" {
  228. str1 += "'" + v + "'"
  229. } else {
  230. str1 += ",'" + v + "'"
  231. }
  232. }
  233. where += " and app_id in('" + str1 + "')"
  234. }
  235. if req.Sort == "" {
  236. req.Sort = "media_revenue desc"
  237. }
  238. sql = fmt.Sprintf(sql, where, req.Sort)
  239. nativeString, _ := db.QueryNativeString(db.Db, sql)
  240. return nativeString
  241. }
  242. func BeforeSevenPoint(c *gin.Context, req md.IndexAppListTableReq) map[string][]string {
  243. start := utils.TimeStdParseUnix(req.StartDate + " 00:00:00")
  244. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-start)/86400 + 1
  245. first := start - day*86400*6
  246. dayAll := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00")-first)/86400 + 1
  247. firstTime := time.Unix(first, 0).Format("2006-01-02")
  248. appData := make(map[string][]string)
  249. for _, v := range req.AppId {
  250. appData[v] = make([]string, dayAll/day+1, dayAll/day+1)
  251. }
  252. dateList := make(map[int]string)
  253. j := 1
  254. for i := 0; i < int(dayAll); i++ {
  255. if i >= (j-1)*int(day) && i < j*int(day) {
  256. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  257. continue
  258. }
  259. j++
  260. dateList[j-1] += "," + time.Unix(first+int64(i)*86400, 0).Format("2006-01-02")
  261. }
  262. if len(req.AppId) > 0 {
  263. date := commTotalByDate(c, firstTime, req.EndDate, req.AppId)
  264. for _, v := range date {
  265. for k1, v1 := range dateList {
  266. if strings.Contains(v1, v["date"]) {
  267. appData[v["app_id"]][k1] = utils.Float64ToStr(utils.StrToFloat64(appData[v["app_id"]][k1]) + utils.StrToFloat64(v["media_revenue"])/100)
  268. }
  269. }
  270. }
  271. }
  272. for k, v := range appData {
  273. for k1, v1 := range v {
  274. if v1 == "" {
  275. appData[k][k1] = "0"
  276. }
  277. }
  278. }
  279. return appData
  280. }
  281. func commTotalByDate(c *gin.Context, startDate, endDate string, appId []string) []map[string]string {
  282. sql := `
  283. SELECT
  284. app_id as app_id,
  285. date as date,
  286. SUM(exposure_count) as exposure_count,
  287. SUM(click_count) as click_count,
  288. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  289. SUM(ecpm) as ecpm,
  290. SUM(media_revenue) as media_revenue
  291. FROM generate_wx_ad_data
  292. where %s group by date,app_id
  293. `
  294. where := "uuid=" + c.GetString("mid")
  295. if startDate != "" {
  296. where += " and date>='" + startDate + "'"
  297. }
  298. if endDate != "" {
  299. where += " and date<='" + endDate + "'"
  300. }
  301. if len(appId) > 0 {
  302. str1 := ""
  303. for _, v := range appId {
  304. if str1 == "" {
  305. str1 += "'" + v + "'"
  306. } else {
  307. str1 += ",'" + v + "'"
  308. }
  309. }
  310. where += " and app_id in('" + str1 + "')"
  311. }
  312. sql = fmt.Sprintf(sql, where)
  313. nativeString, err := db.QueryNativeString(db.Db, sql)
  314. fmt.Println(err)
  315. return nativeString
  316. }