get_contact_list_logic.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package contact
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/contactfieldtemplate"
  6. "wechat-api/ent/label"
  7. "wechat-api/ent/labelrelationship"
  8. "wechat-api/ent/wx"
  9. "wechat-api/ent/contact"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  15. "github.com/suyuan32/simple-admin-common/utils/pointy"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type GetContactListLogic struct {
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. logx.Logger
  22. }
  23. func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
  24. return &GetContactListLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
  31. return types.LabelInfo{
  32. BaseIDInfo: types.BaseIDInfo{
  33. Id: &label.ID,
  34. CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
  35. UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
  36. },
  37. Status: &label.Status,
  38. Type: &label.Type,
  39. Name: &label.Name,
  40. From: &label.From,
  41. Mode: &label.Mode,
  42. Conditions: &label.Conditions,
  43. }
  44. }
  45. func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
  46. organizationId := l.ctx.Value("organizationId").(uint64)
  47. isAdmin := l.ctx.Value("isAdmin").(bool)
  48. var predicates []predicate.Contact
  49. if req.WxWxid == nil || (req.WxWxid != nil && !isAdmin) {
  50. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  51. }
  52. var ctype uint64 = 1
  53. if req.Ctype != nil {
  54. ctype = *req.Ctype
  55. }
  56. predicates = append(predicates, contact.Ctype(ctype))
  57. if len(req.LabelIDs) > 0 {
  58. predicates = append(predicates, contact.HasContactRelationshipsWith(
  59. labelrelationship.HasLabelsWith(
  60. label.IDIn(req.LabelIDs...),
  61. ),
  62. ))
  63. }
  64. if req.WxWxid != nil {
  65. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  66. }
  67. if req.Wxid != nil {
  68. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  69. }
  70. if req.Account != nil {
  71. predicates = append(predicates, contact.AccountContains(*req.Account))
  72. }
  73. if req.Nickname != nil {
  74. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  75. }
  76. if req.Type != nil {
  77. predicates = append(predicates, contact.TypeEQ(*req.Type))
  78. } else {
  79. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  80. }
  81. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  82. query.WithLabels()
  83. }).WithContactFields(func(query *ent.ContactFieldQuery) {
  84. query.WithFieldContact()
  85. }).Page(l.ctx, req.Page, req.PageSize)
  86. if err != nil {
  87. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  88. }
  89. resp := &types.ContactListResp{}
  90. resp.Msg = errormsg.Success
  91. resp.Data.Total = data.PageDetails.Total
  92. wxWxids := []string{}
  93. wxWxidsSet := make(map[string]*ent.Wx)
  94. blockListSet := make(map[string]struct{})
  95. groupBlockListSet := make(map[string]struct{})
  96. for _, v := range data.List {
  97. wxWxids = append(wxWxids, v.WxWxid)
  98. }
  99. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  100. for _, w := range wxs {
  101. wxWxidsSet[w.Wxid] = w
  102. for _, b := range w.BlockList {
  103. blockListSet[w.Wxid+"_"+b] = struct{}{}
  104. }
  105. for _, g := range w.GroupBlockList {
  106. groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
  107. }
  108. }
  109. field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query().
  110. Where(
  111. contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId
  112. ).
  113. Only(l.ctx)
  114. for _, v := range data.List {
  115. // 自定义字段
  116. var custom_field []types.ContactFieldTemplate
  117. if field_template != nil && len(field_template.Template) > 0 {
  118. field_set := make(map[string][]string)
  119. fields := v.Edges.ContactFields
  120. for _, field := range fields {
  121. field_set[field.FormID] = field.Value
  122. }
  123. for _, template := range field_template.Template {
  124. template.Value = field_set[*template.Id]
  125. custom_field = append(custom_field, ConvertCustomToInternal(template))
  126. }
  127. }
  128. isInBlockList := false
  129. labelRelationships := make([]types.ContactLabelList, 0)
  130. if v.Edges.ContactRelationships != nil {
  131. for _, lr := range v.Edges.ContactRelationships {
  132. if lr.Edges.Labels == nil {
  133. continue
  134. }
  135. labelRelationships = append(labelRelationships, types.ContactLabelList{
  136. Label: &lr.Edges.Labels.Name,
  137. Value: &lr.LabelID,
  138. })
  139. }
  140. }
  141. var wxNickname string
  142. if wxWxidsSet[v.WxWxid] == nil {
  143. wxNickname = v.WxWxid
  144. } else {
  145. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  146. }
  147. if v.Type == 1 {
  148. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  149. isInBlockList = true
  150. } else {
  151. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  152. }
  153. } else {
  154. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  155. isInBlockList = true
  156. } else {
  157. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  158. }
  159. }
  160. resp.Data.Data = append(resp.Data.Data,
  161. types.ContactInfo{
  162. BaseIDInfo: types.BaseIDInfo{
  163. Id: &v.ID,
  164. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  165. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  166. },
  167. Status: &v.Status,
  168. WxWxid: &v.WxWxid,
  169. WxWxidNickname: &wxNickname,
  170. Type: &v.Type,
  171. Wxid: &v.Wxid,
  172. Account: &v.Account,
  173. Nickname: &v.Nickname,
  174. Markname: &v.Markname,
  175. Headimg: &v.Headimg,
  176. Sex: &v.Sex,
  177. Starrole: &v.Starrole,
  178. Dontseeit: &v.Dontseeit,
  179. Dontseeme: &v.Dontseeme,
  180. Lag: &v.Lag,
  181. Gid: &v.Gid,
  182. Gname: &v.Gname,
  183. V3: &v.V3,
  184. LabelRelationships: labelRelationships,
  185. IsInBlockList: &isInBlockList,
  186. Ctype: &v.Ctype,
  187. Cname: &v.Cname,
  188. Cage: &v.Cage,
  189. Carea: &v.Carea,
  190. Cc: &v.Cc,
  191. Phone: &v.Phone,
  192. Cbirthday: &v.Cbirthday,
  193. Cbirtharea: &v.Cbirtharea,
  194. CidcardNo: &v.CidcardNo,
  195. Ctitle: &v.Ctitle,
  196. CustomFields: custom_field,
  197. })
  198. }
  199. return resp, nil
  200. }