Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
bom_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
01ee5f70
authored
Jun 11, 2020
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
基础的参数匹配
parent
28618b14
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
87 additions
and
34 deletions
cmd/queue/bom.go
internal/logic/attr.go
internal/logic/match.go
internal/mapping/search_keyword.go
internal/mapping/unit_attr.go
cmd/queue/bom.go
View file @
01ee5f70
...
...
@@ -14,7 +14,6 @@ type RecvPro struct {
}
func
init
()
{
<<<<<<<
HEAD
queueExchange
:=
rabbitmq
.
QueueExchange
{
"bom_match"
,
"bom_match"
,
...
...
@@ -23,7 +22,7 @@ func init() {
"amqp://huntadmin:jy2y2900@192.168.1.237:5672/"
,
}
str
:=
`{"bom_id":3
69
,"delivery_type":1,"sort":1}`
str
:=
`{"bom_id":3
82
,"delivery_type":1,"sort":1}`
rabbitmq
.
Send
(
queueExchange
,
str
)
}
...
...
internal/logic/attr.go
View file @
01ee5f70
...
...
@@ -19,30 +19,28 @@ import (
//根据参数去匹配商品
func
MatchGoodsNameByAttrs
(
bomItems
[]
model
.
BomItem
)
(
result
[]
model
.
BomItem
,
err
error
)
{
fmt
.
Println
(
"开始匹配参数"
)
client
,
err
:=
es
.
NewClient
(
es
.
SetURL
(
configs
.
ESSetting
.
Url
))
if
err
!=
nil
{
return
}
defer
client
.
Stop
()
search
:=
client
.
MultiSearch
()
.
Index
(
"goods_map_test"
)
searchFlag
:=
false
//先根据参数去构建批量查询条件
for
_
,
item
:=
range
bomItems
{
//如果有型号,但是型号有可能是参数,所以先去匹配下参数,有的话转成对应的型号
if
item
.
GoodsName
!=
""
{
search
=
searchAttr
(
item
.
GoodsName
,
search
)
searchFlag
=
true
}
//如果没有型号,但是有参数,那就去匹配参数
if
item
.
GoodsName
==
""
&&
item
.
Attrs
!=
""
{
//先去切割参数得到参数列表
attrs
:=
splitAttrs
(
item
.
Attrs
)
var
attrsSlice
[]
string
//去转换每一个参数,得到去查询es的标准格式
for
_
,
attr
:=
range
attrs
{
attr
=
TransformESParam
(
attr
)
attrsSlice
=
append
(
attrsSlice
,
attr
)
search
=
searchAttr
(
item
.
Attrs
,
search
)
searchFlag
=
true
}
query
:=
getQuery
(
attrsSlice
)
source
:=
es
.
NewSearchSource
()
.
Query
(
query
)
source
=
source
.
From
(
0
)
.
Size
(
1
)
searchRequest
:=
es
.
NewSearchRequest
()
.
Source
(
source
)
search
.
Add
(
searchRequest
)
}
if
!
searchFlag
{
return
bomItems
,
nil
}
res
,
err
:=
search
.
Do
(
context
.
Background
())
if
err
!=
nil
{
...
...
@@ -58,12 +56,32 @@ func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, er
for
_
,
hit
:=
range
responses
.
Hits
.
Hits
{
res
,
_
:=
hit
.
Source
.
MarshalJSON
()
bomItems
[
key
]
.
GoodsName
=
gjson
.
Get
(
string
(
res
),
"goods_name"
)
.
String
()
fmt
.
Println
(
gjson
.
Get
(
string
(
res
),
"goods_name"
)
.
String
())
}
}
}
result
=
bomItems
return
}
func
searchAttr
(
attrOrigin
string
,
search
*
es
.
MultiSearchService
)
(
result
*
es
.
MultiSearchService
)
{
//先去切割参数得到参数列表
attrs
:=
splitAttrs
(
attrOrigin
)
var
attrsSlice
[]
string
fmt
.
Println
(
attrs
)
//去转换每一个参数,得到去查询es的标准格式
for
_
,
attr
:=
range
attrs
{
attr
=
TransformESParam
(
attr
)
attrsSlice
=
append
(
attrsSlice
,
attr
)
}
query
:=
getQuery
(
attrsSlice
)
source
:=
es
.
NewSearchSource
()
.
Query
(
query
)
source
=
source
.
From
(
0
)
.
Size
(
1
)
searchRequest
:=
es
.
NewSearchRequest
()
.
Source
(
source
)
fmt
.
Println
(
searchRequest
.
Body
())
return
search
.
Add
(
searchRequest
)
}
//切割参数
func
splitAttrs
(
attrs
string
)
(
result
[]
string
)
{
result
=
strings
.
Split
(
attrs
,
" "
)
...
...
@@ -82,6 +100,10 @@ func splitAttrs(attrs string) (result []string) {
if
len
(
result
)
>
1
{
return
}
result
=
strings
.
Split
(
attrs
,
"/"
)
if
len
(
result
)
>
1
{
return
}
return
}
...
...
@@ -92,6 +114,7 @@ func changeKeyword(attr string) (result string) {
compile
,
_
:=
regexp
.
Compile
(
regexpStr
)
attr
=
compile
.
ReplaceAllString
(
attr
,
regular
)
}
fmt
.
Println
(
attr
)
return
attr
}
...
...
@@ -101,7 +124,6 @@ func TransformESParam(attr string) (result string) {
unitMapping
:=
mapping
.
UnitValueMapping
//获取该属性对应的单位
for
unit
,
_
:=
range
unitMapping
{
//todo 先去截取属性,得到参数,比如3kΩ(3001)这种形式的参数,要截取成3kΩ
index
:=
strings
.
Index
(
attr
,
unit
)
if
index
>
0
{
attr
=
string
([]
rune
(
attr
)[
:
index
+
len
(
unit
)
-
1
])
...
...
@@ -117,8 +139,8 @@ func TransformESParam(attr string) (result string) {
//±
func
getAttrValueByAttr
(
attr
string
)
(
attrValue
string
)
{
//先找出单位
r
,
_
:=
regexp
.
Compile
(
`[\d.]|±|\+_|\+-|/|\(.*\)|`
)
attrUnit
:=
r
.
ReplaceAllString
(
attr
,
"
"
)
r
,
_
:=
regexp
.
Compile
(
mapping
.
GetAttrUnitRegular
)
attrUnit
:=
strings
.
Trim
(
r
.
ReplaceAllString
(
attr
,
""
),
"
"
)
//如果单位是W,则要进行除法计算
if
strings
.
ToUpper
(
attrUnit
)
==
"W"
{
//提取值,进行计算
...
...
@@ -135,7 +157,7 @@ func getAttrValueByAttr(attr string) (attrValue string) {
}
//再找出纯数字
var
attrNumber
float64
numberR
,
_
:=
regexp
.
Compile
(
`(\d+(\.\d+)?)`
)
numberR
,
_
:=
regexp
.
Compile
(
mapping
.
PureNumberRegular
)
pureNumber
:=
numberR
.
FindString
(
attr
)
attrNumber
,
_
=
strconv
.
ParseFloat
(
pureNumber
,
64
)
//找出对应单位需要转换的值
...
...
@@ -157,7 +179,6 @@ func getAttrValueByAttr(attr string) (attrValue string) {
if
value
,
exist
:=
mapping
.
UnitAttrMapping
[
baseUnit
];
exist
{
attrName
=
value
}
attrNumberStr
:=
common
.
ToString
(
attrNumber
*
value64
)
attrValue
=
attrName
+
"€"
+
attrNumberStr
break
...
...
@@ -179,8 +200,17 @@ func getAttrValueByAttr(attr string) (attrValue string) {
func
getQuery
(
attrs
[]
string
)
(
query
*
es
.
BoolQuery
)
{
subQuery
:=
es
.
NewBoolQuery
()
for
_
,
attr
:=
range
attrs
{
if
strings
.
Contains
(
attr
,
"|"
)
{
name
:=
strings
.
Split
(
attr
,
"€"
)[
0
]
value
:=
strings
.
Split
(
attr
,
"€"
)[
1
]
multiAttr
:=
strings
.
Split
(
name
,
"|"
)
for
_
,
temp
:=
range
multiAttr
{
subQuery
=
subQuery
.
Should
(
es
.
NewTermQuery
(
"attrs.attr_value"
,
temp
+
"€"
+
value
))
}
}
else
{
subQuery
=
subQuery
.
Should
(
es
.
NewTermQuery
(
"attrs.attr_value"
,
attr
))
}
}
nestedQuery
:=
es
.
NewNestedQuery
(
"attrs"
,
subQuery
)
query
=
es
.
NewBoolQuery
()
query
=
query
.
Filter
(
nestedQuery
)
...
...
internal/logic/match.go
View file @
01ee5f70
...
...
@@ -7,7 +7,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"regexp"
"strings"
...
...
@@ -78,11 +77,16 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg
}
defer
client
.
Stop
()
//匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充
MatchGoodsNameByAttrs
(
bomItems
)
//bomItems, err = MatchGoodsNameByAttrs(bomItems)
//if err != nil {
// return err
//MatchGoodsNameByAttrs(bomItems)
bomItems
,
err
=
MatchGoodsNameByAttrs
(
bomItems
)
//for _, item := range bomItems {
// if item.GoodsName != "" {
// fmt.Println(item.GoodsName)
// }
//}
if
err
!=
nil
{
return
err
}
//第一次先去精确匹配
goodsMapList
,
err
:=
getUpdateGoodsData
(
bomId
,
bomItems
,
deliveryType
,
sort
,
client
,
true
)
...
...
@@ -227,7 +231,6 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
if
!
rawSearch
{
bom
.
GoodsName
=
common
.
SubKeyWordStr
(
bom
.
GoodsName
)
if
bom
.
GoodsName
==
""
{
fmt
.
Println
(
"continue"
)
continue
}
}
...
...
@@ -326,12 +329,9 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es
field
:=
"auto_goods_name"
query
=
query
.
Must
(
es
.
NewTermQuery
(
field
,
goodsName
))
}
<<<<<<<
HEAD
=======
//query = query.Filter(es.NewRangeQuery("single_price").Gt(0))
//query = query.Filter(es.NewRangeQuery("stock").Gt(0))
>>>>>>>
ysx
-
修复数据
-
20200609
query
=
query
.
Filter
(
es
.
NewTermQuery
(
"status"
,
1
))
//query = query.Filter(es.NewTermQuery("status", 1))
//query = query.Filter(es.NewRangeQuery("single_price").Gt(0))
//src, err := query.Source()
//if err != nil {
...
...
internal/mapping/search_keyword.go
View file @
01ee5f70
...
...
@@ -5,7 +5,19 @@ var KeywordRegular = map[string]string{
//`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$`: `$1$2Μ$4$5`,
"COG"
:
"C0G"
,
"NPO"
:
"NP0"
,
"(欧姆|欧|O|o|R|r)"
:
"Ω"
,
"华科|华新科技|华新科"
:
"华新"
,
"U|u|μ"
:
"M"
,
`(欧姆|欧|O|o|R|r)`
:
"Ω"
,
`(Uf|uf|μf|uF|UF)`
:
"μF"
,
`(Uh|uh|μh|uH|UH)`
:
"μH"
,
`K`
:
"KΩ"
,
`v`
:
"V"
,
}
var
PureNumberRegular
=
`(\d+(\.\d+)?)`
var
GetAttrUnitRegular
=
`[\d.]|±|\+_|\+-|/|\(.*\)|`
//`^[\d\.]+(欧姆|欧|O|o|R|r)$`: "Ω",
//`^[\d\.]+(Uf|uf|μf|uF|UF)$`: "μF",
//`^[\d\.]+(Uh|uh|μh|uH|UH)$`: "μH",
//`^[\d\.]+K$`: "KΩ",
\ No newline at end of file
internal/mapping/unit_attr.go
View file @
01ee5f70
...
...
@@ -2,13 +2,17 @@ package mapping
//属性单位对应属性
var
UnitAttrMapping
=
map
[
string
]
string
{
"r"
:
"阻值(欧姆)"
,
"Ω"
:
"阻值(欧姆)"
,
"r"
:
"阻值(欧姆)|直流电阻(内阻)"
,
"Ω"
:
"阻值(欧姆)|直流电阻(内阻)"
,
"OHM"
:
"阻值(欧姆)|直流电阻(内阻)"
,
"mh"
:
"电感"
,
"F"
:
"容值"
,
"w"
:
"功率"
,
"W"
:
"功率"
,
"Ω/r "
:
"内阻"
,
"%"
:
"精度"
,
"V"
:
"额定电压"
,
"A"
:
"额定电流"
,
}
//属性对应的基础属性
...
...
@@ -19,21 +23,28 @@ var UnitBaseMapping = map[string]string{
"kΩ"
:
"Ω"
,
"KΩ"
:
"Ω"
,
"MΩ"
:
"Ω"
,
"pF"
:
"F"
,
"nF"
:
"F"
,
"μF"
:
"F"
,
"μf"
:
"F"
,
"mF"
:
"F"
,
"F"
:
"F"
,
"μH"
:
"H"
,
"mH"
:
"H"
,
"H"
:
"H"
,
"V"
:
"V"
,
"kV"
:
"V"
,
"mA"
:
"A"
,
"A"
:
"A"
,
"W"
:
"W"
,
"kW"
:
"W"
,
"KW"
:
"W"
,
"%"
:
"%"
,
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment