get_chat_session_list_logic.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 GetChatSessionListLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewGetChatSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatSessionListLogic {
  19. return &GetChatSessionListLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *GetChatSessionListLogic) GetChatSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
  25. var predicates []predicate.ChatSession
  26. if req.BotType != nil && *req.BotType > 0 {
  27. predicates = append(predicates, chatsession.BotType(*req.BotType))
  28. }
  29. data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. resp := &types.ChatSessionListResp{}
  34. resp.Msg = errormsg.Success
  35. resp.Data.Total = data.PageDetails.Total
  36. for _, v := range data.List {
  37. resp.Data.Data = append(resp.Data.Data,
  38. types.ChatSessionInfo{
  39. BaseIDInfo: types.BaseIDInfo{
  40. Id: &v.ID,
  41. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  42. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  43. },
  44. Name: &v.Name,
  45. UserId: &v.UserID,
  46. BotId: &v.BotID,
  47. BotType: &v.BotType,
  48. })
  49. }
  50. return resp, nil
  51. }