article_draft.go 2.3 KB
package domain

import (
	"context"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)

// 填写文章时保存的草稿

type ArticleDraft struct {
	Id        int64             `json:"id"`
	CompanyId int64             `json:"companyId"`
	CreatedAt int64             `json:"createdAt,omitempty"`
	UpdatedAt int64             `json:"updatedAt,omitempty"`
	DeletedAt int64             `json:"deletedAt,omitempty"`
	Version   int               `json:"version,omitempty"`
	Template  int               `json:"template"`  // 填写内容时用的样板0、无 1、演绎式 2、归纳式
	Content   []string          `json:"content"`   // 文章内容
	AuthorId  int64             `json:"authorId"`  // 发布人
	Title     string            `json:"title"`     // 文章标题
	Images    []Image           `json:"images"`    // 图片
	WhoRead   []int64           `json:"whoRead"`   // 谁可以看
	WhoReview []int64           `json:"whoReview"` // 评论人
	Location  Location          `json:"location"`  // 坐标
	MatchUrl  map[string]string `json:"matchUrl"`  // 匹配文章内容中的url文本

	Section []*ArticleSection `json:"section"` // 分段内容
}
type ArticleDraftRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
	Update(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleDraft, error)
	Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleDraft, error)
}

func (m *ArticleDraft) GetCover() string {
	if len(m.Images) > 0 {
		return m.Images[0].Url
	}
	for _, sec := range m.Section {
		if sec.ParagraphType == ParagraphTypeImages && len(sec.Images) > 0 {
			return sec.Images[0]
		}
	}
	return ""
}

func (m *ArticleDraft) GetImages() []string {
	cover := m.GetCover()
	var result = make([]string, 0)
	for _, img := range m.Images {
		result = append(result, img.Url)
	}
	if len(result) == 0 && len(cover) > 0 {
		result = append(result, cover)
	}
	return result
}