From e5df488268e29b272932e6cc1d2b1e7034590ba0 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 12 六月 2024 11:40:45 +0800
Subject: [PATCH] weedfs包放到项目里

---
 task/month_stats.go |   73 ++++++++++++++++++++++++------------
 1 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/task/month_stats.go b/task/month_stats.go
index 2b33ef6..7a507f0 100644
--- a/task/month_stats.go
+++ b/task/month_stats.go
@@ -2,28 +2,35 @@
 
 import (
 	"encoding/json"
+	"fmt"
 	"github.com/shopspring/decimal"
+	"gorm.io/gorm"
 	"time"
 	"wms/constvar"
 	"wms/models"
 	"wms/pkg/logx"
+	"wms/service"
 )
 
-func MonthStats() {
+func MonthStats() (err error) {
 	//鍔犻攣锛屽彧闇�瑕佷竴涓繘绋嬭繍琛屾浠诲姟
 	var (
 		lockName  = "monthStats"
 		serviceID = "wms"
 	)
-	err := models.NewLockSearch().AcquireLock(lockName, serviceID)
+	err = models.NewLockSearch().AcquireLock(lockName, serviceID)
 	if err != nil {
 		logx.Errorf("MonthStats AcquireLock err:%v", err)
 		return
 	}
 	defer func() {
-		err := models.NewLockSearch().ReleaseLock(lockName, serviceID)
+		if err != nil {
+			service.SendAlarm("鏈堝害缁熻鎵ц澶辫触", "鎶ラ敊: "+err.Error())
+		}
+		err = models.NewLockSearch().ReleaseLock(lockName, serviceID)
 		if err != nil {
 			logx.Errorf("MonthStats ReleaseLock err:%v", err)
+			service.SendAlarm("鏈堝害缁熻鎵ц鎴愬姛瑙i攣澶辫触", err.Error())
 		}
 	}()
 
@@ -65,6 +72,12 @@
 		return
 	}
 	var record models.MonthStats
+
+	err = models.NewMonthStatsSearch().SetDate(date).Delete()
+	if err != nil {
+		return
+	}
+
 	for _, groupSum := range groupSumList {
 		productId := groupSum.Class
 		if productMap[productId] == nil {
@@ -86,7 +99,7 @@
 			inputMoreUnits  string
 			outputMoreUnits string
 		)
-		if product.MoreUnit {
+		if *product.MoreUnit {
 			moreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
 			inputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
 			outputMoreValueArr := make([]models.UnitItems, 0, len(product.MoreUnitList))
@@ -124,29 +137,39 @@
 		}
 
 		record.BeginMoreUnits = moreUnits
-		err = models.NewMonthStatsSearch().Create(&record)
-		if err != nil {
-			logx.Errorf("NewMonthStatsSearch Create err:%v, record: %+v", err, record)
-		}
-
-		if oldRecordsMap[productId] != nil && (!inputMap[productId].IsZero() || !outputMap[productId].IsZero()) {
-			record.InputAmount = inputMap[productId]
-			record.InputMoreUnits = inputMoreUnits
-			record.OutputAmount = outputMap[productId]
-			record.OutputMoreUnits = outputMoreUnits
-			m := map[string]interface{}{
-				"input_amount":      inputMap[productId],
-				"input_more_units":  inputMoreUnits,
-				"output_amount":     outputMap[productId],
-				"output_more_units": outputMoreUnits,
-				"end_more_units":    moreUnits,
-				"end_amount":        amount,
-			}
-			err = models.NewMonthStatsSearch().SetID(oldRecordsMap[productId].Id).UpdateByMap(m)
+		err = models.WithTransaction(func(db *gorm.DB) error {
+			err = models.NewMonthStatsSearch().SetOrm(db).Create(&record)
 			if err != nil {
-				logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[productId].ID, m)
+				logx.Errorf("NewMonthStatsSearch Create err:%v, record: %+v", err, record)
+				service.SendAlarm("鏈堝害缁熻鍒涘缓鏈湀澶辫触", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record))
 			}
-		}
+
+			if oldRecordsMap[productId] != nil {
+				m := map[string]interface{}{
+					"end_more_units": moreUnits,
+					"end_amount":     amount,
+				}
+				if !inputMap[productId].IsZero() || !outputMap[productId].IsZero() {
+					record.InputAmount = inputMap[productId]
+					record.InputMoreUnits = inputMoreUnits
+					record.OutputAmount = outputMap[productId]
+					record.OutputMoreUnits = outputMoreUnits
+					m["input_amount"] = inputMap[productId]
+					m["input_more_units"] = inputMoreUnits
+					m["output_amount"] = outputMap[productId]
+					m["output_more_units"] = outputMoreUnits
+				}
+				err = models.NewMonthStatsSearch().SetOrm(db).SetID(oldRecordsMap[productId].Id).UpdateByMap(m)
+				if err != nil {
+					logx.Errorf("NewMonthStatsSearch UpdateByMap err:%v, id:%v, m:%+v", err, oldRecordsMap[productId].ID, m)
+					service.SendAlarm("鏈堝害缁熻鏇存敼涓婃湀澶辫触", fmt.Sprintf("NewMonthStatsSearch Create err:%v, record: %+v", err, record))
+				}
+			}
+			return nil
+		})
+	}
+	if err == nil {
+		service.SendAlarm("鏈堝害缁熻鎵ц鎴愬姛", "")
 	}
 	return
 }

--
Gitblit v1.8.0