get_agent_list_logic.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // 这里是获得数据集合的修改时间
  59. //var updatedAt int64
  60. //if v.CollectionID != "" {
  61. // info, err := fastgpt.GetCollectionDetail(v.CollectionID)
  62. // if err == nil {
  63. // updatedAt = info.Data.UpdateTime.UnixMilli()
  64. // }
  65. //}
  66. resp.Data.Data = append(resp.Data.Data,
  67. types.AgentInfo{
  68. BaseIDInfo: types.BaseIDInfo{
  69. Id: &v.ID,
  70. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  71. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  72. },
  73. Name: &v.Name,
  74. Role: &v.Role,
  75. Status: &v.Status,
  76. Background: &v.Background,
  77. Examples: &v.Examples,
  78. DatasetId: &v.DatasetID,
  79. CollectionId: &v.CollectionID,
  80. Model: &v.Model,
  81. ApiBase: &v.APIBase,
  82. ApiKey: &v.APIKey,
  83. Type: &v.Type,
  84. })
  85. }
  86. return resp, nil
  87. }