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
743fcefb
authored
Jul 03, 2020
by
mushishixian
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Fix
parent
c4d0e56e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
86 additions
and
75 deletions
controller/bom_controller.go
pkg/common/response.go
pkg/gredis/redis.go
service/bom_service.go
service/common_ly_service.go
service/goods_service.go
test/test.go
controller/bom_controller.go
View file @
743fcefb
...
...
@@ -15,7 +15,7 @@ func AutoSpu(c *gin.Context) {
if
len
(
goods
)
==
0
{
errCode
=
1
}
res
:=
common
.
Response
{
res
:=
common
.
Bom
Response
{
ErrCode
:
errCode
,
ErrMsg
:
""
,
Data
:
goods
,
...
...
pkg/common/response.go
View file @
743fcefb
package
common
type
Response
struct
{
ErrCode
int
`json:"err_code"`
ErrCode
int
`json:"err
or
_code"`
ErrMsg
string
`json:"err_msg"`
Data
interface
{}
`json:"data"`
}
type
BomResponse
struct
{
ErrCode
int
`json:"err_code"`
ErrMsg
string
`json:"err_msg"`
ErrCode
int
`json:"err
or
_code"`
ErrMsg
string
`json:"err
or
_msg"`
Flag
int
`json:"flag"`
Total
int
`json:"total"`
Data
interface
{}
`json:"data"`
...
...
pkg/gredis/redis.go
View file @
743fcefb
...
...
@@ -7,35 +7,24 @@ import (
"time"
)
type
IchuntRedis
struct
{
RedisList
map
[
string
]
*
redis
.
Pool
Current
*
redis
.
Pool
}
func
(
this
*
IchuntRedis
)
Connection
(
connection
string
)
(
*
IchuntRedis
){
this
.
Current
=
this
.
RedisList
[
connection
]
return
this
}
var
writeConn
,
readConn
*
redis
.
Pool
func
Setup
()
(
err
error
)
{
ichuntRedis
:=
&
IchuntRedis
{}
ichuntRedis
.
RedisList
=
make
(
map
[
string
]
*
redis
.
Pool
,
0
)
RedisDatabaseMap
:=
config
.
BuildRedisConfgs
()
for
redisKey
,
redisConfig
:=
range
RedisDatabaseMap
{
ichuntRedis
.
RedisList
[
redisKey
],
err
=
getConn
(
redisConfig
.
Host
,
redisConfig
.
Password
)
if
err
!=
nil
{
panic
(
err
)
}
writeHost
:=
config
.
Get
(
"redis.write_host"
)
.
String
()
readHost
:=
config
.
Get
(
"redis.read_host"
)
.
String
()
writePassword
:=
config
.
Get
(
"redis.write_password"
)
.
String
()
readPassword
:=
config
.
Get
(
"redis.read_password"
)
.
String
()
writeConn
,
err
=
getConn
(
writeHost
,
writePassword
)
if
err
!=
nil
{
return
}
readConn
,
err
=
getConn
(
readHost
,
readPassword
)
if
err
!=
nil
{
return
}
return
nil
}
func
getConn
(
writeHost
,
password
string
)
(
pool
*
redis
.
Pool
,
err
error
)
{
maxIdle
,
_
:=
config
.
Get
(
"redis.max_idle"
)
.
Int
()
maxActive
,
_
:=
config
.
Get
(
"redis.max_active"
)
.
Int
()
...
...
@@ -65,8 +54,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) {
//最基础的键值操作
func
(
this
*
IchuntRedis
)
Set
(
key
string
,
data
interface
{})
error
{
conn
:=
this
.
Current
.
Get
()
func
Set
(
key
string
,
data
interface
{})
error
{
conn
:=
writeConn
.
Get
()
defer
conn
.
Close
()
value
,
err
:=
json
.
Marshal
(
data
)
...
...
@@ -82,26 +71,8 @@ func (this *IchuntRedis) Set(key string, data interface{}) error {
return
nil
}
//Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。
func
(
this
*
IchuntRedis
)
Setnx
(
key
string
,
data
interface
{})
error
{
conn
:=
this
.
Current
.
Get
()
defer
conn
.
Close
()
value
,
err
:=
json
.
Marshal
(
data
)
if
err
!=
nil
{
return
err
}
_
,
err
=
conn
.
Do
(
"SETNX"
,
key
,
value
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
(
this
*
IchuntRedis
)
Exists
(
key
string
)
bool
{
conn
:=
this
.
Current
.
Get
()
func
Exists
(
key
string
)
bool
{
conn
:=
readConn
.
Get
()
defer
conn
.
Close
()
exists
,
err
:=
redis
.
Bool
(
conn
.
Do
(
"EXISTS"
,
key
))
...
...
@@ -112,8 +83,8 @@ func(this *IchuntRedis) Exists(key string) bool {
return
exists
}
func
(
this
*
IchuntRedis
)
Get
(
key
string
)
(
interface
{},
error
)
{
conn
:=
this
.
Current
.
Get
()
func
Get
(
key
string
)
(
interface
{},
error
)
{
conn
:=
readConn
.
Get
()
defer
conn
.
Close
()
reply
,
err
:=
conn
.
Do
(
"GET"
,
key
)
...
...
@@ -124,8 +95,8 @@ func(this *IchuntRedis) Get(key string) (interface{}, error) {
return
reply
,
nil
}
func
(
this
*
IchuntRedis
)
Delete
(
key
string
)
(
bool
,
error
)
{
conn
:=
this
.
Current
.
Get
()
func
Delete
(
key
string
)
(
bool
,
error
)
{
conn
:=
writeConn
.
Get
()
defer
conn
.
Close
()
return
redis
.
Bool
(
conn
.
Do
(
"DEL"
,
key
))
...
...
@@ -133,8 +104,8 @@ func(this *IchuntRedis) Delete(key string) (bool, error) {
//哈希操作
func
(
this
*
IchuntRedis
)
HSet
(
key
string
,
k
interface
{},
data
interface
{})
error
{
conn
:=
this
.
Current
.
Get
()
func
HSet
(
key
string
,
k
interface
{},
data
interface
{})
error
{
conn
:=
writeConn
.
Get
()
defer
conn
.
Close
()
value
,
err
:=
json
.
Marshal
(
data
)
...
...
@@ -150,8 +121,8 @@ func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error
return
nil
}
func
(
this
*
IchuntRedis
)
HGet
(
key
string
,
k
interface
{})
(
interface
{},
error
)
{
conn
:=
this
.
Current
.
Get
()
func
HGet
(
key
string
,
k
interface
{})
(
interface
{},
error
)
{
conn
:=
readConn
.
Get
()
defer
conn
.
Close
()
reply
,
err
:=
conn
.
Do
(
"HGET"
,
key
,
k
)
...
...
@@ -162,10 +133,9 @@ func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) {
return
reply
,
nil
}
func
(
this
*
IchuntRedis
)
HDelete
(
key
string
,
k
interface
{})
(
bool
,
error
)
{
conn
:=
this
.
Current
.
Get
()
func
HDelete
(
key
string
,
k
interface
{})
(
bool
,
error
)
{
conn
:=
writeConn
.
Get
()
defer
conn
.
Close
()
return
redis
.
Bool
(
conn
.
Do
(
"HDEL"
,
key
,
k
))
}
service/bom_service.go
View file @
743fcefb
...
...
@@ -101,7 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList
=
append
(
goodsIdList
,
goodsId
)
}
goodsIdListStr
=
strings
.
Join
(
goodsIdList
,
","
)
goodsList
,
err
:=
GetGoodsInfo
(
goodsIdListStr
)
goodsList
,
err
:=
GetGoodsInfo
ByApi
(
goodsIdListStr
,
1
)
response
.
Data
=
goodsList
response
.
Flag
=
req
.
Flag
return
&
response
...
...
service/common_ly_service.go
View file @
743fcefb
...
...
@@ -6,7 +6,6 @@ import (
_
"github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/config"
"search_server/pkg/gredis"
"strings"
)
...
...
@@ -40,15 +39,16 @@ func getSkuByGoodsSn(goods_list map[string]*model.LyClearGoodsList, supplier_inf
//
//fmt.Println(productList)
//return productList
originGoods
:=
make
(
map
[
string
]
interface
{},
0
)
sku_uique_judge
:=
config
.
Get
(
"redis_all.SKU_UNIQUE_JUDGE"
)
.
String
()
for
goods_sn
,
info
:=
range
goods_list
{
sn_sku
:=
php2go
.
Md5
(
strings
.
ToLower
(
goods_sn
))
sku_id
,
_
:=
gredis
.
HGet
(
sku_uique_judge
,
sn_sku
)
//查询唯一值,反查sku_id
if
sku_id
==
""
{
//为空,先创建sku
fmt
.
Println
(
sku_uique_judge
,
sn_sku
)
//sku_id, _ := gredis.IchuntRedis.Hget(sku_uique_judge, sn_sku) //查询唯一值,反查sku_id
sku_id
:=
""
if
sku_id
==
""
{
//为空,先创建sku
//lock_key := "searchapi_"+sn_sku;
//flag := gredis.Setnx(lock_key,php2go.Time()+2)
//if flag { //存在锁
...
...
service/goods_service.go
View file @
743fcefb
package
service
import
(
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/imroc/req"
"github.com/tidwall/gjson"
"github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/protopb/bom"
"search_server/requests"
"strings"
)
func
GetGoodsInfo
(
goodsIdsStr
string
)
(
goodsList
[]
*
bom
.
GoodsModel
,
err
error
)
{
//获取商品信息
//需要传入userId用于判断是否登陆
func
GetGoodsInfoByApi
(
goodsIdsStr
string
,
userId
int
)
(
goodsList
[]
*
bom
.
GoodsModel
,
err
error
)
{
isNewCustomer
,
isMember
:=
CheckIsNewCustomer
(
userId
)
goodsServerUrl
:=
config
.
Get
(
"goods.api_url"
)
.
String
()
params
:=
req
.
Param
{
"goods_id"
:
goodsIdsStr
,
"goods_id"
:
goodsIdsStr
,
"power[newCustomer]"
:
isNewCustomer
,
"power[member]"
:
isMember
,
}
//fmt.Println(goodsIdsStr)
resp
,
err
:=
req
.
Post
(
goodsServerUrl
+
"/synchronization"
,
params
)
...
...
@@ -70,16 +80,38 @@ func GetGoodsInfo(goodsIdsStr string) (goodsList []*bom.GoodsModel, err error) {
return
}
func
CheckIsNewCustomer
(
userId
int
)
(
isNewCustomer
,
isMember
bool
)
{
//判断新客价
if
userId
!=
0
{
userInfoStr
,
err
:=
redis
.
String
(
gredis
.
HGet
(
"api_user"
,
userId
))
if
err
!=
nil
{
return
}
// 新客户显示新客价
isNewExist
:=
gjson
.
Get
(
userInfoStr
,
"is_new"
)
.
Exists
()
isNew
:=
gjson
.
Get
(
userInfoStr
,
"is_new"
)
.
Int
()
if
isNewExist
&&
isNew
==
0
{
isNewCustomer
=
true
}
isMember
=
true
}
else
{
// 未登录显示新客价
isNewCustomer
=
true
isMember
=
false
}
return
}
//根据供应商获取商品信息
func
GetGoodsInfoBySupplier
(
req
requests
.
QuoteIndexRequest
)
error
{
//
supplierIndex := config.Get("es.search_supplier").String()
//
//
获取查询条件
//queryString
:= getSupplierQuery(req)
//
res, err := es.CurlES(supplierIndex, queryString)
//
if err != nil {
//
return err
//
}
//fmt.Println(supplierIndex
)
supplierIndex
:=
config
.
Get
(
"es.search_supplier"
)
.
String
()
//获取查询条件
queryString
,
err
:=
getSupplierQuery
(
req
)
res
,
err
:=
es
.
CurlES
(
supplierIndex
,
queryString
)
if
err
!=
nil
{
return
err
}
fmt
.
Println
(
res
)
return
nil
}
...
...
@@ -240,7 +272,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
//todo : $params['preference'] = '_primary_first';
//判断是否要排除某些品牌
//todo : 待确定需不需要接入
excludeBrandId
:=
model
.
GetExcludeBrandIds
(
1
)
for
_
,
brandId
:=
range
excludeBrandId
{
query
.
MustNot
(
elastic
.
NewTermQuery
(
"brand_id"
,
brandId
))
}
searchRequest
:=
elastic
.
NewSearchRequest
()
.
Source
(
source
)
...
...
test/test.go
View file @
743fcefb
...
...
@@ -22,6 +22,12 @@ func main() {
mysql
.
Connection
(
"bom"
)
.
Raw
(
"SELECT * FROM lie_bin"
)
.
Scan
(
&
result
)
//mysql.Connection()("bom").Table("bin").Find(&result)
fmt
.
Println
(
result
)
//fmt.Println(model.GetExcludeBrandIds(1))
//client := client.
//c := course.NewCourseService("go.micro.api.jtthink.course", client)
//course_rsp, _ := c.ListForTop(context.TODO(), &course.ListRequest{Size: 10})
//fmt.Println(course_rsp.Result)
type
Result2
struct
{
BusinessName
string
`json:"business_name"`
...
...
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