get_contact_simple_list_logic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package contact
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/ent/contact"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetContactSimpleListLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewGetContactSimpleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactSimpleListLogic {
  18. return &GetContactSimpleListLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *GetContactSimpleListLogic) GetContactSimpleList(req *types.ContactListReq) (*types.ContactSimpleListResp, error) {
  24. var predicates []predicate.Contact
  25. var ctype uint64 = 1
  26. if req.Ctype != nil {
  27. ctype = *req.Ctype
  28. }
  29. predicates = append(predicates, contact.Ctype(ctype))
  30. if req.WxWxid != nil {
  31. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  32. }
  33. if req.Wxid != nil {
  34. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  35. }
  36. if req.Account != nil {
  37. predicates = append(predicates, contact.AccountContains(*req.Account))
  38. }
  39. if req.Nickname != nil {
  40. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  41. }
  42. if req.Type != nil {
  43. predicates = append(predicates, contact.TypeEQ(*req.Type))
  44. } else {
  45. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  46. }
  47. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  48. if err != nil {
  49. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  50. }
  51. resp := &types.ContactSimpleListResp{}
  52. resp.Msg = errormsg.Success
  53. resp.Data.Total = data.PageDetails.Total
  54. for _, v := range data.List {
  55. resp.Data.Data = append(resp.Data.Data,
  56. types.ContactSimpleInfo{
  57. Wxid: &v.Wxid,
  58. Nickname: &v.Nickname,
  59. })
  60. }
  61. return resp, nil
  62. }