add_friend_list_logic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.DeletedAtIsNil())
  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, 6)
  61. case 3:
  62. IsCanAdd = append(IsCanAdd, 2)
  63. default:
  64. IsCanAdd = append(IsCanAdd, 0, 1, 2, 3, 4, 5, 6)
  65. }
  66. if len(IsCanAdd) < 7 {
  67. predicates = append(predicates, addwechatfriendlog.IsCanAddIn(IsCanAdd...))
  68. }
  69. }
  70. data, err := l.svcCtx.DB.AddWechatFriendLog.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
  71. logx.Info("数据", data)
  72. if err != nil {
  73. return nil, errorx.NewInvalidArgumentError("请联系管理员")
  74. }
  75. resp = &types.AddFriendListResp{}
  76. resp.Msg = errormsg.Success
  77. resp.Data.Total = data.PageDetails.Total
  78. for _, v := range data.List {
  79. var wxString string
  80. if v.OwnerWxType == 1 {
  81. wxString = "个微"
  82. } else {
  83. wxString = "企微"
  84. }
  85. resp.Data.Data = append(resp.Data.Data,
  86. types.FriendList{
  87. Id: v.ID,
  88. OwnerWxId: &v.OwnerWxID,
  89. OwnerWxType: &wxString,
  90. FindContent: &v.FindContent,
  91. Status: func() *string {
  92. statusMap := map[int]string{
  93. 0: "数据准备中",
  94. 1: "待执行",
  95. 2: "成功邀请",
  96. 3: "timeout及其他错误",
  97. 4: "用户不存在",
  98. 5: "后台取消",
  99. 6: "已是好友",
  100. }
  101. statusStr := statusMap[v.IsCanAdd]
  102. return &statusStr
  103. }(),
  104. IsShow: func() *int {
  105. statusMap := map[int]int{
  106. 0: 1,
  107. 1: 1,
  108. 2: 0,
  109. 3: 0,
  110. 4: 0,
  111. 5: 0,
  112. 6: 0,
  113. }
  114. isShow := statusMap[v.IsCanAdd]
  115. return &isShow
  116. }(),
  117. TaskCount: &v.TaskCount,
  118. Message: &v.Message,
  119. CreateTime: utils.UnixTimeToBeijing(v.CreatedAt),
  120. UpdateTime: utils.UnixTimeToBeijing(v.UpdatedAt),
  121. },
  122. )
  123. }
  124. return resp, nil
  125. }