作者 zhuangmx

扣减订阅次数

... ... @@ -54,8 +54,8 @@ func (l *MiniSubscribeLogic) getOpenId(conn transaction.Conn, userId int64) (str
return userWechat.OpenId, nil
}
// saveMessage 保存订阅消息
func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) error {
// saveAndDecrease 保存并扣减订阅消息
func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) error {
templateData := make(map[string]interface{})
_ = copier.Copy(&templateData, msg.Data)
subscribeMessage := &domain.MessageSubscribe{
... ... @@ -73,7 +73,21 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI
subscribeMessage.Result = "ok"
}
_, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
return err
if err != nil {
return err
}
//扣减次数
if sendErr == nil {
userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)
if err == nil && userSubscribe.Count > 0 {
userSubscribe.Count = userSubscribe.Count - 1
_, err = l.svcCtx.UserSubscribeRepository.Update(l.ctx, conn, userSubscribe)
if err != nil {
return err
}
}
}
return nil
}
func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe {
... ... @@ -142,7 +156,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", article.CountComment)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveMessage(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err)
err = l.saveAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -159,7 +173,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", parent.CountReply)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveMessage(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -180,7 +194,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("%v在评论中提到了你", comment.FromUser.Name)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveMessage(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err)
err = l.saveAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -215,7 +229,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err)
err = l.saveAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
... ... @@ -248,7 +262,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
... ... @@ -285,7 +299,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err)
err = l.saveAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err)
if err != nil {
return xerr.NewErrMsgErr("保存订阅消息失败", err)
}
... ...