set_whatsapp_contact_label_logic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package label_relationship
  2. import (
  3. "context"
  4. "wechat-api/ent/contact"
  5. "wechat-api/ent/labelrelationship"
  6. "wechat-api/internal/utils/dberrorhandler"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type SetWhatsappContactLabelLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewSetWhatsappContactLabelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetWhatsappContactLabelLogic {
  17. return &SetWhatsappContactLabelLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx}
  21. }
  22. func (l *SetWhatsappContactLabelLogic) SetWhatsappContactLabel(req *types.LabelRelationshipsInfo) (*types.BaseMsgResp, error) {
  23. organizationId := l.ctx.Value("organizationId").(uint64)
  24. resp := types.BaseMsgResp{}
  25. // 开始事务
  26. tx, err := l.svcCtx.DB.Tx(context.Background())
  27. if err != nil {
  28. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  29. }
  30. // 获取联系人信息
  31. _, err = tx.Contact.Query().Where(
  32. contact.ID(*req.ContactId),
  33. contact.OrganizationIDEQ(organizationId),
  34. ).Only(l.ctx)
  35. // 获取联系人当前已关联的标签
  36. currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(*req.ContactId)).All(l.ctx)
  37. if err != nil {
  38. _ = tx.Rollback()
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. // 提取当前标签ID
  42. var currentLabelIds []uint64
  43. for _, relationship := range currentLabelRelationships {
  44. currentLabelIds = append(currentLabelIds, relationship.LabelID)
  45. }
  46. // 对比新旧标签ID,找出需要新增和移除的标签
  47. addLabelIds, removeLabelIds := diffLabelIds(req.LabelIds, currentLabelIds)
  48. l.Logger.Infof("req.LabelIds=%v, currentLabelIds=%v\n", req.LabelIds, currentLabelIds)
  49. l.Logger.Infof("addLabelIds=%v, removeLabelIds=%v\n", addLabelIds, removeLabelIds)
  50. // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
  51. // 删除需要移除的标签关系
  52. l.Logger.Errorf("------------------------removeLabelIds--------------------------- %+v\n", removeLabelIds)
  53. for _, id := range currentLabelIds {
  54. _, err := tx.LabelRelationship.
  55. Delete().
  56. Where(
  57. labelrelationship.ContactID(*req.ContactId),
  58. labelrelationship.LabelID(id),
  59. ).
  60. Exec(l.ctx)
  61. if err != nil {
  62. _ = tx.Rollback()
  63. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  64. }
  65. }
  66. // 创建需要新增的标签关系
  67. l.Logger.Errorf("------------------------addLabelIds--------------------------- %+v\n", addLabelIds)
  68. for _, id := range req.LabelIds {
  69. _, _ = tx.LabelRelationship.Create().
  70. SetLabelID(id).
  71. SetContactID(*req.ContactId).
  72. SetOrganizationID(organizationId).
  73. Save(l.ctx)
  74. //if err != nil {
  75. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  76. //}
  77. }
  78. _ = tx.Commit()
  79. return &resp, nil
  80. }
  81. func diffLabelIds(newLabelIds []uint64, currentLabelIds []uint64) (addLabelIds []uint64, removeLabelIds []uint64) {
  82. for _, v := range newLabelIds {
  83. if !inArray(v, currentLabelIds) {
  84. addLabelIds = append(addLabelIds, v)
  85. }
  86. }
  87. for _, v := range currentLabelIds {
  88. if !inArray(v, newLabelIds) {
  89. removeLabelIds = append(removeLabelIds, v)
  90. }
  91. }
  92. return
  93. }
  94. func inArray(ele uint64, array []uint64) bool {
  95. for _, v := range array {
  96. if ele == v {
  97. return true
  98. }
  99. }
  100. return false
  101. }