package models import ( "fmt" "strings" "time" "github.com/beego/beego/v2/client/orm" ) // CmsArticle CMS文章表: yz_cms_article type CmsArticle struct { ID uint64 `orm:"column(id);pk;auto" json:"id"` Tid uint64 `orm:"column(tid)" 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(text);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(1)" json:"status"` // 1草稿 2已发布 3已下架 Views int64 `orm:"column(views);default(0)" json:"views"` Likes int64 `orm:"column(likes);default(0)" json:"likes"` Top int8 `orm:"column(top);default(0)" json:"top"` Recommend int8 `orm:"column(recommend);default(0)" json:"recommend"` PublishTime *time.Time `orm:"column(publish_time);type(datetime);null" json:"publish_time"` PublisherID uint64 `orm:"column(publisher_id);default(0)" json:"publisher_id"` CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"` UpdateTime *time.Time `orm:"column(update_time);type(datetime);auto_now;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" } // CmsArticleCategory CMS文章分类表: yz_cms_article_category type CmsArticleCategory struct { ID uint64 `orm:"column(id);pk;auto" json:"id"` Tid uint64 `orm:"column(tid)" json:"tid"` Cid uint64 `orm:"column(cid);default(0)" json:"cid"` // 父级分类ID 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);type(datetime);auto_now_add" json:"create_time"` UpdateTime *time.Time `orm:"column(update_time);type(datetime);auto_now;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" } // EnsureCmsArticleTables 确保 CMS 文章相关表存在。 func EnsureCmsArticleTables() error { sqls := []string{ `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 DEFAULT CURRENT_TIMESTAMP, ` + "`update_time`" + ` datetime NULL DEFAULT NULL, ` + "`delete_time`" + ` datetime NULL DEFAULT NULL, PRIMARY KEY (` + "`id`" + `), KEY ` + "`idx_tid`" + ` (` + "`tid`" + `), KEY ` + "`idx_tid_cid`" + ` (` + "`tid`" + `,` + "`cid`" + `), KEY ` + "`idx_delete_time`" + ` (` + "`delete_time`" + `) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, `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`" + ` longtext NULL, ` + "`desc`" + ` varchar(500) NOT NULL DEFAULT '', ` + "`image`" + ` varchar(500) NOT NULL DEFAULT '', ` + "`is_trans`" + ` tinyint NOT NULL DEFAULT 0, ` + "`transurl`" + ` varchar(500) NULL DEFAULT NULL, ` + "`status`" + ` tinyint NOT NULL DEFAULT 1, ` + "`views`" + ` bigint NOT NULL DEFAULT 0, ` + "`likes`" + ` bigint NOT NULL DEFAULT 0, ` + "`top`" + ` tinyint NOT NULL DEFAULT 0, ` + "`recommend`" + ` tinyint NOT NULL DEFAULT 0, ` + "`publish_time`" + ` datetime NULL DEFAULT NULL, ` + "`publisher_id`" + ` bigint unsigned NOT NULL DEFAULT 0, ` + "`create_time`" + ` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, ` + "`update_time`" + ` datetime NULL DEFAULT NULL, ` + "`delete_time`" + ` datetime NULL DEFAULT NULL, PRIMARY KEY (` + "`id`" + `), KEY ` + "`idx_tid`" + ` (` + "`tid`" + `), KEY ` + "`idx_tid_cate`" + ` (` + "`tid`" + `,` + "`cate_id`" + `), KEY ` + "`idx_tid_status`" + ` (` + "`tid`" + `,` + "`status`" + `), KEY ` + "`idx_delete_time`" + ` (` + "`delete_time`" + `) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, } for _, sqlStr := range sqls { if _, err := Orm.Raw(sqlStr).Exec(); err != nil { return err } } return nil } // CmsFormatTime 格式化 CMS 可空时间。 func CmsFormatTime(t *time.Time) string { if t == nil || t.IsZero() { return "" } return t.Format("2006-01-02 15:04:05") } // CmsCategoryNameMap 批量获取分类ID到分类名称的映射。 func CmsCategoryNameMap(tid uint64, cateIDs []uint64) map[uint64]string { result := make(map[uint64]string) if tid == 0 || len(cateIDs) == 0 { return result } seen := make(map[uint64]bool) ids := make([]uint64, 0, len(cateIDs)) for _, id := range cateIDs { if id == 0 || seen[id] { continue } seen[id] = true ids = append(ids, id) } if len(ids) == 0 { return result } var rows []CmsArticleCategory _, err := Orm.QueryTable(new(CmsArticleCategory)). Filter("tid", tid). Filter("id__in", ids). Filter("delete_time__isnull", true). All(&rows, "id", "name") if err != nil && err != orm.ErrNoRows { return result } for _, row := range rows { result[row.ID] = row.Name } return result } // CmsSimilarArticles 根据标题查找相似文章,用于创建文章时提示重复内容。 func CmsSimilarArticles(tid uint64, title string, limit int) ([]map[string]interface{}, error) { title = strings.TrimSpace(title) if tid == 0 || title == "" { return []map[string]interface{}{}, nil } if limit <= 0 { limit = 5 } var rows []CmsArticle _, err := Orm.QueryTable(new(CmsArticle)). Filter("tid", tid). Filter("title__icontains", title). Filter("delete_time__isnull", true). OrderBy("-id"). Limit(limit). All(&rows, "id", "title", "cate_id", "status", "create_time") if err != nil && err != orm.ErrNoRows { return nil, err } out := make([]map[string]interface{}, 0, len(rows)) for _, row := range rows { out = append(out, map[string]interface{}{ "id": row.ID, "title": row.Title, "cate_id": row.CateID, "status": row.Status, "create_time": row.CreateTime.Format("2006-01-02 15:04:05"), "similarity": fmt.Sprintf("标题包含“%s”", title), }) } return out, nil }