文档分类增删改查管理

dev
truthhun 2 years ago
parent 62795b74c6
commit f1fb3c6a60

@ -2,6 +2,7 @@ package biz
import (
"context"
"strings"
pb "moredoc/api/v1"
"moredoc/middleware/auth"
@ -42,10 +43,25 @@ func (s *CategoryAPIService) CreateCategory(ctx context.Context, req *pb.Categor
cate := &model.Category{}
util.CopyStruct(req, &cate)
titles := strings.Split(cate.Title, "\n")
for _, title := range titles {
title = strings.TrimSpace(title)
if title == "" {
continue
}
err = s.dbModel.CreateCategory(cate)
if err != nil {
return nil, err
cate.Title = title
exist, _ := s.dbModel.GetCategoryByParentIdTitle(cate.ParentId, cate.Title, "id")
if exist.Id > 0 {
continue
}
cate.Id = 0
err = s.dbModel.CreateCategory(cate)
if err != nil {
s.logger.Error("CreateCategory", zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
}
}
return &emptypb.Empty{}, nil
@ -70,6 +86,11 @@ func (s *CategoryAPIService) UpdateCategory(ctx context.Context, req *pb.Categor
return nil, status.Error(codes.Internal, "分类名称已存在")
}
err = s.dbModel.UpdateCategory(cate)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &emptypb.Empty{}, nil
}
@ -106,7 +127,7 @@ func (s *CategoryAPIService) GetCategory(ctx context.Context, req *pb.GetCategor
func (s *CategoryAPIService) ListCategory(ctx context.Context, req *pb.ListCategoryRequest) (*pb.ListCategoryReply, error) {
opt := &model.OptionGetCategoryList{
WithCount: true,
WithCount: false,
QueryIn: make(map[string][]interface{}),
SelectFields: req.Field,
Page: int(req.Page),

@ -8,13 +8,12 @@ import (
)
type Category struct {
Id int `form:"id" json:"id,omitempty" gorm:"primaryKey;autoIncrement;column:id;comment:;"`
Id int64 `form:"id" json:"id,omitempty" gorm:"primaryKey;autoIncrement;column:id;comment:;"`
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;"`
Title string `form:"title" json:"title,omitempty" gorm:"column:title;type:varchar(64);size:64;index:parent_id_title,unique;comment:分类名称;"`
Cover string `form:"cover" json:"cover,omitempty" gorm:"column:cover;type:varchar(255);size:255;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;comment:排序,值越大越靠前;"`
Enable bool `form:"enable" json:"enable,omitempty" gorm:"column:enable;type:tinyint(1);size:1;default:1;"`
Sort int `form:"sort" json:"sort,omitempty" gorm:"column:sort;type:int(11);size:11;default:0;index:idx_sort;comment:排序,值越大越靠前;"`
Enable bool `form:"enable" json:"enable,omitempty" gorm:"column:enable;type:tinyint(1);size:1;index:idx_enable;default:1;"`
CreatedAt *time.Time `form:"created_at" json:"created_at,omitempty" gorm:"column:created_at;type:datetime;comment:创建时间;"`
UpdatedAt *time.Time `form:"updated_at" json:"updated_at,omitempty" gorm:"column:updated_at;type:datetime;comment:更新时间;"`
}
@ -124,7 +123,7 @@ func (m *DBModel) GetCategoryList(opt *OptionGetCategoryList) (categoryList []Ca
db = db.Offset((opt.Page - 1) * opt.Size).Limit(opt.Size)
err = db.Order("parent_id asc, sort desc").Find(&categoryList).Error
err = db.Order("enable desc, parent_id asc, sort desc").Find(&categoryList).Error
if err != nil && err != gorm.ErrRecordNotFound {
m.logger.Error("GetCategoryList", zap.Error(err))
}
@ -133,7 +132,23 @@ func (m *DBModel) GetCategoryList(opt *OptionGetCategoryList) (categoryList []Ca
// DeleteCategory 删除数据
// 分类下存在文档的分类,则不允许删除
// 查找子分类,如果子分类下存在文档,则不允许删除
func (m *DBModel) DeleteCategory(ids []int64) (err error) {
var (
children []Category
childrenIds []int64
)
m.db.Select("id").Where("parent_id in (?) and doc_count = ?", ids, 0).Find(&children)
if len(children) > 0 {
for _, v := range children {
childrenIds = append(childrenIds, v.Id)
}
if err = m.DeleteCategory(childrenIds); err != nil {
return
}
}
err = m.db.Where("id in (?) and doc_count = ?", ids, 0).Delete(&Category{}).Error
if err != nil {
m.logger.Error("DeleteCategory", zap.Error(err))

@ -125,6 +125,15 @@ export default {
}
this.loading = true
const category = { ...this.category }
if (category.parent_id) {
if (typeof category.parent_id === 'object') {
category.parent_id =
category.parent_id[category.parent_id.length - 1]
}
} else {
category.parent_id = 0
}
if (this.category.id > 0) {
const res = await updateCategory(category)
if (res.status === 200) {

@ -78,8 +78,14 @@ export default {
this.loading = true
const res = await listCategory(this.search)
if (res.status === 200) {
this.categories = res.data.category
this.trees = categoryToTrees(res.data.category)
let categories = res.data.category || []
categories = categories.map((item) => {
item.disable_delete = item.doc_count > 0
return item
})
this.categories = categories
this.trees = categoryToTrees(categories)
this.total = res.data.total
} else {
this.$message.error(res.data.message)
@ -99,7 +105,7 @@ export default {
this.listCategory()
},
onCreate() {
this.category = { id: 0 }
this.category = { id: 0, enable: true }
this.formVisible = true
},
async editRow(row) {
@ -117,7 +123,7 @@ export default {
},
batchDelete() {
this.$confirm(
`您确定要删除选中的【${this.selectedRow.length}个】分类吗?删除之后不可恢复!`,
`您确定要删除选中的【${this.selectedRow.length}个】分类吗?本次删除会连同子分类一起删除,删除之后不可恢复!`,
'温馨提示',
{
confirmButtonText: '确定',
@ -139,7 +145,7 @@ export default {
},
deleteRow(row) {
this.$confirm(
`您确定要删除分类【${row.title}】吗?删除之后不可恢复!`,
`您确定要删除分类【${row.title}】吗?本次删除会连同子分类一起删除,删除之后不可恢复!`,
'温馨提示',
{
confirmButtonText: '确定',

Loading…
Cancel
Save