get_contact_list_logic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/contact"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  13. "github.com/suyuan32/simple-admin-common/utils/pointy"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetContactListLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
  22. return &GetContactListLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
  29. return types.LabelInfo{
  30. BaseIDInfo: types.BaseIDInfo{
  31. Id: &label.ID,
  32. CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
  33. UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
  34. },
  35. Status: &label.Status,
  36. Type: &label.Type,
  37. Name: &label.Name,
  38. From: &label.From,
  39. Mode: &label.Mode,
  40. Conditions: &label.Conditions,
  41. }
  42. }
  43. func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
  44. var predicates []predicate.Contact
  45. if len(req.LabelIDs) > 0 {
  46. predicates = append(predicates, contact.HasContactRelationshipsWith(
  47. labelrelationship.HasLabelsWith(
  48. label.IDIn(req.LabelIDs...),
  49. ),
  50. ))
  51. }
  52. if req.WxWxid != nil {
  53. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  54. }
  55. if req.Wxid != nil {
  56. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  57. }
  58. if req.Account != nil {
  59. predicates = append(predicates, contact.AccountContains(*req.Account))
  60. }
  61. if req.Type != nil {
  62. predicates = append(predicates, contact.TypeEQ(*req.Type))
  63. }
  64. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  65. query.WithLabels()
  66. }).Page(l.ctx, req.Page, req.PageSize)
  67. if err != nil {
  68. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  69. }
  70. resp := &types.ContactListResp{}
  71. resp.Msg = errormsg.Success
  72. resp.Data.Total = data.PageDetails.Total
  73. for _, v := range data.List {
  74. labelRelationships := make([]types.ContactLabelList, 0)
  75. if v.Edges.ContactRelationships != nil {
  76. for _, lr := range v.Edges.ContactRelationships {
  77. labelRelationships = append(labelRelationships, types.ContactLabelList{
  78. Label: &lr.Edges.Labels.Name,
  79. Value: &lr.LabelID,
  80. })
  81. }
  82. }
  83. resp.Data.Data = append(resp.Data.Data,
  84. types.ContactInfo{
  85. BaseIDInfo: types.BaseIDInfo{
  86. Id: &v.ID,
  87. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  88. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  89. },
  90. Status: &v.Status,
  91. WxWxid: &v.WxWxid,
  92. Type: &v.Type,
  93. Wxid: &v.Wxid,
  94. Account: &v.Account,
  95. Nickname: &v.Nickname,
  96. Markname: &v.Markname,
  97. Headimg: &v.Headimg,
  98. Sex: &v.Sex,
  99. Starrole: &v.Starrole,
  100. Dontseeit: &v.Dontseeit,
  101. Dontseeme: &v.Dontseeme,
  102. Lag: &v.Lag,
  103. Gid: &v.Gid,
  104. Gname: &v.Gname,
  105. V3: &v.V3,
  106. LabelRelationships: labelRelationships,
  107. })
  108. }
  109. return resp, nil
  110. }