|
@@ -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,
|