作者 zhuangmx

fixed:发送订阅前增加判断次数

... ... @@ -54,8 +54,8 @@ func (l *MiniSubscribeLogic) getOpenId(conn transaction.Conn, userId int64) (str
return userWechat.OpenId, nil
}
// saveAndDecrease 保存并扣减订阅消息
func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message, sendErr error) error {
// sendAndDecrease 保存并扣减订阅消息
func (l *MiniSubscribeLogic) sendAndDecrease(conn transaction.Conn, companyId, userId int64, mType int, msg *subscribe.Message) error {
templateData := make(map[string]interface{})
_ = copier.Copy(&templateData, msg.Data)
subscribeMessage := &domain.MessageSubscribe{
... ... @@ -66,27 +66,33 @@ func (l *MiniSubscribeLogic) saveAndDecrease(conn transaction.Conn, companyId, u
TemplateId: msg.TemplateID,
TemplateData: templateData,
}
if sendErr != nil {
//获取订阅次数
userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)
if err != nil || userSubscribe.Count <= 0 {
subscribeMessage.Result = "fail"
subscribeMessage.Error = sendErr.Error()
subscribeMessage.Error = "订阅次数已用完"
} else {
subscribeMessage.Result = "ok"
//扣减订阅次数
userSubscribe.Count = userSubscribe.Count - 1
_, err = l.svcCtx.UserSubscribeRepository.Update(l.ctx, conn, userSubscribe)
if err != nil {
return err
}
//发送订阅消息
subCtx := l.getSubScribe()
sendErr := subCtx.Send(msg)
if sendErr != nil {
subscribeMessage.Result = "fail"
subscribeMessage.Error = sendErr.Error()
} else {
subscribeMessage.Result = "ok"
}
}
_, err := l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
//保存订阅结果
_, err = l.svcCtx.MessageSubscribeRepository.Insert(l.ctx, conn, subscribeMessage)
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
}
... ... @@ -123,7 +129,6 @@ func (l *MiniSubscribeLogic) getReplyCommentUserInfo(conn transaction.Conn, comp
// @param article 文章
// @param comment 评论
func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain.Article, comment *domain.ArticleComment) error {
subCtx := l.getSubScribe()
//评论用户+职位 例: 张三-董事办
fromUserName, err := l.getReplyCommentUserInfo(conn, comment.CompanyId, comment.FromUserId)
if err != nil {
... ... @@ -156,8 +161,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
userCount, err := l.svcCtx.ArticleCommentRepository.CommentUserCount(l.ctx, conn, comment.CompanyId, comment.ArticleId)
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", userCount)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg, err)
err = l.sendAndDecrease(conn, comment.CompanyId, article.AuthorId, domain.SubscribeTypeReplyComment, msg)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -173,8 +177,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
if err == nil {
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg, err)
err = l.sendAndDecrease(conn, comment.CompanyId, comment.ToUserId, domain.SubscribeTypeReplyComment, msg)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -194,8 +197,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.saveAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg, err)
err = l.sendAndDecrease(conn, comment.CompanyId, at.Id, domain.SubscribeTypeReplyComment, msg)
if err != nil {
return xerr.NewErrMsgErr("评论失败", err)
}
... ... @@ -206,7 +208,6 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
// LikeArticle 帖子点赞订阅消息
func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.Article, userInfo *domain.User) error {
subCtx := l.getSubScribe()
openId, err := l.getOpenId(conn, article.AuthorId)
if err != nil || openId == "" {
return nil
... ... @@ -233,8 +234,7 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg, err)
err = l.sendAndDecrease(conn, article.CompanyId, article.AuthorId, domain.SubscribeTypeLike, msg)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
... ... @@ -243,7 +243,6 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
// LikeComment 点赞评论订阅消息
func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.ArticleComment, userInfo *domain.User) error {
subCtx := l.getSubScribe()
openId, err := l.getOpenId(conn, comment.FromUserId)
if err != nil || openId == "" {
return nil
... ... @@ -271,8 +270,7 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg, err)
err = l.sendAndDecrease(conn, comment.CompanyId, comment.FromUserId, domain.SubscribeTypeLike, msg)
if err != nil {
return xerr.NewErrMsgErr("点赞失败", err)
}
... ... @@ -281,7 +279,6 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
// FollowArticle 发帖关注更新提醒
func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domain.Article) error {
subCtx := l.getSubScribe()
//获取关注帖子作者的人员
_, userInfo, err := l.svcCtx.UserFollowRepository.Find(l.ctx, conn, domain.NewQueryOptions().WithKV("toUserIds", []int64{article.AuthorId}))
if err == nil && len(userInfo) > 0 {
... ... @@ -308,8 +305,7 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg, err)
err = l.sendAndDecrease(conn, article.CompanyId, item.FromUserId, domain.SubscribeTypeFollow, msg)
if err != nil {
return xerr.NewErrMsgErr("保存订阅消息失败", err)
}
... ...