作者 yangfu
... ... @@ -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 {
// 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,14 +66,34 @@ func (l *MiniSubscribeLogic) saveMessage(conn transaction.Conn, companyId, userI
TemplateId: msg.TemplateID,
TemplateData: templateData,
}
//获取订阅次数
userSubscribe, err := l.svcCtx.UserSubscribeRepository.FindOneByType(l.ctx, conn, companyId, userId, mType)
if err != nil || userSubscribe.Count <= 0 {
subscribeMessage.Result = "fail"
subscribeMessage.Error = "订阅次数已用完"
} else {
//扣减订阅次数
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
}
return nil
}
func (l *MiniSubscribeLogic) messageSubscribe(companyId, userId int64, mType int) *domain.MessageSubscribe {
... ... @@ -109,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 {
... ... @@ -124,7 +143,7 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
//评论内容
"thing2": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
//评论时间
"thing3": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
"time3": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//评论用户
"thing5": &subscribe.DataItem{Value: fromUserName},
//备注
... ... @@ -139,10 +158,10 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
msg.ToUser = openId
msg.Page = fmt.Sprintf("/pages/detail/more-comment?id=%v", article.Id) //跳转页面 帖子评论聚合页
//备注
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的帖子最近已有%v人评论,点击查看详情", article.CountComment)}
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.saveMessage(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)
}
... ... @@ -154,12 +173,11 @@ func (l *MiniSubscribeLogic) ReplyComment(conn transaction.Conn, article *domain
msg.ToUser = toOpenId
msg.Page = fmt.Sprintf("/pages/detail/reply-comment?id=%v&commentId=%v", article.Id, comment.Pid) //跳转页面评论聚合页
//备注
parent, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, comment.Pid)
if err == nil && parent.Id > 0 {
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", parent.CountReply)}
replyCount, err := l.svcCtx.ArticleCommentRepository.ReplyUserCount(l.ctx, conn, comment.CompanyId, comment.Pid)
if err == nil {
msg.Data["thing9"] = &subscribe.DataItem{Value: fmt.Sprintf("您的评论最近已有%v人回复,点击查看详情", replyCount)}
//发送微信订阅消息
err = subCtx.Send(msg)
err = l.saveMessage(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)
}
... ... @@ -179,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.saveMessage(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)
}
... ... @@ -191,11 +208,14 @@ 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
}
newArticle, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, article.Id)
if err != nil {
return xerr.NewErrMsgErr("未获取到帖子信息", err)
}
msg := &subscribe.Message{
ToUser: openId,
TemplateID: domain.SubscribeTemplateLike,
... ... @@ -204,18 +224,17 @@ func (l *MiniSubscribeLogic) LikeArticle(conn transaction.Conn, article *domain.
//点赞用户
"name1": &subscribe.DataItem{Value: userInfo.Name},
//点赞时间
"data2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
"date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//动态内容
"thing8": &subscribe.DataItem{Value: article.GetSubscribeMessageTitle()},
//被赞次数
"number4": &subscribe.DataItem{Value: article.CountLove},
"number4": &subscribe.DataItem{Value: newArticle.CountLove},
//温馨提示
"thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(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)
}
... ... @@ -224,11 +243,15 @@ 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
}
//获取被赞次数
newComment, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, comment.Id)
if err != nil {
return xerr.NewErrMsgErr("未获取到评论信息", err)
}
msg := &subscribe.Message{
ToUser: openId,
TemplateID: domain.SubscribeTemplateLike,
... ... @@ -237,18 +260,17 @@ func (l *MiniSubscribeLogic) LikeComment(conn transaction.Conn, comment *domain.
//点赞用户
"name1": &subscribe.DataItem{Value: userInfo.Name},
//点赞时间
"data2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
"date2": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//动态内容
"thing8": &subscribe.DataItem{Value: comment.GetSubscribeMessageContent()},
//被赞次数
"number4": &subscribe.DataItem{Value: comment.CountUserLove},
"number4": &subscribe.DataItem{Value: newComment.CountUserLove},
//温馨提示
"thing5": &subscribe.DataItem{Value: "这条内容很受欢迎哦,快来看看吧"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(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)
}
... ... @@ -257,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 {
... ... @@ -278,14 +299,13 @@ func (l *MiniSubscribeLogic) FollowArticle(conn transaction.Conn, article *domai
//内容摘要
"thing5": &subscribe.DataItem{Value: ""},
//发布时间
"thing6": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
"time6": &subscribe.DataItem{Value: time.Now().Format("2006-01-02 15:04:05")},
//温馨提示
"thing3": &subscribe.DataItem{Value: "你关注的人发布了新的帖子"},
},
MiniprogramState: l.svcCtx.Config.Wechat.QrcodeEnv,
}
err = subCtx.Send(msg)
err = l.saveMessage(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)
}
... ...
... ... @@ -437,6 +437,28 @@ func (repository *ArticleCommentRepository) CustomSearchBy(ctx context.Context,
}
// CommentUserCount 统计评论人数
func (repository *ArticleCommentRepository) CommentUserCount(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) (int64, error) {
var (
err error
tx = conn.DB()
c int64
)
err = tx.Model(&models.ArticleComment{}).Where("company_id = ? and article_id = ?", companyId, articleId).Group("from_user_id").Count(&c).Error
return c, err
}
// ReplyUserCount 统计回复人数
func (repository *ArticleCommentRepository) ReplyUserCount(ctx context.Context, conn transaction.Conn, companyId int64, commentId int64) (int64, error) {
var (
err error
tx = conn.DB()
c int64
)
err = tx.Model(&models.ArticleComment{}).Where("company_id = ? and pid = ?", companyId, commentId).Group("from_user_id").Count(&c).Error
return c, err
}
func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
return &ArticleCommentRepository{CachedRepository: cache}
}
... ...
... ... @@ -80,6 +80,10 @@ type ArticleCommentRepository interface {
// 特定的查询
CustomSearchBy(ctx context.Context, conn transaction.Conn, companyId int64, page int, size int,
topId int64, articleTitle string, contentLike string, fromUserId int64, show int, createdAtRange [2]int64) (int, []*ArticleCommentShow, error)
// CommentUserCount 统计帖子评论人数
CommentUserCount(ctx context.Context, conn transaction.Conn, companyId int64, articleId int64) (int64, error)
// ReplyUserCount 统计评论回复人数
ReplyUserCount(ctx context.Context, conn transaction.Conn, companyId int64, commentId int64) (int64, error)
}
// 运营点数 填写的最大值
... ...