Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_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
6d1f8af8
authored
Jul 06, 2020
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
获取商品服务
parent
930b9c90
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
115 additions
and
38 deletions
conf/search.ini
controller/bom_controller.go
controller/quote_controller.go
requests/quote_request.go
routes/router.go
service/attr_service.go
service/bom_service.go
service/goods_service.go
conf/search.ini
View file @
6d1f8af8
...
...
@@ -3,6 +3,10 @@
2
=
"verical"
3
=
"tme"
#相似物料供应商
[alike_supplier]
1
=
"ti"
[common]
'MOUSER_API'='http://footstone.liexin.net/webapi/handle_mouser'
...
...
controller/bom_controller.go
View file @
6d1f8af8
...
...
@@ -9,10 +9,6 @@ import (
//搜索型号
func
AutoSpu
(
c
*
gin
.
Context
)
{
service1
:=
service
.
GoodsService
{}
res1
,
_
:=
service1
.
GetGoodsInfoByApi
(
"1151005920927710088,1150961641855982990,1150961724815213435"
)
c
.
JSON
(
200
,
res1
)
return
goodsName
,
_
:=
c
.
GetPostForm
(
"goods_name"
)
goods
:=
service
.
AutoSpu
(
goodsName
)
var
errCode
int
...
...
controller/quote_controller.go
View file @
6d1f8af8
...
...
@@ -8,24 +8,42 @@ import (
"search_server/service"
)
//搜索快手搜搜
//check_button=2&pf=1&keyword=123213&supplier_id=7&offset=2&follow_hit=1&p=1&_=1593582814402
func
QuoteIndex
(
c
*
gin
.
Context
)
{
var
request
requests
.
QuoteIndexRequest
var
(
request
requests
.
QuoteIndexRequest
)
//绑定参数到结构体
if
err
:=
c
.
ShouldBind
(
&
request
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
if
request
.
Keyword
==
""
||
request
.
SupplierId
==
""
{
c
.
JSON
(
200
,
common
.
GetResponse
(
1
,
"缺少参数"
,
[]
string
{}))
//检查参数,有错误就返回错误
errCode
,
errMsg
:=
requests
.
CheckQuoteRequest
(
request
)
if
errCode
!=
0
{
c
.
JSON
(
200
,
common
.
Response
{
ErrCode
:
errCode
,
ErrMsg
:
errMsg
,
Data
:
[]
string
{},
})
return
}
supplierName
:=
"temp"
if
supplierName
==
""
{
c
.
JSON
(
200
,
common
.
GetResponse
(
1
,
"供应商id有误"
,
[]
string
{}))
return
if
request
.
FollowHit
!=
0
&&
request
.
FollowHit
!=
1
{
service
.
GetGoodsBySupplier
(
request
)
}
else
{
}
service
.
GetGoodsInfoBySupplier
(
request
)
flag
:=
1
if
true
{
flag
=
0
}
//构建返回
c
.
JSON
(
200
,
common
.
Response
{
ErrCode
:
flag
,
ErrMsg
:
""
,
Data
:
nil
,
})
}
requests/quote_request.go
View file @
6d1f8af8
package
requests
import
(
"github.com/syyongx/php2go"
"search_server/pkg/config"
)
type
QuoteIndexRequest
struct
{
Keyword
string
`form:"key
_
word"`
Keyword
string
`form:"keyword"`
CheckButton
int
`form:"check_button"`
SupplierId
string
`form:"supplier_id"`
Offset
int
`form:"offset"`
...
...
@@ -9,7 +14,7 @@ type QuoteIndexRequest struct {
Stock
int
`form:"stock"`
GoodsNameEq
string
`form:"goods_name/eq"`
IsExact
int
`form:"is_exact"`
BrandId
string
`form:"brand_id"`
BrandId
string
`form:"brand_id"`
AvailRank
int
`form:"avail_rank"`
SingleRank
int
`form:"single_rank"`
StockRank
int
`form:"stock_rank"`
...
...
@@ -18,3 +23,23 @@ type QuoteIndexRequest struct {
P
int
`form:"p"`
Topic
int
`form:"topic"`
}
func
CheckQuoteRequest
(
r
QuoteIndexRequest
)
(
errCode
int
,
errMsg
string
)
{
//解码参数
var
err
error
if
r
.
Keyword
!=
""
{
r
.
Keyword
,
err
=
php2go
.
URLDecode
(
r
.
Keyword
)
if
err
!=
nil
{
return
1
,
"参数解码失败"
}
}
//判断参数
if
r
.
Keyword
==
""
||
r
.
SupplierId
==
""
{
return
1
,
"缺少参数"
}
//判断供应商
if
config
.
Get
(
"supplier_all."
+
r
.
SupplierId
)
.
String
()
==
""
{
return
1
,
"供应商id有误"
}
return
}
routes/router.go
View file @
6d1f8af8
...
...
@@ -17,8 +17,10 @@ func InitRouter() *gin.Engine {
r
.
POST
(
"/search/bom/recommend"
,
controller
.
Recommend
)
r
.
POST
(
"search/ZiYing/zyh"
,
controller
.
Zyh
)
//快手平台相关
//r.GET("/search/quote", controller.QuoteIndex)
r
.
GET
(
"/search/quote"
,
controller
.
QuoteIndex
)
r
.
POST
(
"/search/quote"
,
controller
.
QuoteIndex
)
return
r
}
service/attr_service.go
View file @
6d1f8af8
...
...
@@ -48,7 +48,7 @@ func SearchAttr(attrOrigin, encap string) (goodsName string, err error) {
numberR
,
_
:=
regexp
.
Compile
(
common
.
PureNumberRegular
)
pureNumber
:=
numberR
.
FindString
(
encap
)
//再去找对应属性
redisConn
:=
gredis
.
Conn
(
"
default_redis_read
"
)
redisConn
:=
gredis
.
Conn
(
"
search_r
"
)
defer
redisConn
.
Close
()
attrName
,
_
:=
redis
.
String
(
redisConn
.
Do
(
"HGET"
,
"sku_map2"
,
pureNumber
))
if
attrName
!=
""
{
...
...
@@ -203,7 +203,7 @@ func getAttrValueByAttr(attr string) (attrValue string) {
}
}
else
{
//再去找没有单位的对应属性
redisConn
:=
gredis
.
Conn
(
"
default_redis_read
"
)
redisConn
:=
gredis
.
Conn
(
"
search_r
"
)
defer
redisConn
.
Close
()
attrName
,
_
:=
redis
.
String
(
redisConn
.
Do
(
"HGET"
,
"sku_map2"
,
pureNumber
))
if
attrName
!=
""
{
...
...
service/bom_service.go
View file @
6d1f8af8
...
...
@@ -101,8 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList
=
append
(
goodsIdList
,
goodsId
)
}
goodsIdListStr
=
strings
.
Join
(
goodsIdList
,
","
)
goodsService
:=
GoodsService
{}
goodsList
,
err
:=
goodsService
.
GetGoodsInfoByApi
(
goodsIdListStr
)
goodsList
,
err
:=
GetGoodsInfoByApi
(
goodsIdListStr
)
response
.
Data
=
goodsList
response
.
Flag
=
req
.
Flag
return
&
response
...
...
service/goods_service.go
View file @
6d1f8af8
...
...
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/imroc/req"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5"
...
...
@@ -16,11 +17,8 @@ import (
"strings"
)
type
GoodsService
struct
{
}
//获取商品信息,需要传入userId用于判断是否登陆
func
(
gs
*
GoodsService
)
GetGoodsInfo
(
goodsIdsStr
string
,
userId
int
)
(
goodsListMap
map
[
string
]
*
model
.
ApiGoods
,
err
error
)
{
func
GetGoodsInfo
(
goodsIdsStr
string
,
userId
int
)
(
goodsListMap
map
[
string
]
*
model
.
ApiGoods
,
err
error
)
{
isNewCustomer
,
isMember
:=
CheckIsNewCustomer
(
userId
)
params
:=
req
.
Param
{
"goods_id"
:
goodsIdsStr
,
...
...
@@ -32,7 +30,7 @@ func (gs *GoodsService) GetGoodsInfo(goodsIdsStr string, userId int) (goodsListM
}
//获取商品信息
func
(
gs
*
GoodsService
)
GetGoodsInfoByApi
(
goodsIdsStr
string
)
(
goodsList
[]
*
model
.
ApiGoods
,
err
error
)
{
func
GetGoodsInfoByApi
(
goodsIdsStr
string
)
(
goodsList
[]
*
model
.
ApiGoods
,
err
error
)
{
params
:=
req
.
Param
{
"goods_id"
:
goodsIdsStr
,
}
...
...
@@ -184,6 +182,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []*model.Api
return
}
//判断是否是新客价
func
CheckIsNewCustomer
(
userId
int
)
(
isNewCustomer
,
isMember
bool
)
{
//判断新客价
if
userId
!=
0
{
...
...
@@ -209,22 +208,44 @@ func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
}
//根据供应商获取商品信息
func
GetGoodsInfoBySupplier
(
req
requests
.
QuoteIndexRequest
)
error
{
supplierIndex
:=
config
.
Get
(
"es.search_supplier"
)
.
String
()
func
GetGoodsBySupplier
(
r
requests
.
QuoteIndexRequest
)
(
err
error
)
{
//索引字典
supplierIndex
:=
getSupplierIndex
(
r
.
SupplierId
)
//获取查询条件
queryString
,
err
:=
getSupplierQuery
(
req
)
queryString
,
err
:=
getSupplierQuery
(
r
)
queryString
=
""
res
,
err
:=
es
.
CurlES
(
supplierIndex
,
queryString
)
if
err
!=
nil
{
return
err
}
fmt
.
Println
(
res
)
goodsIds
:=
GetGoodsIdsByEs
(
res
)
//if err != nil {
// return err
//}
goodsIdsStr
:=
strings
.
Join
(
goodsIds
,
","
)
goodsList
,
err
:=
GetGoodsInfo
(
goodsIdsStr
,
0
)
fmt
.
Println
(
goodsList
)
return
nil
}
func
getSupplierIndex
(
supplierId
string
)
string
{
return
"temp"
func
getSupplierIndex
(
supplierId
string
)
(
supplierIndex
string
)
{
supplierIndexMap
:=
config
.
Cfg
.
Section
(
"supplier_all"
)
.
KeysHash
()
if
index
,
exist
:=
supplierIndexMap
[
supplierId
];
exist
{
return
index
}
if
strings
.
Contains
(
supplierId
,
","
)
{
supplierIdSlice
:=
strings
.
Split
(
supplierId
,
","
)
for
_
,
supplierId
:=
range
supplierIdSlice
{
if
index
,
exist
:=
supplierIndexMap
[
supplierId
];
exist
{
if
supplierIndex
==
""
{
supplierIndex
=
index
}
else
{
supplierIndex
=
supplierIndex
+
","
+
index
}
}
}
}
return
}
//获取根据供应商搜索
func
getSupplierQuery
(
req
requests
.
QuoteIndexRequest
)
(
string
,
error
)
{
req
.
SupplierId
=
strings
.
TrimSpace
(
req
.
SupplierId
)
source
:=
elastic
.
NewSearchSource
()
...
...
@@ -242,6 +263,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
query
.
Must
(
elastic
.
NewRangeQuery
(
"update_time"
)
.
Gte
(
oneMonthTimeStamp
))
keyword
:=
strings
.
ToUpper
(
req
.
Keyword
)
//判断关键词是否为空
if
req
.
Keyword
!=
""
{
r1
,
_
:=
regexp
.
Compile
(
`/[\x{4e00}-\x{9fff}]+/u`
)
...
...
@@ -250,7 +272,6 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
r2
,
_
:=
regexp
.
Compile
(
`/[^A-Za-z0-9]+/`
)
req
.
Keyword
=
r2
.
ReplaceAllString
(
removeCnKeyword
,
""
)
//
if
req
.
IsExact
==
1
{
query
.
Must
(
elastic
.
NewTermQuery
(
"auto_goods_name.raw"
,
keyword
))
}
else
{
...
...
@@ -265,8 +286,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
}
//判断是否存在类似物料搜索
//todo:类似索引获取
if
true
{
supplierIndexName
:=
getSupplierIndex
(
req
.
SupplierId
)
//获取相似物料供应商列表
alikeSuppliers
:=
config
.
Cfg
.
Section
(
"alike_supplier"
)
.
KeyStrings
()
if
php2go
.
InArray
(
supplierIndexName
,
alikeSuppliers
)
&&
keyword
!=
""
{
query
.
Should
(
elastic
.
NewTermQuery
(
"goods_name_alike"
,
keyword
))
}
...
...
@@ -275,7 +298,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
//判断库存
if
req
.
Stock
>
0
{
query
.
Must
(
elastic
.
NewRangeQuery
(
"st
i
ck"
)
.
Gte
(
req
.
Stock
))
query
.
Must
(
elastic
.
NewRangeQuery
(
"st
o
ck"
)
.
Gte
(
req
.
Stock
))
}
//判断是否要根据品牌id搜索
...
...
@@ -382,8 +405,18 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
for
_
,
brandId
:=
range
excludeBrandId
{
query
.
MustNot
(
elastic
.
NewTermQuery
(
"brand_id"
,
brandId
))
}
source
.
Query
(
query
)
searchRequest
:=
elastic
.
NewSearchRequest
()
.
Source
(
source
)
return
searchRequest
.
Body
()
}
//根据es的结果获取goods_id列表
func
GetGoodsIdsByEs
(
res
string
)
(
goodsIds
[]
string
)
{
//直接用gjson去获取goods_id列表
gjArray
:=
gjson
.
Get
(
res
,
"hits.hits.#._id"
)
.
Array
()
for
_
,
item
:=
range
gjArray
{
goodsIds
=
append
(
goodsIds
,
item
.
String
())
}
return
}
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