100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
package sys
|
|
|
|
import (
|
|
"SciencesServer/app/api/admin/model"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/session"
|
|
"errors"
|
|
)
|
|
|
|
type Industry struct {
|
|
*session.Admin
|
|
}
|
|
|
|
type IndustryHandle func(session *session.Admin) *Industry
|
|
|
|
type (
|
|
// IndustryInfo 行业领域信息
|
|
IndustryInfo struct {
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parent_id"`
|
|
Name string `json:"name"`
|
|
Children []*IndustryInfo `json:"children"`
|
|
}
|
|
// IndustryParams 行业领域参数信息
|
|
IndustryParams struct {
|
|
ID, PatentID uint64
|
|
Name string
|
|
}
|
|
)
|
|
|
|
func (c *Industry) tree(src []*model2.SysIndustry, parentID uint64) []*IndustryInfo {
|
|
out := make([]*IndustryInfo, 0)
|
|
|
|
for _, v := range src {
|
|
if v.ParentID == parentID {
|
|
out = append(out, &IndustryInfo{
|
|
ID: v.GetEncodeID(),
|
|
ParentID: (&model2.Model{ID: v.ParentID}).GetEncodeID(),
|
|
Name: v.Name,
|
|
Children: c.tree(src, v.ID),
|
|
})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Instance 首页信息
|
|
func (c *Industry) Instance() ([]*IndustryInfo, error) {
|
|
mSysIndustry := model.NewSysIndustry()
|
|
out := make([]*model2.SysIndustry, 0)
|
|
|
|
if err := model2.ScanFields(mSysIndustry.SysIndustry, &out, []string{"id", "parent_id", "name"}); err != nil {
|
|
return nil, err
|
|
}
|
|
return c.tree(out, 0), nil
|
|
}
|
|
|
|
// Form 数据操作
|
|
func (c *Industry) Form(params *IndustryParams) error {
|
|
mSysIndustry := model.NewSysIndustry()
|
|
|
|
if params.ID > 0 {
|
|
mSysIndustry.ID = params.ID
|
|
|
|
if isExist, err := model2.First(mSysIndustry.SysIndustry); err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,行业信息不存在或已被删除")
|
|
}
|
|
}
|
|
mSysIndustry.ParentID = params.PatentID
|
|
mSysIndustry.Name = params.Name
|
|
|
|
if mSysIndustry.ID > 0 {
|
|
return model2.Updates(mSysIndustry.SysIndustry, mSysIndustry.SysIndustry)
|
|
}
|
|
return model2.Create(mSysIndustry.SysIndustry)
|
|
}
|
|
|
|
// Delete 删除操作
|
|
func (c *Industry) Delete(id uint64) error {
|
|
mSysIndustry := model.NewSysIndustry()
|
|
mSysIndustry.ID = id
|
|
|
|
isExist, err := model2.FirstField(mSysIndustry.SysIndustry, []string{"id"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,行业信息不存在或已被删除")
|
|
}
|
|
return model2.Delete(mSysIndustry.SysIndustry)
|
|
}
|
|
|
|
func NewIndustry() IndustryHandle {
|
|
return func(session *session.Admin) *Industry {
|
|
return &Industry{session}
|
|
}
|
|
}
|