ソースを参照

fix stages is null

sun-pc-linux 1 ヶ月 前
コミット
cb7a750a9d
1 ファイル変更91 行追加30 行削除
  1. 91 30
      boxcost/api/stages.go

+ 91 - 30
boxcost/api/stages.go

@@ -79,43 +79,104 @@ func AddStage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, err
 	}
 
-	// 更新数据库,添加多个stage
-	update := bson.M{
-		"$push": bson.M{
-			"pack.components.$[components].stages": bson.M{
-				"$each": req.Stages,
+	// // 更新数据库,添加多个stage
+	// update := bson.M{
+	// 	"$push": bson.M{
+	// 		"pack.components.$[components].stages": bson.M{
+	// 			"$each": req.Stages,
+	// 		},
+	// 	},
+	// 	"$set": bson.M{
+	// 		"updateTime": time.Now(),
+	// 	},
+	// }
+
+	// if req.Stages == nil {
+	// 	update = bson.M{
+	// 		"$push": bson.M{
+	// 			"pack.components.$[components].stages": bson.M{
+	// 				"$each": []string{},
+	// 			},
+	// 		},
+	// 	}
+
+	// }
+
+	// arrayFilters := []interface{}{
+	// 	bson.M{"components.id": req.CompId},
+	// }
+
+	// opts := options.Update().SetArrayFilters(options.ArrayFilters{
+	// 	Filters: arrayFilters,
+	// })
+	// db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
+
+	// result, err := db.UpdateOne(c.Request.Context(),
+	// 	bson.M{"_id": planId},
+	// 	update,
+	// 	opts,
+	// )
+
+	// 构建聚合管道更新
+	pipeline := bson.A{
+		bson.M{
+			"$set": bson.M{
+				"pack.components": bson.M{
+					"$map": bson.M{
+						"input": "$pack.components",
+						"as":    "component",
+						"in": bson.M{
+							"$cond": bson.A{
+								// 通过条件直接匹配 CompId,无需 arrayFilters
+								bson.M{"$eq": bson.A{"$$component.id", req.CompId}},
+								// 匹配时合并 stages
+								bson.M{
+									"$mergeObjects": bson.A{
+										"$$component",
+										bson.M{
+											"stages": bson.M{
+												"$concatArrays": bson.A{
+													// 处理 null 或不存在的情况
+													bson.M{"$ifNull": bson.A{"$$component.stages", bson.A{}}},
+													req.Stages,
+												},
+											},
+										},
+									},
+								},
+								// 不匹配时保留原组件
+								"$$component",
+							},
+						},
+					},
+				},
+				"updateTime": time.Now(),
 			},
 		},
-		"$set": bson.M{
-			"updateTime": time.Now(),
-		},
 	}
+	db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
 
-	if req.Stages == nil {
-		update = bson.M{
-			"$push": bson.M{
-				"pack.components.$[components].stages": bson.M{
-					"$each": []string{},
-				},
-			},
-		}
-
-	}
+	// 移除 arrayFilters 和 opts
+	result, err := db.UpdateOne(
+		c.Request.Context(),
+		bson.M{"_id": planId}, // 直接通过 _id 匹配文档
+		pipeline,
+	)
 
-	arrayFilters := []interface{}{
-		bson.M{"components.id": req.CompId},
-	}
+	// arrayFilters := options.ArrayFilters{
+	// 	Filters: []interface{}{
+	// 		bson.M{"components.id": req.CompId},
+	// 	},
+	// }
+	// opts := options.Update().SetArrayFilters(arrayFilters)
 
-	opts := options.Update().SetArrayFilters(options.ArrayFilters{
-		Filters: arrayFilters,
-	})
-	db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
+	// db := apictx.Svc.Mongo.GetCollection(repo.CollectionProductPlan)
 
-	result, err := db.UpdateOne(c.Request.Context(),
-		bson.M{"_id": planId},
-		update,
-		opts,
-	)
+	// result, err := db.UpdateOne(c.Request.Context(),
+	// 	bson.M{"_id": planId},
+	// 	pipeline,
+	// 	opts,
+	// )
 
 	if err != nil {
 		return nil, err