get_contact_list_logic.go 3.5 KB

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