Commit 4bd6c57d by hcy

并发问题

parent 873bdca7
...@@ -18,8 +18,6 @@ import ( ...@@ -18,8 +18,6 @@ import (
"github.com/syyongx/php2go" "github.com/syyongx/php2go"
) )
const goodsSliceCount = 10 //每多少个型号id开启一个协程
/* /*
查询商品详情(自营或者联营) 查询商品详情(自营或者联营)
@doc http://192.168.1.237:3000/project/128/interface/api/649 @doc http://192.168.1.237:3000/project/128/interface/api/649
...@@ -70,10 +68,11 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -70,10 +68,11 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
//////////初始化公共变量 以及 批量查询/////////// //////////初始化公共变量 以及 批量查询///////////
redisLySkuArr := make(map[string]string) //redis.sku redisLySkuArr := make(map[string]string) //redis.sku
redisZySkuArr := make(map[string]string) //redis.Self_SelfGoods redisZySkuArr := make(map[string]string) //redis.Self_SelfGoods
redisLySpuArr := make(map[string]string) //redis.spu
preSkuIds := make([]int64, 0) //归档sku preSkuIds := make([]int64, 0) //归档sku
zyGoodsId := make([]string, 0, goodsSliceCount) zyGoodsId := make([]string, 0)
lyGoodsId := make([]string, 0, goodsSliceCount) lyGoodsId := make([]string, 0)
for _, goodsId := range goodsIdArr { for _, goodsId := range goodsIdArr {
if len(goodsId) < 19 { //自营 if len(goodsId) < 19 { //自营
...@@ -92,11 +91,12 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -92,11 +91,12 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
} }
} }
} }
//批量查询自营sku
if len(zyGoodsId) > 0 { if len(zyGoodsId) > 0 {
redisZySkuArr = gredis.Hmget("default_r", "Self_SelfGoods", zyGoodsId) redisZySkuArr = gredis.Hmget("default_r", "Self_SelfGoods", zyGoodsId)
} }
//批量查询归档sku
if len(preSkuIds) > 0 { //存在归档sku if len(preSkuIds) > 0 {
var prevSkuArr []bson.M var prevSkuArr []bson.M
prevSkuMongo := mongo.Conn("pre_sku") prevSkuMongo := mongo.Conn("pre_sku")
...@@ -120,9 +120,13 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -120,9 +120,13 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
} }
} }
//查询spu信息 //批量查询spu信息
redisLySpuArr := SpuService.GetSpuList(redisLySkuArr) if len(redisLySkuArr) > 0 {
SpuService.SetInit() //设置查询公共变量 redisLySpuArr = SpuService.GetSpuList(redisLySkuArr)
}
//设置查询公共变量
SpuService.SetInit(redisLySkuArr, zyGoodsId)
/////////////////多线程/////////////////////////// /////////////////多线程///////////////////////////
......
...@@ -3,7 +3,6 @@ package service ...@@ -3,7 +3,6 @@ package service
import ( import (
"context" "context"
"github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"go_sku_server/model" "go_sku_server/model"
"go_sku_server/pkg/common" "go_sku_server/pkg/common"
...@@ -40,9 +39,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk ...@@ -40,9 +39,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk
redisConn := gredis.Conn("search_r") redisConn := gredis.Conn("search_r")
redisConnSpu := gredis.Conn("spu") redisConnSpu := gredis.Conn("spu")
// 连接prev_sku MongoDB // 连接prev_sku MongoDB
prevSkuMongo := mongo.Conn("pre_sku")
defer func() { defer func() {
prevSkuMongo.Close()
redisConn.Close() redisConn.Close()
redisConnSpu.Close() redisConnSpu.Close()
}() }()
...@@ -69,8 +66,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk ...@@ -69,8 +66,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk
//读取包装字段的缓存(分别是DGK,avnet,mro) //读取包装字段的缓存(分别是DGK,avnet,mro)
if sku.SupplierId == 7 || sku.SupplierId == 13 || sku.SupplierId == 1688 || sku.SupplierId == 17 { if sku.SupplierId == 7 || sku.SupplierId == 13 || sku.SupplierId == 1688 || sku.SupplierId == 17 {
//sku_raw_map哪里写入(成意写的) //sku_raw_map哪里写入(成意写的)
packing, _ := redis.String(redisConnSpu.Do("HGET", "sku_raw_map", goodsId)) sku.Packing = gjson.Get(AllSkuRawMap[goodsId], "pack").String()
sku.Packing = gjson.Get(packing, "pack").String()
} }
sku = ls.GetGoodsImages(sku, spu) sku = ls.GetGoodsImages(sku, spu)
sku = ls.GetPdf(sku, spu) sku = ls.GetPdf(sku, spu)
...@@ -92,7 +88,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk ...@@ -92,7 +88,7 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk
//获取品牌名称 //获取品牌名称
brandId := gjson.Get(spu, "brand_id").Int() brandId := gjson.Get(spu, "brand_id").Int()
brandName, _ := redis.String(redisConn.Do("HGET", "brand", brandId)) brandName, _ := AllLyBrand[gconv.String(brandId)]
sku.BrandName = brandName sku.BrandName = brandName
sku.BrandId = brandId sku.BrandId = brandId
......
...@@ -2,6 +2,7 @@ package service ...@@ -2,6 +2,7 @@ package service
import ( import (
"encoding/json" "encoding/json"
"github.com/gogf/gf/util/gconv"
"go_sku_server/model" "go_sku_server/model"
c "go_sku_server/pkg/common" c "go_sku_server/pkg/common"
"go_sku_server/pkg/gredis" "go_sku_server/pkg/gredis"
...@@ -9,7 +10,6 @@ import ( ...@@ -9,7 +10,6 @@ import (
_ "go_sku_server/pkg/mongo" _ "go_sku_server/pkg/mongo"
"go_sku_server/service/sorter" "go_sku_server/service/sorter"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/gomodule/redigo/redis" "github.com/gomodule/redigo/redis"
...@@ -264,9 +264,7 @@ func (ls *LyService) GetStock(sku model.LySku) (stock int64) { ...@@ -264,9 +264,7 @@ func (ls *LyService) GetStock(sku model.LySku) (stock int64) {
//如果是寄售的数据(source=12),获取的库存还要减去锁库的库存 //如果是寄售的数据(source=12),获取的库存还要减去锁库的库存
if sku.Source == 12 || php2go.InArray(sku.Canal, []string{"L0018319", "L0013521", "L0018562", "L0017764", "L0003270", "L0012413", "L0013521"}) { if sku.Source == 12 || php2go.InArray(sku.Canal, []string{"L0018319", "L0013521", "L0018562", "L0017764", "L0003270", "L0012413", "L0013521"}) {
//获取锁库库存 //获取锁库库存
redisCon := gredis.Conn("search_r") lockStock := gconv.Int64(AllSkuLockMap[sku.GoodsId])
stockStr, _ := redis.String(redisCon.Do("HGET", "sku_lock_stock", sku.GoodsId))
lockStock, _ := strconv.ParseInt(stockStr, 10, 64)
if sku.Stock < lockStock { if sku.Stock < lockStock {
return 0 return 0
} else { } else {
...@@ -327,19 +325,16 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku { ...@@ -327,19 +325,16 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
} }
if sku.Canal == "L0018562" { if sku.Canal == "L0018562" {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
usdRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", 2))
for _, price := range sku.OriginalPrice { for _, price := range sku.OriginalPrice {
originalPrice = append(originalPrice, model.OriginPrice{ originalPrice = append(originalPrice, model.OriginPrice{
Purchases: price.Purchases, Purchases: price.Purchases,
PriceCn: c.MyRound(c.MulFloat(price.PriceUs, usdRatio*tax), 4), PriceCn: c.MyRound(c.MulFloat(price.PriceUs, UsdRate*tax), 4),
PriceUs: price.PriceUs, PriceUs: price.PriceUs,
}) })
} }
for index, price := range sku.LadderPrice { for index, price := range sku.LadderPrice {
ladderPrice[index].PriceCn = c.MyRound(c.MulFloat(price.PriceUs, usdRatio*tax), 4) ladderPrice[index].PriceCn = c.MyRound(c.MulFloat(price.PriceUs, UsdRate*tax), 4)
} }
} }
...@@ -509,12 +504,10 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku { ...@@ -509,12 +504,10 @@ func (ls *LyService) GetCoefficientAndPrice(sku model.LySku) model.LySku {
priceCostCn = c.MulFloat(price.PriceCn, 1) priceCostCn = c.MulFloat(price.PriceCn, 1)
} else { } else {
//这里是去取实时美金汇率,并且计算人民币价格 //这里是去取实时美金汇率,并且计算人民币价格
var usdRatio float64
if sku.CustomizeRateRMB != 0 { if sku.CustomizeRateRMB != 0 {
priceCostCn = c.MulFloat(price.PriceCn, 1, tax) priceCostCn = c.MulFloat(price.PriceCn, 1, tax)
} else { } else {
usdRatio, _ = redis.Float64(redisCon.Do("HGET", "erp_rate", 2)) priceCostCn = c.MulFloat(price.PriceUs, UsdRate, tax)
priceCostCn = c.MulFloat(price.PriceUs, usdRatio, tax)
} }
} }
priceCostCn = c.MulFloat(c.MyRound(priceCostCn, 4), sku.DiscountRatio.Ratio) priceCostCn = c.MulFloat(c.MyRound(priceCostCn, 4), sku.DiscountRatio.Ratio)
......
...@@ -721,7 +721,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS ...@@ -721,7 +721,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS
defer func() { defer func() {
redisCon.Close() redisCon.Close()
}() }()
usRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", 2)) usRatio := UsdRate //美金汇率
var currency int var currency int
var currencyConfig string var currencyConfig string
if sku.SupplierId != 17 { if sku.SupplierId != 17 {
...@@ -764,7 +764,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS ...@@ -764,7 +764,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS
//sku.OriginalPrice[index].PriceCn = priceCn //sku.OriginalPrice[index].PriceCn = priceCn
//这里面还要判断,如果是usToCn为true代表也要走美金转人民币的逻辑(前提是没有人民币价格) //这里面还要判断,如果是usToCn为true代表也要走美金转人民币的逻辑(前提是没有人民币价格)
if price.PriceCn == 0 && usToCn == true { if price.PriceCn == 0 && usToCn == true {
rmbRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", currency)) rmbRatio := GetCurrencyRate(currency)
priceCn := c.MyRound(c.MulFloat(priceUs, rmbRatio, 1.13), 4) priceCn := c.MyRound(c.MulFloat(priceUs, rmbRatio, 1.13), 4)
sku.LadderPrice[index].PriceCn = priceCn sku.LadderPrice[index].PriceCn = priceCn
} }
...@@ -787,7 +787,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS ...@@ -787,7 +787,7 @@ func (ps *PriceService) TransformSpecialSupplierPrice(sku model.LySku) model.LyS
if customizeRateUsd == 0 { if customizeRateUsd == 0 {
//这里进行转换,因为这里都只能取到对应的币种转人民币的比率,我们没有直接各种币种转美金的数据,所以我这边要 //这里进行转换,因为这里都只能取到对应的币种转人民币的比率,我们没有直接各种币种转美金的数据,所以我这边要
//先根据对应币种转人民币,然后根据人民币转美金,才能得到不同币种对应美金的汇率 //先根据对应币种转人民币,然后根据人民币转美金,才能得到不同币种对应美金的汇率
rmbRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", currency)) rmbRatio := GetCurrencyRate(currency)
//fmt.Println("特殊转换", rmbRatio) //fmt.Println("特殊转换", rmbRatio)
//fmt.Println(rmbRatio, usRatio) //fmt.Println(rmbRatio, usRatio)
//人民币汇率转美金汇率 //人民币汇率转美金汇率
......
package service package service
import ( import (
"github.com/gomodule/redigo/redis" "github.com/gogf/gf/util/gconv"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"go_sku_server/pkg/gredis" "go_sku_server/pkg/gredis"
) )
var UsdRate float64 //美金匯率 var UsdRate float64 //美金匯率
var AllCurrencyRate map[string]string //所有汇率
var AllLyBrand map[string]string //所有联营品牌
var AllSkuRawMap map[string]string //所有原始编码 sku_raw_map 数据
var AllSkuLockMap map[string]string //自营锁库库存
var AllExtendFee map[string]string //所有附加费
// 币种中文
var CURRENCY_CN = map[int]string{
1: "人民币",
2: "美元",
3: "港币",
4: "欧元",
5: "英镑",
6: "瑞士法郎",
7: "澳大利亚元",
8: "加拿大元",
9: "日元",
10: "新加坡",
11: "新台币",
12: "泰铢",
13: "越南盾",
}
type SpuService struct { type SpuService struct {
} }
func (ss *SpuService) GetSpuList(skuArr map[string]string) (spuList map[string]string) { // 批量获取spu+品牌
func (ss *SpuService) GetSpuList(skuArr map[string]string) map[string]string {
var spuIds []string var spuIds []string
for _, skuStr := range skuArr { for _, skuStr := range skuArr {
spuId := gjson.Get(skuStr, "spu_id").String() spuId := gjson.Get(skuStr, "spu_id").String()
spuIds = append(spuIds, spuId) spuIds = append(spuIds, spuId)
} }
//批量获取spu详情 //批量获取spu详情
spuList = gredis.Hmget("spu", "spu", spuIds) spuList := gredis.Hmget("spu", "spu", spuIds)
return
//批量查询普通品牌名称
brandIds := make([]string, 0)
for _, spuInfo := range spuList {
//设置查询公共变量
brandId := gjson.Get(spuInfo, "brand_id").String()
if !php2go.InArray(brandId, brandIds) {
brandIds = append(brandIds, brandId)
}
}
AllLyBrand = gredis.Hmget("default_r", "brand", brandIds)
return spuList
} }
// 设置查询公共变量 // 设置查询公共变量
func (ss *SpuService) SetInit() { func (ss *SpuService) SetInit(redisLySkuArr map[string]string, zyGoodsId []string) {
redisCon := gredis.Conn("default_r")
usdRatio, _ := redis.Float64(redisCon.Do("HGET", "erp_rate", 2)) needRawGoodsIds := make([]string, 0) //批量获取 原始编码
UsdRate = usdRatio needExtendFeeId := make([]string, 0) //批量获取 附加费
for goods_id, info := range redisLySkuArr {
supplierId := gjson.Get(info, "supplier_id").Int()
source := gjson.Get(info, "source").Int()
canal := gjson.Get(info, "canal").String()
if php2go.InArray(supplierId, []int{7, 13, 1688, 17}) {
needRawGoodsIds = append(needRawGoodsIds, goods_id)
}
if source == 12 || php2go.InArray(canal, []string{"L0018319", "L0013521", "L0018562", "L0017764", "L0003270", "L0012413", "L0013521"}) {
zyGoodsId = append(zyGoodsId, goods_id)
}
feekey := ""
if supplierId == 17 {
feekey = "17." + canal
} else {
feekey = gconv.String(supplierId)
}
if !php2go.InArray(feekey, needExtendFeeId) {
needExtendFeeId = append(needExtendFeeId, feekey)
}
}
//批量获取汇率
AllCurrencyRate = gredis.Hmget("default_r", "erp_rate", []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"})
UsdRate = gconv.Float64(AllCurrencyRate["2"])
//批量获取原始编码
if len(needRawGoodsIds) > 0 {
AllSkuRawMap = gredis.Hmget("default_r", "sku_raw_map", needRawGoodsIds)
}
//批量获取锁库信息
if len(zyGoodsId) > 0 {
AllSkuLockMap = gredis.Hmget("default_r", "sku_lock_stock", zyGoodsId) //所有锁库信息
}
//查询附加费
}
// 获取指定的币种汇率
func GetCurrencyRate(currency int) float64 {
return gconv.Float64(AllCurrencyRate[gconv.String(currency)])
} }
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