86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package service
|
||
|
||
import (
|
||
"SciencesServer/serve/es"
|
||
"SciencesServer/serve/logger"
|
||
"encoding/json"
|
||
)
|
||
|
||
type ESAchievement struct {
|
||
ID uint64 `json:"id"` // 成果ID
|
||
Title string `json:"title"` // 成果名称
|
||
Industry string `json:"industry"` // 行业领域
|
||
Keyword string `json:"keyword"` // 关键词
|
||
}
|
||
|
||
type ESAchievementOption func(achievement *ESAchievement)
|
||
|
||
func (this *ESAchievement) Index() string {
|
||
return "es_achievement_index"
|
||
}
|
||
|
||
func (this *ESAchievement) Create() error {
|
||
if this.Industry != "" {
|
||
this.Title = this.Industry + " - " + this.Title
|
||
}
|
||
_bytes, _ := json.Marshal(this)
|
||
return es.Create(this.Index(), _bytes)
|
||
}
|
||
|
||
func (this *ESAchievement) Search(page, pageSize int) (interface{}, error) {
|
||
termParams := make(map[string]interface{}, 0)
|
||
mustParams := make(map[string]interface{}, 0)
|
||
|
||
if this.Title != "" {
|
||
mustParams["title"] = this.Title
|
||
}
|
||
if this.Industry != "" {
|
||
termParams["industry"] = this.Industry
|
||
}
|
||
if this.Keyword != "" {
|
||
mustParams["keyword"] = this.Keyword
|
||
}
|
||
out, err := es.Search(this, this.Index(), &es.SearchParams{
|
||
TermParams: termParams,
|
||
MustParams: mustParams,
|
||
}, page, pageSize)
|
||
|
||
if err != nil {
|
||
logger.ErrorF("查询ES信息错误:【%s】", err)
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func WithAchievementID(id uint64) ESAchievementOption {
|
||
return func(achievement *ESAchievement) {
|
||
achievement.ID = id
|
||
}
|
||
}
|
||
|
||
func WithAchievementTitle(title string) ESAchievementOption {
|
||
return func(achievement *ESAchievement) {
|
||
achievement.Title = title
|
||
}
|
||
}
|
||
|
||
func WithAchievementIndustry(industry string) ESAchievementOption {
|
||
return func(achievement *ESAchievement) {
|
||
achievement.Industry = industry
|
||
}
|
||
}
|
||
|
||
func WithAchievementKeyword(keyword string) ESAchievementOption {
|
||
return func(achievement *ESAchievement) {
|
||
achievement.Industry = keyword
|
||
}
|
||
}
|
||
|
||
func NewESAchievement(options ...ESAchievementOption) *ESAchievement {
|
||
out := new(ESAchievement)
|
||
|
||
for _, v := range options {
|
||
v(out)
|
||
}
|
||
return out
|
||
}
|