get_agent_list_logic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. var predicates []predicate.Agent
  27. if req.Name != nil {
  28. predicates = append(predicates, agent.NameContains(*req.Name))
  29. }
  30. if req.Role != nil {
  31. predicates = append(predicates, agent.RoleContains(*req.Role))
  32. }
  33. if req.Background != nil {
  34. predicates = append(predicates, agent.BackgroundContains(*req.Background))
  35. }
  36. data, err := l.svcCtx.DB.Agent.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  37. if err != nil {
  38. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  39. }
  40. resp := &types.AgentListResp{}
  41. resp.Msg = errormsg.Success
  42. resp.Data.Total = data.PageDetails.Total
  43. for _, v := range data.List {
  44. resp.Data.Data = append(resp.Data.Data,
  45. types.AgentInfo{
  46. BaseIDInfo: types.BaseIDInfo{
  47. Id: &v.ID,
  48. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  49. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  50. },
  51. Name: &v.Name,
  52. Role: &v.Role,
  53. Status: &v.Status,
  54. Background: &v.Background,
  55. Examples: &v.Examples,
  56. })
  57. }
  58. return resp, nil
  59. }