refactor: reorganize modules and add Linux build tooling
This commit is contained in:
28
module/base/cms/internal/models/cms_accessory.go
Normal file
28
module/base/cms/internal/models/cms_accessory.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CmsAccessory 文章附件表
|
||||
type CmsAccessory struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
PostId uint `gorm:"column:post_id;not null" json:"post_id"` // 关联文章id
|
||||
PostIdentity string `gorm:"column:post_identity;type:varchar(36);index;" json:"post_identity"` // 关联文章标识
|
||||
PagesId uint `gorm:"column:pages_id;not null" json:"pages_id"` // 关联文章页面id
|
||||
PagesIdentity string `gorm:"column:pages_identity;type:varchar(36);index;" json:"pages_identity"` // 关联文章页面标识
|
||||
Title string `gorm:"column:title;type:varchar(255);default:''" json:"title"` // 附件标题
|
||||
FilePath string `gorm:"column:file_path;type:varchar(500);not null" json:"file_path"` // 附件文件地址
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsAccessory{})
|
||||
}
|
||||
|
||||
// TableName .CmsAccessory 分类表
|
||||
func (c *CmsAccessory) TableName() string {
|
||||
return "cms_accessory" // 对应数据库表名
|
||||
}
|
||||
44
module/base/cms/internal/models/cms_category.go
Normal file
44
module/base/cms/internal/models/cms_category.go
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* CMS分类数据模型
|
||||
* @Author: david.yan@qq.com
|
||||
* @Date: 2021-11-29 15:16:26
|
||||
* @LastEditors: david.yan@qq.com
|
||||
* @LastEditTime: 2021-11-30 17:40:50
|
||||
* @Description: 定义分类相关的数据结构和数据库映射,支持层级分类
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CmsCategory CMS分类数据模型
|
||||
// 支持层级分类结构,每个分类可以有父分类和子分类
|
||||
type CmsCategory struct {
|
||||
gorm.Model // GORM基础模型(ID、CreatedAt、UpdatedAt、DeletedAt)
|
||||
types.Std_Identity // 标准身份标识字段
|
||||
SiteIdentity string `gorm:"column:site_identity;type:varchar(36);index;" json:"site_identity"`
|
||||
|
||||
// 分类基本信息
|
||||
CategoryKey string `gorm:"column:category_key;type:varchar(255);not null;uniqueIndex;" json:"category_key"` // 分类唯一键值
|
||||
Title string `gorm:"column:title;type:varchar(255);not null" json:"title"` // 分类标题
|
||||
CoverPath string `gorm:"column:cover_path;type:varchar(255);default:''" json:"cover_path"` // 分类封面图片路径
|
||||
Intro string `gorm:"column:intro;type:text;default:''" json:"intro"` // 分类介绍/描述
|
||||
|
||||
// 层级关系
|
||||
ParentId uint `gorm:"column:parent_id;index;" json:"parent_id"` // 父级分类ID,0表示顶级分类
|
||||
Children []CmsCategory `gorm:"foreignkey:ParentId;references:ID"` // 子分类列表
|
||||
}
|
||||
|
||||
// init 初始化函数,将模型注册到数据库迁移表
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsCategory{})
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
// 返回: 数据库表名
|
||||
func (c *CmsCategory) TableName() string {
|
||||
return "cms_category"
|
||||
}
|
||||
42
module/base/cms/internal/models/cms_comment.go
Normal file
42
module/base/cms/internal/models/cms_comment.go
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @Author: david.yan@qq.com
|
||||
* @Date: 2021-11-29 15:16:26
|
||||
* @LastEditors: david.yan@qq.com
|
||||
* @LastEditTime: 2021-12-08 15:48:55
|
||||
* @FilePath: /src/git.buka.tv/Cms/internal/models/comment.go
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 内容评论
|
||||
type CmsComment struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
PostIdentity string `gorm:"column:post_identity;type:varchar(36);index;" json:"post_identity"` // 关联的帖子身份标识符
|
||||
OwnerName string `gorm:"column:owner_name;type:varchar(255);index;" json:"owner_name"` // 作者名称
|
||||
OwnerIdentity string `gorm:"column:owner_identity;type:varchar(36);index;" json:"owner_identity"` // 作者身份标识符
|
||||
Role string `gorm:"column:role;type:varchar(255);index;" json:"role"` // 角色
|
||||
ParentId uint `gorm:"column:parent_id;index;" json:"parent_id"` // 关联的父级评论id
|
||||
ReplyIdentity string `gorm:"column:reply_identity;type:varchar(36);index;" json:"reply_identity"` // 被回复的评论的身份标识符
|
||||
Cms string `gorm:"column:Cms;type:text;default:'';" json:"Cms"` // 评论内容
|
||||
Hits int64 `gorm:"column:hits;default:0" json:"hits"` // 点击量
|
||||
LikeHits int64 `gorm:"column:like_hits;default:0" json:"like_hits"` // 点赞量
|
||||
UnlikeHits int64 `gorm:"column:unlike_hits;default:0" json:"unlike_hits"` // 踩赞量
|
||||
CommentHits int64 `gorm:"column:comment_hits;default:0" json:"comment_hits"` // 评论量
|
||||
|
||||
Children []CmsComment `gorm:"foreignkey:parent_id;references:ID"` // 子评论
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsComment{})
|
||||
}
|
||||
|
||||
// TableName .CmsComment 内容评论表
|
||||
func (table *CmsComment) TableName() string {
|
||||
return "cms_comment" // 对应数据库表名
|
||||
}
|
||||
42
module/base/cms/internal/models/cms_pages.go
Normal file
42
module/base/cms/internal/models/cms_pages.go
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @Author: david.yan@qq.com
|
||||
* @Date: 2021-11-29 15:16:26
|
||||
* @LastEditors: david.yan@qq.com
|
||||
* @LastEditTime: 2021-12-08 15:59:00
|
||||
* @FilePath: /src/git.buka.tv/Cms/internal/models/post.go
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// CmsPost 单页文章
|
||||
type CmsPages struct {
|
||||
types.Std_IICUDS
|
||||
SiteIdentity string `gorm:"column:site_identity;type:varchar(36);index;" json:"site_identity"`
|
||||
|
||||
Title string `gorm:"column:title;type:varchar(255);default:'';" json:"title"` // 标题
|
||||
Key string `gorm:"column:key;type:varchar(255);uniqueIndex;" json:"key"` // 内容KEY,唯一,无空格
|
||||
Description string `gorm:"column:description;type:varchar(255);default:'';" json:"description"` // 描述
|
||||
CoverPath string `gorm:"column:cover_path;type:varchar(255);default:'';" json:"cover_path"` // 封面
|
||||
Content string `gorm:"column:content;type:text;default:'';" json:"content"` // 内容
|
||||
HasAccessory bool `gorm:"column:has_accessory;type:bool;default:false" json:"has_accessory"` // 是否有附件
|
||||
Type int32 `gorm:"column:type;default:0" json:"type"` // 类型
|
||||
Hits int64 `gorm:"column:hits;default:0" json:"hits"` // 点击量
|
||||
|
||||
AccessoryIdentityArray []string `gorm:"-" json:"accessory_identity_array"` // 附件标识
|
||||
Accessories []*CmsAccessory `gorm:"foreignkey:PagesIdentity ;references:Identity"` // 附件
|
||||
Tags []*CmsRelateTags `gorm:"foreignkey:PagesIdentity;references:Identity"` // 标签
|
||||
TagsIdentityArray []string `gorm:"-" json:"tags_identity_array"` // 标签标识
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsPages{})
|
||||
}
|
||||
|
||||
// TableName .CmsPages 单页文章表
|
||||
func (table *CmsPages) TableName() string {
|
||||
return "cms_pages" // 对应数据库表名
|
||||
}
|
||||
80
module/base/cms/internal/models/cms_post.go
Normal file
80
module/base/cms/internal/models/cms_post.go
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* CMS文章数据模型
|
||||
* @Author: david.yan@qq.com
|
||||
* @Date: 2021-11-29 15:16:26
|
||||
* @LastEditors: david.yan@qq.com
|
||||
* @LastEditTime: 2021-12-08 15:59:00
|
||||
* @Description: 定义文章相关的数据结构和数据库映射
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
)
|
||||
|
||||
// CmsPost CMS文章数据模型
|
||||
// 包含文章的基本信息、统计数据、关联数据等
|
||||
type CmsPost struct {
|
||||
types.Std_IICUDS // 标准IICUDS字段(Identity、CreatedAt、UpdatedAt等)
|
||||
types.Std_Owner // 标准所有者字段
|
||||
SiteIdentity string `gorm:"column:site_identity;type:varchar(36);index;" json:"site_identity"`
|
||||
|
||||
// 基本信息
|
||||
Types int32 `gorm:"column:types;type:varchar(255);default:'';" json:"types"` // 文章类型
|
||||
Title string `gorm:"column:title;type:varchar(255);default:'';" json:"title"` // 文章标题
|
||||
Hash string `gorm:"column:hash;type:varchar(255);uniqueIndex;" json:"hash"` // 文章唯一键值,用于URL等
|
||||
Description string `gorm:"column:description;type:varchar(255);default:'';" json:"description"` // 文章描述/摘要
|
||||
CoverPath string `gorm:"column:cover_path;type:varchar(255);default:'';" json:"cover_path"` // 封面图片路径
|
||||
Content string `gorm:"column:content;type:text;default:'';" json:"content"` // 文章正文内容
|
||||
Lang string `gorm:"type:varchar(20);"` // 语言
|
||||
TargetUrl string `gorm:"column:target_url;type:varchar(255);default:''" json:"target_url"` // 目标链接
|
||||
Rights string `gorm:"column:rights;type:varchar(255);default:''" json:"rights"` // 权限
|
||||
|
||||
// 作者信息
|
||||
Author string `gorm:"column:author;type:varchar(255);default:''" json:"author"` // 作者姓名
|
||||
AuthorIdentity string `gorm:"column:author_identity;type:varchar(36);default:'';" json:"author_identity"` // 作者唯一标识
|
||||
|
||||
// 来源方
|
||||
SourceOrigin string `gorm:"type:varchar(255);"` // 简称
|
||||
SourceUrl string `gorm:"column:source_url;type:varchar(255);default:''" json:"source_url"` // 源链接
|
||||
|
||||
// 统计数据
|
||||
Hits int64 `gorm:"column:hits;default:0" json:"hits"` // 点击量/浏览量
|
||||
LikeHits int64 `gorm:"column:like_hits;default:0" json:"like_hits"` // 点赞数量
|
||||
UnlikeHits int64 `gorm:"column:unlike_hits;default:0" json:"unlike_hits"` // 踩赞数量
|
||||
CommentHits int64 `gorm:"column:comment_hits;default:0" json:"comment_hits"` // 评论数量
|
||||
|
||||
// 其他属性
|
||||
HasAccessory bool `gorm:"column:has_accessory;type:bool;default:false" json:"has_accessory"` // 是否有附件
|
||||
|
||||
// 关联数据标识(不存储到数据库,仅用于数据传输)
|
||||
CategoryIdentityArray []string `gorm:"-" json:"category_identity_array"` // 关联的分类标识列表
|
||||
TagsIdentityArray []string `gorm:"-" json:"tags_identity_array"` // 关联的标签标识列表
|
||||
AccessoryIdentityArray []string `gorm:"-" json:"accessory_identity_array"` // 关联的附件标识列表
|
||||
|
||||
// 关联数据对象(通过外键关联)
|
||||
Accessories []*CmsAccessory `gorm:"foreignkey:PostId;references:ID"` // 文章附件列表
|
||||
Categories []*CmsRelateCategory `gorm:"foreignkey:PostIdentity;references:Identity"` // 文章分类关联列表
|
||||
Tags []*CmsRelateTags `gorm:"foreignkey:PostIdentity;references:Identity"` // 文章标签关联列表
|
||||
|
||||
ExtendUrl string // 扩展:URL
|
||||
ExtendData string // 扩展:数据
|
||||
ExtendImg string // 扩展:图片
|
||||
ExtendDesc string // 扩展:描述
|
||||
|
||||
Published time.Time // 事件发生时间
|
||||
}
|
||||
|
||||
// init 初始化函数,将模型注册到数据库迁移表
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsPost{})
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
// 返回: 数据库表名
|
||||
func (table *CmsPost) TableName() string {
|
||||
return "cms_post"
|
||||
}
|
||||
22
module/base/cms/internal/models/cms_relate_category.go
Normal file
22
module/base/cms/internal/models/cms_relate_category.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
import "git.apinb.com/bsm-sdk/core/database"
|
||||
|
||||
// post 关联分类表
|
||||
type CmsRelateCategory struct {
|
||||
ID uint `gorm:"primarykey"` // Id
|
||||
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;"` // 唯一标识
|
||||
PostIdentity string `gorm:"column:post_identity;type:varchar(36);index;" json:"post_identity"` // 关联的post唯一标识
|
||||
CategoryIdentity string `gorm:"column:category_identity;type:varchar(36);index;" json:"category_identity"` // 关联的category唯一标识
|
||||
|
||||
Category CmsCategory `gorm:"foreignKey:Identity;references:CategoryIdentity"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsRelateCategory{})
|
||||
}
|
||||
|
||||
// TableName .CmsRelateCategory 内容相关类别表
|
||||
func (table *CmsRelateCategory) TableName() string {
|
||||
return "cms_relate_category" // 对应数据库表名
|
||||
}
|
||||
23
module/base/cms/internal/models/cms_relate_tags.go
Normal file
23
module/base/cms/internal/models/cms_relate_tags.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package models
|
||||
|
||||
import "git.apinb.com/bsm-sdk/core/database"
|
||||
|
||||
// post 内容相关主题表
|
||||
type CmsRelateTags struct {
|
||||
ID uint `gorm:"primarykey"` // Id
|
||||
Identity string `gorm:"column:identity;type:varchar(36);uniqueIndex;"` // 唯一标识
|
||||
PostIdentity string `gorm:"column:post_identity;type:varchar(36);index;" json:"post_identity"` // 关联文章唯一标识
|
||||
TagsIdentity string `gorm:"column:tags_identity;type:varchar(36);index;" json:"tags_identity"` // 关联分类唯一标识
|
||||
PagesIdentity string `gorm:"column:pages_identity;type:varchar(36);index;" json:"pages_identity"`
|
||||
|
||||
Tags CmsTags `gorm:"foreignKey:Identity;references:TagsIdentity"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsRelateTags{})
|
||||
}
|
||||
|
||||
// TableName .CmsRelateTopic 内容相关主题表
|
||||
func (table *CmsRelateTags) TableName() string {
|
||||
return "cms_relate_tags" // 对应数据库表名
|
||||
}
|
||||
31
module/base/cms/internal/models/cms_site.go
Normal file
31
module/base/cms/internal/models/cms_site.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CmsSite 站点配置
|
||||
type CmsSite struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
|
||||
Title string `gorm:"column:title;type:varchar(255);not null" json:"title"`
|
||||
Description string `gorm:"column:description;type:varchar(255);not null" json:"description"`
|
||||
IconPath string `gorm:"column:icon_path;type:varchar(255);not null" json:"icon_path"`
|
||||
Keywords string `gorm:"column:keywords;type:varchar(255);not null" json:"keywords"`
|
||||
Domain string `gorm:"column:domain;type:varchar(255);not null" json:"domain"`
|
||||
Seo string `gorm:"column:seo;type:text;not null" json:"seo"`
|
||||
Theme string `gorm:"column:theme;type:varchar(255);not null" json:"theme"`
|
||||
Configs string `gorm:"column:configs;type:text;not null" json:"configs"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsSite{})
|
||||
}
|
||||
|
||||
// TableName .CmsAccessory 分类表
|
||||
func (c *CmsSite) TableName() string {
|
||||
return "cms_site" // 对应数据库表名
|
||||
}
|
||||
33
module/base/cms/internal/models/cms_tags.go
Normal file
33
module/base/cms/internal/models/cms_tags.go
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* @Author: david.yan@qq.com
|
||||
* @Date: 2021-11-29 15:16:26
|
||||
* @LastEditors: david.yan@qq.com
|
||||
* @LastEditTime: 2021-11-30 17:40:50
|
||||
* @FilePath: /src/git.buka.tv/Cms/internal/models/category.go
|
||||
*/
|
||||
package models
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/database"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 内容标签
|
||||
type CmsTags struct {
|
||||
gorm.Model
|
||||
types.Std_Identity
|
||||
SiteIdentity string `gorm:"column:site_identity;type:varchar(36);index;" json:"site_identity"`
|
||||
Title string `gorm:"column:title;type:varchar(255);not null" json:"title"` // 标题
|
||||
CoverPath string `gorm:"column:cover_path;type:varchar(255);default:''" json:"cover_path"` // 封面
|
||||
Intro string // 介绍
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.MigrateTables = append(database.MigrateTables, &CmsTags{})
|
||||
}
|
||||
|
||||
// TableName .CmsTags 内容标签表
|
||||
func (c *CmsTags) TableName() string {
|
||||
return "cms_tags" // 对应数据库表名
|
||||
}
|
||||
389
module/base/cms/internal/models/query.go
Normal file
389
module/base/cms/internal/models/query.go
Normal file
@@ -0,0 +1,389 @@
|
||||
// Package models 提供CMS数据模型和数据库操作
|
||||
// 包含文章、分类、标签、评论等实体的数据结构和CRUD操作
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 数据库表名常量
|
||||
const (
|
||||
postTable = "cms_post" // 文章表
|
||||
accessoryTable = "cms_accessory" // 附件表
|
||||
postRelateCategoryTable = "cms_relate" // 关联表
|
||||
)
|
||||
|
||||
// InitData 初始化基础数据
|
||||
// 创建系统必需的默认数据,如根分类等
|
||||
func InitData() {
|
||||
var cnt int64 = 0
|
||||
|
||||
// 检查是否存在根分类,如果不存在则创建
|
||||
err := impl.DBService.Model(&CmsCategory{}).Where("identity=?", "_RootCategory").Count(&cnt).Error
|
||||
if cnt == 0 || err != nil {
|
||||
data := &CmsCategory{
|
||||
Title: "根目录",
|
||||
ParentId: 0, // 0表示顶级分类
|
||||
Std_Identity: types.Std_Identity{Identity: "_RootCategory"},
|
||||
}
|
||||
impl.DBService.Create(data)
|
||||
}
|
||||
}
|
||||
|
||||
// AddPost 添加文章及其关联数据
|
||||
// 使用事务确保数据一致性,同时创建文章、分类关联、标签关联和附件信息
|
||||
// 参数:
|
||||
// - post: 文章数据模型
|
||||
// - accessory: 附件数据列表
|
||||
// - category: 分类标识列表
|
||||
// - tags: 标签标识列表
|
||||
//
|
||||
// 返回:
|
||||
// - error: 错误信息
|
||||
func AddPost(post *CmsPost, accessory []*pb.AccessoryItem, category, tags []string) (err error) {
|
||||
var (
|
||||
accessoryPath = make([]CmsAccessory, 0) // 文章附件数据
|
||||
categoryData = make([]CmsRelateCategory, 0) // 文章分类关联数据
|
||||
TagsData = make([]CmsRelateTags, 0) // 文章标签关联数据
|
||||
)
|
||||
|
||||
// 构建分类关联数据
|
||||
for _, va := range category {
|
||||
categoryData = append(categoryData, CmsRelateCategory{
|
||||
Identity: utils.UUID(),
|
||||
PostIdentity: post.Identity,
|
||||
CategoryIdentity: va,
|
||||
})
|
||||
}
|
||||
|
||||
// 构建标签关联数据
|
||||
for _, val := range tags {
|
||||
TagsData = append(TagsData, CmsRelateTags{
|
||||
Identity: utils.ULID(),
|
||||
PostIdentity: post.Identity,
|
||||
TagsIdentity: val,
|
||||
})
|
||||
}
|
||||
|
||||
// 使用事务确保数据一致性
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 创建文章记录
|
||||
if err = tx.Create(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 创建附件关联记录
|
||||
if len(accessory) != 0 {
|
||||
for _, v := range accessory {
|
||||
accessoryPath = append(accessoryPath, CmsAccessory{
|
||||
Std_Identity: types.Std_Identity{Identity: utils.UUID()},
|
||||
PostId: post.ID,
|
||||
PostIdentity: post.Identity,
|
||||
FilePath: v.FilePath,
|
||||
Title: v.Title,
|
||||
})
|
||||
}
|
||||
if err := tx.Create(&accessoryPath).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 创建分类关联记录
|
||||
if len(category) != 0 {
|
||||
if err := tx.Create(&categoryData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 创建标签关联记录
|
||||
if len(tags) != 0 {
|
||||
if err := tx.Create(&TagsData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// PostList 文章列表
|
||||
func PostList(page, size int64, categoryIdentity, keyword string, userType int64) (list []*CmsPost, cnt int64, err error) {
|
||||
|
||||
tx := impl.DBService.Debug().Model(&CmsPost{})
|
||||
if categoryIdentity != "" {
|
||||
tx = tx.Joins("left join cms_relate_category crc on crc.post_identity = cms_post.identity ")
|
||||
tx = tx.Where("crc.category_identity = ?", categoryIdentity)
|
||||
}
|
||||
|
||||
if keyword != "" {
|
||||
tx = tx.Where("cms_post.title like ?", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
if userType != 0 {
|
||||
tx = tx.Where("cms_post.types = ?", userType)
|
||||
}
|
||||
|
||||
if err = tx.Count(&cnt).Order("cms_post.created_at desc").Limit(int(size)).Offset(int((page - 1) * size)).Error; err != nil {
|
||||
return nil, 0, errcode.ErrDB
|
||||
}
|
||||
err = tx.Preload("Accessories").Preload("Categories.Category").Preload("Tags.Tags").Find(&list).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ModifyPost 修改文章
|
||||
func ModifyPost(identity string, post *CmsPost, accessory, category, tags []string) (err error) {
|
||||
var (
|
||||
accessoryPath = make([]CmsAccessory, 0)
|
||||
categoryData = make([]CmsRelateCategory, 0)
|
||||
tagsData = make([]CmsRelateTags, 0)
|
||||
)
|
||||
for _, v := range category {
|
||||
categoryData = append(categoryData, CmsRelateCategory{
|
||||
Identity: utils.UUID(),
|
||||
PostIdentity: identity,
|
||||
CategoryIdentity: v,
|
||||
})
|
||||
}
|
||||
for _, val := range tags {
|
||||
tagsData = append(tagsData, CmsRelateTags{
|
||||
Identity: utils.ULID(),
|
||||
PostIdentity: identity,
|
||||
TagsIdentity: val,
|
||||
})
|
||||
}
|
||||
for _, v := range accessory {
|
||||
accessoryPath = append(accessoryPath, CmsAccessory{
|
||||
Std_Identity: types.Std_Identity{Identity: utils.UUID()},
|
||||
PostId: post.ID,
|
||||
PostIdentity: identity,
|
||||
FilePath: v,
|
||||
})
|
||||
}
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err = tx.Where("identity = ?", identity).Updates(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除旧的附件、分类和标签关联
|
||||
if err := tx.Where("post_identity = ?", identity).Delete(&CmsAccessory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("post_identity = ?", identity).Delete(&CmsRelateCategory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("post_identity = ?", identity).Delete(&CmsRelateTags{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 创建新的分类关联
|
||||
if len(category) != 0 {
|
||||
if err := tx.Create(&categoryData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 创建新的标签关联
|
||||
if len(tags) != 0 {
|
||||
if err := tx.Create(&tagsData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 创建新的附件关联
|
||||
if len(accessory) != 0 {
|
||||
if err := tx.Create(&accessoryPath).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePost 删除文章
|
||||
func DeletePost(identity, authorIdentity string) (err error) {
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err = tx.Where("identity = ?", identity).Delete(&CmsPost{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Where("post_identity = ?", identity).Delete(&CmsRelateCategory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = tx.Where("post_identity = ?", identity).Delete(&CmsRelateTags{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tx.Where("post_identity = ?", identity).Delete(&CmsAccessory{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// GetPost 根据指定字段获取文章详情
|
||||
// 查询成功后会更新文章的点击量
|
||||
// 参数:
|
||||
// - key: 查询字段名(如"identity"、"key"等)
|
||||
// - val: 查询字段值
|
||||
//
|
||||
// 返回:
|
||||
// - *CmsPost: 文章详情
|
||||
// - error: 错误信息
|
||||
func GetPost(key, val string) (*CmsPost, error) {
|
||||
var post = &CmsPost{}
|
||||
|
||||
// 使用事务确保查询和更新操作的原子性
|
||||
return post, impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 查询文章详情,预加载关联数据
|
||||
if err := tx.Model(&CmsPost{}).Preload("Accessories").Preload("Categories").Preload("Tags").
|
||||
Where(key+" = ?", val).First(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新文章点击量,只有在查询成功后才更新
|
||||
if err := tx.Model(&CmsPost{}).Where(key+" = ?", val).
|
||||
UpdateColumn("hits", gorm.Expr("hits + 1")).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// IncrOrDescPostField 增加或减少文章字段值
|
||||
// 用于更新文章的点赞、踩赞、评论等计数字段
|
||||
// 参数:
|
||||
// - identity: 文章唯一标识
|
||||
// - column: 要更新的字段名
|
||||
// - desc: true表示减少,false表示增加
|
||||
//
|
||||
// 返回:
|
||||
// - error: 错误信息
|
||||
func IncrOrDescPostField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
}
|
||||
if err := impl.DBService.Model(&CmsPost{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddComment(comment *CmsComment, authorIdentity string) (err error) {
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(comment).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 添加评论,更新文章评论量
|
||||
err := tx.Model(&CmsPost{}).Where("identity = ?", comment.PostIdentity).UpdateColumn("comment_hits", gorm.Expr("comment_hits + ?", 1)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if comment.ParentId != 0 {
|
||||
// 更新被评论的那条评论的计数
|
||||
err = tx.Model(&CmsComment{}).Where("id = ?", comment.ParentId).UpdateColumn("comment_hits", gorm.Expr("comment_hits + ?", 1)).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func ModifyComment(comment *CmsComment) (err error) {
|
||||
return impl.DBService.Where("identity = ?", comment.Identity).Updates(comment).Error
|
||||
}
|
||||
|
||||
func DeleteComment(identity string) (err error) {
|
||||
// get comment
|
||||
comment := new(CmsComment)
|
||||
err = impl.DBService.Where("identity = ?", identity).First(comment).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 删除评论,更新文章评论量
|
||||
if err := tx.Where("identity = ?", identity).Delete(&CmsComment{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&CmsPost{}).Where("identity = ?", comment.PostIdentity).
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if comment.ParentId != 0 {
|
||||
// 更新被评论的那条评论的计数
|
||||
if err := tx.Model(&CmsComment{}).Where("id = ?", comment.ParentId).
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 删除子级评论
|
||||
err = tx.Where("parent_id = ?", comment.ID).Delete(&CmsComment{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// IncrOrDescCommentField 增加或减少评论字段值
|
||||
// 用于更新评论的点赞、踩赞等计数字段
|
||||
// 参数:
|
||||
// - identity: 评论唯一标识
|
||||
// - column: 要更新的字段名
|
||||
// - desc: true表示减少,false表示增加
|
||||
//
|
||||
// 返回:
|
||||
// - error: 错误信息
|
||||
//
|
||||
// TODO: 记录点赞对象避免重复点赞
|
||||
func IncrOrDescCommentField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
}
|
||||
if err = impl.DBService.Model(&CmsComment{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CategoryList 文章分类列表
|
||||
func CategoryList() (list []CmsCategory, cnt int64, err error) {
|
||||
result := impl.DBService.Preload("Children").Find(&list)
|
||||
if result.Error != nil {
|
||||
err = result.Error
|
||||
return
|
||||
}
|
||||
cnt = result.RowsAffected
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteCategory 删除分类
|
||||
func DeleteCategory(identity string) (err error) {
|
||||
var (
|
||||
cnt int64 = 0
|
||||
category CmsCategory
|
||||
)
|
||||
err = impl.DBService.Model(&CmsCategory{}).Where("identity = ?", identity).Find(&category).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
impl.DBService.Model(&CmsCategory{}).Where("parent_id = ?", category.ID).Count(&cnt)
|
||||
if cnt != 0 {
|
||||
return errcode.NewError(500, "ErrCategoryHasSubcategories")
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("identity = ?", identity).Delete(&CmsCategory{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user