81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package model
|
|
|
|
import (
|
|
"background/db"
|
|
"background/utils"
|
|
"github.com/pkg/errors"
|
|
"gopkg.in/olivere/elastic.v3"
|
|
"qiniupkg.com/x/log.v7"
|
|
)
|
|
|
|
const (
|
|
ERR_COLUMN_EXISTED = "column_existed"
|
|
)
|
|
func HardwareTypeMapping() (string){
|
|
return `"mappings":{
|
|
"hardware":{
|
|
"properties":{
|
|
"id":{"type":"keyword"},
|
|
"name":{"type":"keyword"},
|
|
"desc":{"type":"text"},
|
|
"pic":{"type":"doc"},
|
|
"doc":{"type":"doc"}
|
|
}
|
|
}
|
|
}`
|
|
}
|
|
|
|
// this api is based on elasticsearch
|
|
type Hardware struct {
|
|
ID int32 `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"` //文档资料
|
|
}
|
|
|
|
func (this *Hardware )CreateHardware( ) error{
|
|
if nil == this{
|
|
return errors.New(utils.ERRNULLPOINTER)
|
|
}
|
|
log.Print(this.Name)
|
|
matchPhraseQuery := elastic.NewMatchQuery("name", this.Name)
|
|
existedHardware,e := QueryHardwares(matchPhraseQuery,10,1)
|
|
log.Print(e,existedHardware)
|
|
if len(existedHardware) > 0{
|
|
return errors.New(ERR_COLUMN_EXISTED)
|
|
}
|
|
e = db.GetElastic().Create("hardware","0","",*this)
|
|
if nil != e{
|
|
log.Print("shit1")
|
|
log.Print(e.Error())
|
|
return e
|
|
}
|
|
log.Print("shit2")
|
|
return nil;
|
|
}
|
|
|
|
func GetHardwares(limit int,size int) ([]Hardware,error){
|
|
var ret []Hardware
|
|
data,e := db.GetElastic().Query("hardware",nil,Hardware{},limit,size)
|
|
if nil != e{
|
|
return nil,e
|
|
}
|
|
for _,v := range data{
|
|
ret = append(ret,v.(Hardware))
|
|
}
|
|
return ret,nil
|
|
}
|
|
|
|
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
|
var ret []Hardware
|
|
data,e := db.GetElastic().Query("hardware",query,Hardware{},limit,offset)
|
|
if nil != e{
|
|
return nil,e
|
|
}
|
|
for _,v := range data{
|
|
ret = append(ret,v.(Hardware))
|
|
}
|
|
return ret,nil
|
|
} |