2020-03-23 15:58:39 +00:00
|
|
|
package model
|
|
|
|
|
2020-03-25 02:57:25 +00:00
|
|
|
import (
|
|
|
|
"background/db"
|
2020-03-27 16:00:46 +00:00
|
|
|
"background/utils"
|
2021-02-05 17:46:59 +00:00
|
|
|
"reflect"
|
|
|
|
|
2020-03-25 02:57:25 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-03-29 18:10:18 +00:00
|
|
|
"gopkg.in/olivere/elastic.v3"
|
2020-03-25 02:57:25 +00:00
|
|
|
"qiniupkg.com/x/log.v7"
|
|
|
|
)
|
2020-03-23 15:58:39 +00:00
|
|
|
|
2020-03-27 16:00:46 +00:00
|
|
|
const (
|
|
|
|
ERR_COLUMN_EXISTED = "column_existed"
|
|
|
|
)
|
2021-02-05 17:46:59 +00:00
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
func HardwareTypeMapping() string {
|
2020-03-25 16:30:07 +00:00
|
|
|
return `"mappings":{
|
|
|
|
"hardware":{
|
|
|
|
"properties":{
|
|
|
|
"id":{"type":"keyword"},
|
2020-03-31 07:19:57 +00:00
|
|
|
"name":{"type":"keyword"},
|
2020-03-25 16:30:07 +00:00
|
|
|
"desc":{"type":"text"},
|
|
|
|
"pic":{"type":"doc"},
|
|
|
|
"doc":{"type":"doc"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:58:39 +00:00
|
|
|
// this api is based on elasticsearch
|
|
|
|
type Hardware struct {
|
2021-02-07 09:21:19 +00:00
|
|
|
ID string `json:"_id,omitempty"`
|
|
|
|
BuyDate string `json:"buy_date,omitempty"` //购入时间
|
|
|
|
Name string `json:"name,omitempty"` // 名字
|
|
|
|
Desc string `json:"desc,omitempty"` // 描述
|
|
|
|
Pic string `json:"pic,omitempty"` // 图片
|
|
|
|
Doc string `json:"doc,omitempty"` //文档资料
|
2020-03-23 15:58:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
func (this *Hardware) CreateHardware() error {
|
|
|
|
if nil == this {
|
2020-03-27 16:00:46 +00:00
|
|
|
return errors.New(utils.ERRNULLPOINTER)
|
|
|
|
}
|
2020-03-29 18:10:18 +00:00
|
|
|
log.Print(this.Name)
|
2020-04-20 15:37:01 +00:00
|
|
|
matchPhraseQuery := elastic.NewMatchQuery("name", this.Name)
|
2021-02-07 09:21:19 +00:00
|
|
|
existedHardware, e := QueryHardwares(matchPhraseQuery, 10, 0)
|
|
|
|
log.Print(e, existedHardware)
|
2020-03-31 17:07:32 +00:00
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
for _, v := range existedHardware {
|
|
|
|
if v.Name == this.Name {
|
2020-03-31 17:07:32 +00:00
|
|
|
log.Print(v.ID)
|
|
|
|
return errors.New(ERR_COLUMN_EXISTED)
|
|
|
|
}
|
2020-03-25 02:57:25 +00:00
|
|
|
}
|
2021-02-07 09:21:19 +00:00
|
|
|
e = db.GetElastic().Create("hardware_data", "0", "", *this)
|
|
|
|
if nil != e {
|
2020-03-29 18:10:18 +00:00
|
|
|
log.Print("shit1")
|
2020-03-25 02:57:25 +00:00
|
|
|
log.Print(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
2020-03-29 18:10:18 +00:00
|
|
|
log.Print("shit2")
|
2021-02-07 09:21:19 +00:00
|
|
|
return nil
|
2020-03-25 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
func GetHardwares(limit int, size int) ([]Hardware, error) {
|
2020-03-26 05:17:20 +00:00
|
|
|
var ret []Hardware
|
2021-02-07 09:21:19 +00:00
|
|
|
ids, e := db.GetElastic().Query("hardware_data", nil, reflect.TypeOf(Hardware{}), limit, size)
|
|
|
|
if nil != e {
|
|
|
|
return nil, e
|
2020-03-26 05:17:20 +00:00
|
|
|
}
|
2020-03-31 17:07:32 +00:00
|
|
|
i := 0
|
2021-02-07 09:21:19 +00:00
|
|
|
for _, v := range ids {
|
|
|
|
ret[i].ID = v
|
2020-03-31 17:07:32 +00:00
|
|
|
i++
|
2020-03-26 05:17:20 +00:00
|
|
|
}
|
2021-02-07 09:21:19 +00:00
|
|
|
return ret, nil
|
2020-03-25 16:30:07 +00:00
|
|
|
}
|
2020-03-26 12:15:04 +00:00
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
func QueryHardwares(query elastic.Query, limit int, offset int) ([]Hardware, error) {
|
|
|
|
ret := []Hardware{}
|
|
|
|
ids, e := db.GetElastic().Query("hardware_data", query, ret, limit, offset)
|
|
|
|
if nil != e {
|
|
|
|
return nil, e
|
2020-03-29 18:10:18 +00:00
|
|
|
}
|
2020-03-31 17:07:32 +00:00
|
|
|
i := 0
|
2021-02-07 09:21:19 +00:00
|
|
|
for _, v := range ids {
|
|
|
|
ret[i].ID = v
|
2020-03-31 17:07:32 +00:00
|
|
|
i++
|
2020-03-29 18:10:18 +00:00
|
|
|
}
|
2021-02-07 09:21:19 +00:00
|
|
|
return ret, nil
|
2020-03-31 17:07:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 09:21:19 +00:00
|
|
|
func DeleteHardware(name string) error {
|
|
|
|
query := elastic.NewTermQuery("name", name)
|
|
|
|
err := db.GetElastic().Delete(query, "hardware_data")
|
|
|
|
if err != nil {
|
2020-04-26 06:11:36 +00:00
|
|
|
return err
|
2020-03-31 17:07:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-02-07 09:21:19 +00:00
|
|
|
}
|