Commit 4bd6c57d by hcy

并发问题

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