Files

121 lines
2.6 KiB
Go
Raw Normal View History

package service
import (
"SciencesServer/serve/es"
"encoding/json"
"fmt"
)
type ESManage struct {
ID uint64 `json:"id"` // ID
Identity int `json:"identity"` // 身份信息
Industry string `json:"industry"` // 行业领域
Title string `json:"title"` // 名称
Keyword string `json:"keyword"` // 关键词
Research string `json:"research"` // 研究方向
}
type ESManageOption func(manage *ESManage)
func (this *ESManage) Index() string {
if this.Identity > 0 {
return fmt.Sprintf("es_manage_index_%d", this.Identity)
}
return "es_manage_index"
}
func (this *ESManage) Create() error {
_bytes, _ := json.Marshal(this)
2022-01-18 09:20:18 +08:00
return es.Create(this.Index(), fmt.Sprintf("%d", this.ID), _bytes)
}
2022-02-09 11:06:59 +08:00
func (this *ESManage) 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.Research != "" {
_map["research"] = this.Research
}
return es.Update(this.Index(), fmt.Sprintf("%d", this.ID), _map)
}
2022-01-20 11:51:02 +08:00
func (this *ESManage) Search(page, pageSize int, params map[string]interface{}) (interface{}, int64, error) {
2021-12-24 09:13:16 +08:00
termParams := make(map[string]interface{}, 0)
shouldParams := make(map[string]interface{}, 0)
2021-12-24 09:13:16 +08:00
if this.Industry != "" {
termParams["industry"] = this.Industry
}
if this.Title != "" {
shouldParams["title"] = this.Title
}
if this.Keyword != "" {
shouldParams["keyword"] = this.Keyword
}
if this.Research != "" {
shouldParams["research"] = this.Research
}
2022-01-04 15:04:37 +08:00
if len(params) > 0 {
for k, v := range params {
shouldParams[k] = v
}
}
2022-01-20 11:51:02 +08:00
return es.Search(this, this.Index(), &es.SearchParams{
2021-12-24 09:13:16 +08:00
TermParams: termParams,
ShouldParams: shouldParams,
}, page, pageSize)
}
func WithManageID(id uint64) ESManageOption {
return func(manage *ESManage) {
manage.ID = id
}
}
func WithManageIdentity(identity int) ESManageOption {
return func(manage *ESManage) {
manage.Identity = identity
}
}
func WithManageTitle(title string) ESManageOption {
return func(manage *ESManage) {
manage.Title = title
}
}
func WithManageIndustry(industry string) ESManageOption {
return func(manage *ESManage) {
manage.Industry = industry
}
}
func WithManageKeyword(keyword string) ESManageOption {
return func(manage *ESManage) {
manage.Keyword = keyword
}
}
func WithManageResearch(research string) ESManageOption {
return func(manage *ESManage) {
manage.Research = research
}
}
func NewESManage(options ...ESManageOption) *ESManage {
out := new(ESManage)
for _, option := range options {
option(out)
}
return out
}