Browse Source

分类管理

master
chenyang 9 months ago
parent
commit
4c5e86d5ab
  1. 1
      app/api/category/category.go
  2. 45
      app/api/category/v1/category.go
  3. 7
      app/controller/category/category_v1_batch_delete.go
  4. 7
      app/controller/category/category_v1_create.go
  5. 7
      app/controller/category/category_v1_delete.go
  6. 7
      app/controller/category/category_v1_get_list.go
  7. 14
      app/controller/category/category_v1_get_tree.go
  8. 7
      app/controller/category/category_v1_update.go
  9. 170
      app/logic/category/category.go
  10. 32
      app/model/category.go
  11. 10
      app/service/category.go

1
app/api/category/category.go

@ -11,6 +11,7 @@ import (
)
type ICategoryV1 interface {
GetTree(ctx context.Context, req *v1.GetTreeReq) (res *v1.GetTreeRes, err error)
GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error)
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)

45
app/api/category/v1/category.go

@ -2,6 +2,7 @@ package v1
import (
"github.com/gogf/gf/v2/frame/g"
"xgit.pub/module/cms/app/model"
"xgit.pub/st52/xcore/dto"
)
@ -29,7 +30,21 @@ type GetListRes struct {
// CreateReq 创建分类请求参数
type CreateReq struct {
g.Meta `path:"/category/create" method:"post" summary:"创建" tags:"分类"`
g.Meta `path:"/category/create" method:"post" summary:"创建" tags:"分类"`
ParentId int `json:"parent_id" description:"父节点"`
Name string `json:"name" description:"分类名字"`
Union string `json:"union" description:"关联"`
Sort int `json:"sort" description:"排序"`
Status int `json:"status" description:"状态"`
SeoTitle string `json:"seo_title" description:""`
SeoKey string `json:"seo_key" description:""`
SeoDes string `json:"seo_des" description:""`
TplIndex string `json:"tpl_index" description:"Index"`
TplList string `json:"tpl_list" description:"GetList"`
TplDetail string `json:"tpl_detail" description:"Detail"`
TplDown string `json:"tpl_down" description:"Down"`
TplPlay string `json:"tpl_play" description:"player"`
model.CategoryExtend
}
// CreateRes 创建分类返回参数
@ -37,7 +52,22 @@ type CreateRes struct{}
// UpdateReq 更新分类请求参数
type UpdateReq struct {
g.Meta `path:"/category/update" method:"post" summary:"更新" tags:"分类"`
g.Meta `path:"/category/update" method:"post" summary:"更新" tags:"分类"`
Id int `json:"id" description:""`
ParentId int `json:"parent_id" description:"父节点"`
Name string `json:"name" description:"分类名字"`
Union string `json:"union" description:"关联"`
Sort int `json:"sort" description:"排序"`
Status int `json:"status" description:"状态"`
SeoTitle string `json:"seo_title" description:""`
SeoKey string `json:"seo_key" description:""`
SeoDes string `json:"seo_des" description:""`
TplIndex string `json:"tpl_index" description:"Index"`
TplList string `json:"tpl_list" description:"GetList"`
TplDetail string `json:"tpl_detail" description:"Detail"`
TplDown string `json:"tpl_down" description:"Down"`
TplPlay string `json:"tpl_play" description:"player"`
model.CategoryExtend
}
// UpdateRes 更新分类返回参数
@ -60,3 +90,14 @@ type BatchDeleteReq struct {
// BatchDeleteRes 批量删除分类返回参数
type BatchDeleteRes struct{}
// Drop 交换分类
type Drop struct {
g.Meta `path:"/category/drop" method:"post" summary:"交换" tags:"分类"`
Source int `json:"source"`
Target int `json:"target"`
Action string `json:"action"`
}
// DropRes 交换分类返回参数
type DropRes struct{}

7
app/controller/category/category_v1_batch_delete.go

@ -2,13 +2,12 @@ package category
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
err = service.Category().BatchDelete(ctx, req)
return
}

7
app/controller/category/category_v1_create.go

@ -2,13 +2,12 @@ package category
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
res, err = service.Category().Create(ctx, req)
return
}

7
app/controller/category/category_v1_delete.go

@ -2,13 +2,12 @@ package category
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
err = service.Category().Delete(ctx, req)
return
}

7
app/controller/category/category_v1_get_list.go

@ -2,13 +2,12 @@ package category
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
res, err = service.Category().GetList(ctx, req)
return
}

14
app/controller/category/category_v1_get_tree.go

@ -0,0 +1,14 @@
package category
import (
"context"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) GetTree(ctx context.Context, req *v1.GetTreeReq) (res *v1.GetTreeRes, err error) {
res = &v1.GetTreeRes{}
res.Data, err = service.Category().GetTree(ctx, req)
return
}

7
app/controller/category/category_v1_update.go

@ -2,13 +2,12 @@ package category
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"xgit.pub/module/cms/app/service"
"xgit.pub/module/cms/app/api/category/v1"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
err = service.Category().Update(ctx, req)
return
}

170
app/logic/category/category.go

@ -2,8 +2,17 @@ package category
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
v1 "xgit.pub/module/cms/app/api/category/v1"
"xgit.pub/module/cms/app/dao"
"xgit.pub/module/cms/app/model"
"xgit.pub/module/cms/app/model/entity"
"xgit.pub/module/cms/app/service"
"xgit.pub/st52/xcore/util/pinyin"
)
type sCategory struct {
@ -19,32 +28,179 @@ func New() *sCategory {
}
// GetTree 获取树
func (s *sCategory) GetTree(ctx context.Context, req *v1.GetTreeReq) (res *v1.GetTreeRes, err error) {
func (s *sCategory) GetTree(ctx context.Context, req *v1.GetTreeReq) (categories []*model.Category, err error) {
var rs []*model.Category
err = dao.Category.Ctx(ctx).OrderAsc(dao.Category.Columns().Sort).Scan(&rs)
if err != nil {
return
}
for _, r := range rs {
if r.Extend != "" {
if err = gjson.DecodeTo(r.Extend, &r.CategoryExtend); err != nil {
g.Log().Error(ctx, err)
return
}
}
if r.ParentId == 0 {
r.Children = s.getSubCategory(rs, r.Id)
categories = append(categories, r)
}
}
return
}
// GetList 获取列表
func (s *sCategory) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
res = &v1.GetListRes{}
tx := dao.Category.Ctx(ctx)
res.Total, err = tx.Count()
if err != nil {
return
}
var list []*entity.Category
err = tx.OrderAsc(dao.Category.Columns().Sort).Scan(list)
if err != nil {
return
}
res.Page = req.Page
res.PageSize = req.PageSize
res.Rows = list
return
}
// Create 创建
func (s *sCategory) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
err = dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if req.Union == "" {
req.Union, _ = pinyin.New(req.Name).Split("").Mode(pinyin.InitialsInCapitals).Convert()
}
m := entity.Category{}
if err = gconv.Struct(req, &m); err != nil {
return nil
}
if m.Extend, err = gjson.EncodeString(req.CategoryExtend); err != nil {
return nil
}
//id, err = dao.Category.Ctx(ctx).InsertAndGetId(in)
_, err = dao.Category.Ctx(ctx).InsertAndGetId(m)
return err
})
return
}
// Update 更新
func (s *sCategory) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
func (s *sCategory) Update(ctx context.Context, in *v1.UpdateReq) (err error) {
return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if in.Id == 0 {
return gerror.New("编号不能为空")
}
if in.Union == "" {
in.Union, _ = pinyin.New(in.Name).Split("").Mode(pinyin.InitialsInCapitals).Convert()
}
ump := g.Map{}
ump = gconv.Map(in)
ump[dao.Category.Columns().Extend], err = gjson.EncodeString(in.CategoryExtend)
if err != nil {
return nil
}
return
//_, err = dao.Category.Ctx(ctx).OmitEmpty().Data(in).Where(dao.Category.Columns().Id, in.Id).Update()
_, err = dao.Category.Ctx(ctx).OmitEmpty().Data(ump).Where(dao.Category.Columns().Id, in.Id).Update()
return err
})
}
// Delete 删除
func (s *sCategory) Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error) {
return
func (s *sCategory) Delete(ctx context.Context, in *v1.DeleteReq) (err error) {
return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if in.Id < 1 {
return gerror.New("编号不能为空")
}
_, err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, in.Id).Delete()
return nil
})
}
// BatchDelete 批量删除
func (s *sCategory) BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error) {
return
func (s *sCategory) BatchDelete(ctx context.Context, in *v1.BatchDeleteReq) (err error) {
return dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
if len(in.Ids) < 1 {
return gerror.New("编号不能为空")
}
_, err = dao.Category.Ctx(ctx).WhereIn(dao.Category.Columns().Id, in.Ids).Delete()
return nil
})
}
func (s *sCategory) Drop(ctx context.Context, source int, target int, action string) (err error) {
targetEntity := entity.Category{}
err = dao.Category.Ctx(ctx).Where(dao.Category.Columns().Id, target).Scan(&targetEntity)
err = dao.Category.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
if err != nil {
return err
}
if action == "inner" {
_, err = dao.Category.Ctx(ctx).
Data(dao.Category.Columns().ParentId, target).
Where(dao.Category.Columns().Id, source).Update()
if err != nil {
return err
}
}
if action == "before" {
if err != nil {
return err
}
_, err = dao.Category.Ctx(ctx).
Data(g.Map{
dao.Category.Columns().ParentId: targetEntity.ParentId,
dao.Category.Columns().Sort: targetEntity.Sort - 5,
}).
Where(dao.Category.Columns().Id, source).Update()
if err != nil {
return err
}
}
if action == "after" {
_, err = dao.Category.Ctx(ctx).
Data(g.Map{
dao.Category.Columns().ParentId: targetEntity.ParentId,
dao.Category.Columns().Sort: targetEntity.Sort + 5,
}).
Where(dao.Category.Columns().Id, source).Update()
if err != nil {
return err
}
}
return
})
var menus []entity.Category
err = dao.Category.Ctx(ctx).
Where(dao.Category.Columns().ParentId, targetEntity.ParentId).
OrderAsc(dao.Category.Columns().Sort).
Scan(&menus)
if err != nil {
return
}
for i, menu := range menus {
_, _ = dao.Category.Ctx(ctx).
Data(g.Map{
dao.Category.Columns().Sort: (i + 1) * 10,
}).
Where(dao.Category.Columns().Id, menu.Id).Update()
}
return err
}
// getSubCategory 获取子分类
func (s *sCategory) getSubCategory(rs []*model.Category, id int) []*model.Category {
var children []*model.Category
for _, r := range rs {
if r.ParentId == id {
r.Children = s.getSubCategory(rs, r.Id)
children = append(children, r)
}
}
return children
}

32
app/model/category.go

@ -0,0 +1,32 @@
package model
type Category struct {
CategoryExtend
Id int `json:"id" description:""`
ParentId int `json:"parent_id" description:"父节点"`
Name string `json:"name" description:"分类名字"`
Union string `json:"union" description:"关联"`
Sort int `json:"sort" description:"排序"`
Status int `json:"status" description:"状态"`
SeoTitle string `json:"seo_title" description:""`
SeoKey string `json:"seo_key" description:""`
SeoDes string `json:"seo_des" description:""`
TplIndex string `json:"tpl_index" description:"Index"`
TplList string `json:"tpl_list" description:"GetList"`
TplDetail string `json:"tpl_detail" description:"Detail"`
TplDown string `json:"tpl_down" description:"Down"`
TplPlay string `json:"tpl_play" description:"player"`
Extend string `json:"-" description:"extend"`
Children []*Category `json:"children"`
}
type CategoryExtend struct {
Class string `json:"class" description:"扩展分类 逗号,分隔"`
Area string `json:"area" description:"扩展地区 逗号,分隔"`
Lang string `json:"lang" description:"扩展语言 逗号,分隔"`
Year string `json:"year" description:"扩展年份 逗号,分隔"`
Star string `json:"star" description:"扩展演员 逗号,分隔"`
Director string `json:"director" description:"扩展导演 逗号,分隔"`
State string `json:"state" description:"扩展资源 逗号,分隔"`
Version string `json:"version" description:"扩展版本 逗号,分隔"`
}

10
app/service/category.go

@ -9,22 +9,24 @@ import (
"context"
v1 "xgit.pub/module/cms/app/api/category/v1"
"xgit.pub/module/cms/app/model"
)
type (
ICategory interface {
// GetTree 获取树
GetTree(ctx context.Context, req *v1.GetTreeReq) (res *v1.GetTreeRes, err error)
GetTree(ctx context.Context, req *v1.GetTreeReq) (categories []*model.Category, err error)
// GetList 获取列表
GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error)
// Create 创建
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
// Update 更新
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
Update(ctx context.Context, in *v1.UpdateReq) (err error)
// Delete 删除
Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error)
Delete(ctx context.Context, in *v1.DeleteReq) (err error)
// BatchDelete 批量删除
BatchDelete(ctx context.Context, req *v1.BatchDeleteReq) (res *v1.BatchDeleteRes, err error)
BatchDelete(ctx context.Context, in *v1.BatchDeleteReq) (err error)
Drop(ctx context.Context, source int, target int, action string) (err error)
}
)

Loading…
Cancel
Save