get_label_contacts_logic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package label
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/suyuan32/simple-admin-common/utils/pointy"
  7. "wechat-api/ent"
  8. "wechat-api/ent/label"
  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 GetLabelContactsLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewGetLabelContactsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLabelContactsLogic {
  21. return &GetLabelContactsLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *GetLabelContactsLogic) GetLabelContacts(req *types.LabelListReq) (*types.LabelListResp, error) {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. if len(req.LabelIDs) == 0 {
  29. return nil, errors.New("label id list cannot be empty")
  30. }
  31. var predicates []predicate.Label
  32. predicates = append(predicates, label.OrganizationIDEQ(organizationId))
  33. if len(req.LabelIDs) > 0 {
  34. predicates = append(predicates, label.IDIn(req.LabelIDs...))
  35. }
  36. if req.Type != nil {
  37. predicates = append(predicates, label.TypeEQ(*req.Type))
  38. }
  39. if req.Name != nil {
  40. predicates = append(predicates, label.NameContains(*req.Name))
  41. }
  42. if req.From != nil {
  43. predicates = append(predicates, label.FromEQ(*req.From))
  44. }
  45. if req.Mode != nil {
  46. predicates = append(predicates, label.ModeEQ(*req.Mode))
  47. }
  48. data, err := l.svcCtx.DB.Label.Query().Where(predicates...).WithLabelRelationships(func(query *ent.LabelRelationshipQuery) {
  49. query.WithContacts()
  50. }).Page(l.ctx, req.Page, req.PageSize)
  51. if err != nil {
  52. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  53. }
  54. resp := &types.LabelListResp{}
  55. resp.Msg = errormsg.Success
  56. resp.Data.Total = data.PageDetails.Total
  57. for _, v := range data.List {
  58. labelRelationships := make([]types.LabelRelationshipInfo, 0)
  59. if v.Edges.LabelRelationships != nil {
  60. for _, lr := range v.Edges.LabelRelationships {
  61. labelRelationships = append(labelRelationships, types.LabelRelationshipInfo{
  62. BaseIDInfo: types.BaseIDInfo{
  63. Id: &lr.ID,
  64. CreatedAt: pointy.GetPointer(lr.CreatedAt.UnixMilli()),
  65. UpdatedAt: pointy.GetPointer(lr.UpdatedAt.UnixMilli()),
  66. },
  67. Status: &lr.Status,
  68. LabelId: &lr.LabelID,
  69. ContactId: &lr.ContactID,
  70. Contact: convertContactToContactInfo(lr.Edges.Contacts),
  71. })
  72. }
  73. }
  74. resp.Data.Data = append(resp.Data.Data,
  75. types.LabelInfo{
  76. BaseIDInfo: types.BaseIDInfo{
  77. Id: &v.ID,
  78. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  79. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  80. },
  81. Status: &v.Status,
  82. Type: &v.Type,
  83. Name: &v.Name,
  84. From: &v.From,
  85. Mode: &v.Mode,
  86. Conditions: &v.Conditions,
  87. LabelRelationships: labelRelationships,
  88. })
  89. }
  90. return resp, nil
  91. }