get_whatsapp_list_logic.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package whatsapp
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  5. "wechat-api/ent/predicate"
  6. "wechat-api/ent/whatsapp"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  11. "github.com/suyuan32/simple-admin-common/utils/pointy"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetWhatsappListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetWhatsappListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhatsappListLogic {
  20. return &GetWhatsappListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*types.WhatsappListResp, error) {
  27. var predicates []predicate.Whatsapp
  28. if req.Ak != nil {
  29. predicates = append(predicates, whatsapp.AkContains(*req.Ak))
  30. }
  31. if req.Sk != nil {
  32. predicates = append(predicates, whatsapp.SkContains(*req.Sk))
  33. }
  34. if req.Callback != nil {
  35. predicates = append(predicates, whatsapp.CallbackContains(*req.Callback))
  36. }
  37. data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  38. if err != nil {
  39. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  40. }
  41. resp := &types.WhatsappListResp{}
  42. resp.Msg = errormsg.Success
  43. resp.Data.Total = data.PageDetails.Total
  44. for _, v := range data.List {
  45. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  46. if err != nil {
  47. l.Error("获取部门信息失败", err)
  48. }
  49. agent := types.AgentInfo{}
  50. //l.Logger.Infof("v.agent=%v v=%v\n", v.Edges.Agent, v)
  51. if v.Edges.Agent != nil {
  52. agent = types.AgentInfo{
  53. BaseIDInfo: types.BaseIDInfo{
  54. Id: &v.AgentID,
  55. },
  56. Name: &v.Edges.Agent.Name,
  57. Role: &v.Edges.Agent.Role,
  58. Status: &v.Edges.Agent.Status,
  59. Background: &v.Edges.Agent.Background,
  60. Examples: &v.Edges.Agent.Examples,
  61. }
  62. }
  63. resp.Data.Data = append(resp.Data.Data,
  64. types.WhatsappInfo{
  65. BaseIDInfo: types.BaseIDInfo{
  66. Id: &v.ID,
  67. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  68. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  69. },
  70. Ak: &v.Ak,
  71. Sk: &v.Sk,
  72. Callback: &v.Callback,
  73. Account: &v.Account,
  74. Nickname: &v.Nickname,
  75. Phone: &v.Phone,
  76. OrganizationId: &v.OrganizationID,
  77. OrganizationName: departmentInfo.Name,
  78. AgentId: &v.AgentID,
  79. AgentInfo: &agent,
  80. ApiBase: &v.APIBase,
  81. ApiKey: &v.APIKey,
  82. })
  83. }
  84. return resp, nil
  85. }