Sfoglia il codice sorgente

添加编号的后台生成

infish2018 1 settimana fa
parent
commit
a1d4d1124c
1 ha cambiato i file con 55 aggiunte e 8 eliminazioni
  1. 55 8
      sku3d/sku3d/api/a-service-img.go

+ 55 - 8
sku3d/sku3d/api/a-service-img.go

@@ -28,10 +28,56 @@ func getSerialNum(img *model.MatImage, ctx *repo.RepoSession) (string, error) {
 	catId1 := ""
 	catId2 := ""
 	catId3 := ""
+	cat := &model.Category{
+		CreateTime: time.Now(),
+	}
+	find, err := repo.RepoSeachDoc(ctx, &repo.DocSearchOptions{CollectName: repo.CollectionCategory, Query: repo.Map{}, Project: []string{"name", "type", "children", "createTime"}}, cat)
+	if !find || err != nil {
+		fmt.Println("find category error", err)
+		return "", err
+	}
+	setMap := make(map[string]*model.Category, len(cat.Children)*20)
+	for _, s := range cat.Children {
+		for _, s1 := range s.Children {
+			setMap[s1.IdStr] = s1
+		}
+	}
+	setParentMap := make(map[string]*model.Category, len(cat.Children)*20)
+	for _, s := range cat.Children {
+		for _, s1 := range s.Children {
+			setParentMap[s1.IdStr] = s
+		}
+	}
+
+	for _, v := range img.Categories {
+		c, ok := setMap[v]
+		if !ok {
+			continue
+		}
+		p := setParentMap[v]
+
+		if p.Name == "商品分类" {
+			catId1 = c.CusNum
+			fmt.Println("p name", c.CusNum)
+			continue
+		}
+		if p.Name == "产品系列" {
+			catId2 = c.CusNum
+			continue
+		}
+		if p.Name == "产品用途" {
+			catId3 = c.CusNum
+		}
+	}
+
+	if len(catId1) < 1 || len(catId2) < 1 || len(catId3) < 1 {
+		fmt.Println("cat err", catId1, catId2, catId3)
+		return "", NewError("商品分类/产品系列/产品用途为空")
+	}
+
 	filter := bson.M{
-		"catId1": "",
-		"catId2": "",
-		"catId3": "",
+		"catId1": catId1,
+		"catId2": catId2,
 	}
 
 	update := bson.M{
@@ -39,8 +85,6 @@ func getSerialNum(img *model.MatImage, ctx *repo.RepoSession) (string, error) {
 		"$setOnInsert": bson.M{
 			"catId1": catId1,
 			"catId2": catId2,
-			"catId3": catId3,
-			"next":   1, // 初始值
 		},
 	}
 
@@ -50,11 +94,12 @@ func getSerialNum(img *model.MatImage, ctx *repo.RepoSession) (string, error) {
 
 	var result model.SerialNumer
 	col := ctx.Client.GetCollection(repo.CollectionSerialNos)
-	err := col.FindOneAndUpdate(ctx.Ctx, filter, update, opts).Decode(&result)
+	err = col.FindOneAndUpdate(ctx.Ctx, filter, update, opts).Decode(&result)
 	if err != nil {
+		fmt.Println("get SerialNumer error", err)
 		return "", fmt.Errorf("failed to get next serial number: %v", err)
 	}
-	return fmt.Sprintf("%s%s%d%s", "1", "2", result.Next, "3"), nil
+	return fmt.Sprintf("%s%s%03d%s", catId1, catId2, result.Next, catId3), nil
 }
 
 func UpdateImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
@@ -129,7 +174,7 @@ func SearchByFields(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		Size:        size,
 		Query:       query,
 		// Project:     []string{"nameCn", "createTime", "thumbnail", "price", "from"},
-		// Sort: bson.M{"_id"},
+		Sort: bson.M{"createTime": -1},
 	}
 	return repo.RepoPageSearch(apictx.CreateRepoCtx(), pageOption)
 }
@@ -146,9 +191,11 @@ func createImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	data.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
 	serial, err := getSerialNum(data, apictx.CreateRepoCtx())
 	if err != nil {
+		fmt.Println(err)
 		return nil, NewError("编号生成失败")
 	}
 	data.CusNum = serial
+	fmt.Println("CusNum=>", serial)
 
 	idstr, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, data)
 	if err != nil {