160 lines
5.6 KiB
Go
160 lines
5.6 KiB
Go
package models
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
)
|
|
|
|
// CmsArticleCategory CMS 文章分类 yz_cms_article_category
|
|
type CmsArticleCategory struct {
|
|
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
|
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
|
Cid uint64 `orm:"column(cid);default(0)" json:"cid"`
|
|
Name string `orm:"column(name);size(100)" json:"name"`
|
|
Image string `orm:"column(image);size(500);default()" json:"image"`
|
|
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
|
Sort int `orm:"column(sort);default(0)" json:"sort"`
|
|
Status int8 `orm:"column(status);default(1)" json:"status"`
|
|
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
|
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
|
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
|
}
|
|
|
|
func (m *CmsArticleCategory) TableName() string {
|
|
return "yz_cms_article_category"
|
|
}
|
|
|
|
// CmsArticle CMS 文章 yz_cms_article
|
|
type CmsArticle struct {
|
|
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
|
Tid uint64 `orm:"column(tid);default(0)" json:"tid"`
|
|
Title string `orm:"column(title);size(255)" json:"title"`
|
|
Author string `orm:"column(author);size(100);default()" json:"author"`
|
|
CateID uint64 `orm:"column(cate_id);default(0)" json:"cate_id"`
|
|
Content string `orm:"column(content);type(mediumtext);null" json:"content"`
|
|
Desc string `orm:"column(desc);size(500);default()" json:"desc"`
|
|
Image string `orm:"column(image);size(500);default()" json:"image"`
|
|
IsTrans int8 `orm:"column(is_trans);default(0)" json:"is_trans"`
|
|
TransURL *string `orm:"column(transurl);size(500);null" json:"transurl"`
|
|
Status int8 `orm:"column(status);default(0)" json:"status"`
|
|
Top int8 `orm:"column(top);default(0)" json:"top"`
|
|
Recommend int8 `orm:"column(recommend);default(0)" json:"recommend"`
|
|
Views int `orm:"column(views);default(0)" json:"views"`
|
|
Likes int `orm:"column(likes);default(0)" json:"likes"`
|
|
PublisherID *uint64 `orm:"column(publisher_id);null" json:"publisher_id"`
|
|
PublishTime *time.Time `orm:"column(publish_time);type(datetime);null" json:"publish_time"`
|
|
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"create_time"`
|
|
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null" json:"update_time"`
|
|
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
|
}
|
|
|
|
func (m *CmsArticle) TableName() string {
|
|
return "yz_cms_article"
|
|
}
|
|
|
|
var cmsArticleTablesOnce sync.Once
|
|
|
|
// EnsureCmsArticleTables 首次使用时自动建表(若不存在)。
|
|
func EnsureCmsArticleTables() error {
|
|
var err error
|
|
cmsArticleTablesOnce.Do(func() {
|
|
_, err = Orm.Raw(`
|
|
CREATE TABLE IF NOT EXISTS yz_cms_article_category (
|
|
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
|
tid bigint unsigned NOT NULL DEFAULT 0,
|
|
cid bigint unsigned NOT NULL DEFAULT 0,
|
|
name varchar(100) NOT NULL DEFAULT '',
|
|
image varchar(500) NOT NULL DEFAULT '',
|
|
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
|
sort int NOT NULL DEFAULT 0,
|
|
status tinyint NOT NULL DEFAULT 1,
|
|
create_time datetime NOT NULL,
|
|
update_time datetime DEFAULT NULL,
|
|
delete_time datetime DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY idx_tid_cid (tid, cid)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
|
if err != nil {
|
|
return
|
|
}
|
|
_, err = Orm.Raw(`
|
|
CREATE TABLE IF NOT EXISTS yz_cms_article (
|
|
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
|
tid bigint unsigned NOT NULL DEFAULT 0,
|
|
title varchar(255) NOT NULL DEFAULT '',
|
|
author varchar(100) NOT NULL DEFAULT '',
|
|
cate_id bigint unsigned NOT NULL DEFAULT 0,
|
|
content mediumtext,
|
|
` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '',
|
|
image varchar(500) NOT NULL DEFAULT '',
|
|
is_trans tinyint NOT NULL DEFAULT 0,
|
|
transurl varchar(500) DEFAULT NULL,
|
|
status tinyint NOT NULL DEFAULT 0,
|
|
top tinyint NOT NULL DEFAULT 0,
|
|
recommend tinyint NOT NULL DEFAULT 0,
|
|
views int NOT NULL DEFAULT 0,
|
|
likes int NOT NULL DEFAULT 0,
|
|
publisher_id bigint unsigned DEFAULT NULL,
|
|
publish_time datetime DEFAULT NULL,
|
|
create_time datetime NOT NULL,
|
|
update_time datetime DEFAULT NULL,
|
|
delete_time datetime DEFAULT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY idx_tid_status (tid, status),
|
|
KEY idx_cate_id (cate_id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`).Exec()
|
|
})
|
|
return err
|
|
}
|
|
|
|
func CmsCategoryNameMap(tid uint64, ids []uint64) map[uint64]string {
|
|
out := make(map[uint64]string)
|
|
if len(ids) == 0 {
|
|
return out
|
|
}
|
|
var rows []CmsArticleCategory
|
|
_, _ = Orm.QueryTable(new(CmsArticleCategory)).
|
|
Filter("tid", tid).
|
|
Filter("id__in", ids).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows, "ID", "Name")
|
|
for _, r := range rows {
|
|
out[r.ID] = r.Name
|
|
}
|
|
return out
|
|
}
|
|
|
|
func CmsFormatTime(t *time.Time) string {
|
|
if t == nil {
|
|
return ""
|
|
}
|
|
return t.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
func CmsSimilarArticles(tid uint64, title string, limit int) ([]orm.Params, error) {
|
|
if limit <= 0 {
|
|
limit = 5
|
|
}
|
|
var rows []CmsArticle
|
|
_, err := Orm.QueryTable(new(CmsArticle)).
|
|
Filter("tid", tid).
|
|
Filter("delete_time__isnull", true).
|
|
Filter("title__icontains", title).
|
|
Limit(limit).
|
|
All(&rows, "ID", "Title")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]orm.Params, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, orm.Params{
|
|
"id": r.ID,
|
|
"title": r.Title,
|
|
"similarity": 80,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|