get_contact_list_logic.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package contact
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/label"
  6. "wechat-api/ent/labelrelationship"
  7. "wechat-api/ent/wx"
  8. "wechat-api/ent/contact"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  14. "github.com/suyuan32/simple-admin-common/utils/pointy"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type GetContactListLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
  23. return &GetContactListLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
  30. return types.LabelInfo{
  31. BaseIDInfo: types.BaseIDInfo{
  32. Id: &label.ID,
  33. CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
  34. UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
  35. },
  36. Status: &label.Status,
  37. Type: &label.Type,
  38. Name: &label.Name,
  39. From: &label.From,
  40. Mode: &label.Mode,
  41. Conditions: &label.Conditions,
  42. }
  43. }
  44. func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
  45. organizationId := l.ctx.Value("organizationId").(uint64)
  46. var predicates []predicate.Contact
  47. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  48. if len(req.LabelIDs) > 0 {
  49. predicates = append(predicates, contact.HasContactRelationshipsWith(
  50. labelrelationship.HasLabelsWith(
  51. label.IDIn(req.LabelIDs...),
  52. ),
  53. labelrelationship.DeletedAtIsNil(),
  54. ))
  55. }
  56. if req.WxWxid != nil {
  57. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  58. }
  59. if req.Wxid != nil {
  60. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  61. }
  62. if req.Account != nil {
  63. predicates = append(predicates, contact.AccountContains(*req.Account))
  64. }
  65. if req.Nickname != nil {
  66. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  67. }
  68. if req.Type != nil {
  69. predicates = append(predicates, contact.TypeEQ(*req.Type))
  70. }
  71. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  72. query.WithLabels()
  73. }).Page(l.ctx, req.Page, req.PageSize)
  74. if err != nil {
  75. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  76. }
  77. resp := &types.ContactListResp{}
  78. resp.Msg = errormsg.Success
  79. resp.Data.Total = data.PageDetails.Total
  80. wxWxids := []string{}
  81. wxWxidsSet := make(map[string]string)
  82. for _, v := range data.List {
  83. wxWxids = append(wxWxids, v.WxWxid)
  84. }
  85. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  86. for _, w := range wxs {
  87. wxWxidsSet[w.Wxid] = w.Nickname
  88. }
  89. for _, v := range data.List {
  90. labelRelationships := make([]types.ContactLabelList, 0)
  91. if v.Edges.ContactRelationships != nil {
  92. for _, lr := range v.Edges.ContactRelationships {
  93. if lr.Edges.Labels == nil {
  94. continue
  95. }
  96. labelRelationships = append(labelRelationships, types.ContactLabelList{
  97. Label: &lr.Edges.Labels.Name,
  98. Value: &lr.LabelID,
  99. })
  100. }
  101. }
  102. //wxWxName := ""
  103. //l.Logger.Errorf("------------------------v.Edges.ContactWxWxid--------------------------- %+v", v.Edges.ContactWxWxid)
  104. //if v.Edges.ContactWxWxid != nil {
  105. // wxWxName = v.Edges.ContactWxWxid[0].Wxid
  106. //}
  107. l.Logger.Errorf("------------------------v.WxWxid--------------------------- %+v", v.WxWxid)
  108. var wxNickname string
  109. if wxWxidsSet[v.WxWxid] == "" {
  110. wxNickname = v.WxWxid
  111. } else {
  112. wxNickname = wxWxidsSet[v.WxWxid]
  113. }
  114. l.Logger.Errorf("------------------------wxNickname--------------------------- %+v", wxNickname)
  115. resp.Data.Data = append(resp.Data.Data,
  116. types.ContactInfo{
  117. BaseIDInfo: types.BaseIDInfo{
  118. Id: &v.ID,
  119. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  120. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  121. },
  122. Status: &v.Status,
  123. WxWxid: &wxNickname,
  124. Type: &v.Type,
  125. Wxid: &v.Wxid,
  126. Account: &v.Account,
  127. Nickname: &v.Nickname,
  128. Markname: &v.Markname,
  129. Headimg: &v.Headimg,
  130. Sex: &v.Sex,
  131. Starrole: &v.Starrole,
  132. Dontseeit: &v.Dontseeit,
  133. Dontseeme: &v.Dontseeme,
  134. Lag: &v.Lag,
  135. Gid: &v.Gid,
  136. Gname: &v.Gname,
  137. V3: &v.V3,
  138. LabelRelationships: labelRelationships,
  139. })
  140. }
  141. return resp, nil
  142. }