get_contact_simple_list_logic.go 2.1 KB

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