Commit 64a90538 by 孙龙

up

parent 4327d17d
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"search_server/model" "search_server/model"
"search_server/pkg/common"
"search_server/service" "search_server/service"
"strings" "strings"
) )
...@@ -34,3 +35,18 @@ func Zyh(ctx *gin.Context){ ...@@ -34,3 +35,18 @@ func Zyh(ctx *gin.Context){
//fmt.Printf("%+v",zyhResponse) //fmt.Printf("%+v",zyhResponse)
ctx.JSON(200,zyhResponse) ctx.JSON(200,zyhResponse)
} }
func Index(ctx *gin.Context){
zyhRequest := make(map[string]string,0);
zyhResponse := &model.ZyhResponse{}
ctx.MultipartForm()
for requestName, requstValue := range ctx.Request.Form {
if requstValue[0] != "" {
zyhRequest[requestName] = strings.TrimSpace(requstValue[0])
}
}
zyServiceImpl := service.NewZyServiceImpl()
zyhResponse = zyServiceImpl.Index(ctx,zyhRequest)
common.Output(int(zyhResponse.ErrorCode), zyhResponse.ErrorMsg, zyhResponse.Data)
return
}
\ No newline at end of file
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"search_server/middleware" "search_server/middleware"
"search_server/pkg/config" "search_server/pkg/config"
"search_server/pkg/vars" "search_server/pkg/vars"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
...@@ -323,3 +324,22 @@ func RemoveSliceString(s []string, i string) []string { ...@@ -323,3 +324,22 @@ func RemoveSliceString(s []string, i string) []string {
} }
return b return b
} }
//map排序
func MapSort(mapList map[int]int) []int{
var(
keys []int
newList []int
)
newList = make([]int,0)
for _,v:=range mapList{
keys = append(keys,v)
}
sort.Ints(keys)
for _,k:=range keys{
newList = append(newList,mapList[k])
}
return newList
}
\ No newline at end of file
...@@ -21,6 +21,8 @@ func InitRouter() *gin.Engine { ...@@ -21,6 +21,8 @@ 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/ZiYing/zyh", controller.Zyh) r.GET("search/ZiYing/zyh", controller.Zyh)
r.GET("search/ZiYing/index", controller.Index)
r.POST("search/ZiYing/index", controller.Index)
r.POST("search/mouser/GetMouserData", controller.GetMouserData) r.POST("search/mouser/GetMouserData", controller.GetMouserData)
r.GET("search/mouser/GetMouserData", controller.GetMouserData) r.GET("search/mouser/GetMouserData", controller.GetMouserData)
......
...@@ -37,7 +37,7 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo ...@@ -37,7 +37,7 @@ func (this *ZyServiceImpl) Zyh(ctx *gin.Context, req map[string]string) (rsp *mo
need_aggs bool need_aggs bool
_type byte _type byte
) )
isAdmin = true isAdmin = false
if req["need_aggs"] == "1"{ if req["need_aggs"] == "1"{
need_aggs = true need_aggs = true
}else{ }else{
...@@ -137,6 +137,12 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){ ...@@ -137,6 +137,12 @@ func zy(req map[string]string,_type byte,isCheck byte) (rsp *model.ZyhResponse){
return rsp return rsp
} }
func(this *ZyServiceImpl) Index(ctx *gin.Context, req map[string]string) (rsp *model.ZyhResponse){
req2 := req
zy_orgin_search = common.CopyMapString(req)//原始搜索數據
rsp = searchZiYingGoods(req2,false,nil,nil,1)
return rsp
}
//自营型号查询 //自营型号查询
/* /*
...@@ -194,7 +200,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -194,7 +200,7 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
if strings.Contains(k,"/") { if strings.Contains(k,"/") {
k_temp = strings.Split(k,"/") k_temp = strings.Split(k,"/")
if len(k_temp) == 2 { if len(k_temp) == 2 {
isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword,&is_sample) isok := makeQuery(query,source,k_temp,&goods_temp_status,&v,&keyword,&is_sample,&goods_name_arr)
if !isok { if !isok {
continue; continue;
} }
...@@ -257,8 +263,8 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -257,8 +263,8 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
//综合排序 //综合排序
if com_rank,ok :=req["com_rank"];ok && strings.TrimSpace(com_rank) == "1" { if com_rank,ok :=req["com_rank"];ok && strings.TrimSpace(com_rank) == "1" {
source.Sort("sort",true) //asc source.Sort("sort",false) //desc
source.Sort("single_price",false) //desc source.Sort("single_price",true) //asc
defaultSort = false defaultSort = false
} }
if defaultSort { if defaultSort {
...@@ -269,13 +275,13 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -269,13 +275,13 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
length := int64(10) length := int64(10)
if offset,ok := req["offset"];ok { if offset,ok := req["offset"];ok {
if offsetInt,err:=strconv.ParseInt(offset, 10, 64); err != nil && offsetInt > 0{ if offsetInt,err:=strconv.ParseInt(offset, 10, 64); err == nil && offsetInt > 0{
length = offsetInt length = offsetInt
} }
} }
page := int64(1) page := int64(1)
if p,ok := req["p"];ok { if p,ok := req["p"];ok {
if pInt,err:=strconv.ParseInt(p, 10, 64); err != nil && pInt > 0{ if pInt,err:=strconv.ParseInt(p, 10, 64); err == nil && pInt > 0{
page = pInt page = pInt
} }
} }
...@@ -470,106 +476,113 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL ...@@ -470,106 +476,113 @@ func searchZiYingGoods( req map[string]string,zy bool,classList *model.ZhyClassL
} }
referer := middleware.Context.GetHeader("Referer") referer := middleware.Context.GetHeader("Referer")
if flag <= 0 && referer != "" && php2go.Stripos(referer,"/s/?k=",0) == -1{ if flag <= 0 && php2go.Stripos(referer,"/s/?k=",0) == -1{
pagesize := length response = getPage(response,length,hitsTotal)
totalPage := math.Ceil(float64(hitsTotal)/float64(pagesize)) }
var total int64
if totalPage > 1{ rsp.Data = response
if hitsTotal > 10000 { return rsp
total = 10000 }
//處理分頁信息
func getPage(response map[string]interface{},length int64,hitsTotal int64) map[string]interface{}{
pagesize := length
totalPage := math.Ceil(float64(hitsTotal)/float64(pagesize))
var (
total int64
offsetInt int
err error
)
if totalPage > 1{
if hitsTotal > 10000 {
total = 10000
}else{
total = hitsTotal
}
_zy_orgin_search := zy_orgin_search
p := _zy_orgin_search["p"]
if p == ""{
p = "1"
}
offset := _zy_orgin_search["offset"]
offsetInt, err = strconv.Atoi(offset)
if err == nil && offsetInt > 0{
offset = strconv.FormatInt(int64(offsetInt),10)
}else{
offsetInt = 10
offset = "10"
}
if isAdmin {
//如果是自营同步输出
//自定義分頁
delete(_zy_orgin_search,"callback")
delete(_zy_orgin_search,"PHPSESSID")
delete(_zy_orgin_search,"hcy_test")
delete(_zy_orgin_search,"Array")
page := make(map[string]interface{},0)
page["orgin_search"] = _zy_orgin_search
page["total"] = strconv.FormatInt(total,10)
page["current_page"] = p
page["offset"] = offset
if totalPage > 1000{
page["total_page"] = "1000"
}else{ }else{
total = hitsTotal page["total_page"] = strconv.FormatInt(int64(totalPage),10)
} }
_zy_orgin_search := zy_orgin_search pageData := make(map[int]int,0)
p := _zy_orgin_search["p"] pInt := int(1)
if p == ""{ pInt, err = strconv.Atoi(p)
p = "1" if err != nil{
pInt = int(1)
} }
offset := _zy_orgin_search["offset"]
offsetInt, err := strconv.Atoi(offset)
if err == nil && offsetInt > 0{
offset = strconv.FormatInt(int64(offsetInt),10)
}else{
offset = "10"
}
if isAdmin {
//如果是自营同步输出
//自定義分頁
delete(_zy_orgin_search,"callback")
delete(_zy_orgin_search,"PHPSESSID")
delete(_zy_orgin_search,"hcy_test")
delete(_zy_orgin_search,"Array")
page := make(map[string]interface{},0)
page["orgin_search"] = _zy_orgin_search
page["total"] = strconv.FormatInt(total,10)
page["current_page"] = p
page["offset"] = offset
if totalPage > 1000{
page["total_page"] = "1000"
}else{
page["total_page"] = strconv.FormatInt(int64(totalPage),10)
}
pageData := make(map[int]int,0)
pInt := int(1)
pInt, err = strconv.Atoi(p)
if err != nil{
pInt = int(1)
}
if pInt > 1000 { if pInt > 1000 {
pInt = 1000 pInt = 1000
}
if pInt > 996{
for i:=pInt-10;i<=pInt;i++{
//原始查询参数拼接
pageData[i] = i
} }
if pInt > 996{ }else{
for i:=pInt-10;i<=pInt;i++{ pageNum := 0
//原始查询参数拼接 for i:=1;i<=int(totalPage);i++{
pageData[i] = i if pageNum == 10 {
break
} }
}else{ if pInt >6 && pInt <= 996{
pageNum := 0 //大于10页只获取中间10页
for i:=1;i<=int(totalPage);i++{ if i < pInt -5 || i > pInt+5{
if pageNum == 10 { continue
break
}
if pInt >6 && pInt <= 996{
//大于10页只获取中间10页
if i < pInt -5 || i > pInt+5{
continue
}
} }
fmt.Println("i:",i)
pageData[i] = i
pageNum++
} }
//fmt.Println("i:",i)
pageData[i] = i
pageNum++
} }
page["page_data"] = pageData
response["page"] = page
}else{
//非同步输出自己输出分页样式
page, err := strconv.Atoi(p)
if err != nil{
page = int(1)
}
pageSize := 10
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 10
}
paging := common.Paginator(page, pageSize, 365)
pageHtml := makePageInfo(paging,page)
response["page"] = pageHtml
} }
page["page_data"] = common.MapSort(pageData)
response["page"] = page
}else{ }else{
response["page"] = map[string]interface{}{"total":totalPage} //非同步输出自己输出分页样式
page, err := strconv.Atoi(p)
if err != nil || page < 1 {
page = int(1)
}
paging := common.Paginator(page, offsetInt, int64(hitsTotal))
pageHtml := makePageInfo(paging,page)
response["page"] = pageHtml
} }
}else{
response["page"] = map[string]interface{}{"total":totalPage}
} }
return response
rsp.Data = response
return rsp
} }
func makePageInfo(paginator map[string]interface{},currentPage int) string{ func makePageInfo(paginator map[string]interface{},currentPage int) string{
//fmt.Printf("%+v",paginator)
pageHtml := "" pageHtml := ""
pageHtml += `<div class="pagination">` pageHtml += `<div class="pagination">`
pageHtml += fmt.Sprintf(`<a class="prev" href="/product-%d.html">«</a> `,paginator["firstpage"]) pageHtml += fmt.Sprintf(`<a class="prev" href="/product-%d.html">«</a> `,paginator["firstpage"])
...@@ -588,7 +601,7 @@ func makePageInfo(paginator map[string]interface{},currentPage int) string{ ...@@ -588,7 +601,7 @@ func makePageInfo(paginator map[string]interface{},currentPage int) string{
} }
func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string, func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions []string,goods_temp_status *int,searchValue *string,
keyword *string,is_sample *bool) bool{ keyword *string,is_sample *bool,goods_name_arr *[]string) bool{
var ( var (
err error err error
...@@ -665,6 +678,7 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -665,6 +678,7 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
return false return false
} }
//提取汉子 //提取汉子
//goods_name_cn 里面装的汉子
reg = regexp.MustCompile(`[\p{Han}]+`) reg = regexp.MustCompile(`[\p{Han}]+`)
goods_name_cn := reg.FindAllString(*searchValue,-1) goods_name_cn := reg.FindAllString(*searchValue,-1)
if len(goods_name_cn) > 0 { if len(goods_name_cn) > 0 {
...@@ -672,8 +686,8 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -672,8 +686,8 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
goods_name_cn[k] = strings.TrimSpace(v) goods_name_cn[k] = strings.TrimSpace(v)
} }
} }
//去除汉字,并以空格分词条 //去除汉字,并以空格分词条
//term_v 里面装的非汉子
reg = regexp.MustCompile(`[\P{Han}]+`) reg = regexp.MustCompile(`[\P{Han}]+`)
term_v =reg.FindAllString(*searchValue, -1) term_v =reg.FindAllString(*searchValue, -1)
if len(term_v) > 0 { if len(term_v) > 0 {
...@@ -681,15 +695,15 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions ...@@ -681,15 +695,15 @@ func makeQuery(query *elastic.BoolQuery,source *elastic.SearchSource,conditions
term_v[k] = strings.TrimSpace(v) term_v[k] = strings.TrimSpace(v)
} }
} }
goods_name_arr := append(term_v,goods_name_cn...) *goods_name_arr = append(*goods_name_arr,term_v...)
//fmt.Println("goods_name_arr",goods_name_arr) *goods_name_arr = append(*goods_name_arr,goods_name_cn...)
*keyword = *searchValue *keyword = *searchValue
for _,v:=range term_v{ for _,v:=range term_v{
if v != ""{ if v != ""{
query.Must(elastic.NewTermQuery("goods_name",v)) query.Must(elastic.NewTermQuery("goods_name",v))
} }
} }
if len(goods_name_arr) > 0{ if len(goods_name_cn) > 0{
query.Must(elastic.NewMatchQuery("goods_name_cn",strings.Join(goods_name_cn," ")).MinimumShouldMatch("100%")) query.Must(elastic.NewMatchQuery("goods_name_cn",strings.Join(goods_name_cn," ")).MinimumShouldMatch("100%"))
} }
......
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