get_contact_list_logic.go 5.4 KB

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