get_chat_session_list_logic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package chatsession
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/suyuan32/simple-admin-common/utils/pointy"
  7. "wechat-api/ent/chatsession"
  8. "wechat-api/ent/employee"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/ent/wxcard"
  11. "wechat-api/ent/wxcarduser"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "wechat-api/internal/svc"
  14. "wechat-api/internal/types"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type GetChatSessionListLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewGetChatSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatSessionListLogic {
  23. return &GetChatSessionListLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx}
  27. }
  28. func (l *GetChatSessionListLogic) GetChatSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
  29. resp := &types.ChatSessionListResp{}
  30. resp.Msg = errormsg.Success
  31. //organizationId := l.ctx.Value("organizationId").(uint64)
  32. //isAdmin := l.ctx.Value("isAdmin").(bool)
  33. var predicates []predicate.ChatSession
  34. //if isAdmin {
  35. //
  36. //} else {
  37. // predicates = append(predicates, chatsession.BotType(uint8(2)))
  38. // predicates = append(predicates, chatsession.BotID(organizationId))
  39. //}
  40. if req.BotType != nil && *req.BotType > 0 {
  41. predicates = append(predicates, chatsession.BotType(*req.BotType))
  42. }
  43. if req.BotId != nil && *req.BotId > 0 {
  44. predicates = append(predicates, chatsession.BotID(*req.BotId))
  45. }
  46. //if req.BotType != nil && *req.BotType > 0 {
  47. // predicates = append(predicates, chatsession.BotType(*req.BotType))
  48. //}
  49. //if req.BotName != nil && *req.BotName != "" {
  50. // predicates = append(predicates, chatsession.BotType(*req.BotType))
  51. //
  52. // ids := make([]uint64, 0)
  53. // if *req.BotType == 2 {
  54. // wxcardList, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.NameContains(*req.BotName)).All(l.ctx)
  55. // if err != nil {
  56. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  57. // }
  58. // for _, val := range wxcardList {
  59. // ids = append(ids, val.ID)
  60. // }
  61. // } else if *req.BotType == 3 {
  62. // employeeList, err := l.svcCtx.DB.Employee.Query().Where(employee.TitleContains(*req.BotName)).All(l.ctx)
  63. // if err != nil {
  64. // return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  65. // }
  66. // for _, val := range employeeList {
  67. // ids = append(ids, val.ID)
  68. // }
  69. // }
  70. //
  71. // if len(ids) > 0 {
  72. // predicates = append(predicates, chatsession.BotIDIn(ids...))
  73. // } else {
  74. // return resp, nil
  75. // }
  76. //}
  77. data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  78. if err != nil {
  79. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  80. }
  81. resp.Data.Total = data.PageDetails.Total
  82. for _, v := range data.List {
  83. botType := ""
  84. botName := ""
  85. userName := ""
  86. if v.BotType == 1 {
  87. botType = "微信"
  88. } else if v.BotType == 2 {
  89. botType = "名片"
  90. cardInfo, _ := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(v.BotID)).First(l.ctx)
  91. botName = cardInfo.Name
  92. } else if v.BotType == 3 {
  93. botType = "数字员工"
  94. employeeInfo, _ := l.svcCtx.DB.Employee.Query().Where(employee.ID(v.BotID)).First(l.ctx)
  95. botName = employeeInfo.Title
  96. }
  97. userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(v.UserID)).First(l.ctx)
  98. if userInfo != nil {
  99. userName = userInfo.Nickname
  100. } else {
  101. userName = fmt.Sprintf("用户_%d", v.UserID)
  102. }
  103. resp.Data.Data = append(resp.Data.Data,
  104. types.ChatSessionInfo{
  105. BaseIDInfo: types.BaseIDInfo{
  106. Id: &v.ID,
  107. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  108. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  109. },
  110. Name: &v.Name,
  111. UserId: &v.UserID,
  112. UserName: &userName,
  113. BotId: &v.BotID,
  114. BotName: &botName,
  115. BotType: &v.BotType,
  116. BotTypeStr: &botType,
  117. })
  118. }
  119. return resp, nil
  120. }