Commit c94f6e6b by huangchengyi

比价

parent 06f5ef40
package controller
import (
"github.com/gin-gonic/gin"
"search_server/model"
"search_server/pkg/common"
"search_server/service"
"search_server/service/ly"
)
/*
*获取比价详情
@param number 比价数量,eg:500
@param deliver 交货地: 1 大陆 2 香港 (默认大陆)
@param alike_spu_id 替代型号spu_id,多个spu_id 以逗号分隔,如:2160551099924392802,2160551101943552102
*/
func GetParityData(ctx *gin.Context) {
common.PrintDebugHeader(ctx) //debug
//调外链拿数据
optimumservice := service.OptimumService{}
apiData := optimumservice.GetOptimumAttr(ctx)
common.PrintDebugHtml(ctx,"-----执行结束----") //debug
common.Output(ctx,errCode, "查询成功", "")
}
......@@ -4,6 +4,7 @@ import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"github.com/syyongx/php2go"
"strings"
)
......@@ -17,6 +18,13 @@ func Sha1(query string, pri_key string) string {
}
func main() {
myNum := []int{10, 20, 30, 40, 50}
// 创建一个新切片
// 其长度为 2 个元素,容量为 4 个元素
newNum := myNum[1:4]
fmt.Println(newNum)
return
dd := php2go.InArray("1003",[]string{"1001","1002"})
print(dd)
return
......
......@@ -84,6 +84,7 @@ type ZiyinGoodsInfo struct {
type DullGoodsData struct {
ApiGoods
//额外增加的字段
Tprice float64 `json:"tprice,omitempty"` //用于比价梯度判断
GoodsNameTemp string `json:"goods_name_temp,omitempty"`
Multiple int `json:"-"`
MultipleStr interface{} `json:"multiple,omitempty"`
......
......@@ -39,6 +39,8 @@ func InitRouter() *gin.Engine {
//搜索相关接口
v4 := r.Group("/search/")
{
v4.POST("parity/info", controller.GetParityData)
v4.GET("parity/info", controller.GetParityData)
v4.POST("mouser/a", controller.GetMouserData)
v4.GET("mouser/a", controller.GetMouserData)
v4.POST("digikey/a", controller.GetDigikeyData)
......
package service
import (
"github.com/gin-gonic/gin"
"github.com/gogf/gf/util/gconv"
"github.com/iancoleman/orderedmap"
"github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/service/query"
"sort"
"strconv"
"strings"
)
type ParityService struct {
}
/*
* 查询比价
@param number 比价数量,eg:500
@param deliver 交货地: 1 大陆 2 香港 (默认大陆)
@param target_goods_name 目标比价型号名称,多个型号名称(类似型号一样这样拼接传) 以€分隔,如:LM358DT€LM358SK
*/
func (qs *ParityService) GetParity(ctx *gin.Context) (results model.LyResponse) {
number := ctx.Request.FormValue("number") //比价数量
deliver := ctx.Request.FormValue("deliver") //交货地
target_goods_name := ctx.Request.FormValue("target_goods_name") //比价目标型号
if target_goods_name == "" {
results.ErrorCode = 1001;
results.ErrorMsg = "目标比价型号不得为空"
return
}
//获取查询条件
queryString := query.GetParityQuery(ctx,target_goods_name)
indexTarget := "liexin_ziying"
if deliver == "2" {
indexTarget = config.Get("es.search_supplier").String();
}
//查询es
esResult, err := es.CurlEsPrimary(indexTarget, queryString)
if err != nil {
results.ErrorCode = 1002;
results.ErrorMsg = err.Error()
return
}
common.PrintDebugHtml(ctx,esResult)
//判断返回总条数
total := gjson.Get(esResult, "hits.total").Int()
if total == 0 {
results.ErrorCode = 0;
results.ErrorMsg = "查询没有数据"
return
}
goodsIds := make([]string,0) //goods_id
attrs := make(map[string][]map[string]string) //属性
gjArray := gjson.Get(esResult, "hits.hits.#._source").Array()
for _, item := range gjArray {
goods_id := item.Get("goods_id").String()
goodsIds = append(goodsIds, goods_id)
}
common.PrintDebugHtml(ctx,goodsIds)
//拼接返回数据
A := orderedmap.New() //初始化有序map,拼接data 数据
//组装商品详情
//endArr := make([]model.DullGoodsData,0)
var slic []model.DullGoodsData
goodsList, _ := GetGoodsInfo(ctx,goodsIds)
for _,v := range goodsList{
GoodsUrl := ""
BrandUrl := ""
referer := ctx.GetHeader("Referer")
if referer != ""{
dome := strings.Split(referer,".com")
if len(dome) > 0{
if v.SupplierName == "猎芯自营" {
GoodsUrl = dome[0]+".com/item/"+v.GoodsId+".html"
BrandUrl = dome[0]+".com/v3/brand/list/"+strconv.Itoa(v.BrandId)+".html"
}else{
GoodsUrl = dome[0]+".com/goods_"+v.GoodsId+".html"
BrandUrl = dome[0]+".com/v3/brand/list/"+strconv.Itoa(v.BrandId)+".html"
}
}
}
//比价数量对应的梯度数量
var tprice float64 = 0; //对应梯度价
for _,p := range v.LadderPrice {
if p.Purchases >= gconv.Int(number) {
if deliver == "1" { //大陆
tprice = p.PriceCn
}else{ //香港
tprice = p.PriceUs
}
}
}
slic = append(slic,model.DullGoodsData{
ApiGoods:v,
GoodsUrl: GoodsUrl,
BrandUrl: BrandUrl,
Attrs: attrs[v.GoodsId],
Tprice: tprice, //对应梯度价格
})
}
sort.Sort(DullGoodsData(slic))
//分页计算
page := ctx.Request.FormValue("page")
page_size := ctx.Request.FormValue("page_size")
page_from := 0
if page == "" {
page = "1";
}
if page_size == "" {
page_size = "10";
}
if page != "1" {
page_from = (gconv.Int(page)-1)*gconv.Int(page_size)
if page_from >10000 {
page_from = 10000;
}
}
slicNew := slic[page_from:(page_from+gconv.Int(page_size))]
pageData := PageClear(ctx,total);
pageData["took"] = gjson.Get(esResult, "took").Int()
A.Set("page",pageData) //分页信息
A.Set("goods_lists",slicNew)
results.ErrorCode = 0;
results.ErrorMsg = "查询成功";
results.Data = A;
return
}
//阶梯价格排序算法
type DullGoodsData []model.DullGoodsData
func (a DullGoodsData) Len() int {
return len(a)
}
func (a DullGoodsData) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a DullGoodsData) Less(i, j int) bool {
return a[j].Tprice < a[i].Tprice
}
\ No newline at end of file
package query
import (
"github.com/gin-gonic/gin"
"github.com/syyongx/php2go"
"gopkg.in/olivere/elastic.v5"
"search_server/pkg/common"
)
/*
比价拼接查询语句
@param target_goods_name 目标比价型号名称,多个型号名称(类似型号一样这样拼接传) 以€分隔,如:LM358DT€LM358SK
*/
func GetParityQuery(ctx *gin.Context,target_goods_name string) (param string){
query := elastic.NewBoolQuery()
subQuery := elastic.NewBoolQuery()
if target_goods_name == "" {
return ""
}
goodsSlice:= php2go.Explode("€",target_goods_name);
for _,goods_name := range goodsSlice {
subQuery.Should(elastic.NewTermQuery("auto_goods_name.raw", goods_name))
}
query.Must(elastic.NewTermQuery("goods_status", 1))
query.Must(elastic.NewTermQuery("status", 1))
query.Must(elastic.NewRangeQuery("stock").Gt(0))
query.Must(subQuery)
source := elastic.NewSearchSource()
source.FetchSourceContext(elastic.NewFetchSourceContext(true).Include("goods_id","goods_name")) //显示字段
source.Size(200)
source.From(0)
source.Query(query)
searchRequest := elastic.NewSearchRequest().Source(source)
param, _ = searchRequest.Body()
common.PrintDebugHtml(ctx,param)
return
}
/*
上面查询最终拼接以下es查询:
{
"query": {
"bool": {
"must": [
{
"term": {
"goods_status": {
"value": "1"
}
}
},
{
"term": {
"status": {
"value": "1"
}
}
},
{
"range": {
"stock": {
"gt": 0
}
}
},
{
"bool": {
"should": [
{
"term": {
"auto_goods_name.raw": {
"value": "LM358DT"
}
}
},
{
"term": {
"auto_goods_name.raw": {
"value": "LM358DS"
}
}
}
]
}
}
]
}
}
}
*/
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