get_chat_session_list_logic.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. resp := &types.ChatSessionListResp{}
  26. resp.Msg = errormsg.Success
  27. //organizationId := l.ctx.Value("organizationId").(uint64)
  28. //isAdmin := l.ctx.Value("isAdmin").(bool)
  29. var predicates []predicate.ChatSession
  30. //if isAdmin {
  31. //
  32. //} else {
  33. // predicates = append(predicates, chatsession.BotType(uint8(2)))
  34. // predicates = append(predicates, chatsession.BotID(organizationId))
  35. //}
  36. if req.BotType != nil && *req.BotType > 0 {
  37. predicates = append(predicates, chatsession.BotType(*req.BotType))
  38. }
  39. if req.BotId != nil && *req.BotId > 0 {
  40. predicates = append(predicates, chatsession.BotID(*req.BotId))
  41. }
  42. //if req.BotType != nil && *req.BotType > 0 {
  43. // predicates = append(predicates, chatsession.BotType(*req.BotType))
  44. //}
  45. //if req.BotName != nil && *req.BotName != "" {
  46. // predicates = append(predicates, chatsession.BotType(*req.BotType))
  47. //
  48. // ids := make([]uint64, 0)
  49. // if *req.BotType == 2 {
  50. // wxcardList, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.NameContains(*req.BotName)).All(l.ctx)
  51. // if err != nil {
  52. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  53. // }
  54. // for _, val := range wxcardList {
  55. // ids = append(ids, val.ID)
  56. // }
  57. // } else if *req.BotType == 3 {
  58. // employeeList, err := l.svcCtx.DB.Employee.Query().Where(employee.TitleContains(*req.BotName)).All(l.ctx)
  59. // if err != nil {
  60. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  61. // }
  62. // for _, val := range employeeList {
  63. // ids = append(ids, val.ID)
  64. // }
  65. // }
  66. //
  67. // if len(ids) > 0 {
  68. // predicates = append(predicates, chatsession.BotIDIn(ids...))
  69. // } else {
  70. // return resp, nil
  71. // }
  72. //}
  73. data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  74. if err != nil {
  75. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  76. }
  77. resp.Data.Total = data.PageDetails.Total
  78. for _, v := range data.List {
  79. resp.Data.Data = append(resp.Data.Data,
  80. types.ChatSessionInfo{
  81. BaseIDInfo: types.BaseIDInfo{
  82. Id: &v.ID,
  83. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  84. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  85. },
  86. Name: &v.Name,
  87. UserId: &v.UserID,
  88. BotId: &v.BotID,
  89. BotType: &v.BotType,
  90. })
  91. }
  92. return resp, nil
  93. }