作者 zhuangmx

Merge branch 'dev' into test

@@ -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 -// saveAndDecrease 保存并扣减订阅消息  
58 -func (l *MiniSubscribeLogic) saveAndDecrease(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,27 +66,33 @@ func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, u @@ -66,27 +66,33 @@ func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, u
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 + }
74 } 90 }
75 - _, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage) 91 + //保存订阅结果
  92 + _, err = l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
76 if err != nil { 93 if err != nil {
77 return err 94 return err
78 } 95 }
79 - //扣减次数  
80 - if sendErr == nil {  
81 - userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)  
82 - if err == nil && userSubscribe.Count > 0 {  
83 - userSubscribe.Count = userSubscribe.Count - 1  
84 - _, err = l.svcCtx.UserSubscribeRepository.Update(l.ctx, conn, userSubscribe)  
85 - if err != nil {  
86 - return err  
87 - }  
88 - }  
89 - }  
90 return nil 96 return nil
91 } 97 }
92 98
@@ -123,7 +129,6 @@ func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, comp @@ -123,7 +129,6 @@ func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, comp
123 // @param article 文章 129 // @param article 文章
124 // @param comment 评论 130 // @param comment 评论
125 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 {
126 - subCtx := l.getSubScribe()  
127 //评论用户+职位 例: 张三-董事办 132 //评论用户+职位 例: 张三-董事办
128 fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId) 133 fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId)
129 if err != nil { 134 if err != nil {
@@ -156,8 +161,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -156,8 +161,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
156 userCount, err := l.svcCtx.ArticleCommentRepository.CommentUserCount(l.ctx, conn, comment.CompanyId, comment.ArticleId) 161 userCount, err := l.svcCtx.ArticleCommentRepository.CommentUserCount(l.ctx, conn, comment.CompanyId, comment.ArticleId)
157 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", userCount)} 162 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", userCount)}
158 //发送微信订阅消息 163 //发送微信订阅消息
159 - err = subCtx.Send(msg)  
160 - err = l.saveAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err) 164 + err = l.sendAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg)
161 if err != nil { 165 if err != nil {
162 return xerr.NewErrMsgErr("评论失败", err) 166 return xerr.NewErrMsgErr("评论失败", err)
163 } 167 }
@@ -173,8 +177,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -173,8 +177,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
173 if err == nil { 177 if err == nil {
174 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)} 178 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)}
175 //发送微信订阅消息 179 //发送微信订阅消息
176 - err = subCtx.Send(msg)  
177 - err = l.saveAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err) 180 + err = l.sendAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg)
178 if err != nil { 181 if err != nil {
179 return xerr.NewErrMsgErr("评论失败", err) 182 return xerr.NewErrMsgErr("评论失败", err)
180 } 183 }
@@ -194,8 +197,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -194,8 +197,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
194 //备注 197 //备注
195 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)}
196 //发送微信订阅消息 199 //发送微信订阅消息
197 - err = subCtx.Send(msg)  
198 - err = l.saveAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err) 200 + err = l.sendAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg)
199 if err != nil { 201 if err != nil {
200 return xerr.NewErrMsgErr("评论失败", err) 202 return xerr.NewErrMsgErr("评论失败", err)
201 } 203 }
@@ -206,7 +208,6 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -206,7 +208,6 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
206 208
207 // LikeArticle 帖子点赞订阅消息 209 // LikeArticle 帖子点赞订阅消息
208 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 {
209 - subCtx := l.getSubScribe()  
210 openId, err := l.getOpenId(conn, article.AuthorId) 211 openId, err := l.getOpenId(conn, article.AuthorId)
211 if err != nil || openId == "" { 212 if err != nil || openId == "" {
212 return nil 213 return nil
@@ -233,8 +234,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain. @@ -233,8 +234,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
233 }, 234 },
234 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 235 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
235 } 236 }
236 - err = subCtx.Send(msg)  
237 - err = l.saveAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err) 237 + err = l.sendAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg)
238 if err != nil { 238 if err != nil {
239 return xerr.NewErrMsgErr("点赞失败", err) 239 return xerr.NewErrMsgErr("点赞失败", err)
240 } 240 }
@@ -243,7 +243,6 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain. @@ -243,7 +243,6 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
243 243
244 // LikeComment 点赞评论订阅消息 244 // LikeComment 点赞评论订阅消息
245 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 {
246 - subCtx := l.getSubScribe()  
247 openId, err := l.getOpenId(conn, comment.FromUserId) 246 openId, err := l.getOpenId(conn, comment.FromUserId)
248 if err != nil || openId == "" { 247 if err != nil || openId == "" {
249 return nil 248 return nil
@@ -271,8 +270,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain. @@ -271,8 +270,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
271 }, 270 },
272 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 271 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
273 } 272 }
274 - err = subCtx.Send(msg)  
275 - err = l.saveAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err) 273 + err = l.sendAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg)
276 if err != nil { 274 if err != nil {
277 return xerr.NewErrMsgErr("点赞失败", err) 275 return xerr.NewErrMsgErr("点赞失败", err)
278 } 276 }
@@ -281,7 +279,6 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain. @@ -281,7 +279,6 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
281 279
282 // FollowArticle 发帖关注更新提醒 280 // FollowArticle 发帖关注更新提醒
283 func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error { 281 func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error {
284 - subCtx := l.getSubScribe()  
285 //获取关注帖子作者的人员 282 //获取关注帖子作者的人员
286 _, 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}))
287 if err == nil && len(userInfo) > 0 { 284 if err == nil && len(userInfo) > 0 {
@@ -308,8 +305,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai @@ -308,8 +305,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
308 }, 305 },
309 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 306 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
310 } 307 }
311 - err = subCtx.Send(msg)  
312 - err = l.saveAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err) 308 + err = l.sendAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg)
313 if err != nil { 309 if err != nil {
314 return xerr.NewErrMsgErr("保存订阅消息失败", err) 310 return xerr.NewErrMsgErr("保存订阅消息失败", err)
315 } 311 }