From 2d911d76bffda47005f8a30eb8f3719316bf43c9 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 26 六月 2024 16:29:47 +0800
Subject: [PATCH] update doc

---
 task/month_stats.go |  102 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/task/month_stats.go b/task/month_stats.go
index 2b33ef6..6d82bb4 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,18 +99,18 @@
 			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))
 			for _, unitItem := range product.MoreUnitList {
-				moreValueArr = append(moreValueArr, models.UnitItems{
-					Amount:   amount.Mul(unitItem.Amount),
-					Unit:     unitItem.Unit,
-					Floating: unitItem.Floating,
-				})
-				bys, _ := json.Marshal(moreValueArr)
-				moreUnits = string(bys)
+				if !amount.IsZero() {
+					moreValueArr = append(moreValueArr, models.UnitItems{
+						Amount:   amount.Mul(unitItem.Amount),
+						Unit:     unitItem.Unit,
+						Floating: unitItem.Floating,
+					})
+				}
 
 				if !inputMap[productId].IsZero() {
 					inputMoreValueArr = append(inputMoreValueArr, models.UnitItems{
@@ -105,9 +118,6 @@
 						Unit:     unitItem.Unit,
 						Floating: unitItem.Floating,
 					})
-					bys, _ = json.Marshal(inputMoreValueArr)
-					inputMoreUnits = string(bys)
-
 				}
 
 				if !outputMap[productId].IsZero() {
@@ -116,37 +126,53 @@
 						Unit:     unitItem.Unit,
 						Floating: unitItem.Floating,
 					})
-					bys, _ = json.Marshal(outputMoreValueArr)
-					outputMoreUnits = string(bys)
-
 				}
 			}
+			bys, _ := json.Marshal(moreValueArr)
+			if len(moreValueArr) > 0 {
+				fmt.Println(moreValueArr)
+			}
+			moreUnits = string(bys)
+			bys, _ = json.Marshal(inputMoreValueArr)
+			inputMoreUnits = string(bys)
+			bys, _ = json.Marshal(outputMoreValueArr)
+			outputMoreUnits = string(bys)
 		}
 
 		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