article.go 7.6 KB
package domain

import (
	"context"
	"strings"

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

// 文章
type Article struct {
	Id           int64             `json:"id"`
	CompanyId    int64             `json:"companyId"`
	CreatedAt    int64             `json:"createdAt,omitempty"`
	UpdatedAt    int64             `json:"updatedAt,omitempty"`
	DeletedAt    int64             `json:"deletedAt,omitempty"`
	IsDel        int               `json:"isDel,omitempty"`
	Version      int               `json:"version,omitempty"`
	AuthorId     int64             `json:"authorId"`                   // 发布人
	Author       UserSimple        `json:"author"`                     // 发布人
	Title        string            `json:"title"`                      // 文章标题
	Images       []Image           `json:"images"`                     // 图片
	Videos       []Video           `json:"videos"`                     // 视频
	WhoRead      []int64           `json:"whoRead"`                    // 谁可以看
	WhoReview    []int64           `json:"whoReview"`                  // 评论人
	Location     Location          `json:"location"`                   // 坐标
	TargetUser   ArticleTarget     `json:"targetUser"`                 // 分发方式 0 分发给所有人 1 分发给指定的人
	CountLove    int               `json:"countLove"`                  // 点赞数量
	CountComment int               `json:"countComment"`               // 评论数量
	CountRead    int               `json:"countRead"`                  // 浏览数量
	Show         ArticleShow       `json:"show"`                       // 评论的展示状态(1显示,2不显示、)
	Tags         []int64           `json:"tags"`                       // 定性标签
	Summary      string            `json:"summary"`                    // 内容概要
	MatchUrl     map[string]string `json:"matchUrl"`                   // 匹配文章内容中的url文本
	Source       int               `gorm:"default:1"`                  // 来源 1-用户发布 2-运营发布
	Operator     Operator          `gorm:"type:jsonb;serializer:json"` // 运营操作人
	DeletedType  int               `json:"deletedType"`                // 删除类型 1-运营删除 2-用户删除
	Cover        string            `json:"cover"`                      // 封面
	Attachments  []FileInfo        `json:"attachments"`                // 附件
	// ...more
}

type FileInfo struct {
	Name string `json:"name"`
	Url  string `json:"url"`
}

type ArticleRepository interface {
	Insert(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	Update(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	Delete(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	Restore(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error)
	FindOneWithUnscoped(ctx context.Context, conn transaction.Conn, id int64) (*Article, error)
	Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*Article, error)
	FindAuthorsLatestFirstArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
	FindAuthorsLatestFirstUnreadArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, limit int) (int64, []*Article, error)
	FindAuthorsLatestArticle(ctx context.Context, conn transaction.Conn, companyId int64, authors []int64, whoRead int64, lastId int64, limit int, queryOptions map[string]interface{}) (int64, []*Article, error)
	FindLatestAccessibleArticle(ctx context.Context, conn transaction.Conn, companyId int64, whoRead int64, lastId int64, limit int) (int64, []*Article, error)
	IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error    //点赞数量变动
	IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error //评论数量变动
	IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, articleId int64) error    //浏览数量变动

	// CustomSearchBy 小程序端搜索查询文章
	// userId  人员id,谁查看文章
	// companyId 公司id
	// tagCategory 标签分类
	// tagId 标签id
	// createdAt  文章的发布时间,按范围查询 [开始时间,结束时间]
	// titleLike 搜索标题
	CustomSearchBy(ctx context.Context, conn transaction.Conn, userId int64, companyId int64,
		tagCategory string, tagId int64, createdAt [2]int64, titleLike string, page int, size int) (int64, []*Article, error)
}

type ArticleTarget int

const (
	ArticleTargetAll   ArticleTarget = 0 //内容分发给所有人
	ArticleTargetLimit ArticleTarget = 1 //分发给指定的人

	ArticleSourceUser     int = 1 //文章来源 用户发布
	ArticleSourceOperator int = 2 //文章来源 运营发布

	ArticleDeletedTypeOperator int = 1 // 文章删除类型 运营删除
	ArticleDeletedTypeUser     int = 2 // 文章删除类型 用户删除
)

func (a ArticleTarget) Named() string {
	switch a {
	case ArticleTargetAll:
		return "所以人"
	case ArticleTargetLimit:
		return "指定人"
	}
	return ""
}

// 文章的展示状态(1显示,2不显示)
type ArticleShow int

const (
	ArticleShowEnable  ArticleShow = 1
	ArticleShowDisable ArticleShow = 2
	ArticleShowIllegal ArticleShow = 3
)

func (a ArticleShow) Named() string {
	switch a {
	case ArticleShowEnable:
		return "显示"
	case ArticleShowDisable:
		return "隐藏"
	case ArticleShowIllegal:
		return "违规"
	}
	return ""
}

func (m *Article) SetSummary(sectionList []*ArticleSection) {
	if len(sectionList) == 0 {
		return
	}
	//设置内容概要

	// 截取内容 50个字
	length := 0 //字符串长度
	content := ""
	for i := range sectionList {
		str := strings.TrimSpace(sectionList[i].Content)
		if len(str) > 0 {
			content = str
			break
		}
	}
	runeContent := []rune(content)
	length = len(runeContent)
	if length > 50 {
		length = 50
	}
	m.Summary = string(runeContent[0:length])
	// 设置封面
	m.Cover = m.GetCoverWithSections(sectionList)
}

// GetSubscribeMessageTitle 获取订阅消息标题 超过20个字用...
func (m *Article) GetSubscribeMessageTitle() string {
	runeContent := []rune(m.Title)
	if len(runeContent) > 20 {
		return string(runeContent[0:20]) + "..."
	}
	return m.Title
}

func (m *Article) WhoCanRead(userId int64) bool {
	if m.AuthorId == userId {
		return true
	}
	if len(m.WhoRead) == 0 {
		return true
	}
	for _, val := range m.WhoRead {
		if userId == val {
			return true
		}
	}
	return false
}

func (m *Article) WhoCanReview(userId int64) bool {
	if m.AuthorId == userId {
		return true
	}
	if len(m.WhoReview) == 0 {
		return true
	}
	for _, val := range m.WhoReview {
		if userId == val {
			return true
		}
	}
	return false
}

func (m *Article) GetCover() string {
	if len(m.Cover) > 0 {
		return m.Cover
	}
	if len(m.Images) > 0 {
		return m.Images[0].Url
	}
	return ""
}

func (m *Article) 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 *Article) GetCoverWithSections(list []*ArticleSection) string {
	if len(m.Cover) > 0 {
		return m.Cover
	}
	if len(m.Images) > 0 {
		return m.Images[0].Url
	}
	for _, sec := range list {
		if sec.ParagraphType == ParagraphTypeImages && len(sec.Images) > 0 {
			return sec.Images[0]
		}
	}
	return ""
}