sun-pc 2 months ago
parent
commit
f90dd7e49c
4 changed files with 30 additions and 17 deletions
  1. 1 0
      boxcost/api/bill-produce.go
  2. 1 0
      boxcost/api/bill-product.go
  3. 1 0
      boxcost/api/bill.go
  4. 27 17
      boxcost/api/stages.go

+ 1 - 0
boxcost/api/bill-produce.go

@@ -291,6 +291,7 @@ func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		db := apictx.Svc.Mongo.GetCollection(repo.CollectionBillProduce)
 		_, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx, db)
 		if err != nil {
+			fmt.Println(err)
 			return nil, errors.New("该单据改动同步到产品失败")
 		}
 		fmt.Println("单据同步到产品,planId:", bill.PlanId)

+ 1 - 0
boxcost/api/bill-product.go

@@ -281,6 +281,7 @@ func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		db := apictx.Svc.Mongo.GetCollection(repo.CollectionBillProduct)
 		_, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx, db)
 		if err != nil {
+			fmt.Println(err)
 			return nil, errors.New("该单据改动同步到产品失败")
 		}
 		fmt.Println("单据同步到产品,planId:", bill.PlanId)

+ 1 - 0
boxcost/api/bill.go

@@ -422,6 +422,7 @@ func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		db := apictx.Svc.Mongo.GetCollection(repo.CollectionBillPurchase)
 		_, err := updateBilltoStage(c, bill.PlanId, idStatges, apictx, db)
 		if err != nil {
+			fmt.Println(err)
 			return nil, errors.New("该单据改动同步到产品失败")
 		}
 		fmt.Println("单据同步到产品,planId:", bill.PlanId)

+ 27 - 17
boxcost/api/stages.go

@@ -6,6 +6,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"math"
 	"time"
 
 	"github.com/gin-gonic/gin"
@@ -42,11 +43,13 @@ func updatePrices(c *gin.Context, db *mongo.Collection, planId primitive.ObjectI
 			stagePrice := stage.OrderPrice * float64(stage.OrderCount)
 			compPrice += stagePrice
 		}
+		// 截断组件总价到两位小数
+		_compPrice := math.Trunc(compPrice*1000) / 1000
 		// 更新组件总价
 		if comp.Id == compId {
 			_, err = db.UpdateOne(c.Request.Context(),
 				bson.M{"_id": planId, "pack.components.id": compId},
-				bson.M{"$set": bson.M{"pack.components.$.totalPrice": compPrice}})
+				bson.M{"$set": bson.M{"pack.components.$.totalPrice": _compPrice}})
 			if err != nil {
 				return err
 			}
@@ -54,7 +57,8 @@ func updatePrices(c *gin.Context, db *mongo.Collection, planId primitive.ObjectI
 		totalPrice += compPrice
 	}
 
-	// 3. 更新计划总价
+	// 3. 更新计划总价(截断到两位小数)
+	totalPrice = math.Trunc(totalPrice*1000) / 1000
 	_, err = db.UpdateOne(c.Request.Context(),
 		bson.M{"_id": planId},
 		bson.M{"$set": bson.M{"totalPrice": &totalPrice}})
@@ -190,19 +194,19 @@ func updateStage(c *gin.Context, db *mongo.Collection, req *StageRequest) (inter
 		return nil, fmt.Errorf("invalid planId: %v", err)
 	}
 
-	// 构建更新
+	// 构建更新操作
 	update := bson.M{
 		"$set": bson.M{
-			"pack.components.$[components].stages.$[stage]": req.Stages[0],
-			"updateTime": time.Now(),
+			"pack.components.$[comp].stages.$[stg]": req.Stages[0],
+			"updateTime":                            time.Now(),
 		},
 	}
 
-	// 设置数组过滤器
+	// 使用正确的数组过滤器
 	arrayFilters := options.ArrayFilters{
 		Filters: []interface{}{
-			bson.M{"components.id": req.CompId},
-			bson.M{"stage.id": req.StageId},
+			bson.M{"comp.id": req.CompId},
+			bson.M{"stg.id": req.StageId},
 		},
 	}
 
@@ -211,20 +215,25 @@ func updateStage(c *gin.Context, db *mongo.Collection, req *StageRequest) (inter
 		SetArrayFilters(arrayFilters).
 		SetReturnDocument(options.After)
 
-	result := db.FindOneAndUpdate(
+	var updatedDoc bson.M
+	err = db.FindOneAndUpdate(
 		c.Request.Context(),
 		bson.M{"_id": planId},
 		update,
 		opts,
-	)
+	).Decode(&updatedDoc)
 
-	if result.Err() != nil {
-		if result.Err() == mongo.ErrNoDocuments {
-			return nil, fmt.Errorf("plan or stage not found")
+	if err != nil {
+		if err == mongo.ErrNoDocuments {
+			// 打印更详细的错误信息
+			fmt.Printf("Debug - Failed to update. PlanId: %s, CompId: %s, StageId: %s\n", planId.Hex(), req.CompId, req.StageId)
+			return nil, fmt.Errorf("plan or stage not found, planId: %s, compId: %s, stageId: %s", req.PlanId, req.CompId, req.StageId)
 		}
-		return nil, fmt.Errorf("failed to update stage: %v", result.Err())
+		return nil, fmt.Errorf("failed to update stage: %v", err)
 	}
 
+	fmt.Printf("Debug - Update successful\n")
+
 	// 更新价格
 	err = updatePrices(c, db, planId, req.CompId)
 	if err != nil {
@@ -393,13 +402,14 @@ func AddCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	} else if oldPlan.TotalPrice != nil {
 		newTotalPrice += *oldPlan.TotalPrice
 	}
+	_newTotalPrice := math.Trunc(newTotalPrice*1000) / 1000
 	update := bson.M{
 		"$push": bson.M{
 			"pack.components": component,
 		},
 		"$set": bson.M{
 			"updateTime": time.Now(),
-			"totalPrice": newTotalPrice,
+			"totalPrice": _newTotalPrice,
 		},
 		"$inc": bson.M{
 			"pack.compCounts": 1,
@@ -454,14 +464,14 @@ func DeleteCompnonet(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			newTotalPrice = 0
 		}
 	}
-
+	_newTotalPrice := math.Trunc(newTotalPrice*1000) / 1000
 	update := bson.M{
 		"$pull": bson.M{
 			"pack.components": bson.M{"id": componentId},
 		},
 		"$set": bson.M{
 			"updateTime": time.Now(),
-			"totalPrice": newTotalPrice,
+			"totalPrice": _newTotalPrice,
 		},
 		"$inc": bson.M{
 			"pack.compCounts": -1,