get_agent_list_logic.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. orgId := l.ctx.Value("organizationId").(uint64)
  27. var predicates []predicate.Agent
  28. if req.OrganizationId != nil && *req.OrganizationId != 0 {
  29. isAdmin := l.ctx.Value("isAdmin").(bool)
  30. if isAdmin {
  31. predicates = append(predicates, agent.OrganizationIDEQ(*req.OrganizationId))
  32. } else {
  33. predicates = append(predicates, agent.OrganizationIDEQ(orgId))
  34. }
  35. } else {
  36. predicates = append(predicates, agent.OrganizationID(orgId))
  37. }
  38. if req.Name != nil {
  39. predicates = append(predicates, agent.NameContains(*req.Name))
  40. }
  41. if req.Role != nil {
  42. predicates = append(predicates, agent.RoleContains(*req.Role))
  43. }
  44. if req.Background != nil {
  45. predicates = append(predicates, agent.BackgroundContains(*req.Background))
  46. }
  47. if req.Status != nil {
  48. predicates = append(predicates, agent.Status(*req.Status))
  49. }
  50. data, err := l.svcCtx.DB.Agent.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  51. if err != nil {
  52. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  53. }
  54. resp := &types.AgentListResp{}
  55. resp.Msg = errormsg.Success
  56. resp.Data.Total = data.PageDetails.Total
  57. for _, v := range data.List {
  58. resp.Data.Data = append(resp.Data.Data,
  59. types.AgentInfo{
  60. BaseIDInfo: types.BaseIDInfo{
  61. Id: &v.ID,
  62. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  63. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  64. },
  65. Name: &v.Name,
  66. Role: &v.Role,
  67. Status: &v.Status,
  68. Background: &v.Background,
  69. Examples: &v.Examples,
  70. DatasetId: &v.DatasetID,
  71. CollectionId: &v.CollectionID,
  72. })
  73. }
  74. return resp, nil
  75. }