158 lines
3.7 KiB
Go
158 lines
3.7 KiB
Go
package sys
|
|
|
|
import (
|
|
"SciencesServer/app/api/admin/model"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/session"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type About struct {
|
|
*session.Admin
|
|
}
|
|
|
|
type AboutHandle func(session *session.Admin) *About
|
|
|
|
type (
|
|
// AboutInfo 关于信息
|
|
AboutInfo struct {
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parent_id"`
|
|
Title string `json:"title"`
|
|
Area string `json:"area"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
Children []*AboutInfo `json:"children"`
|
|
}
|
|
// AboutDetailInfo 详细信息
|
|
AboutDetailInfo struct {
|
|
ID string `json:"id"`
|
|
TenantID string `json:"tenant_id"`
|
|
ParentID string `json:"parent_id"`
|
|
*model2.SysAbout
|
|
}
|
|
// AboutParams 关于参数信息
|
|
AboutParams struct {
|
|
ID, TenantID, ParentID uint64
|
|
Title, Content string
|
|
Sort int
|
|
}
|
|
)
|
|
|
|
func (c *About) tree(src []*model.SysAboutInfo, parentID uint64) []*AboutInfo {
|
|
out := make([]*AboutInfo, 0)
|
|
|
|
for _, v := range src {
|
|
if v.ParentID == parentID {
|
|
out = append(out, &AboutInfo{
|
|
ID: v.GetEncodeID(),
|
|
ParentID: (&model2.Model{ID: v.ParentID}).GetEncodeID(),
|
|
Title: v.Title,
|
|
Area: v.FormatBasic(),
|
|
UpdatedAt: v.UpdatedAt,
|
|
Children: c.tree(src, v.ID),
|
|
})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Instance 首页信息
|
|
func (c *About) Instance(tenantID uint64) ([]*AboutInfo, error) {
|
|
mSysAbout := model.NewSysAbout()
|
|
|
|
where := make([]*model2.ModelWhere, 0)
|
|
|
|
if c.TenantID > 0 {
|
|
where = append(where, model2.NewWhere("a.tenant_id", c.TenantID))
|
|
}
|
|
|
|
if tenantID > 0 {
|
|
where = append(where, model2.NewWhere("a.tenant_id", tenantID))
|
|
}
|
|
out, err := mSysAbout.About(where...)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c.tree(out, 0), nil
|
|
}
|
|
|
|
func (c *About) Detail(id uint64) (*AboutDetailInfo, error) {
|
|
mSysAbout := model.NewSysAbout()
|
|
mSysAbout.ID = id
|
|
|
|
isExist, err := model2.First(mSysAbout.SysAbout)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !isExist {
|
|
return nil, errors.New("操作错误,关于信息不存在或已被删除")
|
|
}
|
|
return &AboutDetailInfo{
|
|
ID: mSysAbout.GetEncodeID(),
|
|
TenantID: mSysAbout.GetEncodeTenantID(),
|
|
ParentID: (&model2.Model{ID: mSysAbout.ParentID}).GetEncodeID(),
|
|
SysAbout: mSysAbout.SysAbout,
|
|
}, nil
|
|
}
|
|
|
|
func (c *About) Form(params *AboutParams) error {
|
|
mSysAbout := model.NewSysAbout()
|
|
|
|
if params.ID > 0 {
|
|
mSysAbout.ID = params.ID
|
|
|
|
isExist, err := model2.FirstField(mSysAbout.SysAbout, []string{"id", "tenant_id", "created_at"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,关于信息不存在或已被删除")
|
|
}
|
|
if c.TenantID > 0 && c.TenantID != mSysAbout.TenantID {
|
|
return errors.New("操作错误,无权限操作")
|
|
}
|
|
}
|
|
mSysAbout.Title = params.Title
|
|
mSysAbout.ParentID = params.ParentID
|
|
mSysAbout.Content = params.Content
|
|
mSysAbout.Sort = params.Sort
|
|
|
|
if mSysAbout.ID > 0 {
|
|
if c.TenantID <= 0 {
|
|
mSysAbout.TenantID = params.TenantID
|
|
}
|
|
return model2.Updates(mSysAbout.SysAbout, mSysAbout.SysAbout)
|
|
}
|
|
mSysAbout.TenantID = params.TenantID
|
|
|
|
if c.TenantID > 0 {
|
|
mSysAbout.TenantID = c.TenantID
|
|
}
|
|
return model2.Create(mSysAbout.SysAbout)
|
|
}
|
|
|
|
func (c *About) Delete(id uint64) error {
|
|
mSysAbout := model.NewSysAbout()
|
|
mSysAbout.ID = id
|
|
|
|
isExist, err := model2.FirstField(mSysAbout.SysAbout, []string{"id", "tenant_id"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,关于信息不存在或已被删除")
|
|
}
|
|
if c.TenantID > 0 && mSysAbout.TenantID != c.TenantID {
|
|
return errors.New("操作错误,无权限操作")
|
|
}
|
|
return model2.Delete(mSysAbout.SysAbout)
|
|
}
|
|
|
|
func NewAbout() AboutHandle {
|
|
return func(session *session.Admin) *About {
|
|
return &About{session}
|
|
}
|
|
}
|