mini_create_article_logic.go 2.5 KB
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/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"github.com/samber/lo"
	"github.com/zeromicro/go-zero/core/logx"
)

type MiniCreateArticleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMiniCreateArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniCreateArticleLogic {
	return &MiniCreateArticleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateRequest) (resp *types.MiniArticleCreateResponse, err error) {
	var conn = l.svcCtx.DefaultDBConn()
	// 检查发布人
	author, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, int64(req.AuthorId))
	if err != nil {
		return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
	}
	//TODO 获取图片的尺寸大小
	images := []domain.Image{}
	for _, val := range req.Images {
		images = append(images, domain.Image{
			Url:    val,
			Width:  0,
			Height: 0,
		})
	}
	//检查文章可被哪些人查看
	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, conn, val)
			if err != nil {
				return nil, xerr.NewErrMsgErr("文章可查看人设置错误", err)
			}
			if u.CompanyId != author.CompanyId {
				return nil, xerr.NewErrMsg("文章可查看人设置错误")
			}
		}
	}
	//检查文章可被哪些人评论
	whoReview := []int64{}
	if len(req.WhoReview) > 0 && len(whoRead) > 0 {
		whoReview = lo.Uniq(req.WhoReview)
		// 检查 whoRead 是否 完全包含 whoReview
		ok := lo.Every(whoRead, whoReview)
		if !ok {
			return nil, xerr.NewErrMsg("文章可评论人设置错误")
		}
	}
	if len(whoRead) > 0 && len(whoReview) == 0 {
		//有指定可查看人 ,但未指定可评论人
		return nil, xerr.NewErrMsg("文章可评论人设置错误")
	}
	// if len(whoRead) == 0 && len(whoReview) > 0 {
	// 未指定可查看人(全员可看),有指定可评论人 ,忽略判断
	// }
	// if len(whoRead) == 0 && len(whoReview) == 0 {
	// 未指定可查看人(全员可看),未指定可评论人 ,忽略判断
	// }

	return
}