|
|
package article
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
)
|
|
|
|
|
|
type MiniSetUserLikeLogic struct {
|
|
|
logx.Logger
|
|
|
ctx context.Context
|
|
|
svcCtx *svc.ServiceContext
|
|
|
}
|
|
|
|
|
|
func NewMiniSetUserLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniSetUserLikeLogic {
|
|
|
return &MiniSetUserLikeLogic{
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
ctx: ctx,
|
|
|
svcCtx: svcCtx,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置点赞操作
|
|
|
func (l *MiniSetUserLikeLogic) MiniSetUserLike(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) {
|
|
|
//取消点赞文章
|
|
|
if req.Flag == 2 && req.CommentId == 0 {
|
|
|
return l.cancelSetUserLikeArticle(req)
|
|
|
}
|
|
|
// 取消点赞评论
|
|
|
if req.Flag == 2 && req.CommentId > 0 {
|
|
|
return l.cancelSetUserLikeComment(req)
|
|
|
}
|
|
|
// 设置点赞文章
|
|
|
if req.Flag == 1 && req.CommentId == 0 {
|
|
|
return l.setUserLikeArticle(req)
|
|
|
}
|
|
|
// 设置点赞评论
|
|
|
if req.Flag == 1 && req.CommentId > 0 {
|
|
|
return l.setUserLikeComment(req)
|
|
|
}
|
|
|
|
|
|
return nil, xerr.NewErrMsg("操作失败")
|
|
|
}
|
|
|
|
|
|
// 取消文章点赞
|
|
|
func (l *MiniSetUserLikeLogic) cancelSetUserLikeArticle(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
|
|
|
// 检查id
|
|
|
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err)
|
|
|
}
|
|
|
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
if userInfo.CompanyId != articleInfo.CompanyId {
|
|
|
// 公司无法对应
|
|
|
return nil, xerr.NewErrMsg("没有操作权限")
|
|
|
}
|
|
|
queryOption := domain.NewQueryOptions().
|
|
|
WithFindOnly().
|
|
|
MustWithKV("articleId", req.ArticleId).
|
|
|
MustWithKV("commentId", req.CommentId).
|
|
|
MustWithKV("userId", req.UserId)
|
|
|
_, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
if len(flagList) == 0 {
|
|
|
//没有记录
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: articleInfo.CountLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
flagInfo := flagList[0]
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
// 去除点赞标识
|
|
|
_, err = l.svcCtx.UserLoveFlagRepository.Delete(ctx, c, flagInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 减少文章的点赞数量
|
|
|
err = l.svcCtx.ArticleRepository.IncreaseCountLove(ctx, c, -1, articleInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
articleInfo, err = l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err == nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取点赞数量失败", err)
|
|
|
}
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: articleInfo.CountLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
|
|
|
// 取消评论点赞
|
|
|
func (l *MiniSetUserLikeLogic) cancelSetUserLikeComment(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
// 检查id
|
|
|
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err)
|
|
|
}
|
|
|
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
if userInfo.CompanyId != articleInfo.CompanyId {
|
|
|
// 文章和人员的公司无法对应
|
|
|
return nil, xerr.NewErrMsg("没有操作权限")
|
|
|
}
|
|
|
commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("无法确认评论信息", err)
|
|
|
}
|
|
|
if commentInfo.ArticleId != articleInfo.Id {
|
|
|
return nil, xerr.NewErrMsg("评论和文章不能对应")
|
|
|
}
|
|
|
|
|
|
queryOption := domain.NewQueryOptions().
|
|
|
WithFindOnly().
|
|
|
MustWithKV("articleId", req.ArticleId).
|
|
|
MustWithKV("commentId", req.CommentId).
|
|
|
MustWithKV("userId", req.UserId)
|
|
|
_, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
if len(flagList) == 0 {
|
|
|
//没有记录
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: commentInfo.CountUserLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
flagInfo := flagList[0]
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
// 去除点赞标识
|
|
|
_, err = l.svcCtx.UserLoveFlagRepository.Delete(ctx, c, flagInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 减少评论的点赞数量
|
|
|
err = l.svcCtx.ArticleCommentRepository.IncreaseCountUserLove(ctx, c, -1, commentInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("取消点赞标识失败", err)
|
|
|
}
|
|
|
commentInfo, err = l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取评论的点赞数量失败", err)
|
|
|
}
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: commentInfo.CountUserLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
|
|
|
// 设置文章点赞
|
|
|
func (l *MiniSetUserLikeLogic) setUserLikeArticle(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
|
|
|
// 检查id
|
|
|
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err)
|
|
|
}
|
|
|
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
if userInfo.CompanyId != articleInfo.CompanyId {
|
|
|
// 公司无法对应
|
|
|
return nil, xerr.NewErrMsg("没有操作权限")
|
|
|
}
|
|
|
queryOption := domain.NewQueryOptions().
|
|
|
WithFindOnly().
|
|
|
MustWithKV("articleId", req.ArticleId).
|
|
|
MustWithKV("commentId", req.CommentId).
|
|
|
MustWithKV("userId", req.UserId)
|
|
|
_, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
if len(flagList) > 0 {
|
|
|
//已经有记录
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: articleInfo.CountLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
flagInfo := domain.UserLoveFlag{
|
|
|
Id: 0,
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
UserId: req.UserId,
|
|
|
}
|
|
|
// 去除点赞标识
|
|
|
_, err = l.svcCtx.UserLoveFlagRepository.Insert(ctx, c, &flagInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 增加文章的点赞数量
|
|
|
err = l.svcCtx.ArticleRepository.IncreaseCountLove(ctx, c, 1, articleInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
articleInfo, err = l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err == nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取点赞数量失败", err)
|
|
|
}
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: articleInfo.CountLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
|
|
|
// 设置评论点赞
|
|
|
func (l *MiniSetUserLikeLogic) setUserLikeComment(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) {
|
|
|
var conn = l.svcCtx.DefaultDBConn()
|
|
|
// 检查id
|
|
|
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err)
|
|
|
}
|
|
|
articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId)
|
|
|
if err != nil {
|
|
|
//无法确认 文章的id
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
if userInfo.CompanyId != articleInfo.CompanyId {
|
|
|
// 文章和人员的公司无法对应
|
|
|
return nil, xerr.NewErrMsg("没有操作权限")
|
|
|
}
|
|
|
commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("无法确认评论信息", err)
|
|
|
}
|
|
|
if commentInfo.ArticleId != articleInfo.Id {
|
|
|
return nil, xerr.NewErrMsg("评论和文章不能对应")
|
|
|
}
|
|
|
|
|
|
queryOption := domain.NewQueryOptions().
|
|
|
WithFindOnly().
|
|
|
MustWithKV("articleId", req.ArticleId).
|
|
|
MustWithKV("commentId", req.CommentId).
|
|
|
MustWithKV("userId", req.UserId)
|
|
|
_, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
if len(flagList) > 0 {
|
|
|
//已经有记录
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: commentInfo.CountUserLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
}
|
|
|
|
|
|
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
flagInfo := domain.UserLoveFlag{
|
|
|
Id: 0,
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
UserId: req.UserId,
|
|
|
}
|
|
|
// 设置赞标识
|
|
|
_, err = l.svcCtx.UserLoveFlagRepository.Insert(ctx, c, &flagInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 增加评论的点赞数量
|
|
|
err = l.svcCtx.ArticleCommentRepository.IncreaseCountUserLove(ctx, c, 1, commentInfo)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("设置点赞标识失败", err)
|
|
|
}
|
|
|
commentInfo, err = l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("获取评论的点赞数量失败", err)
|
|
|
}
|
|
|
resp = &types.MiniSetUserLikeResponse{
|
|
|
ArticleId: req.ArticleId,
|
|
|
CommentId: req.CommentId,
|
|
|
Count: commentInfo.CountUserLove,
|
|
|
}
|
|
|
return resp, nil
|
|
|
} |
...
|
...
|
|