2021-12-23 17:43:27 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"SciencesServer/serve/es"
|
|
|
|
"encoding/json"
|
2022-01-18 09:20:18 +08:00
|
|
|
"fmt"
|
2021-12-23 17:43:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ESPatent struct {
|
|
|
|
ID uint64 `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Industry string `json:"industry"` // 行业领域
|
|
|
|
}
|
|
|
|
|
|
|
|
type ESPatentOption func(patent *ESPatent)
|
|
|
|
|
|
|
|
func (this *ESPatent) Index() string {
|
|
|
|
return "es_patent_index"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ESPatent) 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ESPatent) Update() error {
|
2022-02-08 16:11:59 +08:00
|
|
|
_map := make(map[string]interface{}, 0)
|
|
|
|
|
|
|
|
if this.Title != "" {
|
|
|
|
_map["title"] = this.Title
|
|
|
|
}
|
|
|
|
if this.Industry != "" {
|
|
|
|
_map["industry"] = this.Industry
|
|
|
|
}
|
|
|
|
return es.Update(this.Index(), fmt.Sprintf("%d", this.ID), _map)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ESPatent) Delete() error {
|
|
|
|
return es.Delete(this.Index(), fmt.Sprintf("%d", this.ID))
|
2021-12-23 17:43:27 +08:00
|
|
|
}
|
|
|
|
|
2022-01-20 11:51:02 +08:00
|
|
|
func (this *ESPatent) Search(page, pageSize int) (interface{}, int64, error) {
|
2021-12-24 09:13:16 +08:00
|
|
|
termParams := make(map[string]interface{}, 0)
|
2021-12-23 18:12:33 +08:00
|
|
|
mustParams := make(map[string]interface{}, 0)
|
|
|
|
|
|
|
|
if this.Title != "" {
|
2021-12-24 09:13:16 +08:00
|
|
|
mustParams["title"] = this.Title
|
2021-12-23 18:12:33 +08:00
|
|
|
}
|
|
|
|
if this.Industry != "" {
|
2022-01-04 11:59:58 +08:00
|
|
|
termParams["industry"] = this.Industry
|
2021-12-23 18:12:33 +08:00
|
|
|
}
|
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,
|
|
|
|
MustParams: mustParams,
|
2021-12-23 17:43:27 +08:00
|
|
|
}, page, pageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPatentID(id uint64) ESPatentOption {
|
|
|
|
return func(patent *ESPatent) {
|
|
|
|
patent.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPatentTitle(title string) ESPatentOption {
|
|
|
|
return func(patent *ESPatent) {
|
|
|
|
patent.Title = title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPatentIndustry(industry string) ESPatentOption {
|
|
|
|
return func(patent *ESPatent) {
|
|
|
|
patent.Industry = industry
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewESPatent(options ...ESPatentOption) *ESPatent {
|
|
|
|
out := new(ESPatent)
|
|
|
|
|
|
|
|
for _, v := range options {
|
|
|
|
v(out)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|