|
@@ -4,6 +4,7 @@ import (
|
|
|
"context"
|
|
|
"encoding/json"
|
|
|
"entgo.io/ent/dialect/sql"
|
|
|
+ "github.com/zeromicro/go-zero/core/errorx"
|
|
|
"strings"
|
|
|
"time"
|
|
|
"wechat-api/ent"
|
|
@@ -32,6 +33,9 @@ func (l *LabelImportLogic) LabelImport(req *types.LabelImportReq) (resp *types.L
|
|
|
organizationId := l.ctx.Value("organizationId").(uint64)
|
|
|
contentString := req.Content
|
|
|
contentList := splitContent(contentString)
|
|
|
+ if len(contentList) > 200 {
|
|
|
+ return nil, errorx.NewInvalidArgumentError("有效标签数量不能超过200个")
|
|
|
+ }
|
|
|
existingLabels, err := l.svcCtx.DB.Label.Query().
|
|
|
Where(
|
|
|
label.NameIn(contentList...),
|
|
@@ -56,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).
|
|
@@ -73,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
|
|
|
}
|