You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

206 lines
5.5 KiB

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 {
}
func init() {
Category := New()
service.RegisterCategory(Category)
}
func New() *sCategory {
return &sCategory{}
}
// GetTree 获取树
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, 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
}
//_, 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, 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, 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
}