get_chat_session_list_logic.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/employee"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/ent/wxcard"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetChatSessionListLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewGetChatSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatSessionListLogic {
  22. return &GetChatSessionListLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *GetChatSessionListLogic) GetChatSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
  28. resp := &types.ChatSessionListResp{}
  29. resp.Msg = errormsg.Success
  30. var predicates []predicate.ChatSession
  31. if req.BotType == nil || *req.BotType == 0 {
  32. return nil, errorx.NewInvalidArgumentError("BotId cannot be null")
  33. }
  34. if req.BotName != nil && *req.BotName != "" {
  35. predicates = append(predicates, chatsession.BotType(*req.BotType))
  36. ids := make([]uint64, 0)
  37. if *req.BotType == 2 {
  38. wxcardList, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.NameContains(*req.BotName)).All(l.ctx)
  39. if err != nil {
  40. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  41. }
  42. for _, val := range wxcardList {
  43. ids = append(ids, val.ID)
  44. }
  45. } else if *req.BotType == 3 {
  46. employeeList, err := l.svcCtx.DB.Employee.Query().Where(employee.TitleContains(*req.BotName)).All(l.ctx)
  47. if err != nil {
  48. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  49. }
  50. for _, val := range employeeList {
  51. ids = append(ids, val.ID)
  52. }
  53. }
  54. if len(ids) > 0 {
  55. predicates = append(predicates, chatsession.BotIDIn(ids...))
  56. } else {
  57. return resp, nil
  58. }
  59. }
  60. data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  61. if err != nil {
  62. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  63. }
  64. resp.Data.Total = data.PageDetails.Total
  65. for _, v := range data.List {
  66. resp.Data.Data = append(resp.Data.Data,
  67. types.ChatSessionInfo{
  68. BaseIDInfo: types.BaseIDInfo{
  69. Id: &v.ID,
  70. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  71. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  72. },
  73. Name: &v.Name,
  74. UserId: &v.UserID,
  75. BotId: &v.BotID,
  76. BotType: &v.BotType,
  77. })
  78. }
  79. return resp, nil
  80. }