article.go 5.3 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"`
	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文本
	// ...more
}

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)
	UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error)
	FindOne(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 //分发给指定的人
)

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
)

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

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

	// 截取内容 50个字
	runeNumber := 0  //字数
	stringIndex := 0 //字符串长度
	content := ""
	for i := range sectionList {
		str := strings.TrimSpace(sectionList[i].Content)
		if len(str) > 0 {
			content = str
			break
		}
	}
	for i := range content {
		if runeNumber > 50 {
			break
		}
		runeNumber += 1
		stringIndex = i
	}
	m.Summary = content[0:stringIndex]
}

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
}