广告平台(站长使用)
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_finance_index.go 13 KiB

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