作者 yangfu
@@ -54,8 +54,8 @@ func (l *MiniSubscribeLogic) getOpenId(conn transaction.Conn, userId int64) (str @@ -54,8 +54,8 @@ func (l *MiniSubscribeLogic) getOpenId(conn transaction.Conn, userId int64) (str
54 return userWechat.OpenId, nil 54 return userWechat.OpenId, nil
55 } 55 }
56 56
57 -// saveMessage 保存订阅消息  
58 -func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) error { 57 +// sendAndDecrease 保存并扣减订阅消息
  58 +func (l *MiniSubscribeLogic) sendAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message) error {
59 templateData := make(map[string]interface{}) 59 templateData := make(map[string]interface{})
60 _ = copier.Copy(&templateData, msg.Data) 60 _ = copier.Copy(&templateData, msg.Data)
61 subscribeMessage := &domain.MessageSubscribe{ 61 subscribeMessage := &domain.MessageSubscribe{
@@ -66,14 +66,34 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI @@ -66,14 +66,34 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI
66 TemplateId: msg.TemplateID, 66 TemplateId: msg.TemplateID,
67 TemplateData: templateData, 67 TemplateData: templateData,
68 } 68 }
69 - if sendErr != nil { 69 + //获取订阅次数
  70 + userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)
  71 + if err != nil || userSubscribe.Count <= 0 {
70 subscribeMessage.Result = "fail" 72 subscribeMessage.Result = "fail"
71 - subscribeMessage.Error = sendErr.Error() 73 + subscribeMessage.Error = "订阅次数已用完"
72 } else { 74 } else {
73 - subscribeMessage.Result = "ok" 75 + //扣减订阅次数
  76 + userSubscribe.Count = userSubscribe.Count - 1
  77 + _, err = l.svcCtx.UserSubscribeRepository.Update(l.ctx, conn, userSubscribe)
  78 + if err != nil {
  79 + return err
  80 + }
  81 + //发送订阅消息
  82 + subCtx := l.getSubScribe()
  83 + sendErr := subCtx.Send(msg)
  84 + if sendErr != nil {
  85 + subscribeMessage.Result = "fail"
  86 + subscribeMessage.Error = sendErr.Error()
  87 + } else {
  88 + subscribeMessage.Result = "ok"
  89 + }
  90 + }
  91 + //保存订阅结果
  92 + _, err = l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
  93 + if err != nil {
  94 + return err
74 } 95 }
75 - _, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)  
76 - return err 96 + return nil
77 } 97 }
78 98
79 func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe { 99 func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe {
@@ -109,7 +129,6 @@ func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, comp @@ -109,7 +129,6 @@ func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, comp
109 // @param article 文章 129 // @param article 文章
110 // @param comment 评论 130 // @param comment 评论
111 func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain.Article, comment *domain.ArticleComment) error { 131 func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain.Article, comment *domain.ArticleComment) error {
112 - subCtx := l.getSubScribe()  
113 //评论用户+职位 例: 张三-董事办 132 //评论用户+职位 例: 张三-董事办
114 fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId) 133 fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId)
115 if err != nil { 134 if err != nil {
@@ -124,7 +143,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -124,7 +143,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
124 //评论内容 143 //评论内容
125 "thing2": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()}, 144 "thing2": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
126 //评论时间 145 //评论时间
127 - "thing3": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")}, 146 + "time3": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
128 //评论用户 147 //评论用户
129 "thing5": &subscribe.DataItem{Value: fromUserName}, 148 "thing5": &subscribe.DataItem{Value: fromUserName},
130 //备注 149 //备注
@@ -139,10 +158,10 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -139,10 +158,10 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
139 msg.ToUser = openId 158 msg.ToUser = openId
140 msg.Page = fmt.Sprintf("/pages/detail/more-comment?id=%v", article.Id) //跳转页面 帖子评论聚合页 159 msg.Page = fmt.Sprintf("/pages/detail/more-comment?id=%v", article.Id) //跳转页面 帖子评论聚合页
141 //备注 160 //备注
142 - msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", article.CountComment)} 161 + userCount, err := l.svcCtx.ArticleCommentRepository.CommentUserCount(l.ctx, conn, comment.CompanyId, comment.ArticleId)
  162 + msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", userCount)}
143 //发送微信订阅消息 163 //发送微信订阅消息
144 - err = subCtx.Send(msg)  
145 - err = l.saveMessage(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err) 164 + err = l.sendAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg)
146 if err != nil { 165 if err != nil {
147 return xerr.NewErrMsgErr("评论失败", err) 166 return xerr.NewErrMsgErr("评论失败", err)
148 } 167 }
@@ -154,12 +173,11 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -154,12 +173,11 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
154 msg.ToUser = toOpenId 173 msg.ToUser = toOpenId
155 msg.Page = fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", article.Id, comment.Pid) //跳转页面评论聚合页 174 msg.Page = fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", article.Id, comment.Pid) //跳转页面评论聚合页
156 //备注 175 //备注
157 - parent, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, comment.Pid)  
158 - if err == nil && parent.Id > 0 {  
159 - msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", parent.CountReply)} 176 + replyCount, err := l.svcCtx.ArticleCommentRepository.ReplyUserCount(l.ctx, conn, comment.CompanyId, comment.Pid)
  177 + if err == nil {
  178 + msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)}
160 //发送微信订阅消息 179 //发送微信订阅消息
161 - err = subCtx.Send(msg)  
162 - err = l.saveMessage(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err) 180 + err = l.sendAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg)
163 if err != nil { 181 if err != nil {
164 return xerr.NewErrMsgErr("评论失败", err) 182 return xerr.NewErrMsgErr("评论失败", err)
165 } 183 }
@@ -179,8 +197,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -179,8 +197,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
179 //备注 197 //备注
180 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)} 198 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)}
181 //发送微信订阅消息 199 //发送微信订阅消息
182 - err = subCtx.Send(msg)  
183 - err = l.saveMessage(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err) 200 + err = l.sendAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg)
184 if err != nil { 201 if err != nil {
185 return xerr.NewErrMsgErr("评论失败", err) 202 return xerr.NewErrMsgErr("评论失败", err)
186 } 203 }
@@ -191,11 +208,14 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -191,11 +208,14 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
191 208
192 // LikeArticle 帖子点赞订阅消息 209 // LikeArticle 帖子点赞订阅消息
193 func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.Article, userInfo *domain.User) error { 210 func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.Article, userInfo *domain.User) error {
194 - subCtx := l.getSubScribe()  
195 openId, err := l.getOpenId(conn, article.AuthorId) 211 openId, err := l.getOpenId(conn, article.AuthorId)
196 if err != nil || openId == "" { 212 if err != nil || openId == "" {
197 return nil 213 return nil
198 } 214 }
  215 + newArticle, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, article.Id)
  216 + if err != nil {
  217 + return xerr.NewErrMsgErr("未获取到帖子信息", err)
  218 + }
199 msg := &subscribe.Message{ 219 msg := &subscribe.Message{
200 ToUser: openId, 220 ToUser: openId,
201 TemplateID: domain.SubscribeTemplateLike, 221 TemplateID: domain.SubscribeTemplateLike,
@@ -204,18 +224,17 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain. @@ -204,18 +224,17 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
204 //点赞用户 224 //点赞用户
205 "name1": &subscribe.DataItem{Value: userInfo.Name}, 225 "name1": &subscribe.DataItem{Value: userInfo.Name},
206 //点赞时间 226 //点赞时间
207 - "data2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")}, 227 + "date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
208 //动态内容 228 //动态内容
209 "thing8": &subscribe.DataItem{Value: article.GetSubscribeMessageTitle()}, 229 "thing8": &subscribe.DataItem{Value: article.GetSubscribeMessageTitle()},
210 //被赞次数 230 //被赞次数
211 - "number4": &subscribe.DataItem{Value: article.CountLove}, 231 + "number4": &subscribe.DataItem{Value: newArticle.CountLove},
212 //温馨提示 232 //温馨提示
213 "thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"}, 233 "thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
214 }, 234 },
215 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 235 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
216 } 236 }
217 - err = subCtx.Send(msg)  
218 - err = l.saveMessage(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err) 237 + err = l.sendAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg)
219 if err != nil { 238 if err != nil {
220 return xerr.NewErrMsgErr("点赞失败", err) 239 return xerr.NewErrMsgErr("点赞失败", err)
221 } 240 }
@@ -224,11 +243,15 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain. @@ -224,11 +243,15 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
224 243
225 // LikeComment 点赞评论订阅消息 244 // LikeComment 点赞评论订阅消息
226 func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.ArticleComment, userInfo *domain.User) error { 245 func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.ArticleComment, userInfo *domain.User) error {
227 - subCtx := l.getSubScribe()  
228 openId, err := l.getOpenId(conn, comment.FromUserId) 246 openId, err := l.getOpenId(conn, comment.FromUserId)
229 if err != nil || openId == "" { 247 if err != nil || openId == "" {
230 return nil 248 return nil
231 } 249 }
  250 + //获取被赞次数
  251 + newComment, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, comment.Id)
  252 + if err != nil {
  253 + return xerr.NewErrMsgErr("未获取到评论信息", err)
  254 + }
232 msg := &subscribe.Message{ 255 msg := &subscribe.Message{
233 ToUser: openId, 256 ToUser: openId,
234 TemplateID: domain.SubscribeTemplateLike, 257 TemplateID: domain.SubscribeTemplateLike,
@@ -237,18 +260,17 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain. @@ -237,18 +260,17 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
237 //点赞用户 260 //点赞用户
238 "name1": &subscribe.DataItem{Value: userInfo.Name}, 261 "name1": &subscribe.DataItem{Value: userInfo.Name},
239 //点赞时间 262 //点赞时间
240 - "data2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")}, 263 + "date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
241 //动态内容 264 //动态内容
242 "thing8": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()}, 265 "thing8": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
243 //被赞次数 266 //被赞次数
244 - "number4": &subscribe.DataItem{Value: comment.CountUserLove}, 267 + "number4": &subscribe.DataItem{Value: newComment.CountUserLove},
245 //温馨提示 268 //温馨提示
246 "thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"}, 269 "thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
247 }, 270 },
248 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 271 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
249 } 272 }
250 - err = subCtx.Send(msg)  
251 - err = l.saveMessage(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err) 273 + err = l.sendAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg)
252 if err != nil { 274 if err != nil {
253 return xerr.NewErrMsgErr("点赞失败", err) 275 return xerr.NewErrMsgErr("点赞失败", err)
254 } 276 }
@@ -257,7 +279,6 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain. @@ -257,7 +279,6 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
257 279
258 // FollowArticle 发帖关注更新提醒 280 // FollowArticle 发帖关注更新提醒
259 func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error { 281 func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error {
260 - subCtx := l.getSubScribe()  
261 //获取关注帖子作者的人员 282 //获取关注帖子作者的人员
262 _, userInfo, err := l.svcCtx.UserFollowRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("toUserIds", []int64{article.AuthorId})) 283 _, userInfo, err := l.svcCtx.UserFollowRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("toUserIds", []int64{article.AuthorId}))
263 if err == nil && len(userInfo) > 0 { 284 if err == nil && len(userInfo) > 0 {
@@ -278,14 +299,13 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai @@ -278,14 +299,13 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
278 //内容摘要 299 //内容摘要
279 "thing5": &subscribe.DataItem{Value: ""}, 300 "thing5": &subscribe.DataItem{Value: ""},
280 //发布时间 301 //发布时间
281 - "thing6": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")}, 302 + "time6": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
282 //温馨提示 303 //温馨提示
283 "thing3": &subscribe.DataItem{Value: "你关注的人发布了新的帖子"}, 304 "thing3": &subscribe.DataItem{Value: "你关注的人发布了新的帖子"},
284 }, 305 },
285 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 306 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
286 } 307 }
287 - err = subCtx.Send(msg)  
288 - err = l.saveMessage(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err) 308 + err = l.sendAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg)
289 if err != nil { 309 if err != nil {
290 return xerr.NewErrMsgErr("保存订阅消息失败", err) 310 return xerr.NewErrMsgErr("保存订阅消息失败", err)
291 } 311 }
@@ -437,6 +437,28 @@ func (repository *ArticleCommentRepository) CustomSearchBy(ctx context.Context, @@ -437,6 +437,28 @@ func (repository *ArticleCommentRepository) CustomSearchBy(ctx context.Context,
437 437
438 } 438 }
439 439
  440 +// CommentUserCount 统计评论人数
  441 +func (repository *ArticleCommentRepository) CommentUserCount(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) (int64, error) {
  442 + var (
  443 + err error
  444 + tx = conn.DB()
  445 + c int64
  446 + )
  447 + err = tx.Model(&models.ArticleComment{}).Where("company_id = ? and article_id = ?", companyId, articleId).Group("from_user_id").Count(&c).Error
  448 + return c, err
  449 +}
  450 +
  451 +// ReplyUserCount 统计回复人数
  452 +func (repository *ArticleCommentRepository) ReplyUserCount(ctx context.Context, conn transaction.Conn, companyId int64, commentId int64) (int64, error) {
  453 + var (
  454 + err error
  455 + tx = conn.DB()
  456 + c int64
  457 + )
  458 + err = tx.Model(&models.ArticleComment{}).Where("company_id = ? and pid = ?", companyId, commentId).Group("from_user_id").Count(&c).Error
  459 + return c, err
  460 +}
  461 +
440 func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository { 462 func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
441 return &ArticleCommentRepository{CachedRepository: cache} 463 return &ArticleCommentRepository{CachedRepository: cache}
442 } 464 }
@@ -80,6 +80,10 @@ type ArticleCommentRepository interface { @@ -80,6 +80,10 @@ type ArticleCommentRepository interface {
80 // 特定的查询 80 // 特定的查询
81 CustomSearchBy(ctx context.Context, conn transaction.Conn, companyId int64, page int, size int, 81 CustomSearchBy(ctx context.Context, conn transaction.Conn, companyId int64, page int, size int,
82 topId int64, articleTitle string, contentLike string, fromUserId int64, show int, createdAtRange [2]int64) (int, []*ArticleCommentShow, error) 82 topId int64, articleTitle string, contentLike string, fromUserId int64, show int, createdAtRange [2]int64) (int, []*ArticleCommentShow, error)
  83 + // CommentUserCount 统计帖子评论人数
  84 + CommentUserCount(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) (int64, error)
  85 + // ReplyUserCount 统计评论回复人数
  86 + ReplyUserCount(ctx context.Context, conn transaction.Conn, companyId int64, commentId int64) (int64, error)
83 } 87 }
84 88
85 // 运营点数 填写的最大值 89 // 运营点数 填写的最大值