197 lines
3.1 KiB
Go
197 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"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 Page struct{}
|
|
|
|
func (r *Page) Create(req request.PageCreate) int64 {
|
|
r.Pid(req.Pid)
|
|
|
|
m := new(model.Page)
|
|
m.Name = req.Name
|
|
m.Pid = cast.ToInt64(req.Pid)
|
|
m.Page = req.Page
|
|
m.Content = req.Extra
|
|
m.Weight = cast.ToInt64(req.Weight)
|
|
m.Tags = req.Tags
|
|
m.CreateTime = time.Now().Unix()
|
|
|
|
err := global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err := dao.NewPage(tx).Create(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
m.Path = r.Path(m.ID, m.Pid)
|
|
if err := dao.NewPage(tx).Update(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
// no category
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
return m.ID
|
|
}
|
|
|
|
func (r *Page) Path(id, pid int64) string {
|
|
p := ""
|
|
m, err := dao.NewPage().Get(cast.ToInt64(pid))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
p = fmt.Sprintf("/%d", id)
|
|
} else {
|
|
p = fmt.Sprintf("%s/%d", m.Path, id)
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
// Pid :pid check
|
|
func (r *Page) Pid(id string) {
|
|
idx := cast.ToInt64(id)
|
|
if idx == 0 {
|
|
return
|
|
}
|
|
|
|
e, err := dao.NewPage().Exists(idx)
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if !e {
|
|
err2.NotEsxErr()
|
|
}
|
|
}
|
|
|
|
func (r *Page) Update(req request.PageUpdate) {
|
|
r.Pid(req.Pid)
|
|
m, err := dao.NewPage().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
|
|
oldPid := m.Pid
|
|
oldPath := m.Path
|
|
|
|
if req.Name != "" {
|
|
m.Name = req.Name
|
|
}
|
|
|
|
if req.Page != "" {
|
|
m.Page = req.Page
|
|
}
|
|
|
|
if req.Extra != "" {
|
|
m.Content = req.Extra
|
|
}
|
|
|
|
if weight := cast.ToInt64(req.Weight); weight != 0 {
|
|
m.Weight = weight
|
|
}
|
|
|
|
if req.Tags != "" {
|
|
m.Tags = req.Tags
|
|
}
|
|
|
|
if pid := cast.ToInt64(req.Pid); pid != m.Pid {
|
|
if pid == m.ID {
|
|
pid = 0
|
|
}
|
|
|
|
m.Path = fmt.Sprintf("/%d", m.ID)
|
|
if pid != 0 {
|
|
m.Path = r.Path(m.ID, m.Pid)
|
|
}
|
|
|
|
m.Pid = pid
|
|
}
|
|
|
|
m.UpdateTime = time.Now().Unix()
|
|
|
|
err = global.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = dao.NewPage(tx).Update(m); err != nil {
|
|
return err
|
|
}
|
|
|
|
if oldPid == m.Pid {
|
|
return nil
|
|
}
|
|
|
|
// pid was updated
|
|
subs, err := dao.NewPage(tx).GetByPath(oldPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, sub := range subs {
|
|
parent, _ := dao.NewPage(tx).Get(sub.Pid)
|
|
sub.Path = fmt.Sprintf("%s/%d", parent.Path, sub.ID)
|
|
if err = dao.NewPage(tx).Update(sub); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (r *Page) Delete(req request.PageDelete) {
|
|
m, err := dao.NewPage().Get(cast.ToInt64(req.ID))
|
|
if err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
|
|
if m == nil {
|
|
err2.NotEsxErr()
|
|
return
|
|
}
|
|
|
|
if err = dao.NewPage().Delete(m.ID); err != nil {
|
|
err2.DbErr(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (r *Page) List(req request.PageList) (any, int64) {
|
|
req.Pagination()
|
|
lists, count, err := dao.NewPage().List(req.Name, 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
|
|
}
|