get_whatsapp_list_logic.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.OrganizationId != nil {
  29. predicates = append(predicates, whatsapp.OrganizationID(*req.OrganizationId))
  30. }
  31. if req.Phone != nil {
  32. predicates = append(predicates, whatsapp.Phone(*req.Phone))
  33. }
  34. if req.PhoneName != nil {
  35. predicates = append(predicates, whatsapp.PhoneName(*req.PhoneName))
  36. }
  37. if req.WaId != nil && *req.WaId != "" {
  38. predicates = append(predicates, whatsapp.WaID(*req.WaId))
  39. } else {
  40. //wclist, err := l.svcCtx.DB.WhatsappChannel.Query().All(l.ctx)
  41. //if err != nil {
  42. // return nil, errorx.NewInvalidArgumentError(err.Error())
  43. //}
  44. //ids := make([]string, 0)
  45. //for _, v := range wclist {
  46. // ids = append(ids, v.WaID)
  47. //}
  48. }
  49. data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  50. if err != nil {
  51. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  52. }
  53. resp := &types.WhatsappListResp{}
  54. resp.Msg = errormsg.Success
  55. resp.Data.Total = data.PageDetails.Total
  56. //result, err := aliyun.QueryCamsChatappPhoneNumbers(*req.WaId)
  57. //l.Logger.Infof("result=%v err=%v\n", result, err)
  58. //phoneNumbers := result.Body.PhoneNumbers
  59. for _, v := range data.List {
  60. organizationName := ""
  61. if v.OrganizationID > 0 {
  62. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  63. if err != nil {
  64. l.Error("获取部门信息失败", err)
  65. }
  66. if departmentInfo != nil {
  67. organizationName = *departmentInfo.Name
  68. }
  69. }
  70. agent := types.AgentInfo{}
  71. //l.Logger.Infof("v.agent=%v v=%v\n", v.Edges.Agent, v)
  72. if v.Edges.Agent != nil {
  73. agent = types.AgentInfo{
  74. BaseIDInfo: types.BaseIDInfo{
  75. Id: &v.AgentID,
  76. },
  77. Name: &v.Edges.Agent.Name,
  78. Role: &v.Edges.Agent.Role,
  79. Status: &v.Edges.Agent.Status,
  80. Background: &v.Edges.Agent.Background,
  81. Examples: &v.Edges.Agent.Examples,
  82. }
  83. }
  84. var phoneStatus, quality, verifyStatus, frequency string
  85. //currentPhone := v.Cc + v.Phone
  86. //for _, v := range phoneNumbers {
  87. // if *v.PhoneNumber == currentPhone {
  88. // phoneStatus = *v.Status
  89. // verifyStatus = *v.CodeVerificationStatus
  90. // quality = *v.QualityRating
  91. // frequency = *v.MessagingLimitTier
  92. // break
  93. // }
  94. //}
  95. resp.Data.Data = append(resp.Data.Data,
  96. types.WhatsappInfo{
  97. BaseIDInfo: types.BaseIDInfo{
  98. Id: &v.ID,
  99. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  100. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  101. },
  102. WaId: &v.WaID,
  103. WaName: &v.WaName,
  104. Callback: &v.Callback,
  105. Account: &v.Account,
  106. Phone: &v.Phone,
  107. Cc: &v.Cc,
  108. PhoneName: &v.PhoneName,
  109. PhoneStatus: &phoneStatus,
  110. Quality: &quality,
  111. VerifyStatus: &verifyStatus,
  112. Frequency: &frequency,
  113. OrganizationId: &v.OrganizationID,
  114. OrganizationName: &organizationName,
  115. AgentId: &v.AgentID,
  116. AgentInfo: &agent,
  117. ApiBase: &v.APIBase,
  118. ApiKey: &v.APIKey,
  119. })
  120. }
  121. return resp, nil
  122. }