get_api_session_list_logic.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package chatsession
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-common/utils/pointy"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "wechat-api/ent/chatsession"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetApiSessionListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetApiSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiSessionListLogic {
  20. return &GetApiSessionListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetApiSessionListLogic) GetApiSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
  26. userId := l.ctx.Value("userId").(uint64)
  27. var predicates []predicate.ChatSession
  28. predicates = append(predicates, chatsession.UserID(userId))
  29. if req.BotType != nil && *req.BotType > 0 {
  30. predicates = append(predicates, chatsession.BotType(*req.BotType))
  31. } else {
  32. return nil, errorx.NewInvalidArgumentError("BotType cannot be null")
  33. }
  34. if req.BotId != nil && *req.BotId > 0 {
  35. predicates = append(predicates, chatsession.BotID(*req.BotId))
  36. } else {
  37. return nil, errorx.NewInvalidArgumentError("BotId cannot be null")
  38. }
  39. data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  40. if err != nil {
  41. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  42. }
  43. resp := &types.ChatSessionListResp{}
  44. resp.Msg = errormsg.Success
  45. resp.Data.Total = data.PageDetails.Total
  46. for _, v := range data.List {
  47. resp.Data.Data = append(resp.Data.Data,
  48. types.ChatSessionInfo{
  49. BaseIDInfo: types.BaseIDInfo{
  50. Id: &v.ID,
  51. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  52. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  53. },
  54. Name: &v.Name,
  55. UserId: &v.UserID,
  56. BotId: &v.BotID,
  57. BotType: &v.BotType,
  58. })
  59. }
  60. return resp, nil
  61. }