get_contact_list_logic.go 5.5 KB

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