add_friend_list_logic.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package add_friend
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "wechat-api/ent/addwechatfriendlog"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/wx"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "wechat-api/internal/utils"
  13. )
  14. type AddFriendListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewAddFriendListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFriendListLogic {
  20. return &AddFriendListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *AddFriendListLogic) AddFriendList(req *types.AddFriendListReq) (resp *types.AddFriendListResp, err error) {
  26. //获取上下文的组织id
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. //通过organizationId去wx表拿微信机器人的数
  29. query := l.svcCtx.DB.Wx.Query().Where(wx.OrganizationIDEQ(organizationId), wx.StatusEQ(1))
  30. // 动态添加条件示例:假设 req.OwnerWxId 存在时添加额外的 Where 条件
  31. if utils.IsNonEmptyString(req.OwnerWxId) {
  32. query = query.Where(wx.WxidEQ(*req.OwnerWxId))
  33. }
  34. wxIds, err := query.Select(wx.FieldWxid).Strings(l.ctx)
  35. if err != nil {
  36. //数据库错误,不直接返回错误,防止暴露数据库错误
  37. return nil, errorx.NewInvalidArgumentError("请联系管理员")
  38. }
  39. //机器人列表为空,直接返回
  40. if len(wxIds) == 0 {
  41. return nil, errorx.NewInvalidArgumentError("当前组织架构下,没有wx机器人")
  42. }
  43. var predicates []predicate.AddWechatFriendLog
  44. predicates = append(predicates, addwechatfriendlog.OwnerWxIDIn(wxIds...))
  45. ownerWxType := req.OwnerWxType
  46. //查询企业微信机器人加的好友
  47. if ownerWxType != nil && *ownerWxType > 0 {
  48. predicates = append(predicates, addwechatfriendlog.OwnerWxTypeEQ(*ownerWxType))
  49. }
  50. //查找添加好友
  51. if utils.IsNonEmptyString(req.FindContent) {
  52. predicates = append(predicates, addwechatfriendlog.FindContent(*req.FindContent))
  53. }
  54. var IsCanAdd []int
  55. if req.Status != nil {
  56. switch *req.Status {
  57. case 1:
  58. IsCanAdd = append(IsCanAdd, 0, 1)
  59. case 2:
  60. IsCanAdd = append(IsCanAdd, 3, 4)
  61. default:
  62. IsCanAdd = append(IsCanAdd, 2)
  63. }
  64. predicates = append(predicates, addwechatfriendlog.IsCanAddIn(IsCanAdd...))
  65. }
  66. data, err := l.svcCtx.DB.AddWechatFriendLog.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  67. if err != nil {
  68. return nil, errorx.NewInvalidArgumentError("请联系管理员")
  69. }
  70. resp = &types.AddFriendListResp{}
  71. resp.Msg = errormsg.Success
  72. resp.Data.Total = data.PageDetails.Total
  73. for _, v := range data.List {
  74. var wxString string
  75. if v.OwnerWxType == 1 {
  76. wxString = "个微"
  77. } else {
  78. wxString = "企微"
  79. }
  80. resp.Data.Data = append(resp.Data.Data,
  81. types.FriendList{
  82. Id: v.ID,
  83. OwnerWxId: &v.OwnerWxID,
  84. OwnerWxType: &wxString,
  85. FindContent: &v.FindContent,
  86. Status: func() *string {
  87. statusMap := map[int]string{
  88. 0: "数据准备中",
  89. 1: "待执行",
  90. 2: "成功邀请",
  91. 3: "timeout及其他错误",
  92. 4: "用户不存在",
  93. 5: "后台取消",
  94. }
  95. statusStr := statusMap[v.IsCanAdd]
  96. return &statusStr
  97. }(),
  98. TaskCount: &v.TaskCount,
  99. Message: &v.Message,
  100. CreateTime: utils.UnixTimeToBeijing(v.CreatedAt),
  101. UpdateTime: utils.UnixTimeToBeijing(v.UpdatedAt),
  102. },
  103. )
  104. }
  105. return resp, nil
  106. }