Files
2022-01-18 09:20:18 +08:00

86 lines
2.0 KiB
Go

package sys
import (
"SciencesServer/app/api/website/model"
model2 "SciencesServer/app/common/model"
)
type About struct {
tenantID uint64
}
type AboutHandle func(tenantID uint64) *About
type (
// AboutInfo 基本信息
AboutInfo struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Children []*AboutInfo `json:"children"`
}
// AboutNavigationInfo 导航栏信息
AboutNavigationInfo struct {
ID string `json:"id"`
Title string `json:"title"`
}
)
func (c *About) tree(src []*model2.SysAbout, parentID uint64) []*AboutInfo {
out := make([]*AboutInfo, 0)
for _, v := range src {
if v.ParentID == parentID {
out = append(out, &AboutInfo{
ID: v.GetEncodeID(),
Title: v.Title,
Content: v.Content,
Children: nil,
})
}
}
return out
}
// Instance 关于信息
func (c *About) Instance() ([]*AboutInfo, error) {
mSysAbout := model.NewSysAbout()
out := make([]*model2.SysAbout, 0)
if err := model2.ScanFields(mSysAbout.SysAbout, &out, []string{"id", "title", "content"}); err != nil {
return nil, err
}
return c.tree(out, 0), nil
}
// Navigation 导航栏
func (c *About) Navigation() ([]*AboutNavigationInfo, error) {
mSysAbout := model.NewSysAbout()
out := make([]*model2.SysAbout, 0)
if err := model2.ScanFields(mSysAbout.SysAbout, &out, []string{"id", "title"}, &model2.ModelWhereOrder{
Where: model2.NewWhere("parent_id", 0),
Order: model2.NewOrder("sort", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{
Where: model2.NewWhere("tenant_id", c.tenantID),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}); err != nil {
return nil, err
}
list := make([]*AboutNavigationInfo, 0)
for _, v := range out {
list = append(list, &AboutNavigationInfo{
ID: v.GetEncodeID(), Title: v.Title,
})
}
return list, nil
}
func NewAbout() AboutHandle {
return func(tenantID uint64) *About {
return &About{tenantID: tenantID}
}
}