add_friend_list_logic.go 3.5 KB

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