batch_update_label_relationships_logic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package label_relationship
  2. import (
  3. "context"
  4. "wechat-api/internal/svc"
  5. "wechat-api/internal/types"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. )
  8. type BatchUpdateLabelRelationshipsLogic struct {
  9. logx.Logger
  10. ctx context.Context
  11. svcCtx *svc.ServiceContext
  12. }
  13. func NewBatchUpdateLabelRelationshipsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BatchUpdateLabelRelationshipsLogic {
  14. return &BatchUpdateLabelRelationshipsLogic{
  15. Logger: logx.WithContext(ctx),
  16. ctx: ctx,
  17. svcCtx: svcCtx}
  18. }
  19. func (l *BatchUpdateLabelRelationshipsLogic) BatchUpdateLabelRelationships(req *types.BatchLabelRelationshipsInfo) (resp *types.BaseMsgResp, err error) {
  20. //organizationId := l.ctx.Value("organizationId").(uint64)
  21. //// 开始事务
  22. //tx, err := l.svcCtx.DB.Tx(context.Background())
  23. //if err != nil {
  24. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  25. //}
  26. //
  27. //// 遍历所有联系人
  28. //for _, contactId := range req.ContactIds {
  29. // // 获取联系人信息
  30. // c, err := tx.Contact.Query().Where(contact.ID(contactId), contact.OrganizationIDEQ(organizationId)).Only(l.ctx)
  31. // // 获取联系人当前已关联的标签
  32. // currentLabelRelationships, err := tx.LabelRelationship.Query().Where(labelrelationship.ContactID(contactId)).All(l.ctx)
  33. // if err != nil {
  34. // _ = tx.Rollback()
  35. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  36. // }
  37. //
  38. // // 提取当前标签ID
  39. // var currentLabelIds []uint64
  40. // for _, relationship := range currentLabelRelationships {
  41. // currentLabelIds = append(currentLabelIds, relationship.LabelID)
  42. // }
  43. //
  44. // // 对比新旧标签ID,找出需要新增和移除的标签
  45. // addLabelIds, removeLabelIds := compareLabelIds(req.LabelIds, currentLabelIds)
  46. //
  47. // // 如果 req.UpdateType 为空,或 req.UpdateType 的值为 “all” 时
  48. // if req.UpdateType == nil || *req.UpdateType == "all" {
  49. // // 删除需要移除的标签关系
  50. // for _, id := range removeLabelIds {
  51. // _, err := tx.LabelRelationship.
  52. // Delete().
  53. // Where(
  54. // labelrelationship.ContactID(*req.ContactId),
  55. // labelrelationship.LabelID(id),
  56. // ).
  57. // Exec(l.ctx)
  58. // if err != nil {
  59. // _ = tx.Rollback()
  60. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  61. // }
  62. // }
  63. // }
  64. //
  65. // // 创建需要新增的标签关系
  66. // for _, id := range addLabelIds {
  67. // _, _ = tx.LabelRelationship.Create().
  68. // SetLabelID(id).
  69. // SetContactID(*req.ContactId).
  70. // SetOrganizationID(organizationId).
  71. // Save(l.ctx)
  72. // //if err != nil {
  73. // // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  74. // //}
  75. // }
  76. //
  77. // // 获取所有 status 为 3 且 bot_wxid_list 包含 c.wx_wxid 的 sop_task
  78. // sopTasks, err := tx.SopTask.Query().Where(soptask.Status(3), soptask.OrganizationIDEQ(organizationId)).All(l.ctx)
  79. // if err != nil {
  80. // _ = tx.Rollback()
  81. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  82. // }
  83. //
  84. // var filteredSopTasks []*ent.SopTask
  85. // for _, task := range sopTasks {
  86. // for _, botWxid := range task.BotWxidList {
  87. // if botWxid == c.WxWxid {
  88. // filteredSopTasks = append(filteredSopTasks, task)
  89. // break
  90. // }
  91. // }
  92. // }
  93. //
  94. // // 获取所有 filteredSopTasks 的 sop_stages
  95. // var sopStages []*ent.SopStage
  96. // for _, task := range filteredSopTasks {
  97. // stages, err := task.QueryTaskStages().All(l.ctx)
  98. // if err != nil {
  99. // _ = tx.Rollback()
  100. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  101. // }
  102. // sopStages = append(sopStages, stages...)
  103. // }
  104. // err = l.AddLabelRelationships(sopStages, *c, req.LabelIds, organizationId)
  105. //
  106. // if err != nil {
  107. // _ = tx.Rollback()
  108. // return nil, err
  109. // }
  110. // // 所有操作成功,提交事务
  111. // err = tx.Commit()
  112. // if err != nil {
  113. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  114. // }
  115. //}
  116. //
  117. //return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  118. return nil, nil
  119. }