get_contact_list_logic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. organizationId := l.ctx.Value("organizationId").(uint64)
  45. var predicates []predicate.Contact
  46. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  47. if len(req.LabelIDs) > 0 {
  48. predicates = append(predicates, contact.HasContactRelationshipsWith(
  49. labelrelationship.HasLabelsWith(
  50. label.IDIn(req.LabelIDs...),
  51. ),
  52. labelrelationship.DeletedAtIsNil(),
  53. ))
  54. }
  55. if req.WxWxid != nil {
  56. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  57. }
  58. if req.Wxid != nil {
  59. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  60. }
  61. if req.Account != nil {
  62. predicates = append(predicates, contact.AccountContains(*req.Account))
  63. }
  64. if req.Type != nil {
  65. predicates = append(predicates, contact.TypeEQ(*req.Type))
  66. }
  67. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  68. query.WithLabels()
  69. }).Page(l.ctx, req.Page, req.PageSize)
  70. if err != nil {
  71. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  72. }
  73. resp := &types.ContactListResp{}
  74. resp.Msg = errormsg.Success
  75. resp.Data.Total = data.PageDetails.Total
  76. for _, v := range data.List {
  77. labelRelationships := make([]types.ContactLabelList, 0)
  78. if v.Edges.ContactRelationships != nil {
  79. for _, lr := range v.Edges.ContactRelationships {
  80. labelRelationships = append(labelRelationships, types.ContactLabelList{
  81. Label: &lr.Edges.Labels.Name,
  82. Value: &lr.LabelID,
  83. })
  84. }
  85. }
  86. resp.Data.Data = append(resp.Data.Data,
  87. types.ContactInfo{
  88. BaseIDInfo: types.BaseIDInfo{
  89. Id: &v.ID,
  90. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  91. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  92. },
  93. Status: &v.Status,
  94. WxWxid: &v.WxWxid,
  95. Type: &v.Type,
  96. Wxid: &v.Wxid,
  97. Account: &v.Account,
  98. Nickname: &v.Nickname,
  99. Markname: &v.Markname,
  100. Headimg: &v.Headimg,
  101. Sex: &v.Sex,
  102. Starrole: &v.Starrole,
  103. Dontseeit: &v.Dontseeit,
  104. Dontseeme: &v.Dontseeme,
  105. Lag: &v.Lag,
  106. Gid: &v.Gid,
  107. Gname: &v.Gname,
  108. V3: &v.V3,
  109. LabelRelationships: labelRelationships,
  110. })
  111. }
  112. return resp, nil
  113. }