get_contact_simple_list_logic.go 2.4 KB

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