完成知识库模块
This commit is contained in:
@@ -0,0 +1,318 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// Knowledge 知识库模型
|
||||
type Knowledge struct {
|
||||
Id int `orm:"column(knowledge_id);pk;auto" json:"id"`
|
||||
Title string `orm:"column(title);size(200)" json:"title"`
|
||||
CategoryId int `orm:"column(category_id);default(0);null" json:"categoryId"`
|
||||
CategoryName string `orm:"-" json:"categoryName"` // 不映射到数据库,从联查获取
|
||||
Tags string `orm:"column(tags);type(text);null" json:"tags"`
|
||||
Author string `orm:"column(author);size(50)" json:"author"`
|
||||
Content string `orm:"column(content);type(longtext)" json:"content"`
|
||||
Summary string `orm:"column(summary);size(500);null" json:"summary"`
|
||||
CoverUrl string `orm:"column(cover_url);size(500);null" json:"coverUrl"`
|
||||
Status int8 `orm:"column(status);default(0)" json:"status"`
|
||||
ViewCount int `orm:"column(view_count);default(0)" json:"viewCount"`
|
||||
LikeCount int `orm:"column(like_count);default(0)" json:"likeCount"`
|
||||
IsRecommend int8 `orm:"column(is_recommend);default(0)" json:"isRecommend"`
|
||||
IsTop int8 `orm:"column(is_top);default(0)" json:"isTop"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
|
||||
UpdateTime time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"updateTime"`
|
||||
CreateBy string `orm:"column(create_by);size(50);null" json:"createBy"`
|
||||
UpdateBy string `orm:"column(update_by);size(50);null" json:"updateBy"`
|
||||
DeleteTime time.Time `orm:"column(delete_time);type(datetime);null" json:"deleteTime"`
|
||||
DeleteBy string `orm:"column(delete_by);size(50);null" json:"deleteBy"`
|
||||
}
|
||||
|
||||
// TableName 设置表名
|
||||
func (k *Knowledge) TableName() string {
|
||||
return "yz_knowledge"
|
||||
}
|
||||
|
||||
// KnowledgeCategory 知识库分类模型
|
||||
type KnowledgeCategory struct {
|
||||
CategoryId int `orm:"column(category_id);pk;auto" json:"categoryId"`
|
||||
CategoryName string `orm:"column(category_name);size(100)" json:"categoryName"`
|
||||
CategoryDesc string `orm:"column(category_desc);size(500);null" json:"categoryDesc"`
|
||||
ParentId int `orm:"column(parent_id);default(0)" json:"parentId"`
|
||||
SortOrder int `orm:"column(sort_order);default(0)" json:"sortOrder"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
|
||||
UpdateTime time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"updateTime"`
|
||||
}
|
||||
|
||||
// TableName 设置表名
|
||||
func (kc *KnowledgeCategory) TableName() string {
|
||||
return "yz_knowledge_category"
|
||||
}
|
||||
|
||||
// KnowledgeTag 知识库标签模型
|
||||
type KnowledgeTag struct {
|
||||
TagId int `orm:"column(tag_id);pk;auto" json:"tagId"`
|
||||
TagName string `orm:"column(tag_name);size(50);unique" json:"tagName"`
|
||||
TagColor string `orm:"column(tag_color);size(20);null" json:"tagColor"`
|
||||
UsageCount int `orm:"column(usage_count);default(0)" json:"usageCount"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
|
||||
UpdateTime time.Time `orm:"column(update_time);auto_now;type(datetime)" json:"updateTime"`
|
||||
}
|
||||
|
||||
// TableName 设置表名
|
||||
func (kt *KnowledgeTag) TableName() string {
|
||||
return "yz_knowledge_tags"
|
||||
}
|
||||
|
||||
// AddKnowledge 创建知识
|
||||
func AddKnowledge(k *Knowledge) (int64, error) {
|
||||
o := orm.NewOrm()
|
||||
id, err := o.Insert(k)
|
||||
return id, err
|
||||
}
|
||||
|
||||
// GetKnowledgeById 根据ID获取知识详情(使用联查获取分类名称)
|
||||
func GetKnowledgeById(id int) (*Knowledge, error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
// 使用联查获取分类名称(只查询未删除的记录)
|
||||
querySQL := `
|
||||
SELECT k.*, c.category_name
|
||||
FROM yz_knowledge k
|
||||
LEFT JOIN yz_knowledge_category c ON k.category_id = c.category_id
|
||||
WHERE k.knowledge_id = ? AND k.delete_time IS NULL
|
||||
`
|
||||
|
||||
var result struct {
|
||||
Id int `orm:"column(knowledge_id)"`
|
||||
Title string `orm:"column(title)"`
|
||||
CategoryId int `orm:"column(category_id)"`
|
||||
CategoryName string `orm:"column(category_name)"`
|
||||
Tags string `orm:"column(tags)"`
|
||||
Author string `orm:"column(author)"`
|
||||
Content string `orm:"column(content)"`
|
||||
Summary string `orm:"column(summary)"`
|
||||
CoverUrl string `orm:"column(cover_url)"`
|
||||
Status int8 `orm:"column(status)"`
|
||||
ViewCount int `orm:"column(view_count)"`
|
||||
LikeCount int `orm:"column(like_count)"`
|
||||
IsRecommend int8 `orm:"column(is_recommend)"`
|
||||
IsTop int8 `orm:"column(is_top)"`
|
||||
CreateTime time.Time `orm:"column(create_time)"`
|
||||
UpdateTime time.Time `orm:"column(update_time)"`
|
||||
CreateBy string `orm:"column(create_by)"`
|
||||
UpdateBy string `orm:"column(update_by)"`
|
||||
}
|
||||
|
||||
err := o.Raw(querySQL, id).QueryRow(&result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
knowledge := &Knowledge{
|
||||
Id: result.Id,
|
||||
Title: result.Title,
|
||||
CategoryId: result.CategoryId,
|
||||
CategoryName: result.CategoryName,
|
||||
Tags: result.Tags,
|
||||
Author: result.Author,
|
||||
Content: result.Content,
|
||||
Summary: result.Summary,
|
||||
CoverUrl: result.CoverUrl,
|
||||
Status: result.Status,
|
||||
ViewCount: result.ViewCount,
|
||||
LikeCount: result.LikeCount,
|
||||
IsRecommend: result.IsRecommend,
|
||||
IsTop: result.IsTop,
|
||||
CreateTime: result.CreateTime,
|
||||
UpdateTime: result.UpdateTime,
|
||||
CreateBy: result.CreateBy,
|
||||
UpdateBy: result.UpdateBy,
|
||||
}
|
||||
|
||||
// 增加查看次数
|
||||
knowledge.ViewCount++
|
||||
_, err = o.Raw("UPDATE yz_knowledge SET view_count = ? WHERE knowledge_id = ?", knowledge.ViewCount, id).Exec()
|
||||
|
||||
return knowledge, err
|
||||
}
|
||||
|
||||
// GetAllKnowledge 获取所有知识(支持分页和筛选)
|
||||
func GetAllKnowledge(page, pageSize int, status int8, categoryId int, keyword string) ([]*Knowledge, int64, error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
// 构建 WHERE 条件(只查询未删除的记录)
|
||||
whereSQL := "delete_time IS NULL"
|
||||
params := []interface{}{}
|
||||
|
||||
// 状态筛选
|
||||
if status >= 0 {
|
||||
whereSQL += " AND status = ?"
|
||||
params = append(params, status)
|
||||
}
|
||||
|
||||
// 分类筛选
|
||||
if categoryId > 0 {
|
||||
whereSQL += " AND category_id = ?"
|
||||
params = append(params, categoryId)
|
||||
}
|
||||
|
||||
// 关键词搜索
|
||||
if keyword != "" {
|
||||
whereSQL += " AND title LIKE ?"
|
||||
params = append(params, "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
var total int64
|
||||
countSQL := "SELECT COUNT(*) FROM yz_knowledge WHERE " + whereSQL
|
||||
err := o.Raw(countSQL, params...).QueryRow(&total)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 联查获取分类名称
|
||||
querySQL := `
|
||||
SELECT k.*, c.category_name
|
||||
FROM yz_knowledge k
|
||||
LEFT JOIN yz_knowledge_category c ON k.category_id = c.category_id
|
||||
WHERE ` + whereSQL + `
|
||||
ORDER BY k.is_top DESC, k.create_time DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`
|
||||
params = append(params, pageSize, (page-1)*pageSize)
|
||||
|
||||
var knowledges []*Knowledge
|
||||
var results []struct {
|
||||
Id int `orm:"column(knowledge_id)"`
|
||||
Title string `orm:"column(title)"`
|
||||
CategoryId int `orm:"column(category_id)"`
|
||||
CategoryName string `orm:"column(category_name)"`
|
||||
Tags string `orm:"column(tags)"`
|
||||
Author string `orm:"column(author)"`
|
||||
Content string `orm:"column(content)"`
|
||||
Summary string `orm:"column(summary)"`
|
||||
CoverUrl string `orm:"column(cover_url)"`
|
||||
Status int8 `orm:"column(status)"`
|
||||
ViewCount int `orm:"column(view_count)"`
|
||||
LikeCount int `orm:"column(like_count)"`
|
||||
IsRecommend int8 `orm:"column(is_recommend)"`
|
||||
IsTop int8 `orm:"column(is_top)"`
|
||||
CreateTime time.Time `orm:"column(create_time)"`
|
||||
UpdateTime time.Time `orm:"column(update_time)"`
|
||||
CreateBy string `orm:"column(create_by)"`
|
||||
UpdateBy string `orm:"column(update_by)"`
|
||||
}
|
||||
|
||||
_, err = o.Raw(querySQL, params...).QueryRows(&results)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// 转换结果
|
||||
for _, r := range results {
|
||||
k := &Knowledge{
|
||||
Id: r.Id,
|
||||
Title: r.Title,
|
||||
CategoryId: r.CategoryId,
|
||||
CategoryName: r.CategoryName,
|
||||
Tags: r.Tags,
|
||||
Author: r.Author,
|
||||
Content: r.Content,
|
||||
Summary: r.Summary,
|
||||
CoverUrl: r.CoverUrl,
|
||||
Status: r.Status,
|
||||
ViewCount: r.ViewCount,
|
||||
LikeCount: r.LikeCount,
|
||||
IsRecommend: r.IsRecommend,
|
||||
IsTop: r.IsTop,
|
||||
CreateTime: r.CreateTime,
|
||||
UpdateTime: r.UpdateTime,
|
||||
CreateBy: r.CreateBy,
|
||||
UpdateBy: r.UpdateBy,
|
||||
}
|
||||
knowledges = append(knowledges, k)
|
||||
}
|
||||
|
||||
return knowledges, total, nil
|
||||
}
|
||||
|
||||
// UpdateKnowledge 更新知识
|
||||
func UpdateKnowledge(id int, k *Knowledge) error {
|
||||
o := orm.NewOrm()
|
||||
knowledge := &Knowledge{Id: id}
|
||||
err := o.Read(knowledge)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新字段(不包含category_name,因为它是从联查获取的)
|
||||
knowledge.Title = k.Title
|
||||
knowledge.CategoryId = k.CategoryId
|
||||
knowledge.Tags = k.Tags
|
||||
knowledge.Author = k.Author
|
||||
knowledge.Content = k.Content
|
||||
knowledge.Summary = k.Summary
|
||||
knowledge.CoverUrl = k.CoverUrl
|
||||
knowledge.Status = k.Status
|
||||
knowledge.IsRecommend = k.IsRecommend
|
||||
knowledge.IsTop = k.IsTop
|
||||
knowledge.UpdateBy = k.UpdateBy
|
||||
|
||||
_, err = o.Update(knowledge, "title", "category_id", "tags", "author", "content", "summary", "cover_url", "status", "is_recommend", "is_top", "update_by", "update_time")
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteKnowledge 软删除知识
|
||||
func DeleteKnowledge(id int, deleteBy string) error {
|
||||
o := orm.NewOrm()
|
||||
knowledge := &Knowledge{Id: id}
|
||||
err := o.Read(knowledge)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 执行软删除:设置 delete_time 和 delete_by
|
||||
_, err = o.Raw("UPDATE yz_knowledge SET delete_time = ?, delete_by = ? WHERE knowledge_id = ?", time.Now(), deleteBy, id).Exec()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetAllCategories 获取所有分类
|
||||
func GetAllCategories() ([]*KnowledgeCategory, error) {
|
||||
o := orm.NewOrm()
|
||||
var categories []*KnowledgeCategory
|
||||
_, err := o.QueryTable("yz_knowledge_category").OrderBy("sort_order").All(&categories)
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// GetCategoryById 根据ID获取分类
|
||||
func GetCategoryById(id int) (*KnowledgeCategory, error) {
|
||||
o := orm.NewOrm()
|
||||
category := &KnowledgeCategory{CategoryId: id}
|
||||
err := o.Read(category)
|
||||
return category, err
|
||||
}
|
||||
|
||||
// GetAllTags 获取所有标签
|
||||
func GetAllTags() ([]*KnowledgeTag, error) {
|
||||
o := orm.NewOrm()
|
||||
var tags []*KnowledgeTag
|
||||
_, err := o.QueryTable("yz_knowledge_tags").All(&tags)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
// AddCategory 添加分类
|
||||
func AddCategory(category *KnowledgeCategory) (int64, error) {
|
||||
o := orm.NewOrm()
|
||||
id, err := o.Insert(category)
|
||||
return id, err
|
||||
}
|
||||
|
||||
// AddTag 添加标签
|
||||
func AddTag(tag *KnowledgeTag) (int64, error) {
|
||||
o := orm.NewOrm()
|
||||
id, err := o.Insert(tag)
|
||||
return id, err
|
||||
}
|
||||
@@ -268,6 +268,9 @@ func Init() {
|
||||
orm.RegisterModel(new(ProgramCategory))
|
||||
orm.RegisterModel(new(ProgramInfo))
|
||||
orm.RegisterModel(new(FileInfo))
|
||||
orm.RegisterModel(new(Knowledge))
|
||||
orm.RegisterModel(new(KnowledgeCategory))
|
||||
orm.RegisterModel(new(KnowledgeTag))
|
||||
|
||||
ormConfig, err := beego.AppConfig.String("orm")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user