Commit 82930e17 by mushishixian

加入bom相关代码

parent 9c84d33e
package controller
import (
"github.com/gin-gonic/gin"
"search_server/framework/gin_"
"search_server/protopb/course"
"search_server/service"
)
func init() {
courseService := service.NewCourseServiceImpl()
gin_.NewBuilder().WithService(courseService).
WithMiddleware(Check_Middleware()).
WithMiddleware(Cors_Middleware()).
WithEndpoint(Courselist_Endpoint(courseService)).
WithRequest(Courselist_Request()).
WithResponse(Course_Response()).Build("/course", "GET")
gin_.NewBuilder().WithService(courseService).
WithMiddleware(Check_Middleware()).
WithEndpoint(Coursedetail_Endpoint(courseService)).
WithRequest(Coursedetail_Request()).
WithResponse(Course_Response()).Build("/detail/:course_id", "GET")
}
//详细相关
func Coursedetail_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &course.DetailResponse{Course: new(course.CourseModel), Counts: make([]*course.CourseCounts, 0)}
err = c.GetDetail(context, request.(*course.DetailRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func Coursedetail_Request() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &course.DetailRequest{}
err := context.BindUri(bReq)
if err != nil {
return nil, err
}
err = context.BindHeader(bReq)
if err != nil {
return nil, err
}
return bReq, nil
}
}
//获取列表相关
func Courselist_Endpoint(c *service.CourseServiceImpl) gin_.Endpoint {
return func(context *gin.Context, request interface{}) (response interface{}, err error) {
rsp := &course.ListResponse{}
err = c.ListForTop(context, request.(*course.ListRequest), rsp)
return rsp, err
}
}
//这个函数的作用是怎么处理请求
func Courselist_Request() gin_.EncodeRequestFunc {
return func(context *gin.Context) (i interface{}, e error) {
bReq := &course.ListRequest{}
err := context.BindQuery(bReq) //使用的是query 参数
if err != nil {
return nil, err
}
return bReq, nil
}
}
//这个函数作用是:怎么处理响应结果
func Course_Response() gin_.DecodeResponseFunc {
return func(context *gin.Context, res interface{}) error {
context.JSON(200, res)
return nil
}
}
......@@ -14,16 +14,15 @@ require (
github.com/jinzhu/gorm v1.9.12
github.com/micro/go-micro/v2 v2.5.0
github.com/prometheus/common v0.9.1
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
google.golang.org/appengine v1.6.3 // indirect
google.golang.org/genproto v0.0.0-20200527145253-8367513e4ece // indirect
google.golang.org/grpc v1.29.1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/olivere/elastic.v5 v5.0.85
)
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
......@@ -18,7 +18,7 @@ type HitsResult struct {
Hits string `json:"hits"`
}
func CurlES(index string, param string) (err error, result string) {
func CurlES(index string, param string) (result string, err error) {
goodsServerUrl := config.Get("es.url").String()
params := req.BodyJSON(param)
resp, err := req.Get(goodsServerUrl+"/"+index+"/_search", params)
......
syntax = "proto3";
package bom;
service BomService {
rpc ListForTop(ListRequest) returns (ListResponse);
rpc GetDetail(DetailRequest) returns(DetailResponse);
}
message CourseModel{
int32 course_id=1;
string course_name=2;
string course_disp_name=3;
string course_intr=4;
float course_price=5;
float course_price2=6;
// @inject_tag: gorm:"type:timestamp"
int64 addtime=7;
}
message ListRequest {
// @inject_tag: form:"size"
int32 size = 1;
}
message DetailRequest{
// @inject_tag: uri:"course_id"
int32 course_id=1;
// @inject_tag: header:"fetch_type"
int32 fetch_type=2;
}
//计数
message CourseCounts{
// @inject_tag: json:"-"
int32 count_id=1;
// @inject_tag: json:"-"
int32 course_id=2;
string count_key=3;
int32 count_value=4;
}
message ListResponse {
repeated CourseModel result = 1;
}
message DetailResponse{
CourseModel course=1;
repeated CourseCounts counts=2;
}
package service
import (
"context"
"fmt"
elastic "gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/dao"
"search_server/pkg/es"
. "search_server/protopb/course"
"strings"
)
//func NewCourseModel(id int32, name string) *CourseModel {
// return &CourseModel{CourseId: id, CourseName: name}
//}
type BomServiceImpl struct{}
func (bs *BomServiceImpl) ListForTop(ctx context.Context, req *ListRequest, rsp *ListResponse) error {
course := make([]*CourseModel, 0)
err := dao.GetCourseListBySql(req.Size).Find(&course).Error
if err != nil {
return err
}
rsp.Result = course
return nil
}
func (bs *BomServiceImpl) GetDetail(ctx context.Context, req *DetailRequest, rsp *DetailResponse) error {
//只取课程详细
if req.FetchType == 0 || req.FetchType == 1 || req.FetchType == 3 {
if err := dao.GetCourseDetail(int(req.CourseId)).Find(rsp.Course).Error; err != nil {
return err
}
}
//只取计数表详细
if req.FetchType == 2 || req.FetchType == 3 {
if err := dao.GetCourseCounts(int(req.CourseId)).Find(&rsp.Counts).Error; err != nil {
return err
}
}
return nil
}
func NewBomServiceImpl() *BomServiceImpl {
return &BomServiceImpl{}
}
//构建请求参数
func AutoSpu(goodsName string) (searchRequestStr string) {
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName = replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
query := getTermQuery(goodsName)
source := elastic.NewSearchSource().Query(query)
source = source.From(0).Size(10)
searchRequestStr, _ = elastic.NewSearchRequest().Source(source).Body()
fmt.Println(searchRequestStr)
result, err := es.CurlES("lie_spu", searchRequestStr)
fmt.Println(err)
fmt.Println(result)
return searchRequestStr
}
//构建term条件
func getTermQuery(goodsName string) (query *elastic.BoolQuery) {
query = elastic.NewBoolQuery()
query.Filter(elastic.NewTermQuery("auto_goods_name", goodsName))
query.Filter(elastic.NewTermQuery("status", 1))
query.MustNot(elastic.NewTermQuery("brand_id", 0))
return query
}
package service
import (
"github.com/imroc/req"
"search_server/pkg/config"
)
//商品服务请求,传入的是goods_id的字符串,可以是单个id,也可以是多个id用逗号隔开
func GetGoodsInfo(goodsIdsStr string) (result string) {
goodsServerUrl := config.Get("goods.api_url").String()
params := req.Param{
"goods_id": goodsIdsStr,
}
resp, err := req.Post(goodsServerUrl+"/synchronization", params)
if err != nil {
return
}
return resp.String()
}
......@@ -4,6 +4,7 @@ import (
"flag"
"github.com/go-kratos/kratos/pkg/log"
"search_server/boot"
"search_server/service"
)
func main() {
......@@ -13,6 +14,7 @@ func main() {
if err := boot.Boot(path); err != nil {
log.Error("%s", err)
}
service.AutoSpu("a")
//logger.Warn("test")
//logger.Warn("warn555555555555555555555555555555")
//logger.Info("info记录日志555555555555555555555555555555")
......
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