get_usage_detail_list_logic.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package UsageDetail
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-common/utils/pointy"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/ent/usagedetail"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type GetUsageDetailListLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewGetUsageDetailListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUsageDetailListLogic {
  19. return &GetUsageDetailListLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *GetUsageDetailListLogic) GetUsageDetailList(req *types.UsageDetailListReq) (resp *types.UsageDetailListResp, err error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. isAdmin := l.ctx.Value("isAdmin").(bool)
  27. var predicates []predicate.UsageDetail
  28. if !isAdmin {
  29. predicates = append(predicates, usagedetail.OrganizationIDEQ(organizationId))
  30. }
  31. predicates = append(predicates, usagedetail.BotID(*req.BotId))
  32. data, err := l.svcCtx.DB.UsageDetail.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  33. if err != nil {
  34. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  35. }
  36. resp = &types.UsageDetailListResp{}
  37. resp.Msg = errormsg.Success
  38. resp.Data.Total = data.PageDetails.Total
  39. for _, v := range data.List {
  40. resp.Data.Data = append(resp.Data.Data,
  41. types.UsageDetailInfo{
  42. BaseIDInfo: types.BaseIDInfo{
  43. Id: &v.ID,
  44. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  45. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  46. },
  47. Status: &v.Status,
  48. Type: &v.Type,
  49. BotId: &v.BotID,
  50. App: &v.App,
  51. SessionId: &v.SessionID,
  52. Request: &v.Request,
  53. Response: &v.Response,
  54. TotalTokens: &v.TotalTokens,
  55. PromptTokens: &v.PromptTokens,
  56. CompletionTokens: &v.CompletionTokens,
  57. OrganizationId: &v.OrganizationID,
  58. })
  59. }
  60. return resp, nil
  61. //return nil, nil
  62. }