Просмотр исходного кода

标签数量限制,及返回标签数量

lichangdong 10 часов назад
Родитель
Сommit
be6c504b46
3 измененных файлов с 28 добавлено и 12 удалено
  1. 3 3
      desc/wechat/label.api
  2. 22 6
      internal/logic/label/label_import_logic.go
  3. 3 3
      internal/types/types.go

+ 3 - 3
desc/wechat/label.api

@@ -88,9 +88,9 @@ type (
     }
 
     LabelImportInfo struct {
-        Inserted []string `json:"inserted"` // 插入成功的标签
-        Existed  []string `json:"existed"`  // 已存在的标签
-        Failed   []string `json:"failed"`   // 插入失败的标签(预留)
+        Inserted int `json:"inserted"` // 插入成功的标签
+        Existed  int `json:"existed"`  // 已存在的标签
+        Failed   int `json:"failed"`   // 插入失败的标签(预留)
     }
 )
 

+ 22 - 6
internal/logic/label/label_import_logic.go

@@ -60,8 +60,7 @@ func (l *LabelImportLogic) LabelImport(req *types.LabelImportReq) (resp *types.L
 		if existMap[tag] {
 			existed = append(existed, tag)
 			continue
-		}
-		if !existMap[tag] {
+		} else {
 			labelCreates = append(labelCreates, l.svcCtx.DB.Label.Create().
 				SetName(tag).
 				SetType(*req.Type).
@@ -77,24 +76,41 @@ func (l *LabelImportLogic) LabelImport(req *types.LabelImportReq) (resp *types.L
 		inserted = append(inserted, tag)
 		insertNames = append(insertNames, tag) // 新增:记录本次尝试插入的标签
 	}
+
 	if len(labelCreates) > 0 {
-		err := l.svcCtx.DB.Label.CreateBulk(labelCreates...).
+		tx, err := l.svcCtx.DB.Tx(l.ctx)
+		if err != nil {
+			logx.Errorf("开启事务失败:%v", err)
+			return nil, err
+		}
+		err = tx.Label.CreateBulk(labelCreates...).
 			OnConflict(sql.ConflictColumns(label.FieldName, label.FieldOrganizationID)).
 			DoNothing().
 			Exec(l.ctx)
+
 		if err != nil {
+			rollbackErr := tx.Rollback()
+			if rollbackErr != nil {
+				logx.Errorf("标签插入失败,且回滚失败:%v;回滚错误:%v", err, rollbackErr)
+			} else {
+				logx.Errorf("标签插入失败:%v", err)
+			}
 			failed = insertNames
 			jsonBad, _ := json.Marshal(insertNames)
 			logx.Errorf("标签批量插入失败:%v,失败数据:%s", err, string(jsonBad))
 			//return nil, err
 		}
+		if err := tx.Commit(); err != nil {
+			logx.Errorf("提交事务失败:%v", err)
+			return nil, err
+		}
 	}
 	return &types.LabelImportResp{
 		BaseDataInfo: types.BaseDataInfo{Code: 0, Msg: "success"},
 		Data: types.LabelImportInfo{
-			Inserted: nonEmptySlice(inserted),
-			Existed:  nonEmptySlice(existed),
-			Failed:   nonEmptySlice(failed),
+			Inserted: len(inserted),
+			Existed:  len(existed),
+			Failed:   len(failed),
 		},
 	}, nil
 }

+ 3 - 3
internal/types/types.go

@@ -1290,9 +1290,9 @@ type LabelImportResp struct {
 
 // swagger:model LabelImportInfo
 type LabelImportInfo struct {
-	Inserted []string `json:"inserted"` // 插入成功的标签
-	Existed  []string `json:"existed"`  // 已存在的标签
-	Failed   []string `json:"failed"`   // 插入失败的标签(预留)
+	Inserted int `json:"inserted"` // 插入成功的标签
+	Existed  int `json:"existed"`  // 已存在的标签
+	Failed   int `json:"failed"`   // 插入失败的标签(预留)
 }
 
 // The data of label tagging information | LabelTagging信息