Commit 6d1f8af8 by mushishixian

获取商品服务

parent 930b9c90
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
2 ="verical" 2 ="verical"
3 ="tme" 3 ="tme"
#相似物料供应商
[alike_supplier]
1 = "ti"
[common] [common]
'MOUSER_API'='http://footstone.liexin.net/webapi/handle_mouser' 'MOUSER_API'='http://footstone.liexin.net/webapi/handle_mouser'
......
...@@ -9,10 +9,6 @@ import ( ...@@ -9,10 +9,6 @@ import (
//搜索型号 //搜索型号
func AutoSpu(c *gin.Context) { func AutoSpu(c *gin.Context) {
service1:= service.GoodsService{}
res1,_:=service1.GetGoodsInfoByApi("1151005920927710088,1150961641855982990,1150961724815213435")
c.JSON(200,res1)
return
goodsName, _ := c.GetPostForm("goods_name") goodsName, _ := c.GetPostForm("goods_name")
goods := service.AutoSpu(goodsName) goods := service.AutoSpu(goodsName)
var errCode int var errCode int
......
...@@ -8,24 +8,42 @@ import ( ...@@ -8,24 +8,42 @@ import (
"search_server/service" "search_server/service"
) )
//搜索快手搜搜 //搜索快手搜搜
//check_button=2&pf=1&keyword=123213&supplier_id=7&offset=2&follow_hit=1&p=1&_=1593582814402 //check_button=2&pf=1&keyword=123213&supplier_id=7&offset=2&follow_hit=1&p=1&_=1593582814402
func QuoteIndex(c *gin.Context) { func QuoteIndex(c *gin.Context) {
var request requests.QuoteIndexRequest var (
request requests.QuoteIndexRequest
)
//绑定参数到结构体
if err := c.ShouldBind(&request); err != nil { if err := c.ShouldBind(&request); err != nil {
fmt.Println(err) 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 return
} }
supplierName := "temp" if request.FollowHit != 0 && request.FollowHit != 1 {
if supplierName == "" { service.GetGoodsBySupplier(request)
c.JSON(200, common.GetResponse(1, "供应商id有误", []string{})) } else {
return
} }
service.GetGoodsInfoBySupplier(request) flag := 1
if true {
flag = 0
}
//构建返回
c.JSON(200, common.Response{
ErrCode: flag,
ErrMsg: "",
Data: nil,
})
} }
package requests package requests
import (
"github.com/syyongx/php2go"
"search_server/pkg/config"
)
type QuoteIndexRequest struct { type QuoteIndexRequest struct {
Keyword string `form:"key_word"` Keyword string `form:"keyword"`
CheckButton int `form:"check_button"` CheckButton int `form:"check_button"`
SupplierId string `form:"supplier_id"` SupplierId string `form:"supplier_id"`
Offset int `form:"offset"` Offset int `form:"offset"`
...@@ -18,3 +23,23 @@ type QuoteIndexRequest struct { ...@@ -18,3 +23,23 @@ type QuoteIndexRequest struct {
P int `form:"p"` P int `form:"p"`
Topic int `form:"topic"` 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
}
...@@ -17,8 +17,10 @@ func InitRouter() *gin.Engine { ...@@ -17,8 +17,10 @@ func InitRouter() *gin.Engine {
r.POST("/search/bom/recommend", controller.Recommend) r.POST("/search/bom/recommend", controller.Recommend)
r.POST("search/ZiYing/zyh", controller.Zyh) 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 return r
} }
...@@ -48,7 +48,7 @@ func SearchAttr(attrOrigin, encap string) (goodsName string, err error) { ...@@ -48,7 +48,7 @@ func SearchAttr(attrOrigin, encap string) (goodsName string, err error) {
numberR, _ := regexp.Compile(common.PureNumberRegular) numberR, _ := regexp.Compile(common.PureNumberRegular)
pureNumber := numberR.FindString(encap) pureNumber := numberR.FindString(encap)
//再去找对应属性 //再去找对应属性
redisConn := gredis.Conn("default_redis_read") redisConn := gredis.Conn("search_r")
defer redisConn.Close() defer redisConn.Close()
attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber)) attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber))
if attrName != "" { if attrName != "" {
...@@ -203,7 +203,7 @@ func getAttrValueByAttr(attr string) (attrValue string) { ...@@ -203,7 +203,7 @@ func getAttrValueByAttr(attr string) (attrValue string) {
} }
} else { } else {
//再去找没有单位的对应属性 //再去找没有单位的对应属性
redisConn := gredis.Conn("default_redis_read") redisConn := gredis.Conn("search_r")
defer redisConn.Close() defer redisConn.Close()
attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber)) attrName, _ := redis.String(redisConn.Do("HGET", "sku_map2", pureNumber))
if attrName != "" { if attrName != "" {
......
...@@ -101,8 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) { ...@@ -101,8 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList = append(goodsIdList, goodsId) goodsIdList = append(goodsIdList, goodsId)
} }
goodsIdListStr = strings.Join(goodsIdList, ",") goodsIdListStr = strings.Join(goodsIdList, ",")
goodsService := GoodsService{} goodsList, err := GetGoodsInfoByApi(goodsIdListStr)
goodsList, err := goodsService.GetGoodsInfoByApi(goodsIdListStr)
response.Data = goodsList response.Data = goodsList
response.Flag = req.Flag response.Flag = req.Flag
return &response return &response
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
"github.com/imroc/req" "github.com/imroc/req"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/uniplaces/carbon" "github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5" "gopkg.in/olivere/elastic.v5"
...@@ -16,11 +17,8 @@ import ( ...@@ -16,11 +17,8 @@ import (
"strings" "strings"
) )
type GoodsService struct {
}
//获取商品信息,需要传入userId用于判断是否登陆 //获取商品信息,需要传入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) isNewCustomer, isMember := CheckIsNewCustomer(userId)
params := req.Param{ params := req.Param{
"goods_id": goodsIdsStr, "goods_id": goodsIdsStr,
...@@ -32,7 +30,7 @@ func (gs *GoodsService) GetGoodsInfo(goodsIdsStr string, userId int) (goodsListM ...@@ -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{ params := req.Param{
"goods_id": goodsIdsStr, "goods_id": goodsIdsStr,
} }
...@@ -184,6 +182,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []*model.Api ...@@ -184,6 +182,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []*model.Api
return return
} }
//判断是否是新客价
func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) { func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
//判断新客价 //判断新客价
if userId != 0 { if userId != 0 {
...@@ -209,22 +208,44 @@ func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) { ...@@ -209,22 +208,44 @@ func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
} }
//根据供应商获取商品信息 //根据供应商获取商品信息
func GetGoodsInfoBySupplier(req requests.QuoteIndexRequest) error { func GetGoodsBySupplier(r requests.QuoteIndexRequest) (err error) {
supplierIndex := config.Get("es.search_supplier").String() //索引字典
supplierIndex := getSupplierIndex(r.SupplierId)
//获取查询条件 //获取查询条件
queryString, err := getSupplierQuery(req) queryString, err := getSupplierQuery(r)
queryString = ""
res, err := es.CurlES(supplierIndex, queryString) res, err := es.CurlES(supplierIndex, queryString)
if err != nil { goodsIds := GetGoodsIdsByEs(res)
return err //if err != nil {
} // return err
fmt.Println(res) //}
goodsIdsStr := strings.Join(goodsIds, ",")
goodsList, err := GetGoodsInfo(goodsIdsStr, 0)
fmt.Println(goodsList)
return nil return nil
} }
func getSupplierIndex(supplierId string) string { func getSupplierIndex(supplierId string) (supplierIndex string) {
return "temp" 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) { func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
req.SupplierId = strings.TrimSpace(req.SupplierId) req.SupplierId = strings.TrimSpace(req.SupplierId)
source := elastic.NewSearchSource() source := elastic.NewSearchSource()
...@@ -242,6 +263,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -242,6 +263,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp)) query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp))
keyword := strings.ToUpper(req.Keyword) keyword := strings.ToUpper(req.Keyword)
//判断关键词是否为空 //判断关键词是否为空
if req.Keyword != "" { if req.Keyword != "" {
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`) r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
...@@ -250,7 +272,6 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -250,7 +272,6 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`) r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
req.Keyword = r2.ReplaceAllString(removeCnKeyword, "") req.Keyword = r2.ReplaceAllString(removeCnKeyword, "")
//
if req.IsExact == 1 { if req.IsExact == 1 {
query.Must(elastic.NewTermQuery("auto_goods_name.raw", keyword)) query.Must(elastic.NewTermQuery("auto_goods_name.raw", keyword))
} else { } else {
...@@ -265,8 +286,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -265,8 +286,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
} }
//判断是否存在类似物料搜索 //判断是否存在类似物料搜索
//todo:类似索引获取 supplierIndexName := getSupplierIndex(req.SupplierId)
if true { //获取相似物料供应商列表
alikeSuppliers := config.Cfg.Section("alike_supplier").KeyStrings()
if php2go.InArray(supplierIndexName, alikeSuppliers) && keyword != "" {
query.Should(elastic.NewTermQuery("goods_name_alike", keyword)) query.Should(elastic.NewTermQuery("goods_name_alike", keyword))
} }
...@@ -275,7 +298,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -275,7 +298,7 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
//判断库存 //判断库存
if req.Stock > 0 { if req.Stock > 0 {
query.Must(elastic.NewRangeQuery("stick").Gte(req.Stock)) query.Must(elastic.NewRangeQuery("stock").Gte(req.Stock))
} }
//判断是否要根据品牌id搜索 //判断是否要根据品牌id搜索
...@@ -382,8 +405,18 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -382,8 +405,18 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
for _, brandId := range excludeBrandId { for _, brandId := range excludeBrandId {
query.MustNot(elastic.NewTermQuery("brand_id", brandId)) query.MustNot(elastic.NewTermQuery("brand_id", brandId))
} }
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source) searchRequest := elastic.NewSearchRequest().Source(source)
return searchRequest.Body() 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment