Files
2022-02-15 17:19:23 +08:00

315 lines
9.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"
"SciencesServer/utils"
"errors"
"strings"
"time"
)
type Expert struct {
*session.Admin
}
type ExpertHandle func(session *session.Admin) *Expert
type (
// ExpertInstance 专家信息
ExpertInstance struct {
ID string `json:"id"`
Name string `json:"name"`
Industrys []string `json:"industrys"`
ResearchName string `json:"research_name"`
LaboratoryName string `json:"laboratory_name"`
AchievementCount int `json:"achievement_count"`
PatentCount int `json:"patent_count"`
Address string `json:"address"`
model2.Examine
CreatedAt time.Time `json:"created_at"`
Area string `json:"area"`
}
// ExpertDetail 专家详细信息
ExpertDetail struct {
ID string `json:"id"`
TenantID string `json:"tenant_id"`
ResearchID string `json:"research_id"`
LaboratoryID string `json:"laboratory_id"`
*model2.ManageExpert
Industrys []*config.Industry `json:"industrys"`
Keywords []string `json:"keywords"`
Researchs []string `json:"researchs"`
Area string `json:"area"`
}
// ExpertPatent 专家专利信息
ExpertPatent struct {
ID string `json:"id"`
*model.ManageExpertPatent
IsBind bool `json:"is_bind"`
}
)
// Instance 首页信息
func (c *Expert) Instance(tenantID uint64, name string, examineStatus int, page, pageSize int) (*controller.ReturnPages, error) {
mManageExpert := model.NewManageExpert()
where := make([]*model2.ModelWhere, 0)
if c.TenantID > 0 {
where = append(where, model2.NewWhere("e.tenant_id", c.TenantID))
}
if tenantID > 0 {
where = append(where, model2.NewWhere("e.tenant_id", tenantID))
}
if name != "" {
where = append(where, model2.NewWhereLike("e.name", name))
}
if examineStatus > 0 {
where = append(where, model2.NewWhere("e.examine_status", examineStatus))
}
var count int64
out, err := mManageExpert.Experts(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*ExpertInstance, 0)
for _, v := range out {
_industrys := make([]string, 0)
for _, v := range v.GetIndustryAttribute() {
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "/").Value)
}
// 研究机构,实验室
list = append(list, &ExpertInstance{ID: v.GetEncodeID(), Name: v.Name, Industrys: _industrys,
AchievementCount: v.AchievementCount, PatentCount: v.PatentCount,
ResearchName: v.ResearchName, LaboratoryName: v.LaboratoryName, Examine: v.Examine,
Address: v.FormatBasic(), CreatedAt: v.CreatedAt, Area: (&model2.Area{
Province: v.TenantProvince,
City: v.TenantCity,
}).FormatBasic(),
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Expert) Detail(id uint64) (*ExpertDetail, error) {
mManageExpert := model.NewManageExpert()
out, err := mManageExpert.Expert(id)
if err != nil {
return nil, err
} else if out.ManageExpert == 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])
return &ExpertDetail{
ID: out.GetEncodeID(),
TenantID: out.GetEncodeTenantID(),
ResearchID: (&model2.Model{ID: out.ResearchID}).GetEncodeID(),
LaboratoryID: (&model2.Model{ID: out.LaboratoryID}).GetEncodeID(),
ManageExpert: out.ManageExpert,
Industrys: _industrys,
Keywords: out.GetKeywordAttribute(),
Researchs: out.GetResearchAttribute(),
}, nil
}
// Form 数据操作
func (c *Expert) Form(params *BasicParams, other *config.IdentityForExpert) error {
mManageExpert := model.NewManageExpert()
if params.ID > 0 {
mManageExpert.ID = params.ID
isExist, err := model2.FirstField(mManageExpert.ManageExpert, []string{"id", "name", "mobile", "tenant_id", "examine_status", "created_at"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专家信息不存在或已被删除")
}
if c.TenantID > 0 {
if mManageExpert.TenantID != c.TenantID {
return errors.New("操作错误,无权限操作")
}
} else {
if mManageExpert.Mobile != params.Mobile {
if isExist, err = params.isExist(mManageExpert.ManageExpert, model2.NewWhere("mobile", params.Mobile)); err != nil {
return err
} else if isExist {
return errors.New("操作错误,当前站点下已存在相同手机号码")
}
}
}
}
mManageExpert.TenantID = c.TenantID
mManageExpert.Name = params.Name
mManageExpert.ResearchID = other.ConvertResearch()
mManageExpert.LaboratoryID = other.ConvertLaboratory()
mManageExpert.Image.Image = params.Image
mManageExpert.Area = model2.Area{
Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
}
mManageExpert.Education = other.Education
mManageExpert.School = other.School
mManageExpert.Major = other.Major
mManageExpert.Job = other.Job
mManageExpert.Title = other.Title
mManageExpert.Gender = model2.Gender{Gender: model2.GenderKind(other.Gender)}
mManageExpert.WorkAt = utils.DataTimeToDate(other.WorkAt)
mManageExpert.SetIndustryAttribute(params.Industrys)
mManageExpert.SetKeywordAttribute(params.Keywords)
mManageExpert.SetResearchAttribute(other.Researchs)
mManageExpert.Introduce = params.Introduce
if c.TenantID <= 0 {
mManageExpert.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.TenantUserIdentityForExpert),
service.WithManageIndustry(strings.Join(_industrys, "")),
service.WithManageKeyword(strings.Join(params.Keywords, "")),
)
if mManageExpert.ID > 0 {
if err := model2.Updates(mManageExpert.ManageExpert, mManageExpert.ManageExpert); err != nil {
return err
}
if mManageExpert.ExamineStatus == model2.ExamineStatusForAgree {
_ = manage.Update()
}
return nil
}
// 查询手机号码是否在当前租户下是否已经注册了
mManageExpert.Mobile = params.Mobile
mManageExpert.ExamineStatus = model2.ExamineStatusForAgree
mManageExpert.ExamineRemark = "主动创建,无需审核"
if isExist, err := params.isExist(mManageExpert.ManageExpert,
model2.NewWhere("mobile", params.Mobile)); err != nil {
return err
} else if isExist {
return errors.New("操作错误,已存在相同手机号码")
}
if err := model2.Create(mManageExpert.ManageExpert); err != nil {
return err
}
service.WithManageID(mManageExpert.ID)(manage)
service.WithManageTitle(params.Name)(manage)
return manage.Create()
}
// Patent 专利信息
func (c *Expert) Patent(id uint64, title, applyName string) ([]*ExpertPatent, error) {
mManageExpert := model.NewManageExpert()
out, err := mManageExpert.Basic(id)
if err != nil {
return nil, err
} else if out == nil || out.ID <= 0 {
return nil, errors.New("操作错误,未找到对应的专家信息")
}
// 查看专利信息
patents := make([]*model.ManageExpertPatent, 0)
where := []*model2.ModelWhere{
//model2.NewWhereFindInSet("p.apply_name", out.ResearchName),
model2.NewWhereFindInSet("p.inventor", out.Name),
}
if title != "" {
where = append(where, model2.NewWhereLike("p.title", title))
}
if applyName != "" {
where = append(where, model2.NewWhereFindInSet("p.apply_name", applyName))
}
if patents, err = mManageExpert.Patents(where...); err != nil {
return nil, err
}
list := make([]*ExpertPatent, 0)
for _, v := range patents {
list = append(list, &ExpertPatent{
ID: v.GetEncodeID(),
ManageExpertPatent: v,
IsBind: v.UserPatentCount > 0,
})
}
return list, nil
}
// PatentBind 专利认领绑定
func (c *Expert) PatentBind(id uint64, patentIDs []uint64) error {
mManageExpert := model.NewManageExpert()
mManageExpert.ID = id
isExist, err := model2.FirstField(mManageExpert.ManageExpert, []string{"id", "tenant_id"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专家信息不存在或已被删除")
} else if c.TenantID > 0 && c.TenantID != mManageExpert.TenantID {
return errors.New("操作错误,无权限操作")
}
data := make([]*model2.TechnologyPatentExpert, 0)
for _, v := range patentIDs {
data = append(data, &model2.TechnologyPatentExpert{
ExpertID: mManageExpert.ID, PatentID: v,
})
}
if len(data) > 0 {
return model2.Creates(model.NewTechnologyPatentExpert().TechnologyPatentExpert, data)
}
return nil
}
// PatentUnbind 专利认领解绑
func (c *Expert) PatentUnbind(id uint64, patentID []uint64) error {
mManageExpert := model.NewManageExpert()
mManageExpert.ID = id
isExist, err := model2.FirstField(mManageExpert.ManageExpert, []string{"id", "tenant_id"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专家信息不存在或已被删除")
} else if c.TenantID > 0 && c.TenantID != mManageExpert.TenantID {
return errors.New("操作错误,无权限操作")
}
return model2.DeleteWhere(model.NewTechnologyPatentExpert().TechnologyPatentExpert, []*model2.ModelWhere{
model2.NewWhere("expert_id", mManageExpert.ID),
model2.NewWhereIn("patent_id", patentID),
})
}
func NewExpert() ExpertHandle {
return func(session *session.Admin) *Expert {
return &Expert{session}
}
}