userSeeAds.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "designs/global"
  4. "designs/model"
  5. "fmt"
  6. "time"
  7. )
  8. // 汇总每日的广告数据
  9. func SeeAdsSummary(lastDay string) {
  10. //计算出上一日的广告数据汇总
  11. var adsSummary []struct {
  12. Count int `json:"count" gorm:"column:count"`
  13. Pf string `json:"pf" gorm:"column:pf"`
  14. Gid string `json:"gid" gorm:"column:gid"`
  15. SumType1 int `json:"sumType1" gorm:"column:sumType1"`
  16. SumType2 int `json:"sumType2" gorm:"column:sumType2"`
  17. SumType0 int `json:"sumType0" gorm:"column:sumType0"`
  18. }
  19. err := global.App.DB.Table("user_see_ads").
  20. Where("date", lastDay).
  21. Group("user_see_ads.gid,user_see_ads.pf").
  22. Select("count(*) as count", "user_see_ads.pf", "user_see_ads.gid",
  23. "SUM(adsState = 1) AS sumType1",
  24. "SUM(adsState = 2) AS sumType2",
  25. "SUM(adsState = 0) AS sumType0").
  26. Scan(&adsSummary).Error
  27. if err != nil {
  28. global.App.Log.Error("查询广告汇总数据报错:", err)
  29. return
  30. }
  31. var adsSummaryToday []struct {
  32. TodayCount int `json:"todayCount" gorm:"column:todayCount"`
  33. Pf string `json:"pf" gorm:"column:pf"`
  34. Gid string `json:"gid" gorm:"column:gid"`
  35. TodaySumType1 int `json:"todaySumType1" gorm:"column:todaySumType1"`
  36. TodaySumType2 int `json:"todaySumType2" gorm:"column:todaySumType2"`
  37. TodaySumType0 int `json:"todaySumType0" gorm:"column:todaySumType0"`
  38. }
  39. query := global.App.DB.Table("user").WhereRaw("DATE_FORMAT(createdAt, '%Y%m%d') = ?", lastDay).Select("id").SubQuery()
  40. err = global.App.DB.Table("user_see_ads").
  41. Where("date", lastDay).
  42. Group("user_see_ads.gid,user_see_ads.pf").
  43. LeftJoin("user", "user.pf = user_see_ads.pf and user.gid = user_see_ads.gid and user.userId = user_see_ads.userId").
  44. WhereRaw("user.id in (?)", query).
  45. Select("count(*) as todayCount", "user_see_ads.pf", "user_see_ads.gid",
  46. "SUM(adsState = 1) AS todaySumType1",
  47. "SUM(adsState = 2) AS todaySumType2",
  48. "SUM(adsState = 0) AS todaySumType0").
  49. Scan(&adsSummaryToday).Error
  50. if err != nil {
  51. global.App.Log.Error("查询当日注册广告汇总数据报错:", err)
  52. return
  53. }
  54. var SummaryUserSeeAds []model.SummaryUserSeeAds
  55. now := model.XTime{
  56. Time: time.Now(),
  57. }
  58. for _, summary := range adsSummary {
  59. ds := model.SummaryUserSeeAds{
  60. Date: lastDay,
  61. Count: summary.Count,
  62. Pf: summary.Pf,
  63. Gid: summary.Gid,
  64. SumType1: summary.SumType1,
  65. SumType2: summary.SumType2,
  66. SumType0: summary.SumType0,
  67. CreatedAt: now,
  68. }
  69. for _, SummaryToday := range adsSummaryToday {
  70. if SummaryToday.Pf == ds.Pf && SummaryToday.Gid == ds.Gid {
  71. ds.TodaySumType1 = SummaryToday.TodaySumType1
  72. ds.TodaySumType2 = SummaryToday.TodaySumType2
  73. ds.TodaySumType0 = SummaryToday.TodaySumType0
  74. ds.TodayCount = SummaryToday.TodayCount
  75. break
  76. }
  77. }
  78. SummaryUserSeeAds = append(SummaryUserSeeAds, ds)
  79. }
  80. //将汇总数据存入数据库中
  81. err = global.App.DB.Table("summary_user_see_ads").CreateInBatches(&SummaryUserSeeAds, 100).Error
  82. if err != nil {
  83. global.App.Log.Error("存入统计数据失败", err.Error())
  84. fmt.Println(err.Error())
  85. return
  86. }
  87. }