get_api_session_list_logic.go 1.7 KB

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