get_usage_total_list_logic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package UsageTotal
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/suyuan32/simple-admin-common/utils/pointy"
  7. "wechat-api/ent/predicate"
  8. "wechat-api/ent/usagetotal"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetUsageTotalListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetUsageTotalListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUsageTotalListLogic {
  20. return &GetUsageTotalListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetUsageTotalListLogic) GetUsageTotalList(req *types.UsageTotalListReq) (resp *types.UsageTotalListResp, err error) {
  26. isAdmin := l.ctx.Value("isAdmin").(bool)
  27. if !isAdmin {
  28. return nil, fmt.Errorf("GetUsageTotalList failed")
  29. }
  30. var predicates []predicate.UsageTotal
  31. predicates = append(predicates, usagetotal.OrganizationIDEQ(*req.OrganizationId))
  32. data, err := l.svcCtx.DB.UsageTotal.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.UsageTotalListResp{}
  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.UsageTotalInfo{
  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. TotalTokens: &v.TotalTokens,
  51. StartIndex: &v.StartIndex,
  52. EndIndex: &v.EndIndex,
  53. OrganizationId: &v.OrganizationID,
  54. })
  55. }
  56. return resp, nil
  57. //return nil, nil
  58. }