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
fb447380
authored
Jul 27, 2020
by
huangchengyi
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
1.0
parent
2ff90e41
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
122 additions
and
51 deletions
conf/redis_key.ini
conf/search.ini
controller/ly_mouser_controller.go
model/ly.go
pkg/common/function.go
service/ly/common.go
service/ly/mouser.go
conf/redis_key.ini
View file @
fb447380
...
...
@@ -10,6 +10,10 @@ SKU_UNIQUE_JUDGE=sku_unique_judge
SPU_UNIQUE_JUDGE
=
sku_unique_judge
;品牌信息判断,根据md5后的品牌名称获取id
BRAND_NAME_ALL
=
brand_name_all
;所有联营sku缓存
SKU
=
sku
;所有联营spu缓存
SPU
=
spu
;品牌id,获取品牌名称
BRAND
=
brand
;供应商品牌信息判断,存在多对一的情况,根据md5后的品牌名称获取标准品牌id
...
...
conf/search.ini
View file @
fb447380
...
...
@@ -194,4 +194,8 @@ SEARCH_TOKEN_EXPIRE_TIME = 30
999999
=
zhuanmai
[SEARCH_API_LOG]
SEARCH_API_ERROR_PRE
=
search_api_overtime_
\ No newline at end of file
SEARCH_API_ERROR_PRE
=
search_api_overtime_
[LY_UPDATE_LIMIT]
;联营是否开启立刻更新:0否 1是
isUpdate
=
0
controller/ly_mouser_controller.go
View file @
fb447380
...
...
@@ -20,6 +20,8 @@ func GetMouserData(c *gin.Context) {
}
goodsName
:=
_REQUEST
[
"goods_name"
]
flag
:=
_REQUEST
[
"flag"
]
common
.
PrintDebugHeader
(
c
,
&
flag
)
if
goodsName
==
""
{
c
.
JSON
(
200
,
"goods_name不得为空"
)
...
...
@@ -43,6 +45,6 @@ func GetMouserData(c *gin.Context) {
c
.
JSON
(
200
,
common
.
BomResponse
{
ErrCode
:
errCode
,
ErrMsg
:
"查询成功"
,
Data
:
apiData
,
Data
:
""
,
})
}
model/ly.go
View file @
fb447380
...
...
@@ -20,12 +20,12 @@ type LyClearGoodsList struct {
RestrictionMessage
string
`json:"RestrictionMessage"`
//额外购买限制内容,如 ‘当前商品不在本地区销售’
Increment
int64
`json:"increment"`
//最小包装量、倍数 =Mpq
SinglePrice
float64
`json:"single_price"`
//最小单价
Stock
int64
`json:"
increment
"`
//库存
Stock
int64
`json:"
stock
"`
//库存
Moq
int64
`json:"moq"`
//最小起订量、起订量
RawGoodsId
string
`json:"raw_goods_id"`
//外链原始goods_sn,如digikey:{'raw_goods_id': 'AT28C64X-25PC-ND', 'raw_brand_name': 'Microchip Technology'}
RawBrandName
string
`json:"raw_brand_name"`
//外链原始品牌名称,如digikey:{'raw_goods_id': 'AT28C64X-25PC-ND', 'raw_brand_name': 'Microchip Technology'}
Tiered
[]
*
TierItem
//价格梯度数量
PriceTemp
[]
interface
{}
//拼接梯度,目前只有联营推送队列用
Tiered
[]
*
TierItem
`json:"tiered"`
//价格梯度数量
PriceTemp
[]
interface
{}
`json:"price_temp"`
//拼接梯度,目前只有联营推送队列用
}
type
TierItem
struct
{
Purchases
uint64
`json:"purchases"`
//购买数量
...
...
pkg/common/function.go
View file @
fb447380
package
common
import
(
"bytes"
"crypto/md5"
crand
"crypto/rand"
"encoding/hex"
...
...
@@ -26,7 +25,7 @@ import (
输出header
*/
func
PrintDebugHeader
(
ctx
*
gin
.
Context
,
flag
*
string
)
{
if
*
flag
==
"101
"
{
if
*
flag
!=
"
"
{
ctx
.
Header
(
"Content-Type"
,
"text/html; charset=utf-8"
)
}
}
...
...
@@ -38,19 +37,18 @@ func PrintDebugHeader(ctx *gin.Context,flag *string) {
@parm jsondata 需要json输出的内容
*/
func
PrintDebugHtml
(
ctx
*
gin
.
Context
,
flag
*
string
,
jsonStr
interface
{})
{
if
*
flag
!=
"101"
&&
jsonStr
!
=
""
{
if
*
flag
==
""
||
jsonStr
=
=
""
{
return
}
jsonData
,
err
:=
json
.
Marshal
(
jsonStr
)
if
err
!=
nil
{
fmt
.
Println
(
"错误:-----"
,
err
)
if
v
,
p
:=
jsonStr
.
(
string
);
p
{
ctx
.
String
(
200
,
"</br></br>-----------"
+
v
)
}
else
{
jsonData
,
err
:=
json
.
Marshal
(
jsonStr
)
if
err
!=
nil
{
fmt
.
Println
(
"错误:-----"
,
err
)
}
ctx
.
String
(
200
,
"</br></br>-----------"
+
string
(
jsonData
))
}
var
str
bytes
.
Buffer
_
=
json
.
Indent
(
&
str
,
[]
byte
(
string
(
jsonData
)),
""
,
" "
)
fmt
.
Println
(
"formated: "
,
str
.
String
())
ctx
.
String
(
200
,
"</br>"
)
ctx
.
String
(
200
,
str
.
String
())
}
// Md5 md5()
...
...
@@ -378,4 +376,32 @@ func MapSort(mapList map[int]int) []int{
newList
=
append
(
newList
,
mapList
[
k
])
}
return
newList
}
\ No newline at end of file
}
////////////类型转换/////////////////////
func
MyInt8
(
str
string
)
int8
{
res
,
_
:=
strconv
.
ParseInt
(
str
,
10
,
64
)
return
int8
(
res
)
}
func
MyInt16
(
str
string
)
int16
{
res
,
_
:=
strconv
.
ParseInt
(
str
,
10
,
64
)
return
int16
(
res
)
}
func
MyInt64
(
str
string
)
int64
{
res
,
_
:=
strconv
.
ParseInt
(
str
,
10
,
64
)
return
int64
(
res
)
}
func
MyFloat32
(
str
string
)
float32
{
res
,
_
:=
strconv
.
ParseFloat
(
str
,
64
)
return
float32
(
res
)
}
func
MyFloat64
(
str
string
)
float64
{
res
,
_
:=
strconv
.
ParseFloat
(
str
,
64
)
return
float64
(
res
)
}
func
MyFloat64ToStr
(
numb
float64
)
string
{
return
strconv
.
FormatFloat
(
numb
,
'f'
,
6
,
64
)
}
func
MyInt64ToStr
(
numb
int64
)
string
{
return
strconv
.
FormatInt
(
numb
,
10
)
}
////////////类型转换/////////////////////
\ No newline at end of file
service/ly/common.go
View file @
fb447380
...
...
@@ -71,31 +71,43 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo
defer
redisWrite
.
Close
()
t
.
supplierId
=
supplierInfo
.
SupplierId
supplierIdStr
:=
common
.
MyInt64ToStr
(
supplierInfo
.
SupplierId
);
t
.
supplierName
=
supplierInfo
.
SupplierNickname
_
,
haveLimitTime
:=
t
.
supplier_over_time
[
supplierInfo
.
SupplierNickname
]
//获取当前供应商更新周期
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
goodsList
)
for
goodsSn
,
info
:=
range
goodsList
{
snSku
:=
php2go
.
Md5
(
strings
.
ToLower
(
goodsSn
))
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
goodsSn
+
"_"
+
supplierIdStr
)
snSku
:=
php2go
.
Md5
(
strings
.
ToLower
(
goodsSn
+
"_"
+
supplierIdStr
))
skuId
,
_
:=
gredis
.
String
(
redisWrite
.
Do
(
"HGET"
,
config
.
Get
(
"redis_all.SKU_UNIQUE_JUDGE"
)
.
String
(),
snSku
))
//查询唯一值,反查sku_id
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
info
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"snSku:"
+
snSku
+
" 查询得出sku"
+
skuId
+
" info"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"算法goodsSn:"
+
goodsSn
+
"_"
+
supplierIdStr
+
" :md5("
+
strings
.
ToLower
(
goodsSn
+
"_"
+
supplierIdStr
)
+
") = snSku:"
+
snSku
+
" 查询 sku_unique_judge 得出skuid: "
+
skuId
)
/**********新增sku**************/
skuFlag
:=
false
;
//是否新增或者更新db+redis,为true则新增
//skuId = ""; //debug
if
skuId
==
""
{
//redis没有此sku,创建
lock_key
:=
"searchapi_"
+
snSku
;
flag
,
_
:=
redis
.
Bool
(
redisWrite
.
Do
(
"SETNX"
,
lock_key
,
php2go
.
Time
()
+
2
))
if
!
flag
{
//防止并发
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"并发新增"
)
continue
;
}
//新增操作
skuFlag
=
true
//新增标志
redisWrite
.
Do
(
"EXP"
,
lock_key
,
2
)
//给锁有效时间2秒
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"新增sku开始------------"
)
skuId
=
t
.
writeSkuInfo
(
info
,
""
)
//新增redis+sku+spu+brand
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"新增sku结束------------"
)
redisWrite
.
Do
(
"del"
,
lock_key
)
//删除锁
}
...
...
@@ -103,11 +115,13 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo
//获取sku存在redis的详情
redisData
,
_
:=
redis
.
String
(
redisWrite
.
Do
(
"HGET"
,
config
.
Get
(
"redis_all.SKU"
)
.
String
(),
skuId
))
if
redisData
==
""
{
logger
.
Log
(
"此sku不存在redis"
+
skuId
,
"redis_get_sku_error"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"此sku不存在redis:"
+
skuId
)
logger
.
Log
(
"此sku不存在redis:"
+
skuId
,
"redis_get_sku_error"
)
continue
;
}
redisArr
,
ok
:=
gjson
.
Parse
(
redisData
)
.
Value
()
.
(
map
[
string
]
interface
{})
//redis取值转成map
if
!
ok
{
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"此sku不是正确json格式"
+
redisData
)
logger
.
Log
(
"此sku不是正确json格式"
+
redisData
,
"redis_get_sku_error"
)
continue
;
}
...
...
@@ -121,14 +135,21 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo
if
skuFlag
{
//如果是新增
t
.
createSkuDB
(
info
)
//推入队列->(斌队列消费写redis+go+mongodb)
t
.
esSkuInfo
(
info
)
//新增es数据
continue
;
}
/**********更新sku**************/
//计算更新周期
var
isUpdate
int64
//是否到时间更新sku
if
haveLimitTime
{
isUpdate
=
php2go
.
Time
()
-
gjson
.
Get
(
redisData
,
"update_time"
)
.
Int
()
-
t
.
supplier_over_time
[
supplierInfo
.
SupplierNickname
]
if
config
.
Get
(
"redis_all.SKU"
)
.
String
()
==
"0"
{
//计算更新周期
if
haveLimitTime
{
isUpdate
=
php2go
.
Time
()
-
gjson
.
Get
(
redisData
,
"update_time"
)
.
Int
()
-
t
.
supplier_over_time
[
supplierInfo
.
SupplierNickname
]
println
(
isUpdate
,
"ddddd"
)
}
}
else
{
isUpdate
=
100
//debug
}
if
isUpdate
>
0
&&
!
skuFlag
{
//需要更新
//更新redis->sku
redisArr
[
"moq"
]
=
info
.
Moq
...
...
@@ -151,6 +172,9 @@ func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoo
})
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.SKU_RAW_MAP"
)
.
String
(),
skuMapJson
)
}
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"更新sku:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
redisArr
)
//更新db
t
.
updateSkuDB
(
info
)
//更新es
...
...
@@ -172,12 +196,17 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str
spuKey
:=
common
.
GetKey
(
skuInfo
.
GoodsName
+
"_"
+
skuInfo
.
BrandName
)
spuId
,
_
:=
gredis
.
String
(
redisWrite
.
Do
(
"HGET"
,
config
.
Get
(
"redis_all.SKU_UNIQUE_JUDGE"
)
.
String
(),
spuKey
))
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
spuKey
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
spuId
)
if
spuId
==
""
{
//不存在spu,创建新的spu
spuId
=
t
.
writeSpuInfo
(
skuInfo
)
}
if
skuId
==
""
{
skuId
=
common
.
CreateId
(
"sku"
);
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"新增sku:"
+
skuId
)
}
inSkuInfo
:=
model
.
LySkuInfo
{
...
...
@@ -200,13 +229,18 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str
}
jsonStr
,
_
:=
json
.
Marshal
(
inSkuInfo
)
redisWrite
.
Do
(
"HSET"
,
"sku"
,
spuId
,
jsonStr
)
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.SKU"
)
.
String
(),
skuId
,
string
(
jsonStr
))
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"更新redis_sku:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
inSkuInfo
)
skuKey
:=
common
.
GetKey
(
skuInfo
.
GoodsSn
+
"_"
+
strconv
.
FormatInt
(
t
.
supplierId
,
10
))
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.SKU_UNIQUE_JUDGE"
)
.
String
(),
skuKey
,
skuId
)
//todo 如果是digikey新增,需要插入原始映射表
if
t
.
supplierId
==
7
{
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"sku_raw_map:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
jsonStr
)
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.SKU_RAW_MAP"
)
.
String
(),
skuKey
,
skuId
)
}
return
skuId
...
...
@@ -246,6 +280,9 @@ func (t *CommonLyService) writeSpuInfo(skuInfo *model.LyClearGoodsList) string {
jsonStr
,
_
:=
json
.
Marshal
(
Spuinfo
)
redisWrite
.
Do
(
"HSET"
,
"spu"
,
spuId
,
jsonStr
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"新增spu:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
jsonStr
)
spuKey
:=
common
.
GetKey
(
skuInfo
.
GoodsName
+
"_"
+
skuInfo
.
BrandName
)
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.SPU_UNIQUE_JUDGE"
)
.
String
(),
spuKey
,
spuId
)
...
...
@@ -270,6 +307,7 @@ func (t *CommonLyService) writeBrandInfo(skuInfo *model.LyClearGoodsList) int64
return
0
}
if
has
{
//不存在品牌,则创建品牌
sql
:=
"insert into brand(brand_name,create_time,insert_type) values (?, ?, ?)"
results
,
err
:=
dbSpu
.
Exec
(
sql
,
skuInfo
.
BrandName
,
php2go
.
Time
(),
1
)
if
err
!=
nil
{
...
...
@@ -277,6 +315,9 @@ func (t *CommonLyService) writeBrandInfo(skuInfo *model.LyClearGoodsList) int64
return
0
}
brandId
,
_
=
results
.
LastInsertId
()
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"创建品牌:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
brandId
)
}
//更新redis 品牌数据
redisWrite
.
Do
(
"HSET"
,
config
.
Get
(
"redis_all.BRAND_NAME_ALL"
)
.
String
(),
brandkey
,
brandId
)
...
...
@@ -310,16 +351,16 @@ func (t * CommonLyService) esSkuInfo(skuInfo *model.LyClearGoodsList) {
nowTime
:=
php2go
.
Time
()
param
:=
map
[
string
]
interface
{}{
"goods_id"
:
skuInfo
.
SkuId
,
"goods_id"
:
common
.
MyInt64
(
skuInfo
.
SkuId
),
//字符串转64位整数
"goods_name"
:
skuInfo
.
GoodsName
,
"auto_goods_name"
:
php2go
.
Strtoupper
(
skuInfo
.
GoodsName
),
"brand_id"
:
skuInfo
.
BrandId
,
"brand_id"
:
common
.
MyInt64
(
skuInfo
.
BrandId
)
,
"brand_name"
:
skuInfo
.
BrandName
,
"supplier_name"
:
t
.
supplierName
,
"class_id1"
:
0
,
"class_id2"
:
0
,
"class_id3"
:
0
,
"spu_id"
:
skuInfo
.
SpuId
,
"spu_id"
:
common
.
MyInt64
(
skuInfo
.
SpuId
)
,
"sale_time"
:
nowTime
,
"create_time"
:
nowTime
,
"update_time"
:
nowTime
,
...
...
@@ -343,6 +384,7 @@ func (t * CommonLyService) esSkuInfo(skuInfo *model.LyClearGoodsList) {
/*
推入队列=》(斌 go队列去消费更新db+mongodb..等操作)
eg: {"pn":"mouser","stock":[1,3000],"tiered":[[1,0.1],[10000,0.002],[150000,0.001]],"increment":1,"sku_id":1158755507240064444,"brand_name":"Kamaya","goods_name":"RMC10-122JTH","goods_sn":"791-RMC10-122JTH"}
*/
func
(
t
*
CommonLyService
)
updateSkuDB
(
skuInfo
*
model
.
LyClearGoodsList
)
{
if
skuInfo
.
SkuId
==
""
{
...
...
@@ -350,21 +392,19 @@ func (t * CommonLyService) updateSkuDB(skuInfo *model.LyClearGoodsList) {
}
param
:=
map
[
string
]
interface
{}{
"pn"
:
t
.
supplierName
,
"stock"
:
skuInfo
.
Stock
,
"stock"
:
[]
int64
{
skuInfo
.
Moq
,
skuInfo
.
Stock
},
//最小起订量+库存
"tiered"
:
skuInfo
.
PriceTemp
,
"increment"
:
skuInfo
.
Increment
,
"sku_id"
:
skuInfo
.
SkuId
,
"sku_id"
:
common
.
MyInt64
(
skuInfo
.
SkuId
)
,
"brand_name"
:
skuInfo
.
BrandName
,
"goods_name"
:
skuInfo
.
GoodsName
,
"goods_sn"
:
skuInfo
.
GoodsSn
,
}
paramjson
,
_
:=
json
.
Marshal
(
param
)
if
t
.
flag
==
"101"
{
t
.
flagStr
=
"<br/><br/>"
;
t
.
flagStr
=
"推送到go更新db:"
;
t
.
flagStr
=
string
(
paramjson
);
t
.
flagStr
=
"<br/><br/>"
;
}
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"推送到go更新db--search_sku_update_list:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
string
(
paramjson
))
mq
.
PushMsg
(
config
.
Get
(
"rabmq_all.SEARCH_SKU_UPDATE_LIST"
)
.
String
(),
string
(
paramjson
))
}
...
...
@@ -380,12 +420,10 @@ func (t *CommonLyService) pushEs() {
}
param
:=
strings
.
Join
(
t
.
skuEsUpdataList
,
"
\n
"
)
+
"
\n
"
result
,
_
:=
es
.
BulkES
(
param
)
//批量插入
if
t
.
flag
==
"101"
{
t
.
flagStr
+=
"<br/>"
t
.
flagStr
+=
"插入es:"
t
.
flagStr
+=
param
t
.
flagStr
+=
result
}
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"插入或更新es:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"请求参数:"
+
param
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"返回结果:"
+
result
)
}
/*
新增sku,组装好数据推送到db(斌哥队列消费后 更新 mongodb+mysql 等)
...
...
@@ -417,12 +455,10 @@ func (t * CommonLyService) createSkuDB(skuInfo *model.LyClearGoodsList) {
paramjson
,
_
:=
json
.
Marshal
(
param
)
paramStr
:=
string
(
paramjson
)
if
t
.
flag
==
"101"
{
t
.
flagStr
=
"<br/><br/>"
;
t
.
flagStr
=
"推送到go新增db:"
;
t
.
flagStr
=
paramStr
;
t
.
flagStr
=
"<br/><br/>"
;
}
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"推送到go新增db:"
)
common
.
PrintDebugHtml
(
t
.
ctx
,
&
t
.
flag
,
"请求参数:"
+
paramStr
)
mq
.
PushMsg
(
config
.
Get
(
"rabmq_all.SEARCH_SKU_LIST"
)
.
String
(),
paramStr
)
//推送队列
//记录日志
logger
.
Log
(
paramStr
,
"sku_create"
)
...
...
service/ly/mouser.go
View file @
fb447380
This diff is collapsed.
Click to expand it.
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