compute_historical_credit.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package crontask
  2. import (
  3. "fmt"
  4. "github.com/zeromicro/go-zero/core/logx"
  5. "strconv"
  6. "time"
  7. "wechat-api/ent"
  8. "wechat-api/ent/creditusage"
  9. "wechat-api/ent/usagedetail"
  10. )
  11. func (l *CronTask) computeHistoricalCredit() {
  12. // 获取所有机器人信息
  13. //wxbots, err := l.svcCtx.DB.Wx.Query().Select(wx.FieldWxid, wx.FieldID, wx.FieldOrganizationID).All(l.ctx)
  14. //if err != nil {
  15. // l.Errorf("fetch wxids error:%v\n", err)
  16. // return
  17. //}
  18. //
  19. //wxbotsSet := make(map[uint64][]*ent.Wx)
  20. //for _, bot := range wxbots {
  21. // if !strings.HasPrefix(bot.Wxid, "temp-") {
  22. // wxbotsSet[bot.OrganizationID] = append(wxbotsSet[bot.OrganizationID], bot)
  23. // }
  24. //}
  25. orgBots := []OrgBot{}
  26. err := l.svcCtx.DB.UsageDetail.Query().
  27. GroupBy(usagedetail.FieldOrganizationID, usagedetail.FieldBotID).
  28. Aggregate().
  29. Scan(l.ctx, &orgBots)
  30. if err != nil {
  31. l.Errorf("group usage_detail error: %v", err)
  32. return
  33. }
  34. wxbotsSet := make(map[uint64][]string)
  35. for _, ob := range orgBots {
  36. wxbotsSet[ob.OrganizationID] = append(wxbotsSet[ob.OrganizationID], ob.BotID)
  37. }
  38. logx.Info("wxbotsSet: ", wxbotsSet)
  39. /*
  40. 计算本小时的数据
  41. 1. 查询出上小时里所有 usagedetail 内容
  42. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  43. 3. 遍历的时候可能有重复,所以要先检查是否生成了数据,如果有就忽略,没有再生成
  44. ----------------------------------------------------------------------------------------------------------
  45. */
  46. // 获取当前时间
  47. //now := time.Now()
  48. start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
  49. end := time.Date(2025, 4, 24, 23, 0, 0, 0, time.Local)
  50. //start := time.Date(2025, 3, 18, 0, 0, 0, 0, time.Local)
  51. //end := time.Date(2025, 3, 19, 23, 0, 0, 0, time.Local)
  52. for now := end; !now.Before(start); now = now.Add(-time.Hour) {
  53. fmt.Println(now.Format("2006-01-02 15:00:00"))
  54. // 获取本小时的第一分钟
  55. currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
  56. //currentHourInt, _ := strconv.Atoi(currentHour.Format("2006010215"))
  57. // 上一个小时的起始时间
  58. lastHour := currentHour.Add(-time.Hour * 1)
  59. lastHourInt, _ := strconv.Atoi(lastHour.Format("2006010215"))
  60. var allHourConsumeCoinFloat float64
  61. for orgID, wxinfos := range wxbotsSet {
  62. var orgHourConsumeCoinFloat float64
  63. for _, wxinfo := range wxinfos {
  64. l.Logger.Infof("开始计算小时数据:%d\n", lastHourInt)
  65. // 先判断该账号是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  66. var consumeCoinFloat float64
  67. // 计算积分消耗
  68. usageDetails, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  69. usagedetail.BotID(wxinfo),
  70. usagedetail.CreatedAtGTE(lastHour),
  71. usagedetail.CreatedAtLT(currentHour),
  72. ).Order(ent.Desc(usagedetail.FieldCreatedAt)).All(l.ctx)
  73. allHourConsumeCoinFloat += consumeCoinFloat
  74. orgHourConsumeCoinFloat += consumeCoinFloat
  75. for _, usageDetail := range usageDetails {
  76. // 更改积分明细表
  77. hourDataCount, _ := l.svcCtx.DB.CreditUsage.Query().Where(
  78. creditusage.TableEQ("usage_detail"),
  79. creditusage.NidEQ(usageDetail.ID),
  80. ).Count(l.ctx)
  81. if hourDataCount == 0 {
  82. _, err = l.svcCtx.DB.CreditUsage.Create().
  83. SetNotNilNumber(&usageDetail.Credits).
  84. SetNtype(1).
  85. SetTable("usage_detail").
  86. SetOrganizationID(orgID).
  87. SetNid(usageDetail.ID).
  88. Save(l.ctx)
  89. l.Errorf("save hour data error:%v \n", err)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return
  96. }