Commit 55ae9a08 by mushishixian

获取商品信息的方法

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