get_agent_list_logic.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package agent
  2. import (
  3. "context"
  4. "wechat-api/ent/agent"
  5. "wechat-api/ent/predicate"
  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 GetAgentListLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewGetAgentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAgentListLogic {
  19. return &GetAgentListLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *GetAgentListLogic) GetAgentList(req *types.AgentListReq) (*types.AgentListResp, error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. var predicates []predicate.Agent
  28. predicates = append(predicates, agent.OrganizationID(organizationId))
  29. if req.Name != nil {
  30. predicates = append(predicates, agent.NameContains(*req.Name))
  31. }
  32. if req.Role != nil {
  33. predicates = append(predicates, agent.RoleContains(*req.Role))
  34. }
  35. if req.Background != nil {
  36. predicates = append(predicates, agent.BackgroundContains(*req.Background))
  37. }
  38. if req.Status != nil {
  39. predicates = append(predicates, agent.Status(*req.Status))
  40. }
  41. data, err := l.svcCtx.DB.Agent.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  42. if err != nil {
  43. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  44. }
  45. resp := &types.AgentListResp{}
  46. resp.Msg = errormsg.Success
  47. resp.Data.Total = data.PageDetails.Total
  48. for _, v := range data.List {
  49. resp.Data.Data = append(resp.Data.Data,
  50. types.AgentInfo{
  51. BaseIDInfo: types.BaseIDInfo{
  52. Id: &v.ID,
  53. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  54. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  55. },
  56. Name: &v.Name,
  57. Role: &v.Role,
  58. Status: &v.Status,
  59. Background: &v.Background,
  60. Examples: &v.Examples,
  61. })
  62. }
  63. return resp, nil
  64. }