作者 zhuangmx

扣减订阅次数

@@ -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 +// saveAndDecrease 保存并扣减订阅消息
  58 +func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) 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{
@@ -73,7 +73,21 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI @@ -73,7 +73,21 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI
73 subscribeMessage.Result = "ok" 73 subscribeMessage.Result = "ok"
74 } 74 }
75 _, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage) 75 _, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
76 - return err 76 + if err != nil {
  77 + return err
  78 + }
  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
77 } 91 }
78 92
79 func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe { 93 func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe {
@@ -142,7 +156,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -142,7 +156,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
142 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", article.CountComment)} 156 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", article.CountComment)}
143 //发送微信订阅消息 157 //发送微信订阅消息
144 err = subCtx.Send(msg) 158 err = subCtx.Send(msg)
145 - err = l.saveMessage(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err) 159 + err = l.saveAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err)
146 if err != nil { 160 if err != nil {
147 return xerr.NewErrMsgErr("评论失败", err) 161 return xerr.NewErrMsgErr("评论失败", err)
148 } 162 }
@@ -159,7 +173,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -159,7 +173,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
159 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", parent.CountReply)} 173 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", parent.CountReply)}
160 //发送微信订阅消息 174 //发送微信订阅消息
161 err = subCtx.Send(msg) 175 err = subCtx.Send(msg)
162 - err = l.saveMessage(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err) 176 + err = l.saveAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err)
163 if err != nil { 177 if err != nil {
164 return xerr.NewErrMsgErr("评论失败", err) 178 return xerr.NewErrMsgErr("评论失败", err)
165 } 179 }
@@ -180,7 +194,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain @@ -180,7 +194,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
180 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)} 194 msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)}
181 //发送微信订阅消息 195 //发送微信订阅消息
182 err = subCtx.Send(msg) 196 err = subCtx.Send(msg)
183 - err = l.saveMessage(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err) 197 + err = l.saveAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err)
184 if err != nil { 198 if err != nil {
185 return xerr.NewErrMsgErr("评论失败", err) 199 return xerr.NewErrMsgErr("评论失败", err)
186 } 200 }
@@ -215,7 +229,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain. @@ -215,7 +229,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
215 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 229 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
216 } 230 }
217 err = subCtx.Send(msg) 231 err = subCtx.Send(msg)
218 - err = l.saveMessage(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err) 232 + err = l.saveAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err)
219 if err != nil { 233 if err != nil {
220 return xerr.NewErrMsgErr("点赞失败", err) 234 return xerr.NewErrMsgErr("点赞失败", err)
221 } 235 }
@@ -248,7 +262,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain. @@ -248,7 +262,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
248 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 262 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
249 } 263 }
250 err = subCtx.Send(msg) 264 err = subCtx.Send(msg)
251 - err = l.saveMessage(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err) 265 + err = l.saveAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err)
252 if err != nil { 266 if err != nil {
253 return xerr.NewErrMsgErr("点赞失败", err) 267 return xerr.NewErrMsgErr("点赞失败", err)
254 } 268 }
@@ -285,7 +299,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai @@ -285,7 +299,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
285 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv, 299 MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
286 } 300 }
287 err = subCtx.Send(msg) 301 err = subCtx.Send(msg)
288 - err = l.saveMessage(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err) 302 + err = l.saveAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err)
289 if err != nil { 303 if err != nil {
290 return xerr.NewErrMsgErr("保存订阅消息失败", err) 304 return xerr.NewErrMsgErr("保存订阅消息失败", err)
291 } 305 }