242 lines
7.2 KiB
Go
242 lines
7.2 KiB
Go
package manage
|
||
|
||
import (
|
||
"SciencesServer/app/api/admin/model"
|
||
"SciencesServer/app/basic/config"
|
||
"SciencesServer/app/basic/controller"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/service"
|
||
"SciencesServer/app/session"
|
||
config2 "SciencesServer/config"
|
||
"errors"
|
||
"strings"
|
||
)
|
||
|
||
// Research 研究机构
|
||
type Research struct {
|
||
*session.Admin
|
||
}
|
||
|
||
type ResearchHandle func(session *session.Admin) *Research
|
||
|
||
type (
|
||
// ResearchInstance 科研机构信息
|
||
ResearchInstance struct {
|
||
ID string `json:"id"`
|
||
*model.ManageResearchInfo
|
||
Industrys []string `json:"industrys"`
|
||
Researchs []string `json:"researchs"`
|
||
Address string `json:"address"`
|
||
Area string `json:"area"`
|
||
}
|
||
// ResearchSelect 科研机构筛选信息
|
||
ResearchSelect struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
}
|
||
// ResearchDetail 科研机构详细信息
|
||
ResearchDetail struct {
|
||
ID string `json:"id"`
|
||
TenantID string `json:"tenant_id"`
|
||
*model2.ManageResearch
|
||
Industrys []*config.Industry `json:"industrys"`
|
||
Keywords []string `json:"keywords"`
|
||
Researchs []string `json:"researchs"`
|
||
}
|
||
)
|
||
|
||
// Instance 首页信息
|
||
func (c *Research) Instance(tenantID uint64, name string, examineStatus, page, pageSize int) (*controller.ReturnPages, error) {
|
||
mManageResearch := model.NewManageResearch()
|
||
|
||
where := make([]*model2.ModelWhere, 0)
|
||
|
||
if c.TenantID > 0 {
|
||
where = append(where, model2.NewWhere("r.tenant_id", c.TenantID))
|
||
}
|
||
if tenantID > 0 {
|
||
where = append(where, model2.NewWhere("r.tenant_id", tenantID))
|
||
}
|
||
if name != "" {
|
||
where = append(where, model2.NewWhereLike("r.name", name))
|
||
}
|
||
if examineStatus > 0 {
|
||
where = append(where, model2.NewWhere("r.examine_status", examineStatus))
|
||
}
|
||
var count int64
|
||
|
||
out, err := mManageResearch.Researchs(page, pageSize, &count, where...)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
list := make([]*ResearchInstance, 0)
|
||
|
||
for _, v := range out {
|
||
mManageResearch.Industry = v.Industry
|
||
mManageResearch.ManageResearch.Research = v.Research
|
||
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range mManageResearch.GetIndustryAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", ">").Value)
|
||
}
|
||
list = append(list, &ResearchInstance{
|
||
ID: v.GetEncodeID(), ManageResearchInfo: v,
|
||
Industrys: _industrys,
|
||
Researchs: mManageResearch.GetResearchAttribute(),
|
||
Address: v.FormatBasic(), Area: (&model2.Area{
|
||
Province: v.TenantProvince,
|
||
City: v.TenantCity,
|
||
}).FormatBasic(),
|
||
})
|
||
}
|
||
return &controller.ReturnPages{Data: list, Count: count}, nil
|
||
}
|
||
|
||
// Select 筛选信息
|
||
func (c *Research) Select(tenantID uint64) ([]*ResearchSelect, error) {
|
||
if tenantID <= 0 {
|
||
tenantID = c.TenantID
|
||
}
|
||
mManageResearch := model.NewManageResearch()
|
||
|
||
out := make([]*model2.ManageResearch, 0)
|
||
|
||
where := []*model2.ModelWhereOrder{
|
||
&model2.ModelWhereOrder{Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree)},
|
||
}
|
||
if tenantID > 0 {
|
||
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhere("tenant_id", tenantID)})
|
||
}
|
||
|
||
if err := model2.ScanFields(mManageResearch.ManageResearch, &out, []string{"id", "name"}, where...); err != nil {
|
||
return nil, err
|
||
}
|
||
list := make([]*ResearchSelect, 0)
|
||
|
||
for _, v := range out {
|
||
list = append(list, &ResearchSelect{
|
||
ID: v.GetEncodeID(),
|
||
Name: v.Name,
|
||
})
|
||
}
|
||
return list, nil
|
||
}
|
||
|
||
// Form 数据操作
|
||
func (c *Research) Form(params *BasicParams, other *config.IdentityForResearch) error {
|
||
mManageResearch := model.NewManageResearch()
|
||
|
||
if params.ID > 0 {
|
||
mManageResearch.ID = params.ID
|
||
isExist, err := model2.FirstField(mManageResearch.ManageResearch, []string{"id", "name", "code", "tenant_id", "examine_status", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,研究机构信息不存在或已被删除")
|
||
}
|
||
if c.TenantID > 0 {
|
||
if mManageResearch.TenantID != c.TenantID {
|
||
return errors.New("操作错误,无权限操作")
|
||
}
|
||
} else {
|
||
if mManageResearch.Code != params.Code {
|
||
if isExist, err = params.isExist(mManageResearch.ManageResearch,
|
||
model2.NewWhere("code", params.Code)); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,当前站点下已存在同一研究机构代码")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
mManageResearch.TenantID = c.TenantID
|
||
mManageResearch.Image.Image = params.Image
|
||
mManageResearch.Name = params.Name
|
||
mManageResearch.Code = params.Code
|
||
mManageResearch.Area = model2.Area{
|
||
Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
|
||
}
|
||
mManageResearch.SetIndustryAttribute(params.Industrys)
|
||
mManageResearch.SetKeywordAttribute(params.Keywords)
|
||
mManageResearch.SetResearchAttribute(other.Researchs)
|
||
mManageResearch.Introduce = params.Introduce
|
||
|
||
if c.TenantID <= 0 {
|
||
mManageResearch.TenantID = params.TenantID
|
||
}
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range params.Industrys {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "-").Value)
|
||
}
|
||
manage := service.NewESManage(
|
||
service.WithManageIdentity(config.TenantUserIdentityForResearch),
|
||
service.WithManageIndustry(strings.Join(_industrys, ";")),
|
||
service.WithManageKeyword(strings.Join(params.Keywords, ";")),
|
||
service.WithManageResearch(strings.Join(other.Researchs, ";")),
|
||
)
|
||
|
||
if mManageResearch.ID > 0 {
|
||
if err := model2.Updates(mManageResearch.ManageResearch, mManageResearch.ManageResearch); err != nil {
|
||
return err
|
||
}
|
||
if mManageResearch.ExamineStatus == model2.ExamineStatusForAgree {
|
||
_ = manage.Update()
|
||
}
|
||
return nil
|
||
}
|
||
mManageResearch.Name = params.Name
|
||
mManageResearch.Code = params.Code
|
||
mManageResearch.License = other.FilterLicense()
|
||
mManageResearch.ExamineStatus = model2.ExamineStatusForAgree
|
||
mManageResearch.ExamineRemark = "主动创建,无需审核"
|
||
|
||
if isExist, err := params.isExist(mManageResearch.ManageResearch, model2.NewWhere("code", params.Code)); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,已存在同一研究机构代码")
|
||
}
|
||
if err := model2.Create(mManageResearch.ManageResearch); err != nil {
|
||
return err
|
||
}
|
||
service.WithManageID(mManageResearch.ID)(manage)
|
||
service.WithManageTitle(params.Name)(manage)
|
||
return manage.Create()
|
||
|
||
}
|
||
|
||
// Detail 详细信息
|
||
func (c *Research) Detail(id uint64) (*ResearchDetail, error) {
|
||
mManageResearch := model.NewManageResearch()
|
||
|
||
out, err := mManageResearch.Research(id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if out == nil {
|
||
return nil, errors.New("操作错误,科研机构信息不存在或已被删除")
|
||
}
|
||
_industrys := make([]*config.Industry, 0)
|
||
|
||
for _, v := range out.GetIndustryAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", ">"))
|
||
}
|
||
out.Image.Image = out.Image.Analysis(config2.SystemConfig[config2.SysImageDomain])
|
||
out.License = (&model2.Image{Image: out.License}).Analysis(config2.SystemConfig[config2.SysImageDomain])
|
||
|
||
return &ResearchDetail{
|
||
ID: out.GetEncodeID(), TenantID: out.GetEncodeTenantID(),
|
||
ManageResearch: out, Industrys: _industrys,
|
||
Keywords: out.GetKeywordAttribute(), Researchs: out.GetResearchAttribute(),
|
||
}, nil
|
||
}
|
||
|
||
func NewResearch() ResearchHandle {
|
||
return func(session *session.Admin) *Research {
|
||
return &Research{session}
|
||
}
|
||
}
|