label_import_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package label
  2. import (
  3. "context"
  4. "entgo.io/ent/dialect/sql"
  5. "strings"
  6. "time"
  7. "wechat-api/ent"
  8. "wechat-api/ent/label"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type LabelImportLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewLabelImportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LabelImportLogic {
  19. return &LabelImportLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *LabelImportLogic) LabelImport(req *types.LabelImportReq) (resp *types.LabelImportResp, err error) {
  25. // todo: add your logic here and delete this line
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. contentString := req.Content
  28. // contentString := "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
  29. //逗号分隔contentString
  30. contents := strings.Split(contentString, ",")
  31. //查找已经存在的标签
  32. uniqueContents := make(map[string]struct{})
  33. for _, tag := range contents {
  34. tag = strings.TrimSpace(tag)
  35. if tag != "" {
  36. uniqueContents[tag] = struct{}{}
  37. }
  38. }
  39. contentList := make([]string, 0, len(uniqueContents))
  40. for tag := range uniqueContents {
  41. contentList = append(contentList, tag)
  42. }
  43. existingLabels, err := l.svcCtx.DB.Label.Query().
  44. Where(
  45. label.NameIn(contentList...),
  46. label.OrganizationID(organizationId),
  47. ).
  48. Select(label.FieldName).
  49. All(l.ctx)
  50. if err != nil {
  51. return nil, err
  52. }
  53. //var toInsert []string
  54. existMap := make(map[string]bool)
  55. for _, tag := range existingLabels {
  56. existMap[tag.Name] = true
  57. }
  58. labelCreates := make([]*ent.LabelCreate, 0)
  59. inserted := make([]string, 0)
  60. existed := make([]string, 0)
  61. for _, tag := range contentList {
  62. if existMap[tag] {
  63. existed = append(existed, tag)
  64. continue
  65. }
  66. if !existMap[tag] {
  67. labelCreates = append(labelCreates, l.svcCtx.DB.Label.Create().
  68. SetName(tag).
  69. SetType(*req.Type).
  70. SetStatus(1).
  71. SetOrganizationID(organizationId).
  72. SetFrom(2).
  73. SetMode(1).
  74. SetConditions(`{}`).
  75. SetCreatedAt(time.Now()).
  76. SetUpdatedAt(time.Now()),
  77. )
  78. }
  79. inserted = append(inserted, tag)
  80. }
  81. if len(labelCreates) > 0 {
  82. err := l.svcCtx.DB.Label.CreateBulk(labelCreates...).
  83. OnConflict(sql.ConflictColumns(label.FieldName, label.FieldOrganizationID)).
  84. DoNothing().
  85. Exec(l.ctx)
  86. if err != nil {
  87. logx.Error("label 批量插入失败", labelCreates)
  88. return nil, err
  89. }
  90. }
  91. return &types.LabelImportResp{
  92. BaseDataInfo: types.BaseDataInfo{
  93. Code: 0,
  94. Msg: "success",
  95. },
  96. Data: types.LabelImportInfo{
  97. Inserted: inserted,
  98. Existed: existed,
  99. },
  100. }, nil
  101. }