From dd5dbeacdc143ef51202638d8c5b52f563451596 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 12 四月 2024 15:11:48 +0800
Subject: [PATCH] 总差计算 & bug fix

---
 service/fineness.go |   37 ++++++++++++++++++++++++++++++++++---
 1 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/service/fineness.go b/service/fineness.go
index 4731d49..52ea7f8 100644
--- a/service/fineness.go
+++ b/service/fineness.go
@@ -31,7 +31,9 @@
 	for _, finenessItem := range finenessRegister.FinenessList {
 		roundedHalfFineness := ToRoundedHalfFineness(finenessItem.Fineness)
 		fineness := decimal.NewFromFloat32(roundedHalfFineness)
-		allFinenessList = append(allFinenessList, fineness)
+		for i := 0; i < finenessItem.Quantity; i++ {
+			allFinenessList = append(allFinenessList, fineness)
+		}
 		personFinenessList = append(personFinenessList, fineness)
 		carCheckNumber++
 		if carCheckNumber == constvar.EveryPersonCarCheckNumber {
@@ -67,10 +69,10 @@
 		if roundingItemMap[roundedHalfFineness] == nil {
 			roundingItemMap[roundedHalfFineness] = &models.FinenessRoundingItem{
 				Fineness: fineness,
-				Quantity: 1,
+				Quantity: finenessItem.Quantity,
 			}
 		} else {
-			roundingItemMap[roundedHalfFineness].Quantity++
+			roundingItemMap[roundedHalfFineness].Quantity += finenessItem.Quantity
 		}
 	}
 
@@ -85,6 +87,7 @@
 		MarketProcessOrderNumber: "",
 		Inspector:                "",
 	}
+	finenessCheck.TotalDeviation = TotalDeviation(allFinenessList, finenessCheck.AverageFineness)
 	finenessCheck.FinenessGrade = CalcFinenessGrade(finenessCheck.Deviation, finenessCheck.TotalDeviation, finenessCheck.Cleanliness, finenessCheck.Purity, finenessCheck.TwiceChange, standardMap)
 
 	err = models.WithTransaction(func(db *gorm.DB) error {
@@ -231,3 +234,31 @@
 	finenessCheck.FinenessGrade = CalcFinenessGrade(finenessCheck.Deviation, finenessCheck.TotalDeviation, finenessCheck.Cleanliness, finenessCheck.Purity, finenessCheck.TwiceChange, standardMap)
 	return
 }
+
+func TotalDeviation(numbers []decimal.Decimal, average decimal.Decimal) decimal.Decimal {
+	if len(numbers) < 2 {
+		return decimal.Zero
+	}
+
+	n := len(numbers)
+	ratio := decimal.NewFromFloat(0.02)
+	length := decimal.NewFromInt(int64(n)).Mul(ratio).Ceil().IntPart()
+	if length >= int64(n) {
+		return decimal.Zero
+	}
+
+	sort.Slice(numbers, func(i, j int) bool {
+		return numbers[i].LessThan(numbers[j])
+	})
+
+	minPart := numbers[:length-1]
+	maxPart := numbers[length:]
+
+	minAverage := utils.Average(minPart)
+	maxAverage := utils.Average(maxPart)
+
+	minDiff := minAverage.Sub(average).Abs()
+	maxDiff := maxAverage.Sub(average).Abs()
+
+	return decimal.Max(minDiff, maxDiff)
+}

--
Gitblit v1.8.0