Commit 02ea1cbe by mushishixian

Merge branch 'ysx-quote-20200628' into dev

parents 4e661ace eb0b687e
......@@ -23,4 +23,22 @@ password = spu
host = 192.168.1.234
database = liexin_spu
table_prefix =lie_
type = mysql
\ No newline at end of file
type = mysql
#线上地址
#SupDbUser:Supssy2@@!!@$#yxy@172.18.137.21:3306/liexin_supp
[supp]
user_name = liexin_ass
password = liexin_ass#zsyM
host = 192.168.2.232
database = liexin_ass
table_prefix =lie_
type = mysql
[cms]
user_name = ichuntcms
password = ichuntcms#zsyM
host = 192.168.2.232
database = ichuntcms
table_prefix =
type = mysql
[supplier_all]
1 = "rochester"
2 = "verical"
3 = "tme"
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
16 = liexin_lianying1
17 = zhuanmai
18 = liexin_ti
19 = peigenesis
20 = powell
21 = rs
1676 = buerklin
#相似物料供应商
[alike_supplier]
1 = "ti"
[common]
MOUSER_API='http://footstone.liexin.net/webapi/handle_mouser'
MOUSER_API = 'http://footstone.liexin.net/webapi/handle_mouser'
[brand]
......
......@@ -3,49 +3,138 @@ package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"search_server/dao"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/requests"
"search_server/service"
"search_server/service/transformer"
"strings"
)
//搜索快手搜搜
//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 r requests.QuoteIndexRequest
//绑定参数到结构体
if err := c.ShouldBind(&request); err != nil {
if err := c.ShouldBind(&r); err != nil {
fmt.Println(err)
}
//检查参数,有错误就返回错误
errCode, errMsg := requests.CheckQuoteRequest(request)
errCode, errMsg := requests.CheckQuoteRequest(r)
if errCode != 0 {
c.JSON(200, common.Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: []string{},
})
c.JSON(200, common.ErrorResponse(errCode, errMsg))
return
}
var res interface{}
if request.FollowHit != 0 && request.FollowHit != 1 {
res,_ = service.GetGoodsBySupplier(request)
flag := 1
var res map[string]interface{}
if r.FollowHit != 0 && r.FollowHit != 1 {
res, _ = service.GetGoodsBySupplier(r)
if len(res) > 0 {
flag = 0
}
c.JSON(200, common.SuccessResponse(flag, "", res))
return
}
//获取供应商名称
supplierNameMap := config.Cfg.Section("supplier_all").KeysHash()
supplierName, exist := supplierNameMap[r.SupplierId]
if !exist {
c.JSON(200, common.ErrorResponse(1, "供应商有误"))
return
}
if supplierName == "mouser" {
//mouser同步请求搜索
res, _ = mouserOrg(r.Keyword, r.SupplierId)
} else {
res,_ = service.GetGoodsBySupplier(request)
res, _ = service.GetGoodsBySupplierName(r)
}
flag := 1
if true {
if len(res) > 0 {
flag = 0
}
//构建返回
c.JSON(200, common.Response{
ErrCode: flag,
ErrMsg: "",
Data: res,
})
c.JSON(200, common.SuccessResponse(flag, "", res))
return
}
func mouserOrg(keyword, suppliedId string) (result map[string]interface{}, err error) {
mouserRes := service.OutLink(&keyword, "-1")
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
skuMouser := make(map[string]string)
var goodsIds []string
for k, mouseItem := range mouserRes {
if k == "N/A" {
continue
}
snSku := php2go.Md5(strings.ToUpper(k + "_" + suppliedId))
skuId, _ := redis.String(redisCon.Do("HGET", config.Get("SKU_UNIQUE_JUDGE").String(), snSku))
if skuId != "" {
skuMouser[skuId] = mouseItem.RestrictionMessage
goodsIds = append(goodsIds, skuId)
}
}
goodsList, err := service.GetGoodsInfo(goodsIds)
if err != nil {
return
}
dullData, err := transformer.DullDataInfo(keyword, goodsList)
result = service.GetReturnSupplierData(dullData)
if err != nil {
return
}
result["total"] = len(skuMouser)
return
}
func GetDataPur(c *gin.Context) {
supplierData, err := dao.GetAllSupplierCodeAndUid()
if err != nil {
c.String(200, err.Error())
return
}
intracode, err := dao.GetAllIntraCode()
if err != nil {
c.String(200, err.Error())
return
}
userInfo, err := dao.GetAllUserBaseInfo()
if err != nil {
c.String(200, err.Error())
return
}
temp := make(map[int]string)
for _, user := range userInfo {
if user.Name == "" && user.Email != "" {
temp[user.UserId] = user.Email
} else {
temp[user.UserId] = user.Name
}
}
temp2 := make(map[int]string)
for _, code := range intracode {
if _, exist := temp[code.AdminId]; exist {
temp2[code.CodeId] = temp[code.AdminId]
} else {
temp2[code.CodeId] = ""
}
}
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
for _, supplier := range supplierData {
if value, exist := temp2[supplier.ChannelUid]; exist {
res, err := redis.Bool(redisCon.Do("HSET", "search_supplier_canaltopurchase_test", supplier.SupplierCode, value))
if err != nil || !res {
c.String(200, "失败")
return
}
}
}
c.String(200, "ok")
return
}
package dao
import (
"search_server/model"
"search_server/pkg/mysql"
)
//获取所有内部编码
func GetAllIntraCode() (intracode []model.Intracode, err error) {
_, err = mysql.Conn("cms").Table("lie_intracode").Select("supplier_code,channel_uid").
Where("status = ?", 2).Get(&intracode)
return
}
//获取所有的用户基础信息
func GetAllUserBaseInfo() (userBaseInfo []model.CmsUserInfo, err error) {
_, err = mysql.Conn("cms").Table("user_info").Select("name,email,userId").Get(&userBaseInfo)
return
}
package dao
import (
"search_server/model"
"search_server/pkg/mysql"
)
//获取供应商编码和渠道开发员id
func GetAllSupplierCodeAndUid() (supplierChannels []model.SupplierChannel, err error) {
_, err = mysql.Conn("supp").Table("lie_supplier_extra").Select("supplier_code,channel_uid").
Where("status = ?", 2).Get(&supplierChannels)
return
}
## 已经有的公用方法(不少控制器都用到的)
####DullGoodsData
####GetGoodsInfo
\ No newline at end of file
package model
//内部编码表
type Intracode struct {
CodeId int `json:"code_id"`
AdminId int `json:"admin_id"`
UserId int `json:"user_id"`
CreateTime int `json:"create_time"`
UpdateTime int `json:"update_time"`
}
type CmsUserInfo struct {
UserId int `json:"userId"`
Name string `json:"name"`
EngName string `json:"engName"`
Email string `json:"email"`
Mobile string `json:"mobile"`
}
\ No newline at end of file
package model
type SupplierChannel struct {
SupplierCode string `json:"supplier_code"`
ChannelUid int `json:"channel_uid"`
}
......@@ -14,10 +14,18 @@ type BomResponse struct {
Data interface{} `json:"data"`
}
func GetResponse(errCode int, errMsg string, data interface{}) Response {
func SuccessResponse(errCode int, errMsg string, data interface{}) Response {
return Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: data,
}
}
func ErrorResponse(errCode int, errMsg string) Response {
return Response{
ErrCode: errCode,
ErrMsg: errMsg,
Data: []string{},
}
}
\ No newline at end of file
......@@ -34,5 +34,12 @@ func BuildDatabaseList() (DatabaseList map[string]BaseDatabase) {
Database: Get("spu.database").String(),
Prefix: Get("spu.table_prefix").String(),
},
"supp": {
UserName: Get("spu.user_name").String(),
Password: Get("spu.password").String(),
Host: Get("spu.host").String(),
Database: Get("spu.database").String(),
Prefix: Get("spu.table_prefix").String(),
},
}
}
......@@ -9,6 +9,7 @@ type QuoteIndexRequest struct {
Keyword string `form:"keyword"`
CheckButton int `form:"check_button"`
SupplierId string `form:"supplier_id"`
SupplierName string `form:"supplier_name"`
Offset int `form:"offset"`
FollowHit int `form:"follow_hit"`
Stock int `form:"stock"`
......
......@@ -24,6 +24,8 @@ func InitRouter() *gin.Engine {
//快手平台相关
r.GET("/search/quote", controller.QuoteIndex)
r.POST("/search/quote", controller.QuoteIndex)
r.GET("/search/quote/getDataPur", controller.GetDataPur)
r.POST("/search/quote/getDataPur", controller.GetDataPur)
return r
}
......@@ -3,18 +3,11 @@ package service
import (
"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"
"regexp"
"search_server/middleware"
"search_server/model"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/requests"
"search_server/service/transformer"
"strconv"
"strings"
)
......@@ -22,14 +15,8 @@ import (
//获取商品信息,需要传入userId用于判断是否登陆
func GetGoodsInfo(goodsIds []string) (goodsListMap map[string]model.ApiGoods, err error) {
var userIdStr string
userIdStr, err = middleware.Context.Cookie("Yo4teW_uid")
if err != nil {
return
}
userId, err := strconv.Atoi(userIdStr)
if err != nil {
return
}
userIdStr, _ = middleware.Context.Cookie("Yo4teW_uid")
userId, _ := strconv.Atoi(userIdStr)
isNewCustomer, isMember := CheckIsNewCustomer(userId)
goodsIdsStr := strings.Join(goodsIds, ",")
params := req.Param{
......@@ -197,7 +184,7 @@ func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []model.ApiG
func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
//判断新客价
if userId != 0 {
redisConn := gredis.Conn("default_redis_read")
redisConn := gredis.Conn("search_r")
defer redisConn.Close()
userInfoStr, err := redis.String(redisConn.Do("HGET", "api_user", userId))
if err != nil {
......@@ -218,221 +205,28 @@ func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
return
}
//根据供应商获取商品信息
func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
//索引字典
supplierIndex := getSupplierIndex(r.SupplierId)
//获取查询条件
queryString, err := getSupplierQuery(r)
queryString = ""
res, err := es.CurlES(supplierIndex, queryString)
goodsIds := GetGoodsIdsByEs(res)
if err != nil {
return
}
goodsList, err := GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
//将供应商信息放到数据最外层,同时整合es相关数据
func GetReturnSupplierData(dullData []model.DullGoodsData) (result map[string]interface{}) {
result = make(map[string]interface{})
result["data"] = dullData
result["total"] = gjson.Get(res, "hits.total").Int()
return result, nil
}
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()
query := elastic.NewBoolQuery()
//增加联营精确搜索
if req.GoodsNameEq != "" {
var goodsName string
r, _ := regexp.Compile(`/[^A-Za-z0-9]+/u`)
goodsName = r.ReplaceAllString(goodsName, "")
query.Must(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
}
//只显示更新时间在一个月内的数据
oneMonthTimeStamp := carbon.Now().SubMonth().Timestamp()
query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp))
keyword := strings.ToUpper(req.Keyword)
//判断关键词是否为空
if req.Keyword != "" {
r1, _ := regexp.Compile(`/[\x{4e00}-\x{9fff}]+/u`)
//去除中文?
removeCnKeyword := r1.ReplaceAllString(req.Keyword, "")
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 {
if req.Keyword != "" {
subQuery := elastic.NewBoolQuery()
subQuery.Should(elastic.NewTermQuery("goods_name", keyword))
subQuery.Should(elastic.NewTermQuery("goods_name_alike", keyword))
query.Must(subQuery)
}
}
req.Keyword = removeCnKeyword
}
//判断是否存在类似物料搜索
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))
}
//根据状态,排除专卖
query.MustNot(elastic.NewTermQuery("status", 10))
//判断库存
if req.Stock > 0 {
query.Must(elastic.NewRangeQuery("stock").Gte(req.Stock))
}
//判断是否要根据品牌id搜索
if req.BrandId != "" {
brandIdSlice := strings.Split(req.BrandId, ",")
query.Must(elastic.NewTermsQuery("brand_id", brandIdSlice))
}
//是否有货
if req.AvailRank == 1 {
query.Must(elastic.NewTermsQuery("sort", 31))
source.Sort("_score", false)
if req.SupplierId == "17" {
source.Sort("update_time", false)
}
source.Sort("stock", false)
source.Sort("single_price", true)
}
//价格排序
if req.SingleRank != 0 {
//todo : 清空排序
source.Sort("single_price_sort", false)
if req.SingleRank == 1 {
source.Sort("single_price", true)
} else if req.SingleRank == 2 {
source.Sort("single_price", false)
}
source.Sort("_score", false)
}
//库存排序
if req.StockRank != 0 {
source.Sort("stock_sort", false)
if req.StockRank == 2 {
source.Sort("stock", true)
}
if req.StockRank == 1 {
source.Sort("stock", false)
}
source.Sort("_score", false)
}
//综合排序
if req.ComRank == 1 {
//todo : 清空排序
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
//如果什么排序都没有,就开启默认排序
if req.AvailRank == 0 && req.SingleRank == 0 && req.StockRank == 0 && req.ComRank == 0 {
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
var pageSize, pageCurrent, fromSize int
if req.Offset == 0 {
pageSize = 15
} else {
pageSize = req.Offset
}
if req.P == 0 {
pageCurrent = 15
} else {
pageCurrent = req.P
}
//ES从哪里开始获取数据
fromSize = pageSize * (pageCurrent - 1)
if fromSize+pageSize >= 10000 {
fromSize = 9000
pageSize = 100
}
//搜索过来的分页方式 页码,第一页为2页,接下来以每页5条展示
if (req.FirstSearchPage == 2 || req.P == 2) && req.Topic != 1 && req.Keyword != "" {
pageSize = 2
if req.P != 0 {
pageCurrent = req.P
} else {
pageCurrent = 1
}
fromSize = pageSize * (pageCurrent - 1)
if pageCurrent > 1 {
pageSize = 5
fromSize = pageSize*(pageCurrent-1) - 2 - 1
}
}
source.From(fromSize)
source.Size(pageSize)
//todo : $params['preference'] = '_primary_first';
//判断是否要排除某些品牌
excludeBrandId := model.GetExcludeBrandIds(1)
for _, brandId := range excludeBrandId {
query.MustNot(elastic.NewTermQuery("brand_id", brandId))
}
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
return searchRequest.Body()
}
//将供应商信息放到数据最外层,同时整合es相关数据
func getReturnSupplierData(dullData map[string]model.DullGoodsData) {
if len(dullData) > 0 {
data := dullData[0]
result["supplier_ad"] = data.SupplierAd
result["supplier_id"] = data.SupplierId
result["supplier_logo"] = data.SupplierLogo
result["supplier_name"] = data.SupplierName
result["supplier_sort"] = data.SupplierSort
result["coupon_id"] = data.CouponId
result["coupon_title"] = data.CouponTitle
result["page_flag"] = 2
}
return result
}
//根据es的结果获取goods_id列表
func GetGoodsIdsByEs(res string) (goodsIds []string) {
//直接用gjson去获取goods_id列表
gjArray := gjson.Get(res, "hits.hits.#._id").Array()
gjArray := gjson.Get(res, "hits.hits.#._source.goods_id").Array()
for _, item := range gjArray {
goodsIds = append(goodsIds, item.String())
}
......
package service
import (
"fmt"
"github.com/imroc/req"
"github.com/tidwall/gjson"
"search_server/model"
......@@ -78,7 +77,7 @@ func OutLink(goodsName *string,flag string) map[string]*model.LyClearGoodsList {
productList[goodsSn] = &LyClearGoodsList
}
fmt.Println(productList)
//fmt.Println(productList)
return productList
}
\ No newline at end of file
package query
import (
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/requests"
"strings"
)
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 GetGoodsBySupplierNameQuery(r requests.QuoteIndexRequest) (string, error) {
query := elastic.NewBoolQuery()
subQuery := elastic.NewBoolQuery()
subQuery2 := elastic.NewBoolQuery()
source := elastic.NewSearchSource()
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
//获取stockFlag
stockFlag, _ := redis.Int(redisCon.Do("GET", "StockSwitch"))
//todo : type=goods
keywordSlice := strings.Split(strings.ToUpper(strings.TrimSpace(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, "")
if r.SupplierName == "zhuanmai" && r.Keyword != "" {
query.Must(elastic.NewTermQuery("goods_name", strings.ToUpper(r.Keyword)))
} else {
if r.Keyword != "" {
subQuery.Should(elastic.NewTermQuery("goods_name", strings.ToUpper(r.Keyword)))
}
flag := false
for _, word := range keywordSlice {
word = strings.TrimSpace(word)
if word != "" {
flag = true
subQuery2.Must(elastic.NewTermQuery("goods_details", word))
}
}
if flag {
//根据描述查找
subQuery.Should(subQuery2)
query.Must(subQuery)
}
}
r.Keyword = removeCnKeyword
query.MustNot(elastic.NewTermQuery("status", 10))
if r.BrandId != "" {
brandIdSlice := strings.Split(r.BrandId, ",")
//todo : 取整
query.MustNot(elastic.NewTermQuery("brand_id", brandIdSlice))
}
stockLimit := 1000000
if r.Stock > 0 {
if r.Stock > stockLimit {
r.Stock = stockLimit
}
query.Must(elastic.NewRangeQuery("stock").Gte(r.Stock))
} else if stockFlag == 1 && r.SupplierName == "zhuanmai" {
query.Must(elastic.NewRangeQuery("stock").Gt(0))
}
//是否有货
if r.AvailRank == 1 {
source.Sort("sort", false)
source.Sort("_score", false)
}
//价格排序
if r.SingleRank != 0 {
//todo : 清空排序
source.Sort("single_price_sort", false)
if r.SingleRank == 1 {
source.Sort("single_price", true)
} else if r.SingleRank == 2 {
source.Sort("single_price", false)
}
source.Sort("_score", false)
}
//库存排序
if r.StockRank != 0 {
source.Sort("stock_sort", false)
if r.StockRank == 2 {
source.Sort("stock", true)
}
if r.StockRank == 1 {
source.Sort("stock", false)
}
source.Sort("_score", false)
}
//综合排序
if r.ComRank == 1 {
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("_score", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
}
//如果什么排序都没有,就开启默认排序
if r.AvailRank == 0 && r.SingleRank == 0 && r.StockRank == 0 && r.ComRank == 0 {
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("_score", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
}
//页码,第一页为2页,接下来以每页5条展示
var fromSize int
pageSize := 2
if r.SupplierName == "liexin_ti" {
pageSize = 3
}
pageCurrent := 1
if r.P > 0 {
pageCurrent = r.P
}
fromSize = pageSize * (pageCurrent - 1)
if pageCurrent > 1 {
pageSize = 5
//不明白
fromSize = fromSize - 2 - 1
}
//每页显示10条
if r.SupplierName == "liexin_jingdiao" {
pageSize = 10
fromSize = pageSize * (pageCurrent - 1)
}
//todo:快手?
source.From(fromSize)
source.Size(pageSize)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source).Preference("_primary_first")
return searchRequest.Body()
}
func GetGoodsBySupplierIdQuery(r requests.QuoteIndexRequest) (string, error) {
r.SupplierId = strings.TrimSpace(r.SupplierId)
source := elastic.NewSearchSource()
query := elastic.NewBoolQuery()
//增加联营精确搜索
if r.GoodsNameEq != "" {
var goodsName string
r, _ := regexp.Compile(`/[^A-Za-z0-9]+/u`)
goodsName = r.ReplaceAllString(goodsName, "")
query.Must(elastic.NewTermQuery("auto_goods_name.raw", goodsName))
}
//只显示更新时间在一个月内的数据
oneMonthTimeStamp := carbon.Now().SubMonth().Timestamp()
query.Must(elastic.NewRangeQuery("update_time").Gte(oneMonthTimeStamp))
keyword := strings.ToUpper(r.Keyword)
//判断关键词是否为空
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, "")
if r.IsExact == 1 {
query.Must(elastic.NewTermQuery("auto_goods_name.raw", keyword))
} else {
if r.Keyword != "" {
subQuery := elastic.NewBoolQuery()
subQuery.Should(elastic.NewTermQuery("goods_name", keyword))
subQuery.Should(elastic.NewTermQuery("goods_name_alike", keyword))
query.Must(subQuery)
}
}
r.Keyword = removeCnKeyword
}
//判断是否存在类似物料搜索
supplierIndexName := GetSupplierIndex(r.SupplierId)
//获取相似物料供应商列表
alikeSuppliers := config.Cfg.Section("alike_supplier").KeyStrings()
if php2go.InArray(supplierIndexName, alikeSuppliers) && keyword != "" {
query.Should(elastic.NewTermQuery("goods_name_alike", keyword))
}
//根据状态,排除专卖
query.MustNot(elastic.NewTermQuery("status", 10))
//判断库存
if r.Stock > 0 {
query.Must(elastic.NewRangeQuery("stock").Gte(r.Stock))
}
//判断是否要根据品牌id搜索
if r.BrandId != "" {
brandIdSlice := strings.Split(r.BrandId, ",")
query.Must(elastic.NewTermsQuery("brand_id", brandIdSlice))
}
//是否有货
if r.AvailRank == 1 {
query.Must(elastic.NewTermsQuery("sort", 31))
source.Sort("_score", false)
if r.SupplierId == "17" {
source.Sort("update_time", false)
}
source.Sort("stock", false)
source.Sort("single_price", true)
}
//价格排序
if r.SingleRank != 0 {
//todo : 清空排序
source.Sort("single_price_sort", false)
if r.SingleRank == 1 {
source.Sort("single_price", true)
} else if r.SingleRank == 2 {
source.Sort("single_price", false)
}
source.Sort("_score", false)
}
//库存排序
if r.StockRank != 0 {
source.Sort("stock_sort", false)
if r.StockRank == 2 {
source.Sort("stock", true)
}
if r.StockRank == 1 {
source.Sort("stock", false)
}
source.Sort("_score", false)
}
//综合排序
if r.ComRank == 1 {
//todo : 清空排序
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
//如果什么排序都没有,就开启默认排序
if r.AvailRank == 0 && r.SingleRank == 0 && r.StockRank == 0 && r.ComRank == 0 {
source.Sort("status", false)
source.Sort("sort", false)
source.Sort("update_time", false)
source.Sort("stock", false)
source.Sort("single_price", true)
source.Sort("_score", false)
}
var pageSize, pageCurrent, fromSize int
if r.Offset == 0 {
pageSize = 15
} else {
pageSize = r.Offset
}
if r.P == 0 {
pageCurrent = 15
} else {
pageCurrent = r.P
}
//ES从哪里开始获取数据
fromSize = pageSize * (pageCurrent - 1)
if fromSize+pageSize >= 10000 {
fromSize = 9000
pageSize = 100
}
//搜索过来的分页方式 页码,第一页为2页,接下来以每页5条展示
if (r.FirstSearchPage == 2 || r.P == 2) && r.Topic != 1 && r.Keyword != "" {
pageSize = 2
if r.P != 0 {
pageCurrent = r.P
} else {
pageCurrent = 1
}
fromSize = pageSize * (pageCurrent - 1)
if pageCurrent > 1 {
pageSize = 5
fromSize = pageSize*(pageCurrent-1) - 2 - 1
}
}
source.From(fromSize)
source.Size(pageSize)
//判断是否要排除某些品牌
excludeBrandId := model.GetExcludeBrandIds(1)
for _, brandId := range excludeBrandId {
query.MustNot(elastic.NewTermQuery("brand_id", brandId))
}
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source).Preference("_primary_first")
return searchRequest.Body()
}
package service
import (
"fmt"
. "github.com/tidwall/gjson"
"search_server/pkg/es"
"search_server/requests"
"search_server/service/query"
"search_server/service/transformer"
)
//根据供应商获取商品信息
func GetGoodsBySupplier(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
//索引字典
supplierIndex := query.GetSupplierIndex(r.SupplierId)
//获取查询条件
queryString, err := query.GetGoodsBySupplierIdQuery(r)
if err != nil {
return
}
queryString = ""
esResult, err := es.CurlES(supplierIndex, queryString)
if err != nil {
return
}
goodsIds := GetGoodsIdsByEs(esResult)
goodsList, err := GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
result = GetReturnSupplierData(dullData)
result["total"] = Get(esResult, "hits.total").Int()
result["took"] = Get(esResult, "took").Int()
return
}
func GetGoodsBySupplierName(r requests.QuoteIndexRequest) (result map[string]interface{}, err error) {
supplierIndex := r.SupplierName
queryString, err := query.GetGoodsBySupplierNameQuery(r)
fmt.Println(supplierIndex)
fmt.Println(queryString)
esResult, err := es.CurlES(supplierIndex, queryString)
if err != nil {
return
}
goodsIds := GetGoodsIdsByEs(esResult)
goodsList, err := GetGoodsInfo(goodsIds)
dullData, err := transformer.DullDataInfo(r.Keyword, goodsList)
result = GetReturnSupplierData(dullData)
result["total"] = Get(esResult, "hits.total").Int()
result["took"] = Get(esResult, "took").Int()
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