compute_historical_credit.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package crontask
  2. import (
  3. "time"
  4. "wechat-api/ent"
  5. "wechat-api/ent/creditbalance"
  6. "wechat-api/ent/creditusage"
  7. "wechat-api/ent/usagedetail"
  8. "wechat-api/ent/usagetotal"
  9. )
  10. func (l *CronTask) computeHistoricalCredit() {
  11. // 获取所有当时的组织和机器人对应关系
  12. orgBots := []OrgBot{}
  13. err := l.svcCtx.DB.UsageDetail.Query().
  14. GroupBy(usagedetail.FieldOrganizationID, usagedetail.FieldBotID).
  15. Aggregate().
  16. Scan(l.ctx, &orgBots)
  17. if err != nil {
  18. l.Errorf("group usage_detail error: %v", err)
  19. return
  20. }
  21. // 遍历获得每个组织有过哪些机器人
  22. wxbotsSet := make(map[uint64][]string)
  23. for _, ob := range orgBots {
  24. wxbotsSet[ob.OrganizationID] = append(wxbotsSet[ob.OrganizationID], ob.BotID)
  25. }
  26. /*
  27. 计算本小时的数据
  28. 1. 查询出上小时里所有 usagedetail 内容
  29. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  30. 3. 遍历的时候可能有重复,所以要先检查是否生成了数据,如果有就忽略,没有再生成
  31. ----------------------------------------------------------------------------------------------------------
  32. */
  33. // 获取当前时间
  34. //now := time.Now()
  35. //start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
  36. start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
  37. end := time.Date(2025, 4, 27, 23, 0, 0, 0, time.Local)
  38. //start := time.Date(2025, 3, 18, 0, 0, 0, 0, time.Local)
  39. //end := time.Date(2025, 3, 19, 23, 0, 0, 0, time.Local)
  40. balanceOrgSet := make(map[uint64]float64)
  41. balanceBotSet := make(map[string]float64)
  42. for orgID, wxinfos := range wxbotsSet {
  43. // 遍历每个组织
  44. if _, ok := balanceOrgSet[orgID]; !ok {
  45. balanceOrgSet[orgID] = 0
  46. }
  47. for _, wxinfo := range wxinfos {
  48. // 遍历每个组织中曾经有过的机器人
  49. // 建立机器人积分消耗字典,用于累计积分
  50. if _, ok := balanceBotSet[wxinfo]; !ok {
  51. balanceBotSet[wxinfo] = 0
  52. }
  53. l.Logger.Infof("开始计算小时数据:%d\n", start)
  54. // 计算积分消耗
  55. usageDetails, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  56. usagedetail.BotID(wxinfo),
  57. usagedetail.CreatedAtGTE(start),
  58. usagedetail.CreatedAtLT(end),
  59. ).Order(ent.Desc(usagedetail.FieldCreatedAt)).All(l.ctx)
  60. for _, usageDetail := range usageDetails {
  61. balanceBotSet[wxinfo] += usageDetail.Credits
  62. // 更改积分明细表
  63. hourDataCount, _ := l.svcCtx.DB.CreditUsage.Query().Where(
  64. creditusage.TableEQ("usage_detail"),
  65. creditusage.NidEQ(usageDetail.ID),
  66. ).Count(l.ctx)
  67. if hourDataCount == 0 {
  68. balanceOrgSet[orgID] += usageDetail.Credits
  69. _, err = l.svcCtx.DB.CreditUsage.Create().
  70. SetNotNilNumber(&usageDetail.Credits).
  71. SetNtype(1).
  72. SetTable("usage_detail").
  73. SetOrganizationID(orgID).
  74. SetNid(usageDetail.ID).
  75. Save(l.ctx)
  76. l.Errorf("save hour data error:%v \n", err)
  77. }
  78. }
  79. }
  80. }
  81. for orgID, balance := range balanceOrgSet {
  82. creditBalance, _ := l.svcCtx.DB.CreditBalance.Query().Where(
  83. creditbalance.OrganizationIDEQ(orgID),
  84. ).First(l.ctx)
  85. if creditBalance == nil {
  86. _, err = l.svcCtx.DB.CreditBalance.Create().
  87. SetBalance(balance).
  88. SetOrganizationID(orgID).
  89. Save(l.ctx)
  90. l.Errorf("save hour data error:%v \n", err)
  91. } else {
  92. b := creditBalance.Balance + balance
  93. _, err = l.svcCtx.DB.CreditBalance.Update().
  94. Where(creditbalance.OrganizationIDEQ(orgID)).
  95. SetBalance(b).
  96. Save(l.ctx)
  97. l.Errorf("save hour data error:%v \n", err)
  98. }
  99. }
  100. for botID, balance := range balanceBotSet {
  101. _, err = l.svcCtx.DB.UsageTotal.Update().
  102. Where(usagetotal.BotIDEQ(botID)).
  103. SetCredits(balance).
  104. Save(l.ctx)
  105. l.Errorf("save hour data error:%v \n", err)
  106. }
  107. return
  108. }