get_chat_session_list_logic.go 2.8 KB

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