update_label_relationships_logic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package label_relationship
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/labelrelationship"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type UpdateLabelRelationshipsLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLabelRelationshipsLogic {
  17. return &UpdateLabelRelationshipsLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx}
  21. }
  22. func (l *UpdateLabelRelationshipsLogic) UpdateLabelRelationships(req *types.LabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
  23. l.Logger.Info("Start processing UpdateLabelRelationships request", "request", req)
  24. defer func() {
  25. if err != nil {
  26. l.Logger.Error("Error occurred in UpdateLabelRelationships", "error", err)
  27. } else {
  28. l.Logger.Info("Successfully processed UpdateLabelRelationships request")
  29. }
  30. }()
  31. // 获取联系人当前已关联的标签
  32. currentLabelRelationships, err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
  33. if err != nil {
  34. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  35. }
  36. // 提取当前标签ID
  37. var currentLabelIds []uint64
  38. for _, relationship := range currentLabelRelationships {
  39. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  40. }
  41. // 对比新旧标签ID,找出需要新增和移除的标签
  42. addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
  43. // 删除需要移除的标签关系
  44. for _, id := range removeLabelIds {
  45. _, err := l.svcCtx.DB.LabelRelationship.
  46. Delete().
  47. Where(
  48. labelrelationship.ContactID(*req.ContactId),
  49. labelrelationship.LabelID(id),
  50. ).
  51. Exec(l.ctx)
  52. if err != nil {
  53. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  54. }
  55. }
  56. // 创建需要新增的标签关系
  57. for _, id := range addLabelIds {
  58. _, err := l.svcCtx.DB.LabelRelationship.Create().
  59. SetLabelID(id).
  60. SetContactID(*req.ContactId).
  61. SetStatus(*req.Status).
  62. Save(l.ctx)
  63. if err != nil {
  64. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  65. }
  66. }
  67. return &types.BaseMsgResp{
  68. Code: 0,
  69. Msg: errormsg.Success,
  70. }, nil
  71. }
  72. // compareLabelIds compares the new label ids with the current ones and returns the ids to be added and removed
  73. func compareLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
  74. newLabelIdSet := make(map[uint64]struct{}, len(newLabelIds))
  75. for _, id := range newLabelIds {
  76. newLabelIdSet[id] = struct{}{}
  77. }
  78. for _, id := range currentLabelIds {
  79. if _, ok := newLabelIdSet[id]; ok {
  80. delete(newLabelIdSet, id)
  81. } else {
  82. removeLabelIds = append(removeLabelIds, id)
  83. }
  84. }
  85. for id := range newLabelIdSet {
  86. addLabelIds = append(addLabelIds, id)
  87. }
  88. return
  89. }