浏览代码

增加积分余额脚本

boweniac 4 天之前
父节点
当前提交
d3a5874260
共有 2 个文件被更改,包括 56 次插入29 次删除
  1. 25 1
      crontask/compute_historical_credit.go
  2. 31 28
      crontask/init.go

+ 25 - 1
crontask/compute_historical_credit.go

@@ -6,6 +6,7 @@ import (
 	"strconv"
 	"time"
 	"wechat-api/ent"
+	"wechat-api/ent/creditbalance"
 	"wechat-api/ent/creditusage"
 	"wechat-api/ent/usagedetail"
 )
@@ -51,11 +52,14 @@ func (l *CronTask) computeHistoricalCredit() {
 
 	// 获取当前时间
 	//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)
+	start := time.Date(2025, 4, 1, 0, 0, 0, 0, time.Local)
 	end := time.Date(2025, 4, 24, 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)
 
+	balanceSet := make(map[uint64]float64)
+
 	for now := end; !now.Before(start); now = now.Add(-time.Hour) {
 		fmt.Println(now.Format("2006-01-02 15:00:00"))
 
@@ -92,6 +96,7 @@ func (l *CronTask) computeHistoricalCredit() {
 						creditusage.NidEQ(usageDetail.ID),
 					).Count(l.ctx)
 					if hourDataCount == 0 {
+						balanceSet[orgID] += usageDetail.Credits
 						_, err = l.svcCtx.DB.CreditUsage.Create().
 							SetNotNilNumber(&usageDetail.Credits).
 							SetNtype(1).
@@ -105,5 +110,24 @@ func (l *CronTask) computeHistoricalCredit() {
 			}
 		}
 	}
+
+	for orgID, balance := range balanceSet {
+		hourDataCount, _ := l.svcCtx.DB.CreditBalance.Query().Where(
+			creditbalance.OrganizationIDEQ(orgID),
+		).Count(l.ctx)
+		if hourDataCount == 0 {
+			_, err = l.svcCtx.DB.CreditBalance.Create().
+				SetBalance(balance).
+				SetOrganizationID(orgID).
+				Save(l.ctx)
+			l.Errorf("save hour data error:%v \n", err)
+		} else {
+			_, err = l.svcCtx.DB.CreditBalance.Update().
+				Where(creditbalance.OrganizationIDEQ(orgID)).
+				SetBalance(balance).
+				Save(l.ctx)
+			l.Errorf("save hour data error:%v \n", err)
+		}
+	}
 	return
 }

+ 31 - 28
crontask/init.go

@@ -24,39 +24,42 @@ func NewCronTask(ctx context.Context, svcCtx *svc.ServiceContext) *CronTask {
 
 func ScheduleRun(c *cron.Cron, serverCtx *svc.ServiceContext) {
 
-	l := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		l.sendMsg()
-	})
-
-	sendWx := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		sendWx.sendWx()
-	})
-
-	sendWxOnTimeout := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("* * * * *", func() {
-		sendWxOnTimeout.sendWxOnTimeout()
-	})
-
-	computeStatistic := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("0 * * * *", func() {
-		computeStatistic.computeStatistic()
-	})
-
-	//syncWx := NewCronTask(context.Background(), serverCtx)
-	//c.AddFunc("*/30 * * * *", func() {
-	//	syncWx.syncWx()
+	//l := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	l.sendMsg()
+	//})
+	//
+	//sendWx := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	sendWx.sendWx()
+	//})
+	//
+	//sendWxOnTimeout := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("* * * * *", func() {
+	//	sendWxOnTimeout.sendWxOnTimeout()
+	//})
+	//
+	//computeStatistic := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("0 * * * *", func() {
+	//	computeStatistic.computeStatistic()
+	//})
+	//
+	////syncWx := NewCronTask(context.Background(), serverCtx)
+	////c.AddFunc("*/30 * * * *", func() {
+	////	syncWx.syncWx()
+	////})
+	//
+	//contactForm := NewCronTask(context.Background(), serverCtx)
+	//c.AddFunc("1 0 * * *", func() {
+	//	contactForm.analyze()
 	//})
-
-	contactForm := NewCronTask(context.Background(), serverCtx)
-	c.AddFunc("1 0 * * *", func() {
-		contactForm.analyze()
-	})
 
 	computeHistoricalStatistic := NewCronTask(context.Background(), serverCtx)
 	computeHistoricalStatistic.computeHistoricalStatistic()
 
+	computeHistoricalCredit := NewCronTask(context.Background(), serverCtx)
+	computeHistoricalCredit.computeHistoricalCredit()
+
 	//l = NewCronTask(context.Background(), serverCtx)
 	//c.AddFunc("* * * * *", func() {
 	//	MaxWorker := 10