|
@@ -1,9 +1,7 @@
|
|
|
package crontask
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
- "strconv"
|
|
|
"time"
|
|
|
"wechat-api/ent"
|
|
|
"wechat-api/ent/creditbalance"
|
|
@@ -13,19 +11,6 @@ import (
|
|
|
)
|
|
|
|
|
|
func (l *CronTask) computeHistoricalCredit() {
|
|
|
- // 获取所有机器人信息
|
|
|
- //wxbots, err := l.svcCtx.DB.Wx.Query().Select(wx.FieldWxid, wx.FieldID, wx.FieldOrganizationID).All(l.ctx)
|
|
|
- //if err != nil {
|
|
|
- // l.Errorf("fetch wxids error:%v\n", err)
|
|
|
- // return
|
|
|
- //}
|
|
|
- //
|
|
|
- //wxbotsSet := make(map[uint64][]*ent.Wx)
|
|
|
- //for _, bot := range wxbots {
|
|
|
- // if !strings.HasPrefix(bot.Wxid, "temp-") {
|
|
|
- // wxbotsSet[bot.OrganizationID] = append(wxbotsSet[bot.OrganizationID], bot)
|
|
|
- // }
|
|
|
- //}
|
|
|
orgBots := []OrgBot{}
|
|
|
err := l.svcCtx.DB.UsageDetail.Query().
|
|
|
GroupBy(usagedetail.FieldOrganizationID, usagedetail.FieldBotID).
|
|
@@ -62,69 +47,57 @@ func (l *CronTask) computeHistoricalCredit() {
|
|
|
balanceOrgSet := make(map[uint64]float64)
|
|
|
balanceBotSet := make(map[string]float64)
|
|
|
|
|
|
- for now := end; !now.Before(start); now = now.Add(-time.Hour) {
|
|
|
- fmt.Println(now.Format("2006-01-02 15:00:00"))
|
|
|
-
|
|
|
- // 获取本小时的第一分钟
|
|
|
- currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
|
|
|
- //currentHourInt, _ := strconv.Atoi(currentHour.Format("2006010215"))
|
|
|
-
|
|
|
- // 上一个小时的起始时间
|
|
|
- lastHour := currentHour.Add(-time.Hour * 1)
|
|
|
- lastHourInt, _ := strconv.Atoi(lastHour.Format("2006010215"))
|
|
|
-
|
|
|
- for orgID, wxinfos := range wxbotsSet {
|
|
|
- if _, ok := balanceOrgSet[orgID]; !ok {
|
|
|
- balanceOrgSet[orgID] = 0
|
|
|
+ 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
|
|
|
}
|
|
|
- for _, wxinfo := range wxinfos {
|
|
|
- if _, ok := balanceBotSet[wxinfo]; !ok {
|
|
|
- balanceBotSet[wxinfo] = 0
|
|
|
- }
|
|
|
- l.Logger.Infof("开始计算小时数据:%d\n", lastHourInt)
|
|
|
+ l.Logger.Infof("开始计算小时数据:%d\n", start)
|
|
|
|
|
|
- // 计算积分消耗
|
|
|
- usageDetails, _ := l.svcCtx.DB.UsageDetail.Query().Where(
|
|
|
- usagedetail.BotID(wxinfo),
|
|
|
- usagedetail.CreatedAtGTE(lastHour),
|
|
|
- usagedetail.CreatedAtLT(currentHour),
|
|
|
- ).Order(ent.Desc(usagedetail.FieldCreatedAt)).All(l.ctx)
|
|
|
+ // 计算积分消耗
|
|
|
+ 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 _, 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 {
|
|
|
- hourDataCount, _ := l.svcCtx.DB.CreditBalance.Query().Where(
|
|
|
+ creditBalance, _ := l.svcCtx.DB.CreditBalance.Query().Where(
|
|
|
creditbalance.OrganizationIDEQ(orgID),
|
|
|
).First(l.ctx)
|
|
|
- if hourDataCount == nil {
|
|
|
+ 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 := hourDataCount.Balance - balance
|
|
|
+ b := creditBalance.Balance - balance
|
|
|
_, err = l.svcCtx.DB.CreditBalance.Update().
|
|
|
Where(creditbalance.OrganizationIDEQ(orgID)).
|
|
|
SetBalance(b).
|