|
|
package article
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"github.com/samber/lo"
|
|
|
"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/contextdata"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
|
|
|
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
)
|
|
|
|
|
|
type MiniArticleSetViewLogic struct {
|
|
|
logx.Logger
|
|
|
ctx context.Context
|
|
|
svcCtx *svc.ServiceContext
|
|
|
conn transaction.Conn
|
|
|
}
|
|
|
|
|
|
func NewMiniArticleSetViewLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleSetViewLogic {
|
|
|
return &MiniArticleSetViewLogic{
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
ctx: ctx,
|
|
|
svcCtx: svcCtx,
|
|
|
conn: svcCtx.DefaultDBConn(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (l *MiniArticleSetViewLogic) MiniArticleSetView(req *types.MiniArticleSetViewRequest) (resp *types.MiniArticleSetViewResponse, err error) {
|
|
|
// 文章数据
|
|
|
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, l.conn, req.Id)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("帖子不存在", err)
|
|
|
}
|
|
|
userToken := contextdata.GetUserTokenFromCtx(l.ctx)
|
|
|
if userToken.UserId != article.AuthorId || userToken.CompanyId != article.CompanyId {
|
|
|
return nil, xerr.NewErrMsg("您没有权限修改")
|
|
|
}
|
|
|
// 检查发布人
|
|
|
author, err := l.svcCtx.UserRepository.FindOne(l.ctx, l.conn, article.AuthorId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
|
|
|
}
|
|
|
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, l.conn, author.CompanyId)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
|
|
|
}
|
|
|
articleAuthor := domain.UserSimple{
|
|
|
Id: author.Id,
|
|
|
Name: author.Name,
|
|
|
Avatar: author.Avatar,
|
|
|
Position: author.Position,
|
|
|
Company: companyInfo.Name,
|
|
|
CompanyId: author.CompanyId,
|
|
|
}
|
|
|
//检查文章可被哪些人查看
|
|
|
whoRead := []int64{}
|
|
|
if len(req.WhoRead) > 0 {
|
|
|
whoRead = lo.Uniq(req.WhoRead)
|
|
|
var u *domain.User
|
|
|
for _, val := range whoRead {
|
|
|
u, err = l.svcCtx.UserRepository.FindOne(l.ctx, l.conn, val)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("文章可查看人设置错误", err)
|
|
|
}
|
|
|
if u.CompanyId != userToken.CompanyId {
|
|
|
return nil, xerr.NewErrMsg("文章可查看人设置错误")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//文章内容详情
|
|
|
queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("articleId", req.Id)
|
|
|
_, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, l.conn, queryOption)
|
|
|
if err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("帖子不存在", err)
|
|
|
}
|
|
|
var oldBackup domain.ArticleBackup
|
|
|
oldBackup.MakeBackup(articleAuthor, article, sectionList, "设置可见范围")
|
|
|
//更新数据
|
|
|
article.TargetUser = domain.ArticleTargetLimit
|
|
|
article.WhoRead = whoRead
|
|
|
article.WhoReview = whoRead
|
|
|
|
|
|
err = transaction.UseTrans(l.ctx, l.conn.DB(), func(ctx context.Context, c transaction.Conn) error {
|
|
|
//更新文章内容
|
|
|
_, err = l.svcCtx.ArticleRepository.Update(l.ctx, c, article)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("保存帖子失败", err)
|
|
|
}
|
|
|
//备份文章
|
|
|
var backup domain.ArticleBackup
|
|
|
backup.MakeBackup(articleAuthor, article, sectionList, "设置可见范围")
|
|
|
if ok := backup.CheckChangeField(&oldBackup); ok {
|
|
|
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, c, &backup)
|
|
|
if err != nil {
|
|
|
return xerr.NewErrMsgErr("保存文章日志失败", err)
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}, true)
|
|
|
resp = &types.MiniArticleSetViewResponse{
|
|
|
Id: article.Id,
|
|
|
Title: article.Title,
|
|
|
WhoRead: article.WhoRead,
|
|
|
}
|
|
|
return
|
|
|
} |
...
|
...
|
|