compute_historical_credit_back.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package crontask
  2. //
  3. //import (
  4. // "fmt"
  5. // "github.com/zeromicro/go-zero/core/logx"
  6. // "strconv"
  7. // "time"
  8. // "wechat-api/ent"
  9. // "wechat-api/ent/creditbalance"
  10. // "wechat-api/ent/creditusage"
  11. // "wechat-api/ent/usagedetail"
  12. // "wechat-api/ent/usagetotal"
  13. //)
  14. //
  15. //func (l *CronTask) computeHistoricalCredit() {
  16. // orgBots := []OrgBot{}
  17. // err := l.svcCtx.DB.UsageDetail.Query().
  18. // GroupBy(usagedetail.FieldOrganizationID, usagedetail.FieldBotID).
  19. // Aggregate().
  20. // Scan(l.ctx, &orgBots)
  21. //
  22. // if err != nil {
  23. // l.Errorf("group usage_detail error: %v", err)
  24. // return
  25. // }
  26. //
  27. // wxbotsSet := make(map[uint64][]string)
  28. // for _, ob := range orgBots {
  29. // wxbotsSet[ob.OrganizationID] = append(wxbotsSet[ob.OrganizationID], ob.BotID)
  30. // }
  31. // logx.Info("wxbotsSet: ", wxbotsSet)
  32. //
  33. // /*
  34. // 计算本小时的数据
  35. // 1. 查询出上小时里所有 usagedetail 内容
  36. // 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  37. // 3. 遍历的时候可能有重复,所以要先检查是否生成了数据,如果有就忽略,没有再生成
  38. // ----------------------------------------------------------------------------------------------------------
  39. // */
  40. //
  41. // // 获取当前时间
  42. // //now := time.Now()
  43. // //start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
  44. // start := time.Date(2025, 4, 1, 0, 0, 0, 0, time.Local)
  45. // end := time.Date(2025, 4, 24, 23, 0, 0, 0, time.Local)
  46. // //start := time.Date(2025, 3, 18, 0, 0, 0, 0, time.Local)
  47. // //end := time.Date(2025, 3, 19, 23, 0, 0, 0, time.Local)
  48. //
  49. // balanceOrgSet := make(map[uint64]float64)
  50. // balanceBotSet := make(map[string]float64)
  51. //
  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. // // 获取本小时的第一分钟
  56. // currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
  57. // //currentHourInt, _ := strconv.Atoi(currentHour.Format("2006010215"))
  58. //
  59. // // 上一个小时的起始时间
  60. // lastHour := currentHour.Add(-time.Hour * 1)
  61. // lastHourInt, _ := strconv.Atoi(lastHour.Format("2006010215"))
  62. //
  63. // for orgID, wxinfos := range wxbotsSet {
  64. // if _, ok := balanceOrgSet[orgID]; !ok {
  65. // balanceOrgSet[orgID] = 0
  66. // }
  67. // for _, wxinfo := range wxinfos {
  68. // if _, ok := balanceBotSet[wxinfo]; !ok {
  69. // balanceBotSet[wxinfo] = 0
  70. // }
  71. // l.Logger.Infof("开始计算小时数据:%d\n", lastHourInt)
  72. //
  73. // // 计算积分消耗
  74. // usageDetails, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  75. // usagedetail.BotID(wxinfo),
  76. // usagedetail.CreatedAtGTE(lastHour),
  77. // usagedetail.CreatedAtLT(currentHour),
  78. // ).Order(ent.Desc(usagedetail.FieldCreatedAt)).All(l.ctx)
  79. //
  80. // for _, usageDetail := range usageDetails {
  81. // balanceBotSet[wxinfo] += usageDetail.Credits
  82. // // 更改积分明细表
  83. // hourDataCount, _ := l.svcCtx.DB.CreditUsage.Query().Where(
  84. // creditusage.TableEQ("usage_detail"),
  85. // creditusage.NidEQ(usageDetail.ID),
  86. // ).Count(l.ctx)
  87. // if hourDataCount == 0 {
  88. // balanceOrgSet[orgID] += usageDetail.Credits
  89. // _, err = l.svcCtx.DB.CreditUsage.Create().
  90. // SetNotNilNumber(&usageDetail.Credits).
  91. // SetNtype(1).
  92. // SetTable("usage_detail").
  93. // SetOrganizationID(orgID).
  94. // SetNid(usageDetail.ID).
  95. // Save(l.ctx)
  96. // l.Errorf("save hour data error:%v \n", err)
  97. // }
  98. // }
  99. // }
  100. // }
  101. // }
  102. //
  103. // for orgID, balance := range balanceOrgSet {
  104. // creditBalance, _ := l.svcCtx.DB.CreditBalance.Query().Where(
  105. // creditbalance.OrganizationIDEQ(orgID),
  106. // ).First(l.ctx)
  107. // if creditBalance == nil {
  108. // _, err = l.svcCtx.DB.CreditBalance.Create().
  109. // SetBalance(balance).
  110. // SetOrganizationID(orgID).
  111. // Save(l.ctx)
  112. // l.Errorf("save hour data error:%v \n", err)
  113. // } else {
  114. // b := creditBalance.Balance - balance
  115. // _, err = l.svcCtx.DB.CreditBalance.Update().
  116. // Where(creditbalance.OrganizationIDEQ(orgID)).
  117. // SetBalance(b).
  118. // Save(l.ctx)
  119. // l.Errorf("save hour data error:%v \n", err)
  120. // }
  121. // }
  122. //
  123. // for botID, balance := range balanceBotSet {
  124. // _, err = l.svcCtx.DB.UsageTotal.Update().
  125. // Where(usagetotal.BotIDEQ(botID)).
  126. // SetCredits(balance).
  127. // Save(l.ctx)
  128. // l.Errorf("save hour data error:%v \n", err)
  129. // }
  130. // return
  131. //}