Commit 743fcefb by mushishixian

Fix

parent c4d0e56e
...@@ -15,7 +15,7 @@ func AutoSpu(c *gin.Context) { ...@@ -15,7 +15,7 @@ func AutoSpu(c *gin.Context) {
if len(goods) == 0 { if len(goods) == 0 {
errCode = 1 errCode = 1
} }
res := common.Response{ res := common.BomResponse{
ErrCode: errCode, ErrCode: errCode,
ErrMsg: "", ErrMsg: "",
Data: goods, Data: goods,
......
package common package common
type Response struct { type Response struct {
ErrCode int `json:"err_code"` ErrCode int `json:"error_code"`
ErrMsg string `json:"err_msg"` ErrMsg string `json:"err_msg"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type BomResponse struct { type BomResponse struct {
ErrCode int `json:"err_code"` ErrCode int `json:"error_code"`
ErrMsg string `json:"err_msg"` ErrMsg string `json:"error_msg"`
Flag int `json:"flag"` Flag int `json:"flag"`
Total int `json:"total"` Total int `json:"total"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
......
...@@ -7,35 +7,24 @@ import ( ...@@ -7,35 +7,24 @@ import (
"time" "time"
) )
type IchuntRedis struct {
RedisList map[string]*redis.Pool
Current *redis.Pool
}
func(this *IchuntRedis) Connection(connection string) (*IchuntRedis){
this.Current = this.RedisList[connection]
return this
}
var writeConn, readConn *redis.Pool var writeConn, readConn *redis.Pool
func Setup() (err error) { func Setup() (err error) {
ichuntRedis := &IchuntRedis{} writeHost := config.Get("redis.write_host").String()
ichuntRedis.RedisList = make(map[string]*redis.Pool,0) readHost := config.Get("redis.read_host").String()
RedisDatabaseMap := config.BuildRedisConfgs() writePassword := config.Get("redis.write_password").String()
for redisKey,redisConfig := range RedisDatabaseMap{ readPassword := config.Get("redis.read_password").String()
ichuntRedis.RedisList[redisKey],err = getConn(redisConfig.Host, redisConfig.Password) writeConn, err = getConn(writeHost, writePassword)
if err != nil{ if err != nil {
panic(err) return
} }
readConn, err = getConn(readHost, readPassword)
if err != nil {
return
} }
return nil return nil
} }
func getConn(writeHost, password string) (pool *redis.Pool, err error) { func getConn(writeHost, password string) (pool *redis.Pool, err error) {
maxIdle, _ := config.Get("redis.max_idle").Int() maxIdle, _ := config.Get("redis.max_idle").Int()
maxActive, _ := config.Get("redis.max_active").Int() maxActive, _ := config.Get("redis.max_active").Int()
...@@ -65,8 +54,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) { ...@@ -65,8 +54,8 @@ func getConn(writeHost, password string) (pool *redis.Pool, err error) {
//最基础的键值操作 //最基础的键值操作
func (this *IchuntRedis) Set(key string, data interface{}) error { func Set(key string, data interface{}) error {
conn := this.Current.Get() conn := writeConn.Get()
defer conn.Close() defer conn.Close()
value, err := json.Marshal(data) value, err := json.Marshal(data)
...@@ -82,26 +71,8 @@ func (this *IchuntRedis) Set(key string, data interface{}) error { ...@@ -82,26 +71,8 @@ func (this *IchuntRedis) Set(key string, data interface{}) error {
return nil return nil
} }
//Redis Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值。 func Exists(key string) bool {
func(this *IchuntRedis) Setnx(key string, data interface{}) error { conn := readConn.Get()
conn := this.Current.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("SETNX", key, value)
if err != nil {
return err
}
return nil
}
func(this *IchuntRedis) Exists(key string) bool {
conn := this.Current.Get()
defer conn.Close() defer conn.Close()
exists, err := redis.Bool(conn.Do("EXISTS", key)) exists, err := redis.Bool(conn.Do("EXISTS", key))
...@@ -112,8 +83,8 @@ func(this *IchuntRedis) Exists(key string) bool { ...@@ -112,8 +83,8 @@ func(this *IchuntRedis) Exists(key string) bool {
return exists return exists
} }
func(this *IchuntRedis) Get(key string) (interface{}, error) { func Get(key string) (interface{}, error) {
conn := this.Current.Get() conn := readConn.Get()
defer conn.Close() defer conn.Close()
reply, err := conn.Do("GET", key) reply, err := conn.Do("GET", key)
...@@ -124,8 +95,8 @@ func(this *IchuntRedis) Get(key string) (interface{}, error) { ...@@ -124,8 +95,8 @@ func(this *IchuntRedis) Get(key string) (interface{}, error) {
return reply, nil return reply, nil
} }
func(this *IchuntRedis) Delete(key string) (bool, error) { func Delete(key string) (bool, error) {
conn := this.Current.Get() conn := writeConn.Get()
defer conn.Close() defer conn.Close()
return redis.Bool(conn.Do("DEL", key)) return redis.Bool(conn.Do("DEL", key))
...@@ -133,8 +104,8 @@ func(this *IchuntRedis) Delete(key string) (bool, error) { ...@@ -133,8 +104,8 @@ func(this *IchuntRedis) Delete(key string) (bool, error) {
//哈希操作 //哈希操作
func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error { func HSet(key string, k interface{}, data interface{}) error {
conn := this.Current.Get() conn := writeConn.Get()
defer conn.Close() defer conn.Close()
value, err := json.Marshal(data) value, err := json.Marshal(data)
...@@ -150,8 +121,8 @@ func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error ...@@ -150,8 +121,8 @@ func(this *IchuntRedis) HSet(key string, k interface{}, data interface{}) error
return nil return nil
} }
func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) { func HGet(key string, k interface{}) (interface{}, error) {
conn := this.Current.Get() conn := readConn.Get()
defer conn.Close() defer conn.Close()
reply, err := conn.Do("HGET", key, k) reply, err := conn.Do("HGET", key, k)
...@@ -162,10 +133,9 @@ func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) { ...@@ -162,10 +133,9 @@ func(this *IchuntRedis) HGet(key string, k interface{}) (interface{}, error) {
return reply, nil return reply, nil
} }
func(this *IchuntRedis) HDelete(key string, k interface{}) (bool, error) { func HDelete(key string, k interface{}) (bool, error) {
conn := this.Current.Get() conn := writeConn.Get()
defer conn.Close() defer conn.Close()
return redis.Bool(conn.Do("HDEL", key, k)) return redis.Bool(conn.Do("HDEL", key, k))
} }
...@@ -101,7 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) { ...@@ -101,7 +101,7 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList = append(goodsIdList, goodsId) goodsIdList = append(goodsIdList, goodsId)
} }
goodsIdListStr = strings.Join(goodsIdList, ",") goodsIdListStr = strings.Join(goodsIdList, ",")
goodsList, err := GetGoodsInfo(goodsIdListStr) goodsList, err := GetGoodsInfoByApi(goodsIdListStr,1)
response.Data = goodsList response.Data = goodsList
response.Flag = req.Flag response.Flag = req.Flag
return &response return &response
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
_ "github.com/tidwall/gjson" _ "github.com/tidwall/gjson"
"search_server/model" "search_server/model"
"search_server/pkg/config" "search_server/pkg/config"
"search_server/pkg/gredis"
"strings" "strings"
) )
...@@ -40,15 +39,16 @@ func getSkuByGoodsSn(goods_list map[string]*model.LyClearGoodsList, supplier_inf ...@@ -40,15 +39,16 @@ func getSkuByGoodsSn(goods_list map[string]*model.LyClearGoodsList, supplier_inf
// //
//fmt.Println(productList) //fmt.Println(productList)
//return productList //return productList
originGoods := make(map[string]interface{}, 0) originGoods := make(map[string]interface{}, 0)
sku_uique_judge := config.Get("redis_all.SKU_UNIQUE_JUDGE").String() sku_uique_judge := config.Get("redis_all.SKU_UNIQUE_JUDGE").String()
for goods_sn, info := range goods_list { for goods_sn, info := range goods_list {
sn_sku := php2go.Md5(strings.ToLower(goods_sn)) sn_sku := php2go.Md5(strings.ToLower(goods_sn))
sku_id, _ := gredis.HGet(sku_uique_judge, sn_sku) //查询唯一值,反查sku_id fmt.Println(sku_uique_judge, sn_sku)
if sku_id == "" { //为空,先创建sku //sku_id, _ := gredis.IchuntRedis.Hget(sku_uique_judge, sn_sku) //查询唯一值,反查sku_id
sku_id := ""
if sku_id == "" { //为空,先创建sku
//lock_key := "searchapi_"+sn_sku; //lock_key := "searchapi_"+sn_sku;
//flag := gredis.Setnx(lock_key,php2go.Time()+2) //flag := gredis.Setnx(lock_key,php2go.Time()+2)
//if flag { //存在锁 //if flag { //存在锁
......
package service package service
import ( import (
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/imroc/req" "github.com/imroc/req"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/uniplaces/carbon" "github.com/uniplaces/carbon"
"gopkg.in/olivere/elastic.v5" "gopkg.in/olivere/elastic.v5"
"regexp" "regexp"
"search_server/model"
"search_server/pkg/config" "search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/protopb/bom" "search_server/protopb/bom"
"search_server/requests" "search_server/requests"
"strings" "strings"
) )
func GetGoodsInfo(goodsIdsStr string) (goodsList []*bom.GoodsModel, err error) { //获取商品信息
//需要传入userId用于判断是否登陆
func GetGoodsInfoByApi(goodsIdsStr string, userId int) (goodsList []*bom.GoodsModel, err error) {
isNewCustomer, isMember := CheckIsNewCustomer(userId)
goodsServerUrl := config.Get("goods.api_url").String() goodsServerUrl := config.Get("goods.api_url").String()
params := req.Param{ params := req.Param{
"goods_id": goodsIdsStr, "goods_id": goodsIdsStr,
"power[newCustomer]": isNewCustomer,
"power[member]": isMember,
} }
//fmt.Println(goodsIdsStr) //fmt.Println(goodsIdsStr)
resp, err := req.Post(goodsServerUrl+"/synchronization", params) resp, err := req.Post(goodsServerUrl+"/synchronization", params)
...@@ -70,16 +80,38 @@ func GetGoodsInfo(goodsIdsStr string) (goodsList []*bom.GoodsModel, err error) { ...@@ -70,16 +80,38 @@ func GetGoodsInfo(goodsIdsStr string) (goodsList []*bom.GoodsModel, err error) {
return return
} }
func CheckIsNewCustomer(userId int) (isNewCustomer, isMember bool) {
//判断新客价
if userId != 0 {
userInfoStr, err := redis.String(gredis.HGet("api_user", userId))
if err != nil {
return
}
// 新客户显示新客价
isNewExist := gjson.Get(userInfoStr, "is_new").Exists()
isNew := gjson.Get(userInfoStr, "is_new").Int()
if isNewExist && isNew == 0 {
isNewCustomer = true
}
isMember = true
} else {
// 未登录显示新客价
isNewCustomer = true
isMember = false
}
return
}
//根据供应商获取商品信息 //根据供应商获取商品信息
func GetGoodsInfoBySupplier(req requests.QuoteIndexRequest) error { func GetGoodsInfoBySupplier(req requests.QuoteIndexRequest) error {
//supplierIndex := config.Get("es.search_supplier").String() supplierIndex := config.Get("es.search_supplier").String()
////获取查询条件 //获取查询条件
//queryString := getSupplierQuery(req) queryString, err := getSupplierQuery(req)
//res, err := es.CurlES(supplierIndex, queryString) res, err := es.CurlES(supplierIndex, queryString)
//if err != nil { if err != nil {
// return err return err
//} }
//fmt.Println(supplierIndex) fmt.Println(res)
return nil return nil
} }
...@@ -240,7 +272,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) { ...@@ -240,7 +272,10 @@ func getSupplierQuery(req requests.QuoteIndexRequest) (string, error) {
//todo : $params['preference'] = '_primary_first'; //todo : $params['preference'] = '_primary_first';
//判断是否要排除某些品牌 //判断是否要排除某些品牌
//todo : 待确定需不需要接入 excludeBrandId := model.GetExcludeBrandIds(1)
for _, brandId := range excludeBrandId {
query.MustNot(elastic.NewTermQuery("brand_id", brandId))
}
searchRequest := elastic.NewSearchRequest().Source(source) searchRequest := elastic.NewSearchRequest().Source(source)
......
...@@ -22,6 +22,12 @@ func main() { ...@@ -22,6 +22,12 @@ func main() {
mysql.Connection("bom").Raw("SELECT * FROM lie_bin").Scan(&result) mysql.Connection("bom").Raw("SELECT * FROM lie_bin").Scan(&result)
//mysql.Connection()("bom").Table("bin").Find(&result) //mysql.Connection()("bom").Table("bin").Find(&result)
fmt.Println(result) fmt.Println(result)
//fmt.Println(model.GetExcludeBrandIds(1))
//client := client.
//c := course.NewCourseService("go.micro.api.jtthink.course", client)
//course_rsp, _ := c.ListForTop(context.TODO(), &course.ListRequest{Size: 10})
//fmt.Println(course_rsp.Result)
type Result2 struct { type Result2 struct {
BusinessName string `json:"business_name"` BusinessName string `json:"business_name"`
......
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