get_whatsapp_list_logic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package whatsapp
  2. import (
  3. "context"
  4. "github.com/alibabacloud-go/cams-20200606/v2/client"
  5. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "wechat-api/hook/aliyun"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/whatsapp"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils/dberrorhandler"
  13. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  14. "github.com/suyuan32/simple-admin-common/utils/pointy"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type GetWhatsappListLogic struct {
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. logx.Logger
  21. }
  22. func NewGetWhatsappListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhatsappListLogic {
  23. return &GetWhatsappListLogic{
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. Logger: logx.WithContext(ctx),
  27. }
  28. }
  29. func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*types.WhatsappListResp, error) {
  30. var predicates []predicate.Whatsapp
  31. if req.OrganizationId != nil {
  32. predicates = append(predicates, whatsapp.OrganizationID(*req.OrganizationId))
  33. }
  34. if req.Phone != nil {
  35. predicates = append(predicates, whatsapp.Phone(*req.Phone))
  36. }
  37. if req.PhoneName != nil {
  38. predicates = append(predicates, whatsapp.PhoneName(*req.PhoneName))
  39. }
  40. ids := make([]string, 0)
  41. if req.WaId != nil && *req.WaId != "" {
  42. ids = append(ids, *req.WaId)
  43. predicates = append(predicates, whatsapp.WaID(*req.WaId))
  44. } else {
  45. wclist, err := l.svcCtx.DB.WhatsappChannel.Query().All(l.ctx)
  46. if err != nil {
  47. return nil, errorx.NewInvalidArgumentError(err.Error())
  48. }
  49. for _, v := range wclist {
  50. ids = append(ids, v.WaID)
  51. }
  52. }
  53. data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  54. if err != nil {
  55. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  56. }
  57. resp := &types.WhatsappListResp{}
  58. resp.Msg = errormsg.Success
  59. resp.Data.Total = data.PageDetails.Total
  60. phoneNumbers := make([]*client.QueryChatappPhoneNumbersResponseBodyPhoneNumbers, 0)
  61. for _, vid := range ids {
  62. _, err = aliyun.ChatappSyncPhoneNumber(vid)
  63. if err != nil {
  64. l.Logger.Errorf("sync chatapp phoneNumber err=%v\n", err)
  65. }
  66. result, err := aliyun.QueryCamsChatappPhoneNumbers(vid)
  67. l.Logger.Infof("result=%v err=%v vid=%v\n", result, err, vid)
  68. phoneNumbers = append(phoneNumbers, result.Body.PhoneNumbers...)
  69. }
  70. for _, v := range data.List {
  71. organizationName := ""
  72. if v.OrganizationID > 0 {
  73. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  74. if err != nil {
  75. l.Error("获取部门信息失败", err)
  76. }
  77. if departmentInfo != nil {
  78. organizationName = *departmentInfo.Name
  79. }
  80. }
  81. agent := types.AgentInfo{}
  82. //l.Logger.Infof("v.agent=%v v=%v\n", v.Edges.Agent, v)
  83. if v.Edges.Agent != nil {
  84. agent = types.AgentInfo{
  85. BaseIDInfo: types.BaseIDInfo{
  86. Id: &v.AgentID,
  87. },
  88. Name: &v.Edges.Agent.Name,
  89. Role: &v.Edges.Agent.Role,
  90. Status: &v.Edges.Agent.Status,
  91. Background: &v.Edges.Agent.Background,
  92. Examples: &v.Edges.Agent.Examples,
  93. }
  94. }
  95. var phoneStatus, quality, verifyStatus, frequency string
  96. currentPhone := v.Cc + v.Phone
  97. for _, v := range phoneNumbers {
  98. if *v.PhoneNumber == currentPhone {
  99. phoneStatus = *v.Status
  100. verifyStatus = *v.CodeVerificationStatus
  101. quality = *v.QualityRating
  102. frequency = *v.MessagingLimitTier
  103. break
  104. }
  105. }
  106. resp.Data.Data = append(resp.Data.Data,
  107. types.WhatsappInfo{
  108. BaseIDInfo: types.BaseIDInfo{
  109. Id: &v.ID,
  110. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  111. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  112. },
  113. WaId: &v.WaID,
  114. WaName: &v.WaName,
  115. Callback: &v.Callback,
  116. Account: &v.Account,
  117. Phone: &v.Phone,
  118. Cc: &v.Cc,
  119. PhoneName: &v.PhoneName,
  120. PhoneStatus: &phoneStatus,
  121. Quality: &quality,
  122. VerifyStatus: &verifyStatus,
  123. Frequency: &frequency,
  124. OrganizationId: &v.OrganizationID,
  125. OrganizationName: &organizationName,
  126. AgentId: &v.AgentID,
  127. AgentInfo: &agent,
  128. ApiBase: &v.APIBase,
  129. ApiKey: &v.APIKey,
  130. })
  131. }
  132. return resp, nil
  133. }