123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package crontask
- import (
- "time"
- "wechat-api/ent"
- "wechat-api/ent/creditbalance"
- "wechat-api/ent/creditusage"
- "wechat-api/ent/usagedetail"
- "wechat-api/ent/usagetotal"
- )
- func (l *CronTask) computeHistoricalCredit() {
- // 获取所有当时的组织和机器人对应关系
- orgBots := []OrgBot{}
- err := l.svcCtx.DB.UsageDetail.Query().
- GroupBy(usagedetail.FieldOrganizationID, usagedetail.FieldBotID).
- Aggregate().
- Scan(l.ctx, &orgBots)
- if err != nil {
- l.Errorf("group usage_detail error: %v", err)
- return
- }
- // 遍历获得每个组织有过哪些机器人
- wxbotsSet := make(map[uint64][]string)
- for _, ob := range orgBots {
- wxbotsSet[ob.OrganizationID] = append(wxbotsSet[ob.OrganizationID], ob.BotID)
- }
- /*
- 计算本小时的数据
- 1. 查询出上小时里所有 usagedetail 内容
- 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
- 3. 遍历的时候可能有重复,所以要先检查是否生成了数据,如果有就忽略,没有再生成
- ----------------------------------------------------------------------------------------------------------
- */
- // 获取当前时间
- //now := time.Now()
- //start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
- start := time.Date(2024, 12, 25, 0, 0, 0, 0, time.Local)
- end := time.Date(2025, 4, 27, 23, 0, 0, 0, time.Local)
- //start := time.Date(2025, 3, 18, 0, 0, 0, 0, time.Local)
- //end := time.Date(2025, 3, 19, 23, 0, 0, 0, time.Local)
- balanceOrgSet := make(map[uint64]float64)
- balanceBotSet := make(map[string]float64)
- for orgID, wxinfos := range wxbotsSet {
- // 遍历每个组织
- if _, ok := balanceOrgSet[orgID]; !ok {
- balanceOrgSet[orgID] = 0
- }
- for _, wxinfo := range wxinfos {
- // 遍历每个组织中曾经有过的机器人
- // 建立机器人积分消耗字典,用于累计积分
- if _, ok := balanceBotSet[wxinfo]; !ok {
- balanceBotSet[wxinfo] = 0
- }
- l.Logger.Infof("开始计算小时数据:%d\n", start)
- // 计算积分消耗
- usageDetails, _ := l.svcCtx.DB.UsageDetail.Query().Where(
- usagedetail.BotID(wxinfo),
- usagedetail.CreatedAtGTE(start),
- usagedetail.CreatedAtLT(end),
- ).Order(ent.Desc(usagedetail.FieldCreatedAt)).All(l.ctx)
- for _, usageDetail := range usageDetails {
- balanceBotSet[wxinfo] += usageDetail.Credits
- // 更改积分明细表
- hourDataCount, _ := l.svcCtx.DB.CreditUsage.Query().Where(
- creditusage.TableEQ("usage_detail"),
- creditusage.NidEQ(usageDetail.ID),
- ).Count(l.ctx)
- if hourDataCount == 0 {
- balanceOrgSet[orgID] += usageDetail.Credits
- _, err = l.svcCtx.DB.CreditUsage.Create().
- SetNotNilNumber(&usageDetail.Credits).
- SetNtype(1).
- SetTable("usage_detail").
- SetOrganizationID(orgID).
- SetNid(usageDetail.ID).
- Save(l.ctx)
- l.Errorf("save hour data error:%v \n", err)
- }
- }
- }
- }
- for orgID, balance := range balanceOrgSet {
- creditBalance, _ := l.svcCtx.DB.CreditBalance.Query().Where(
- creditbalance.OrganizationIDEQ(orgID),
- ).First(l.ctx)
- if creditBalance == nil {
- _, err = l.svcCtx.DB.CreditBalance.Create().
- SetBalance(balance).
- SetOrganizationID(orgID).
- Save(l.ctx)
- l.Errorf("save hour data error:%v \n", err)
- } else {
- b := creditBalance.Balance + balance
- _, err = l.svcCtx.DB.CreditBalance.Update().
- Where(creditbalance.OrganizationIDEQ(orgID)).
- SetBalance(b).
- Save(l.ctx)
- l.Errorf("save hour data error:%v \n", err)
- }
- }
- for botID, balance := range balanceBotSet {
- _, err = l.svcCtx.DB.UsageTotal.Update().
- Where(usagetotal.BotIDEQ(botID)).
- SetCredits(balance).
- Save(l.ctx)
- l.Errorf("save hour data error:%v \n", err)
- }
- return
- }
|