article_draft_operation.go 3.2 KB
package domain

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"gorm.io/plugin/soft_delete"
)

// ArticleDraftOperation 运营草稿
type ArticleDraftOperation struct {
	Id         int64                 `json:"id"`                  // 唯一标识
	CompanyId  int64                 `json:"companyId,string"`    // 公司ID
	AuthorId   int64                 `json:"authorId"`            // 发布人
	Title      string                `json:"title"`               // 文章标题
	Content    string                `json:"content"`             // 文章内容
	Images     []Image               `json:"images"`              // 图片
	Videos     []Video               `json:"videos"`              // 视频
	TargetUser ArticleTarget         `json:"targetUser"`          // 分发方式 0 分发给所有人 1 分发给指定的人
	WhoRead    []int64               `json:"whoRead"`             // 谁可以看
	WhoReview  []int64               `json:"whoReview"`           // 评论人
	Tags       []int64               `json:"tags"`                //定性标签
	MatchUrl   map[string]string     `json:"matchUrl"`            // 匹配文章内容中的url文本
	Source     int                   `json:"source"`              // 来源 1-用户发布 2-运营发布
	Operator   Operator              `json:"operator"`            // 运营操作人
	Version    int                   `json:"version,omitempty"`   // 版本号
	CreatedAt  int64                 `json:"createdAt,omitempty"` // 创建时间
	UpdatedAt  int64                 `json:"updatedAt,omitempty"` // 编辑时间
	IsDel      soft_delete.DeletedAt `json:"isDel,omitempty"`     // 删除标记
	DeletedAt  int64                 `json:"deletedAt,omitempty"` // 删除时间
	Section    []*ArticleSection     `json:"section"`             // 分段内容
}

type ArticleDraftOperationRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *ArticleDraftOperation) (*ArticleDraftOperation, error)
	Update(ctx context.Context, conn transaction.Conn, dm *ArticleDraftOperation) (*ArticleDraftOperation, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ArticleDraftOperation) (*ArticleDraftOperation, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *ArticleDraftOperation) (*ArticleDraftOperation, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleDraftOperation, error)
	Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleDraftOperation, error)
}

func (m *ArticleDraftOperation) Identify() interface{} {
	if m.Id == 0 {
		return nil
	}
	return m.Id
}

func (m *ArticleDraftOperation) 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
}

func (m *ArticleDraftOperation) 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 ""
}