Commit 55ae9a08 by mushishixian

获取商品信息的方法

parent ed8542df
package model
type ApiGoods struct {
GoodsID string `json:"goods_id"`
GoodsId string `json:"goods_id"`
GoodsName string `json:"goods_name"`
GoodsType int `json:"goods_type"`
SupplierID int `json:"supplier_id"`
Moq int `json:"moq"`
Mpq int `json:"mpq"`
Stock int `json:"stock"`
GoodsType int64 `json:"goods_type"`
SupplierId int64 `json:"supplier_id"`
Moq int64 `json:"moq"`
Mpq int64 `json:"mpq"`
Stock int64 `json:"stock"`
HkDeliveryTime string `json:"hk_delivery_time"`
CnDeliveryTime string `json:"cn_delivery_time"`
LadderPrice []LadderPrice `json:"ladder_price"`
BrandName string `json:"brand_name"`
SupplierName string `json:"supplier_name"`
BrandID int `json:"brand_id"`
IsBuy int `json:"is_buy"`
Mpl int `json:"mpl"`
Status int `json:"status"`
BrandId int64 `json:"brand_id"`
IsBuy int64 `json:"is_buy"`
Mpl int64 `json:"mpl"`
Status int64 `json:"status"`
Pdf string `json:"pdf"`
Encap string `json:"encap"`
AcType int `json:"ac_type"`
AcType int64 `json:"ac_type"`
//ErpTax bool `json:"erp_tax"`
}
type LadderPrice struct {
Purchases int `json:"purchases"`
Purchases int64 `json:"purchases"`
PriceUs float64 `json:"price_us"`
PriceCn float64 `json:"price_cn"`
PriceAc float64 `json:"price_ac"`
......
......@@ -101,7 +101,8 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList = append(goodsIdList, goodsId)
}
goodsIdListStr = strings.Join(goodsIdList, ",")
goodsList, err := GetGoodsInfoByApi(goodsIdListStr, 1)
goodsService := GoodsService{}
goodsList, err := goodsService.GetGoodsInfoByApi(goodsIdListStr)
response.Data = goodsList
response.Flag = req.Flag
return &response
......
......@@ -12,26 +12,45 @@ import (
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/protopb/bom"
"search_server/requests"
"strings"
)
//获取商品信息
//需要传入userId用于判断是否登陆
func GetGoodsInfoByApi(goodsIdsStr string, userId int) (goodsList []*bom.GoodsModel, err error) {
type GoodsService struct {
}
//获取商品信息,需要传入userId用于判断是否登陆
func (gs *GoodsService) GetGoodsInfo(goodsIdsStr string, userId int) (goodsListMap map[string]*model.ApiGoods, err error) {
isNewCustomer, isMember := CheckIsNewCustomer(userId)
goodsServerUrl := config.Get("goods.api_url").String()
params := req.Param{
"goods_id": goodsIdsStr,
"power[newCustomer]": isNewCustomer,
"power[member]": isMember,
}
//fmt.Println(goodsIdsStr)
_, goodsListMap, err = CurlGoodsInfo(goodsIdsStr, params)
return
}
//获取商品信息
func (gs *GoodsService) GetGoodsInfoByApi(goodsIdsStr string) (goodsList []*model.ApiGoods, err error) {
params := req.Param{
"goods_id": goodsIdsStr,
}
goodsList, _, err = CurlGoodsInfo(goodsIdsStr, params)
fmt.Println(goodsList[0].BrandId)
return
}
//isMap:是否以字典形式返回值,默认是数组
func CurlGoodsInfo(goodsIdsStr string, params req.Param) (goodsList []*model.ApiGoods, goodsListMap map[string]*model.ApiGoods, err error) {
//req.Debug = true
goodsServerUrl := config.Get("goods.api_url").String()
resp, err := req.Post(goodsServerUrl+"/synchronization", params)
if err != nil {
return
}
goodsListMap = make(map[string]*model.ApiGoods, 1000)
//先判断返回的data是不是字典,不是字典代表可能是返回字符串了
if gjson.Get(resp.String(), "data").IsObject() {
//排序操作
......@@ -39,9 +58,9 @@ func GetGoodsInfoByApi(goodsIdsStr string, userId int) (goodsList []*bom.GoodsMo
for _, goodsId := range goodsIdList {
for _, data := range gjson.Get(resp.String(), "data").Map() {
if goodsId == data.Get("goods_id").String() {
//还要去判断是否是bool
//还要去判断是否是false,因为商品服务可能返回 "1000":false 这样子的形式
if data.IsObject() {
var goods bom.GoodsModel
var goods model.ApiGoods
goods.GoodsName = data.Get("goods_name").String()
goods.GoodsId = data.Get("goods_id").String()
goods.BrandId = data.Get("goods_id").Int()
......@@ -51,7 +70,7 @@ func GetGoodsInfoByApi(goodsIdsStr string, userId int) (goodsList []*bom.GoodsMo
goods.Moq = data.Get("moq").Int()
goods.SupplierName = data.Get("supplier_name").String()
goods.BrandName = data.Get("brand_name").String()
goods.HkDeliveryType = data.Get("hk_delivery_time").String()
goods.HkDeliveryTime = data.Get("hk_delivery_time").String()
goods.CnDeliveryTime = data.Get("cn_delivery_time").String()
goods.IsBuy = data.Get("is_buy").Int()
goods.Mpl = data.Get("mpl").Int()
......@@ -60,18 +79,21 @@ func GetGoodsInfoByApi(goodsIdsStr string, userId int) (goodsList []*bom.GoodsMo
goods.Status = data.Get("status").Int()
goods.GoodsType = data.Get("goods_type").Int()
goods.AcType = data.Get("ac_type").Int()
ladderPrice := make([]*bom.LadderPrice, 0)
ladderPrice := make([]model.LadderPrice, 0)
for _, price := range data.Get("ladder_price").Array() {
ladder := bom.LadderPrice{
ladder := model.LadderPrice{
Purchases: price.Get("purchases").Int(),
PriceUs: float32(price.Get("price_us").Float()),
PriceCn: float32(price.Get("price_cn").Float()),
PriceAc: float32(price.Get("price_ac").Float()),
PriceUs: price.Get("price_us").Float(),
PriceCn: price.Get("price_cn").Float(),
PriceAc: price.Get("price_ac").Float(),
}
ladderPrice = append(ladderPrice, &ladder)
ladderPrice = append(ladderPrice, ladder)
}
goods.LadderPrice = ladderPrice
goodsList = append(goodsList, &goods)
goodsListMap[goodsId] = &goods
} else {
goodsListMap[goodsId] = nil
}
}
}
......
......@@ -4,7 +4,7 @@ import (
"flag"
"fmt"
"search_server/boot"
"search_server/pkg/mysql"
"search_server/service"
)
func main() {
......@@ -14,13 +14,6 @@ func main() {
if err := boot.Boot(path); err != nil {
fmt.Println(err)
}
type Result struct {
BinId int `json:"bin_id"`
Bin string `json:"bin"`
}
var result []Result
mysql.Connection("bom").Raw("SELECT * FROM lie_bin").Scan(&result)
//mysql.Connection()("bom").Table("bin").Find(&result)
fmt.Println(result)
//fmt.Println(model.GetExcludeBrandIds(1))
service1:= service.GoodsService{}
fmt.Println(service1.GetGoodsInfoByApi("1151005920927710088,1150961641855982990,1150961724815213435"))
}
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