get_agent_list_logic.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. info, err := fastgpt.GetCollectionDetail(v.CollectionID)
  62. if err == nil {
  63. updatedAt = info.Data.UpdateTime.UnixMilli()
  64. }
  65. resp.Data.Data = append(resp.Data.Data,
  66. types.AgentInfo{
  67. BaseIDInfo: types.BaseIDInfo{
  68. Id: &v.ID,
  69. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  70. UpdatedAt: pointy.GetPointer(updatedAt),
  71. },
  72. Name: &v.Name,
  73. Role: &v.Role,
  74. Status: &v.Status,
  75. Background: &v.Background,
  76. Examples: &v.Examples,
  77. DatasetId: &v.DatasetID,
  78. CollectionId: &v.CollectionID,
  79. Model: &v.Model,
  80. ApiBase: &v.APIBase,
  81. ApiKey: &v.APIKey,
  82. Type: &v.Type,
  83. })
  84. }
  85. return resp, nil
  86. }