set_whatsapp_contact_batch_label_logic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package label_relationship
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/contact"
  6. "wechat-api/ent/labelrelationship"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type SetWhatsappContactBatchLabelLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewSetWhatsappContactBatchLabelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetWhatsappContactBatchLabelLogic {
  18. return &SetWhatsappContactBatchLabelLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *SetWhatsappContactBatchLabelLogic) SetWhatsappContactBatchLabel(req *types.BatchLabelRelationshipsInfo) (*types.BaseMsgResp, error) {
  24. organizationId := l.ctx.Value("organizationId").(uint64)
  25. l.Logger.Errorf("contactIds=%v labelIds=%v updateType=%v\n", req.ContactIds, req.LabelIds, req.UpdateType)
  26. // 遍历所有联系人
  27. for _, contactId := range req.ContactIds {
  28. // 开始事务
  29. tx, err := l.svcCtx.DB.Tx(context.Background())
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. // 获取联系人信息
  34. _, err = tx.Contact.Query().Where(
  35. contact.ID(contactId),
  36. contact.OrganizationIDEQ(organizationId),
  37. ).Only(l.ctx)
  38. // 获取联系人当前已关联的标签
  39. currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(contactId)).All(l.ctx)
  40. if err != nil {
  41. _ = tx.Rollback()
  42. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  43. }
  44. // 提取当前标签ID
  45. var currentLabelIds []uint64
  46. for _, relationship := range currentLabelRelationships {
  47. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  48. }
  49. // 对比新旧标签ID,找出需要新增和移除的标签
  50. changeLabelIds, _ := compareLabelIdsForIncrement(req.LabelIds, currentLabelIds, req.UpdateType)
  51. l.Logger.Errorf("contactId=%v req.LabelIds=%v \n", contactId, req.LabelIds)
  52. l.Logger.Errorf("req.LabelIds=%v currentLabelIds=%v \n", req.LabelIds, currentLabelIds)
  53. l.Logger.Errorf("changeLabelIds=%v \n", changeLabelIds)
  54. // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
  55. if req.UpdateType == -1 {
  56. // 删除需要移除的标签关系
  57. for _, id := range changeLabelIds {
  58. _, err := tx.LabelRelationship.
  59. Delete().
  60. Where(
  61. labelrelationship.ContactID(contactId),
  62. labelrelationship.LabelID(id),
  63. ).
  64. Exec(l.ctx)
  65. if err != nil {
  66. _ = tx.Rollback()
  67. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  68. }
  69. }
  70. } else {
  71. // 创建需要新增的标签关系
  72. for _, id := range changeLabelIds {
  73. _, _ = tx.LabelRelationship.Create().
  74. SetLabelID(id).
  75. SetContactID(contactId).
  76. SetOrganizationID(organizationId).
  77. Save(l.ctx)
  78. //if err != nil {
  79. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  80. //}
  81. }
  82. }
  83. // 所有操作成功,提交事务
  84. _ = tx.Commit()
  85. }
  86. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  87. }