feat:完善项目信息
This commit is contained in:
@ -9,6 +9,39 @@ import (
|
||||
|
||||
type Sys struct{}
|
||||
|
||||
func (*Sys) Industry(c *gin.Context) {
|
||||
data, err := sys.NewIndustry()(api.GetSession()(c).(*session.Admin)).Instance()
|
||||
api.APIResponse(err, data)(c)
|
||||
}
|
||||
|
||||
func (*Sys) IndustryForm(c *gin.Context) {
|
||||
form := &struct {
|
||||
api.IDStringForm
|
||||
ParentID string `json:"parent_id" form:"parent_id"`
|
||||
Name string `json:"name" form:"name"`
|
||||
}{}
|
||||
if err := api.Bind(form)(c); err != nil {
|
||||
api.APIFailure(err.(error))(c)
|
||||
return
|
||||
}
|
||||
err := sys.NewIndustry()(api.GetSession()(c).(*session.Admin)).Form(&sys.IndustryParams{
|
||||
ID: form.IDStringForm.Convert(), PatentID: (&api.IDStringForm{ID: form.ParentID}).Convert(),
|
||||
Name: form.Name,
|
||||
})
|
||||
api.APIResponse(err)(c)
|
||||
}
|
||||
|
||||
func (*Sys) IndustryDelete(c *gin.Context) {
|
||||
form := new(api.IDStringForm)
|
||||
|
||||
if err := api.Bind(form)(c); err != nil {
|
||||
api.APIFailure(err.(error))(c)
|
||||
return
|
||||
}
|
||||
err := sys.NewIndustry()(api.GetSession()(c).(*session.Admin)).Delete(form.Convert())
|
||||
api.APIResponse(err)(c)
|
||||
}
|
||||
|
||||
func (*Sys) Navigation(c *gin.Context) {
|
||||
form := new(api.TenantIDStringForm)
|
||||
|
||||
|
99
app/api/admin/controller/sys/industry.go
Normal file
99
app/api/admin/controller/sys/industry.go
Normal file
@ -0,0 +1,99 @@
|
||||
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 := new(model.SysIndustry)
|
||||
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 := new(model.SysIndustry)
|
||||
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}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user