get_agent_list_logic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package agent
  2. import (
  3. "context"
  4. "wechat-api/ent/agent"
  5. "wechat-api/ent/predicate"
  6. "wechat-api/hook/fastgpt"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  11. "github.com/suyuan32/simple-admin-common/utils/pointy"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetAgentListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetAgentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAgentListLogic {
  20. return &GetAgentListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *GetAgentListLogic) GetAgentList(req *types.AgentListReq) (*types.AgentListResp, error) {
  27. orgId := l.ctx.Value("organizationId").(uint64)
  28. var predicates []predicate.Agent
  29. if req.OrganizationId != nil && *req.OrganizationId != 0 {
  30. isAdmin := l.ctx.Value("isAdmin").(bool)
  31. if isAdmin {
  32. predicates = append(predicates, agent.OrganizationIDEQ(*req.OrganizationId))
  33. } else {
  34. predicates = append(predicates, agent.OrganizationIDEQ(orgId))
  35. }
  36. } else {
  37. predicates = append(predicates, agent.OrganizationID(orgId))
  38. }
  39. if req.Name != nil {
  40. predicates = append(predicates, agent.NameContains(*req.Name))
  41. }
  42. if req.Role != nil {
  43. predicates = append(predicates, agent.RoleContains(*req.Role))
  44. }
  45. if req.Background != nil {
  46. predicates = append(predicates, agent.BackgroundContains(*req.Background))
  47. }
  48. if req.Status != nil {
  49. predicates = append(predicates, agent.Status(*req.Status))
  50. }
  51. data, err := l.svcCtx.DB.Agent.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  52. if err != nil {
  53. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  54. }
  55. resp := &types.AgentListResp{}
  56. resp.Msg = errormsg.Success
  57. resp.Data.Total = data.PageDetails.Total
  58. for _, v := range data.List {
  59. // 这里是获得数据集合的修改时间
  60. var updatedAt int64
  61. if v.CollectionID != "" {
  62. info, err := fastgpt.GetCollectionDetail(v.CollectionID)
  63. if err == nil {
  64. updatedAt = info.Data.UpdateTime.UnixMilli()
  65. }
  66. }
  67. resp.Data.Data = append(resp.Data.Data,
  68. types.AgentInfo{
  69. BaseIDInfo: types.BaseIDInfo{
  70. Id: &v.ID,
  71. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  72. UpdatedAt: pointy.GetPointer(updatedAt),
  73. },
  74. Name: &v.Name,
  75. Role: &v.Role,
  76. Status: &v.Status,
  77. Background: &v.Background,
  78. Examples: &v.Examples,
  79. DatasetId: &v.DatasetID,
  80. CollectionId: &v.CollectionID,
  81. Model: &v.Model,
  82. ApiBase: &v.APIBase,
  83. ApiKey: &v.APIKey,
  84. Type: &v.Type,
  85. })
  86. }
  87. return resp, nil
  88. }