Compare commits

...

4 Commits

@ -11,7 +11,7 @@ type Category struct {
Id int64 `form:"id" json:"id,omitempty" gorm:"primaryKey;autoIncrement;column:id;comment:;"`
Icon string `form:"icon" json:"icon,omitempty" gorm:"column:icon;comment:分类图标;type:varchar(255);size:255;"`
Cover string `form:"cover" json:"cover,omitempty" gorm:"column:cover;comment:分类封面;type:varchar(255);size:255;"`
ParentId int `form:"parent_id" json:"parent_id,omitempty" gorm:"column:parent_id;type:int(11);size:11;default:0;index:parent_id_title,unique;index:parent_id;comment:上级ID;"`
ParentId int64 `form:"parent_id" json:"parent_id,omitempty" gorm:"column:parent_id;type:int(11);size:11;default:0;index:parent_id_title,unique;index:parent_id;comment:上级ID;"`
Title string `form:"title" json:"title,omitempty" gorm:"column:title;type:varchar(64);size:64;index:parent_id_title,unique;comment:分类名称;"`
DocCount int `form:"doc_count" json:"doc_count,omitempty" gorm:"column:doc_count;type:int(11);size:11;default:0;comment:文档统计;"`
Sort int `form:"sort" json:"sort,omitempty" gorm:"column:sort;type:int(11);size:11;default:0;index:idx_sort;comment:排序,值越大越靠前;"`
@ -41,7 +41,95 @@ func (m *DBModel) CreateCategory(category *Category) (err error) {
// UpdateCategory 更新Category如果需要更新指定字段则请指定updateFields参数
func (m *DBModel) UpdateCategory(category *Category, updateFields ...string) (err error) {
db := m.db.Model(category)
var existCategory Category
err = m.db.Where("id = ?", category.Id).Find(&existCategory).Error
if err != nil && err != gorm.ErrRecordNotFound {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
tx := m.db.Begin()
defer func() {
if err != nil {
tx.Rollback()
return
}
tx.Commit()
}()
// 如果更新了父分类,则需要更新文档统计
if existCategory.ParentId != category.ParentId {
var (
oldParentIds, newParentIds, documentIds []int64
docCates []DocumentCategory
limit = 100
page = 1
modelDocCate = &DocumentCategory{}
modelCate = &Category{}
)
oldParentIds = m.GetCategoryParentIds(existCategory.Id)
if category.ParentId > 0 {
newParentIds = append(newParentIds, category.ParentId)
newParentIds = append(newParentIds, m.GetCategoryParentIds(category.ParentId)...)
}
// 获取需要更新的文档ID
for {
m.db.Model(modelDocCate).Select("document_id", "category_id").Where("category_id = ?", existCategory.Id).Limit(limit).Offset((page - 1) * limit).Find(&docCates)
if len(docCates) == 0 {
break
}
page++
for _, v := range docCates {
documentIds = append(documentIds, v.DocumentId)
}
if len(oldParentIds) > 0 {
// 删除旧的分类关联
err = tx.Model(modelDocCate).Where("category_id in (?) and document_id in (?)", oldParentIds, documentIds).Delete(modelDocCate).Error
if err != nil {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
// 更新旧的分类统计
err = tx.Model(modelCate).Where("id in (?)", oldParentIds).Update("doc_count", gorm.Expr("doc_count - ?", len(documentIds))).Error
if err != nil {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
}
if len(newParentIds) > 0 {
var newDocCates []DocumentCategory
for _, documentId := range documentIds {
for _, newCateId := range newParentIds {
newDocCates = append(newDocCates, DocumentCategory{
DocumentId: documentId,
CategoryId: newCateId,
})
}
}
// 创建新的分类关联
err = tx.Create(&newDocCates).Error
if err != nil {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
// 更新新的分类统计
err = tx.Model(modelCate).Where("id in (?)", newParentIds).Update("doc_count", gorm.Expr("doc_count + ?", len(documentIds))).Error
if err != nil {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
}
}
}
db := tx.Model(category)
updateFields = m.FilterValidFields(Category{}.TableName(), updateFields...)
if len(updateFields) > 0 { // 更新指定字段
@ -53,6 +141,7 @@ func (m *DBModel) UpdateCategory(category *Category, updateFields ...string) (er
err = db.Where("id = ?", category.Id).Updates(category).Error
if err != nil {
m.logger.Error("UpdateCategory", zap.Error(err))
return
}
if category.Cover != "" {
@ -79,7 +168,7 @@ func (m *DBModel) GetCategory(id interface{}, fields ...string) (category Catego
}
// GetCategoryByParentIdTitle(parentId int, title string, fields ...string) 根据唯一索引获取Category
func (m *DBModel) GetCategoryByParentIdTitle(parentId int, title string, fields ...string) (category Category, err error) {
func (m *DBModel) GetCategoryByParentIdTitle(parentId int64, title string, fields ...string) (category Category, err error) {
db := m.db
fields = m.FilterValidFields(Category{}.TableName(), fields...)
@ -177,3 +266,19 @@ func (m *DBModel) CountCategory() (count int64, err error) {
}
return
}
// 查询指定分类ID的所有父级分类ID
func (m *DBModel) GetCategoryParentIds(id int64) (ids []int64) {
var category Category
err := m.db.Model(&Category{}).Select("id", "parent_id").Where("id = ?", id).Find(&category).Error
if err != nil && err != gorm.ErrRecordNotFound {
m.logger.Error("GetCategoryParentIds", zap.Error(err), zap.Int64("id", id))
return
}
if category.ParentId > 0 {
ids = append(ids, category.ParentId)
ids = append(ids, m.GetCategoryParentIds(category.ParentId)...)
}
return
}

@ -692,7 +692,7 @@ func (m *DBModel) initConfig() (err error) {
{Category: ConfigCategoryFooter, Name: ConfigFooterFeedback, Label: "意见反馈", Value: "/article/feedback", Placeholder: "请输入意见反馈的链接地址,留空表示不显示", InputType: InputTypeText, Sort: 28, Options: ""},
// 转换配置项
{Category: ConfigCategoryConverter, Name: ConfigConverterMaxPreview, Label: "最大预览页数", Value: "0", Placeholder: "文档允许的最大预览页数0表示不限制", InputType: InputTypeNumber, Sort: 15, Options: ""},
{Category: ConfigCategoryConverter, Name: ConfigConverterMaxPreview, Label: "最大预览页数", Value: "12", Placeholder: "文档允许的最大预览页数0表示不限制", InputType: InputTypeNumber, Sort: 15, Options: ""},
{Category: ConfigCategoryConverter, Name: ConfigConverterTimeout, Label: "转换超时(分钟)", Value: "30", Placeholder: "文档转换超时时间默认为30分钟", InputType: InputTypeNumber, Sort: 16, Options: ""},
{Category: ConfigCategoryConverter, Name: ConfigConverterEnableGZIP, Label: "是否启用GZIP压缩", Value: "true", Placeholder: "是否对文档SVG预览文件启用GZIP压缩启用后转换效率会【稍微】下降但相对直接的SVG文件减少75%的存储空间", InputType: InputTypeSwitch, Sort: 17, Options: ""},
{Category: ConfigCategoryConverter, Name: ConfigConverterEnableSVGO, Label: "是否启用SVGO", Value: "false", Placeholder: "是否对文档SVG预览文件启用SVGO压缩启用后转换效率会【明显】下降但相对直接的SVG文件减少50%左右的存储空间", InputType: InputTypeSwitch, Sort: 18, Options: ""},

@ -150,7 +150,10 @@
<nuxt />
</el-main>
<el-footer>
<div v-if="$route.path === '/'" class="footer-friendlink">
<div
v-if="$route.path === '/' && friendlinks.length > 0"
class="footer-friendlink"
>
<el-card class="box-card" shadow="never">
<div slot="header" class="clearfix">
<strong></strong>

@ -131,9 +131,9 @@ export default {
this.search = { ...this.search, ...search, page: 1 }
if (
location.pathname + location.search ===
        this.$router.resolve({
          query: this.search,
        }).href
this.$router.resolve({
query: this.search,
}).href
) {
this.listCategory()
} else {
@ -240,7 +240,7 @@ export default {
}
this.tableListFields = [
{ prop: 'title', label: '', minWidth: 120, fixed: 'left' },
{ prop: 'icon', label: '', width: 48, type: 'image' },
{ prop: 'id', label: 'ID', width: 80, type: 'number', fixed: 'left' },
{
prop: 'enable',
label: '',
@ -259,9 +259,9 @@ export default {
width: 80,
type: 'number',
},
{ prop: 'icon', label: '', width: 48, type: 'image' },
{ prop: 'cover', label: '', width: 100, type: 'image' },
{ prop: 'doc_count', label: '', width: 80, type: 'number' },
{ prop: 'id', label: 'ID', width: 80, type: 'number' },
{ prop: 'description', label: '', minWidth: 200 },
{ prop: 'created_at', label: '', width: 160, type: 'datetime' },
{ prop: 'updated_at', label: '', width: 160, type: 'datetime' },

@ -512,6 +512,33 @@ export default {
.keywords {
.el-card__body {
padding-bottom: 10px;
max-height: 480px;
box-sizing: border-box;
overflow: auto;
/*定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
&::-webkit-scrollbar {
background-color: transparent;
width: 6px;
height: 6px;
}
&:hover::-webkit-scrollbar {
background-color: rgb(241, 241, 241);
}
/*定义滚动条轨道 内阴影+圆角*/
&::-webkit-scrollbar-track {
background-color: transparent;
}
/*定义滑块 内阴影+圆角*/
&::-webkit-scrollbar-thumb {
background-color: transparent;
border-radius: 3px;
}
&:hover::-webkit-scrollbar-thumb {
background-color: rgb(193, 193, 193);
}
&:hover::-webkit-scrollbar-thumb::hover {
background-color: rgb(168, 168, 168);
}
}
a {
margin-right: 10px;

Loading…
Cancel
Save