200 lines
4.2 KiB
Go
200 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
"yintai-api/app/admin/request"
|
|
"yintai-api/app/common/dao"
|
|
err2 "yintai-api/app/common/err"
|
|
"yintai-api/app/common/model"
|
|
"yintai-api/base/global"
|
|
|
|
"github.com/spf13/cast"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Category struct{}
|
|
|
|
func (r *Category) Create(req request.CategoryCreate) int64 {
|
|
if cast.ToInt64(req.Pid) != 0 {
|
|
ok, err := dao.NewCategory().Exists(cast.ToInt64(req.Pid))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if !ok {
|
|
err2.NotEsxErr()
|
|
}
|
|
}
|
|
|
|
if !slices.Contains(model.CategoryTypes, req.Type) {
|
|
err2.ParamErr()
|
|
}
|
|
|
|
m := new(model.Category)
|
|
m.Pid = cast.ToInt64(req.Pid)
|
|
m.Name = req.Name
|
|
m.NameEn = req.NameEn
|
|
m.Type = req.Type
|
|
m.CreateTime = time.Now().Unix()
|
|
err := global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err := dao.NewCategory(tx).Create(m); err != nil {
|
|
return fmt.Errorf("<category create>: create category error: " + err.Error())
|
|
}
|
|
|
|
path := fmt.Sprintf("%d/", m.ID)
|
|
if m.Pid != 0 {
|
|
parent, err := dao.NewCategory(tx).Get(m.Pid)
|
|
if err != nil {
|
|
return fmt.Errorf("<category create>: create category error: " + err.Error())
|
|
}
|
|
|
|
path = fmt.Sprintf("%s%d/", parent.Path, m.ID)
|
|
}
|
|
|
|
m.Path = path
|
|
if err := dao.NewCategory(tx).Update(m); err != nil {
|
|
return fmt.Errorf("<category create>: update category path error: " + err.Error())
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
err2.DbErr(err.Error())
|
|
}
|
|
|
|
return m.ID
|
|
}
|
|
|
|
func (r *Category) Update(req request.CategoryUpdate) {
|
|
m, err := dao.NewCategory().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
if req.Name != "" {
|
|
m.Name = req.Name
|
|
}
|
|
if req.NameEn != "" {
|
|
m.NameEn = req.NameEn
|
|
}
|
|
if req.Type != "" {
|
|
if !slices.Contains(model.CategoryTypes, req.Type) {
|
|
err2.ParamErr()
|
|
}
|
|
m.Type = req.Type
|
|
}
|
|
|
|
oldPath := m.Path
|
|
oldPid := m.Pid
|
|
m.Pid = cast.ToInt64(req.Pid)
|
|
err = global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
path := fmt.Sprintf("%d/", m.ID)
|
|
if m.Pid != 0 {
|
|
if parentCate, err := dao.NewCategory(tx).Get(m.Pid); err != nil {
|
|
return fmt.Errorf("<category update>: get category error: " + err.Error())
|
|
} else {
|
|
path = fmt.Sprintf("%s%d/", parentCate.Path, m.ID)
|
|
}
|
|
}
|
|
|
|
m.Path = path
|
|
m.UpdateTime = time.Now().Unix()
|
|
if err = dao.NewCategory(tx).Update(m); err != nil {
|
|
return fmt.Errorf("<category update>: update category path error: " + err.Error())
|
|
}
|
|
|
|
if oldPid == m.Pid {
|
|
return nil
|
|
}
|
|
|
|
// 更新子级别
|
|
subCate, err := dao.NewCategory(tx).GetByPath(oldPath)
|
|
if err != nil {
|
|
return fmt.Errorf("<category update>: get sub category data error: " + err.Error())
|
|
}
|
|
|
|
for _, category := range subCate {
|
|
parentCate, _ := dao.NewCategory(tx).Get(category.Pid)
|
|
category.Path = fmt.Sprintf("%s%d/", parentCate.Path, category.ID)
|
|
category.UpdateTime = time.Now().Unix()
|
|
if err = dao.NewCategory(tx).Update(category); err != nil {
|
|
return fmt.Errorf("<category update>: update sub category path error: " + err.Error())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (r *Category) Delete(req request.CategoryDelete) {
|
|
m, err := dao.NewCategory().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
|
|
// 查询这个分类下的所有数据
|
|
ms, err := dao.NewCategory().GetByPath(m.Path)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if ms == nil {
|
|
err2.NotEsxErr()
|
|
}
|
|
ids := make([]int64, 0)
|
|
for _, category := range ms {
|
|
ids = append(ids, cast.ToInt64(category.ID))
|
|
}
|
|
|
|
relation, err := dao.NewCategoryRelation().GetByIds(ids...)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if relation != nil && len(relation) > 0 {
|
|
err2.InUseErr()
|
|
}
|
|
|
|
if err = dao.NewCategory().Delete(m.Path); err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Category) List(req request.CategoryList) (any, int64) {
|
|
if !slices.Contains(model.CategoryTypes, req.Type) {
|
|
err2.ParamErr()
|
|
}
|
|
|
|
req.Pagination()
|
|
lists, count, err := dao.NewCategory().List(cast.ToInt64(req.Pid), req.Name, req.NameEn, req.Type, req.Page, req.Size, req.Sort)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
res := make([]map[string]any, 0)
|
|
for _, list := range lists {
|
|
res = append(res, list.Format())
|
|
}
|
|
|
|
return res, count
|
|
}
|