Commit 7c4539de by mushishixian

othercontroller

parent c6c884f9
[rabmq]
url = amqp:;guest:guest@192.168.2.232:5672/
url = amqp://guest:guest@192.168.2.232:5672/
;存放本系统所有的队列名称
[rabmq_all]
; bom任务id
......
......@@ -13,7 +13,7 @@
12 = alliedelec
13 = avnet
14 = mouser
16 = liexin_lianying1
#16 = liexin_lianying1
17 = zhuanmai
18 = liexin_ti
19 = peigenesis
......@@ -34,6 +34,35 @@
19 = peigenesis
20 = powell
21 = rs
[SEARCH_SUPPLIER]
1 = future
#2 = powerandsignal
3 = rochester
4 = tme
5 = verical
6 = element14
7 = digikey
8 = chip1stop
9 = aipco
10 = arrow
#11 = bisco
12 = alliedelec
13 = avnet
14 = mouser
#15 = company
#16 = liexin_lianying1
17 = zhuanmai
#18 = liexin_ti
19 = peigenesis
20 = powell
21 = rs
#22 = liexin_sell
#100 = ziying
#101 = liexin_jingdiao
1676 = buerklin
#相似物料供应商
[ALIKE_SUPPLIER]
1 = ti
......
package controller
import (
"github.com/gin-gonic/gin"
"regexp"
"search_server/pkg/common"
"search_server/requests"
"search_server/service"
"strings"
)
//查询联营型号是否有库存
func CheckHasStock(c *gin.Context) {
noRule, _ := c.GetPostForm("no_rule")
if noRule != "1122" {
flag := common.CheckSignApi()
if flag > 0 {
common.Output(flag, "验证不通过:", nil)
return
}
}
goodsName, _ := c.GetPostForm("goods_name")
if goodsName == "" {
common.Output(1001, "型号不存在", nil)
return
}
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName = replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
otherService := service.OtherService{}
hasStock, err := otherService.CheckHasStock(goodsName)
if err != nil {
common.Output(1, "查询失败,系统报错"+err.Error(), nil)
return
}
common.Output(0, "查询成功:"+goodsName, hasStock)
return
}
//精确搜索型号名称
func ExactGoods(c *gin.Context) {
var r requests.ExactGoodsRequest
c.ShouldBind(&r)
if r.GoodsName == "" {
common.Output(1, "查询型号名称不得为空", nil)
return
}
//查询条件
otherService := service.OtherService{}
otherService.ExactGoods(r.SupplierId, r.GoodsName)
}
......@@ -161,7 +161,8 @@ func CopyMapString(distmap map[string]string) map[string]string {
}
//反爬虫加密验证
func CheckSignApi(params map[string]string) (resNo int) {
func CheckSignApi() (resNo int) {
params := make(map[string]string)
ctx := middleware.Context
if ctx == nil {
return
......
package requests
type ExactGoodsRequest struct {
SupplierId string `json:"supplier_id"`
GoodsName string `json:"goods_name"`
}
......@@ -32,6 +32,10 @@ func InitRouter() *gin.Engine {
r.GET("/search/get_data", controller.GetDataIndex)
r.POST("/search/get_data", controller.GetDataIndex)
//Other控制器相关
r.POST("/search/other/hasStock", controller.CheckHasStock)
r.POST("/search/other/exactGoods", controller.ExactGoods)
r.GET("/test", func(c *gin.Context) {
common.Output(1000, "testest", nil)
})
......
package service
import (
"fmt"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"regexp"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/service/query"
"strings"
)
type OtherService struct {
}
func (os *OtherService) CheckHasStock(goodsName string) (hasStock int, err error) {
//获取所有供应商名称
supplierAllSlice := config.GetSectionValues("SUPPLIER_ALL")
index := strings.Join(supplierAllSlice, ",")
queryString := query.GetCheckStockQuery(goodsName)
esResult, err := es.CurlES(index, queryString)
if err != nil {
return
}
fmt.Println(queryString)
//获取是否有结果,有结果就是有库存
total := gjson.Get(esResult, "hits.total").Int()
if total > 0 {
hasStock = 1
}
return
}
func (os *OtherService) ExactGoods(supplierId, goodsName string) {
//获取索引
index := getExactGoodsIndex(supplierId)
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文和特殊字符串
removeCnGoodsName := r1.ReplaceAllString(goodsName, "")
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
goodsName = r2.ReplaceAllString(removeCnGoodsName, "")
//查询条件
queryString := query.GetExactGoodsQuery(goodsName)
esResult, err := es.CurlES(index, queryString)
if err != nil {
return
}
//最终结果接受者
var result interface{}
//所有供应商
if supplierId == "-1" {
var goodsList []interface{}
for _, jsonItem := range gjson.Get(esResult, "hits.hits").Array() {
goods := make(map[string]string)
goods["_index"] = jsonItem.Get("_index").String()
goods["goods_id"] = jsonItem.Get("_source.goods_id").String()
goods["old_goods_id"] = jsonItem.Get("_source.old_goods_id").String()
goodsList = append(goodsList, goods)
}
result = goodsList
} else {
//获取所有的商品id
ids := gjson.Get(esResult, "hits.hits.#._id").Array()
var goodsIds []string
for _, id := range ids {
goodsIds = append(goodsIds, id.String())
}
result = goodsIds
}
common.Output(0, "", result)
return
}
//获取这个方法对应的索引
func getExactGoodsIndex(supplierId string) (index string) {
supplierAllSlice := config.GetSectionValues("SEARCH_SUPPLIER")
supplierAllMap := config.Cfg.Section("SEARCH_SUPPLIER").KeysHash()
searchSupplierIds := config.Cfg.Section("SEARCH_SUPPLIER").KeyStrings()
if supplierId == "-1" {
index = strings.Join(supplierAllSlice, ",") + "," + config.Get("ZIYING_CONFIG.Index").String()
} else {
if php2go.InArray(supplierId, searchSupplierIds) {
index = supplierAllMap[supplierId]
} else {
//不存在就去搜索自营的
index = config.Get("ZIYING_CONFIG.Index").String()
}
}
return
}
package query
import (
"gopkg.in/olivere/elastic.v5"
)
//获取查询语句
func GetCheckStockQuery(goodsName string) (queryString string) {
query := elastic.NewBoolQuery()
query.Filter(elastic.NewRangeQuery("stock").Gt(0))
query.Filter(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
source := elastic.NewSearchSource()
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_name", "stock"))
source.Size(1)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
queryString, _ = searchRequest.Body()
return
}
//精确匹配商品的查询语句
func GetExactGoodsQuery(goodsName string) (queryString string) {
query := elastic.NewBoolQuery()
query.Must(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
source := elastic.NewSearchSource()
source.From(0)
source.Size(200)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source).Preference("_primary_first")
queryString, _ = searchRequest.Body()
return
}
......@@ -191,7 +191,7 @@ func GetGoodsBySupplierIdQuery(r requests.GetGoodsBySupplierRequest) (string, er
//判断关键词是否为空
if r.Keyword != "" {
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文?
//去除中文
removeCnKeyword := r1.ReplaceAllString(r.Keyword, "")
r2, _ := regexp.Compile(`/[^A-Za-z0-9]+/`)
r.Keyword = r2.ReplaceAllString(removeCnKeyword, "")
......
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