get_contact_list_logic.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. predicates = append(predicates, contact.Ctype(1))
  49. if len(req.LabelIDs) > 0 {
  50. predicates = append(predicates, contact.HasContactRelationshipsWith(
  51. labelrelationship.HasLabelsWith(
  52. label.IDIn(req.LabelIDs...),
  53. ),
  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. } else {
  71. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  72. }
  73. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  74. query.WithLabels()
  75. }).Page(l.ctx, req.Page, req.PageSize)
  76. if err != nil {
  77. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  78. }
  79. resp := &types.ContactListResp{}
  80. resp.Msg = errormsg.Success
  81. resp.Data.Total = data.PageDetails.Total
  82. wxWxids := []string{}
  83. wxWxidsSet := make(map[string]*ent.Wx)
  84. blockListSet := make(map[string]struct{})
  85. groupBlockListSet := make(map[string]struct{})
  86. for _, v := range data.List {
  87. wxWxids = append(wxWxids, v.WxWxid)
  88. }
  89. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  90. for _, w := range wxs {
  91. wxWxidsSet[w.Wxid] = w
  92. for _, b := range w.BlockList {
  93. blockListSet[w.Wxid+"_"+b] = struct{}{}
  94. }
  95. for _, g := range w.GroupBlockList {
  96. groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
  97. }
  98. }
  99. for _, v := range data.List {
  100. isInBlockList := false
  101. labelRelationships := make([]types.ContactLabelList, 0)
  102. if v.Edges.ContactRelationships != nil {
  103. for _, lr := range v.Edges.ContactRelationships {
  104. if lr.Edges.Labels == nil {
  105. continue
  106. }
  107. labelRelationships = append(labelRelationships, types.ContactLabelList{
  108. Label: &lr.Edges.Labels.Name,
  109. Value: &lr.LabelID,
  110. })
  111. }
  112. }
  113. var wxNickname string
  114. if wxWxidsSet[v.WxWxid] == nil {
  115. wxNickname = v.WxWxid
  116. } else {
  117. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  118. }
  119. if v.Type == 1 {
  120. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  121. isInBlockList = true
  122. } else {
  123. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  124. }
  125. } else {
  126. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  127. isInBlockList = true
  128. } else {
  129. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  130. }
  131. }
  132. resp.Data.Data = append(resp.Data.Data,
  133. types.ContactInfo{
  134. BaseIDInfo: types.BaseIDInfo{
  135. Id: &v.ID,
  136. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  137. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  138. },
  139. Status: &v.Status,
  140. WxWxid: &v.WxWxid,
  141. WxWxidNickname: &wxNickname,
  142. Type: &v.Type,
  143. Wxid: &v.Wxid,
  144. Account: &v.Account,
  145. Nickname: &v.Nickname,
  146. Markname: &v.Markname,
  147. Headimg: &v.Headimg,
  148. Sex: &v.Sex,
  149. Starrole: &v.Starrole,
  150. Dontseeit: &v.Dontseeit,
  151. Dontseeme: &v.Dontseeme,
  152. Lag: &v.Lag,
  153. Gid: &v.Gid,
  154. Gname: &v.Gname,
  155. V3: &v.V3,
  156. LabelRelationships: labelRelationships,
  157. IsInBlockList: &isInBlockList,
  158. Ctype: &v.Ctype,
  159. Cname: &v.Cname,
  160. Cage: &v.Cage,
  161. Carea: &v.Carea,
  162. Cc: &v.Cc,
  163. Phone: &v.Phone,
  164. Cbirthday: &v.Cbirthday,
  165. Cbirtharea: &v.Cbirtharea,
  166. CidcardNo: &v.CidcardNo,
  167. Ctitle: &v.Ctitle,
  168. })
  169. }
  170. return resp, nil
  171. }