article_section.go 1.4 KB
package models

import (
	"fmt"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gorm.io/gorm"
	"gorm.io/plugin/soft_delete"
)

type ArticleSection struct {
	Id           int64 `gorm:"primaryKey"` // 唯一标识
	CompanyId    int64
	CreatedAt    int64
	UpdatedAt    int64
	DeletedAt    int64
	IsDel        soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
	Version      int
	ArticleId    int64  // 文章id
	Content      string // 文本内容
	SortBy       int    // 排序
	TotalComment int    // 评论的数量
}

func (m *ArticleSection) TableName() string {
	return "article_section"
}

func (m *ArticleSection) BeforeCreate(tx *gorm.DB) (err error) {
	m.CreatedAt = time.Now().Unix()
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *ArticleSection) BeforeUpdate(tx *gorm.DB) (err error) {
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *ArticleSection) CacheKeyFunc() string {
	if m.Id == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}

func (m *ArticleSection) CacheKeyFuncByObject(obj interface{}) string {
	if v, ok := obj.(*ArticleSection); ok {
		return v.CacheKeyFunc()
	}
	return ""
}

func (m *ArticleSection) CachePrimaryKeyFunc() string {
	if len("") == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}