get_wx_list_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package Wx
  2. import (
  3. "context"
  4. "wechat-api/ent/predicate"
  5. "wechat-api/ent/wx"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  10. "github.com/suyuan32/simple-admin-common/utils/pointy"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type GetWxListLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewGetWxListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxListLogic {
  19. return &GetWxListLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *GetWxListLogic) GetWxList(req *types.WxListReq) (*types.WxListResp, error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. var predicates []predicate.Wx
  28. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  29. if req.Port != nil {
  30. predicates = append(predicates, wx.PortContains(*req.Port))
  31. }
  32. if req.ProcessId != nil {
  33. predicates = append(predicates, wx.ProcessIDContains(*req.ProcessId))
  34. }
  35. if req.Callback != nil {
  36. predicates = append(predicates, wx.CallbackContains(*req.Callback))
  37. }
  38. data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  39. if err != nil {
  40. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  41. }
  42. resp := &types.WxListResp{}
  43. resp.Msg = errormsg.Success
  44. resp.Data.Total = data.PageDetails.Total
  45. for _, v := range data.List {
  46. var agent types.AgentInfo
  47. if v.Edges.Agent != nil {
  48. agent = types.AgentInfo{
  49. BaseIDInfo: types.BaseIDInfo{
  50. Id: &v.AgentID,
  51. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  52. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  53. },
  54. Name: &v.Edges.Agent.Name,
  55. Role: &v.Edges.Agent.Role,
  56. Status: &v.Edges.Agent.Status,
  57. Background: &v.Edges.Agent.Background,
  58. Examples: &v.Edges.Agent.Examples,
  59. }
  60. }
  61. resp.Data.Data = append(resp.Data.Data,
  62. types.WxInfo{
  63. BaseIDInfo: types.BaseIDInfo{
  64. Id: &v.ID,
  65. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  66. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  67. },
  68. Status: &v.Status,
  69. ServerId: &v.ServerID,
  70. Port: &v.Port,
  71. ProcessId: &v.ProcessID,
  72. Callback: &v.Callback,
  73. Wxid: &v.Wxid,
  74. Account: &v.Account,
  75. Nickname: &v.Nickname,
  76. Tel: &v.Tel,
  77. HeadBig: &v.HeadBig,
  78. OrganizationId: &v.OrganizationID,
  79. AgentId: &v.AgentID,
  80. AgentInfo: &agent,
  81. })
  82. }
  83. return resp, nil
  84. }