88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package technology
|
|
|
|
import (
|
|
"SciencesServer/app/api/admin/model"
|
|
"SciencesServer/app/basic/controller"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/session"
|
|
"errors"
|
|
)
|
|
|
|
type Project struct {
|
|
*session.Admin
|
|
}
|
|
|
|
type ProjectHandle func(session *session.Admin) *Project
|
|
|
|
type (
|
|
// ProjectInstance 项目信息
|
|
ProjectInstance struct {
|
|
ID string `json:"id"`
|
|
TenantID string `json:"tenant_id"`
|
|
*model2.TechnologyProject
|
|
Area string `json:"area"`
|
|
}
|
|
)
|
|
|
|
// Instance 首页信息
|
|
func (c *Project) Instance(tenantID uint64, title string, page, pageSize int) (*controller.ReturnPages, error) {
|
|
mTechnologyProject := model.NewTechnologyProject()
|
|
|
|
where := make([]*model2.ModelWhere, 0)
|
|
|
|
if c.TenantID > 0 {
|
|
where = append(where, model2.NewWhere("p.tenant_id", c.TenantID))
|
|
}
|
|
if tenantID > 0 {
|
|
where = append(where, model2.NewWhere("p.tenant_id", tenantID))
|
|
}
|
|
if title != "" {
|
|
where = append(where, model2.NewWhereLike("p.name", title))
|
|
}
|
|
var count int64
|
|
|
|
out, err := mTechnologyProject.Projects(page, pageSize, &count, where...)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]*ProjectInstance, len(out))
|
|
|
|
for k, v := range out {
|
|
list[k] = &ProjectInstance{
|
|
ID: v.GetEncodeID(), TenantID: v.GetEncodeTenantID(),
|
|
TechnologyProject: v.TechnologyProject, Area: v.FormatBasic(),
|
|
}
|
|
}
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
|
}
|
|
|
|
// Shelf 上下架操作
|
|
func (c *Project) Shelf(id uint64) error {
|
|
return controller.NewShelf(controller.WithShelfSessionAdmin(c.Admin)).Handle(model2.NewTechnologyProject(), id, nil)
|
|
}
|
|
|
|
// Delete 删除操作
|
|
func (c *Project) Delete(id uint64) error {
|
|
mTechnologyProject := model.NewTechnologyProject()
|
|
mTechnologyProject.ID = id
|
|
|
|
isExist, err := model2.FirstField(mTechnologyProject.TechnologyProject, []string{"id", "tenant_id"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,产品信息不存在或已被删除")
|
|
}
|
|
if c.TenantID > 0 && c.TenantID != mTechnologyProject.TenantID {
|
|
return errors.New("操作错误,无权限操作")
|
|
}
|
|
return model2.Delete(mTechnologyProject.TechnologyProject)
|
|
}
|
|
|
|
func NewProject() ProjectHandle {
|
|
return func(session *session.Admin) *Project {
|
|
return &Project{session}
|
|
}
|
|
}
|