userAction.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package crons
  2. import (
  3. "designs/global"
  4. "designs/model"
  5. "fmt"
  6. "os"
  7. "time"
  8. )
  9. func ActiveDelete() {
  10. //读取前一日数据
  11. now := time.Now()
  12. for i := 30; i <= 40; i++ {
  13. last := now.AddDate(0, 0, -i).Format("2006-01-02")
  14. path := "storage" + "/" + last
  15. //删除前一天的文件夹
  16. err := os.RemoveAll(path)
  17. if err != nil {
  18. fmt.Println("删除文件夹失败:"+path, err)
  19. } else {
  20. fmt.Println("删除文件夹完成:" + path)
  21. }
  22. }
  23. }
  24. func OnlineDatabaseDelete() {
  25. //获取到表名
  26. now := time.Now()
  27. date := now.AddDate(0, 0, -29).Format("20060102")
  28. tableName := "user_online_" + date
  29. var tableList []string
  30. sql := "SELECT table_name FROM information_schema.tables WHERE table_schema = 'chunhao' AND table_name LIKE '" + tableName + "%'"
  31. err := global.App.DB.Raw(sql).Pluck("table_name", &tableList).Error
  32. if err != nil {
  33. global.App.Log.Error("查询", date, "user_online数据表失败", err.Error())
  34. return
  35. }
  36. //批量删除数据表
  37. for _, table := range tableList {
  38. sql := "drop table IF EXISTS " + table
  39. err := global.App.DB.Exec(sql).Error
  40. if err != nil {
  41. global.App.Log.Error("删除", table, "数据表失败", err.Error())
  42. return
  43. }
  44. }
  45. global.App.Log.Info(date, "数据表清理完成", tableList)
  46. }
  47. //// 对留存数据进行汇算
  48. //func RemainDataSummary() {
  49. // //计算上一日的留存
  50. // lastDay := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  51. //
  52. // //读取到所有的gid ,根据GID计算这些数据
  53. //
  54. //
  55. //}
  56. //对广告数据进行汇算
  57. func AdsDataSummary() {
  58. lastDay := time.Now().AddDate(0, 0, -1).Format("20060102")
  59. //计算出上一日的广告数据汇总
  60. var adsSummary []struct {
  61. Count int `json:"count" gorm:"column:count"`
  62. Pf string `json:"pf" gorm:"column:pf"`
  63. Gid string `json:"gid" gorm:"column:gid"`
  64. SumType1 string `json:"sumType1" gorm:"column:sumType1"`
  65. SumType2 string `json:"sumType2" gorm:"column:sumType2"`
  66. SumType0 string `json:"sumType0" gorm:"column:sumType0"`
  67. }
  68. err := global.App.DB.Table("user_see_ads").
  69. Where("date", lastDay).
  70. Group("gid,pf").
  71. Select("count(*) as count", "pf", "gid",
  72. "SUM(adsState = 1) AS sumType1",
  73. "SUM(adsState = 2) AS sumType2",
  74. "SUM(adsState = 0) AS sumType0").
  75. Scan(&adsSummary).Error
  76. fmt.Println(adsSummary)
  77. if err != nil {
  78. global.App.Log.Error("查询广告汇总数据报错:", err)
  79. return
  80. }
  81. var SummaryUserSeeAds []model.SummaryUserSeeAds
  82. now := model.XTime{
  83. Time: time.Now(),
  84. }
  85. for _, summary := range adsSummary {
  86. SummaryUserSeeAds = append(SummaryUserSeeAds, model.SummaryUserSeeAds{
  87. Date: lastDay,
  88. Count: summary.Count,
  89. Pf: summary.Pf,
  90. Gid: summary.Gid,
  91. SumType1: summary.SumType1,
  92. SumType2: summary.SumType2,
  93. SumType0: summary.SumType0,
  94. CreatedAt: now,
  95. })
  96. }
  97. //将汇总数据存入数据库中
  98. err = global.App.DB.Table("summary_user_see_ads").CreateInBatches(&SummaryUserSeeAds, 100).Error
  99. if err != nil {
  100. global.App.Log.Error("存入统计数据失败", err.Error())
  101. fmt.Println(err.Error())
  102. return
  103. }
  104. return
  105. }
  106. //// 对打点数据进行汇算
  107. //func ActionDataSummary() {
  108. // lastDay := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  109. //
  110. // //
  111. //}