get_agent_by_id_logic.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package agent
  2. import (
  3. "context"
  4. "wechat-api/ent"
  5. "wechat-api/ent/agent"
  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 GetAgentByIdLogic struct {
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. logx.Logger
  17. }
  18. func NewGetAgentByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAgentByIdLogic {
  19. return &GetAgentByIdLogic{
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. Logger: logx.WithContext(ctx),
  23. }
  24. }
  25. func (l *GetAgentByIdLogic) GetAgentById(req *types.IDReq) (*types.AgentInfoResp, error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. data, err := l.svcCtx.DB.Agent.Query().Where(agent.ID(req.Id), agent.OrganizationID(organizationId)).First(l.ctx)
  28. if err != nil && !ent.IsNotFound(err) {
  29. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  30. }
  31. return &types.AgentInfoResp{
  32. BaseDataInfo: types.BaseDataInfo{
  33. Code: 0,
  34. Msg: errormsg.Success,
  35. },
  36. Data: types.AgentInfo{
  37. BaseIDInfo: types.BaseIDInfo{
  38. Id: &data.ID,
  39. CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
  40. UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
  41. },
  42. Name: &data.Name,
  43. Role: &data.Role,
  44. Status: &data.Status,
  45. Background: &data.Background,
  46. Examples: &data.Examples,
  47. DatasetId: &data.DatasetID,
  48. CollectionId: &data.CollectionID,
  49. },
  50. }, nil
  51. }