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. organizationId := l.ctx.Value("organizationId").(uint64)
  25. var predicates []predicate.Contact
  26. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  27. var ctype uint64 = 1
  28. if req.Ctype != nil {
  29. ctype = *req.Ctype
  30. }
  31. predicates = append(predicates, contact.Ctype(ctype))
  32. if req.WxWxid != nil {
  33. predicates = append(predicates, contact.WxWxid(*req.WxWxid))
  34. }
  35. if req.Wxid != nil {
  36. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  37. }
  38. if req.Account != nil {
  39. predicates = append(predicates, contact.AccountContains(*req.Account))
  40. }
  41. if req.Nickname != nil {
  42. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  43. }
  44. if req.Type != nil {
  45. predicates = append(predicates, contact.TypeEQ(*req.Type))
  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. }