Files
cas_tt_cloud_backend/app/service/es_achievement.go
2022-02-11 11:02:29 +08:00

109 lines
2.5 KiB
Go

package service
import (
"SciencesServer/serve/es"
"encoding/json"
"fmt"
)
type ESAchievement struct {
ID uint64 `json:"id"` // 成果ID
Title string `json:"title"` // 成果名称
Industry string `json:"industry"` // 行业领域
Keyword string `json:"keyword"` // 关键词
IsShow int `json:"is_show"` // 展示
}
type ESAchievementOption func(achievement *ESAchievement)
func (this *ESAchievement) Index() string {
return "es_achievement_index"
}
func (this *ESAchievement) Create() error {
_bytes, _ := json.Marshal(this)
return es.Create(this.Index(), fmt.Sprintf("%d", this.ID), _bytes)
}
func (this *ESAchievement) Update() error {
_map := make(map[string]interface{}, 0)
if this.Title != "" {
_map["title"] = this.Title
}
if this.Industry != "" {
_map["industry"] = this.Industry
}
if this.Keyword != "" {
_map["keyword"] = this.Keyword
}
if this.IsShow != 0 {
_map["is_show"] = this.IsShow
}
return es.Update(this.Index(), fmt.Sprintf("%d", this.ID), _map)
}
func (this *ESAchievement) Search(page, pageSize int) (interface{}, int64, 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
}
//termParams["is_show"] = 1
return es.Search(this, this.Index(), &es.SearchParams{
TermParams: termParams,
MustParams: mustParams,
}, page, pageSize)
}
func (this *ESAchievement) Delete() error {
return es.Delete(this.Index(), fmt.Sprintf("%d", this.ID))
}
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 WithAchievementShow(show int) ESAchievementOption {
return func(achievement *ESAchievement) {
achievement.IsShow = show
}
}
func NewESAchievement(options ...ESAchievementOption) *ESAchievement {
out := new(ESAchievement)
for _, v := range options {
v(out)
}
return out
}