get_contact_simple_list_logic.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package contact
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-common/utils/pointy"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetContactSimpleListLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewGetContactSimpleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactSimpleListLogic {
  22. return &GetContactSimpleListLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *GetContactSimpleListLogic) GetContactSimpleList(req *types.ContactListReq) (*types.ContactSimpleListResp, error) {
  28. organizationId := l.ctx.Value("organizationId").(uint64)
  29. var predicates []predicate.Contact
  30. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  31. var ctype uint64 = 1
  32. if req.Ctype != nil {
  33. ctype = *req.Ctype
  34. }
  35. predicates = append(predicates, contact.Ctype(ctype))
  36. if len(req.LabelIDs) > 0 {
  37. predicates = append(predicates, contact.HasContactRelationshipsWith(
  38. labelrelationship.HasLabelsWith(
  39. label.IDIn(req.LabelIDs...),
  40. ),
  41. ))
  42. }
  43. if req.WxWxid != nil {
  44. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  45. }
  46. if req.Wxid != nil {
  47. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  48. }
  49. if req.Account != nil {
  50. predicates = append(predicates, contact.AccountContains(*req.Account))
  51. }
  52. if req.Nickname != nil {
  53. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  54. }
  55. if req.Type != nil {
  56. predicates = append(predicates, contact.TypeEQ(*req.Type))
  57. } else {
  58. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  59. }
  60. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  61. query.WithLabels()
  62. }).Page(l.ctx, req.Page, req.PageSize)
  63. if err != nil {
  64. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  65. }
  66. resp := &types.ContactSimpleListResp{}
  67. resp.Msg = errormsg.Success
  68. resp.Data.Total = data.PageDetails.Total
  69. for _, v := range data.List {
  70. resp.Data.Data = append(resp.Data.Data,
  71. types.ContactSimpleInfo{
  72. BaseIDInfo: types.BaseIDInfo{
  73. Id: &v.ID,
  74. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  75. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  76. },
  77. Wxid: &v.Wxid,
  78. Nickname: &v.Nickname,
  79. })
  80. }
  81. return resp, nil
  82. }