Commit 873bdca7 by hcy

并发问题

parent 43292f45
...@@ -68,6 +68,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -68,6 +68,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
} }
//////////初始化公共变量 以及 批量查询/////////// //////////初始化公共变量 以及 批量查询///////////
redisLySkuArr := make(map[string]string) //redis.sku
redisZySkuArr := make(map[string]string) //redis.Self_SelfGoods
preSkuIds := make([]int64, 0) //归档sku
zyGoodsId := make([]string, 0, goodsSliceCount) zyGoodsId := make([]string, 0, goodsSliceCount)
lyGoodsId := make([]string, 0, goodsSliceCount) lyGoodsId := make([]string, 0, goodsSliceCount)
...@@ -79,17 +83,20 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -79,17 +83,20 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
} }
} }
//批量查询redis-sku信息 //批量查询redis-sku信息
redisLySkuArr := gredis.Hmget("default_r", "sku", lyGoodsId) if len(lyGoodsId) > 0 {
redisLySkuArr = gredis.Hmget("default_r", "sku", lyGoodsId)
//批量查询归档的sku //批量查询归档的sku
preSkuIds := make([]int64, 0) for _, goodsId := range goodsIdArr {
for _, goodsId := range goodsIdArr { if redisLySkuArr[goodsId] == "" {
if redisLySkuArr[goodsId] == "" { preSkuIds = append(preSkuIds, gconv.Int64(goodsId))
preSkuIds = append(preSkuIds, gconv.Int64(goodsId)) }
} }
} }
if len(zyGoodsId) > 0 {
redisZySkuArr = gredis.Hmget("default_r", "Self_SelfGoods", zyGoodsId)
}
if len(preSkuIds) > 0 { if len(preSkuIds) > 0 { //存在归档sku
var prevSkuArr []bson.M var prevSkuArr []bson.M
prevSkuMongo := mongo.Conn("pre_sku") prevSkuMongo := mongo.Conn("pre_sku")
...@@ -120,8 +127,8 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -120,8 +127,8 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
/////////////////多线程/////////////////////////// /////////////////多线程///////////////////////////
var wg sync.WaitGroup var wg sync.WaitGroup
ch := make(chan sync.Map, 500) //管道 ch := make(chan sync.Map, len(goodsIdArr)) //管道
semaphore := make(chan struct{}, 10) // 限制最大并发数为10 semaphore := make(chan struct{}, 10) // 限制最大并发数为10
temp := make(map[string]interface{}) //最后输出计算结果 temp := make(map[string]interface{}) //最后输出计算结果
sd := 0 sd := 0
...@@ -130,27 +137,27 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -130,27 +137,27 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
if goodsId == "" { if goodsId == "" {
continue continue
} }
if len(goodsId) < 19 { //自营 if len(goodsId) < 19 { //自营
zyGoodsId = append(zyGoodsId, goodsId) common.PrintDebugHtml(ctx, "zy增加协程:"+goodsId)
if len(zyGoodsId) >= goodsSliceCount { //单个sku详情
common.PrintDebugHtml(ctx, "zy增加协程1001:") if _, ok := redisLySkuArr[goodsId]; !ok {
common.PrintDebugHtml(ctx, zyGoodsId) temp[goodsId] = false
wg.Add(1) //协程计数一 continue
semaphore <- struct{}{} // 获取信号量 }
skuStr := redisZySkuArr[goodsId]
idsToProcess := make([]string, len(zyGoodsId)) wg.Add(1) //协程计数一
copy(idsToProcess, zyGoodsId) semaphore <- struct{}{} // 获取信号量
// 启动协程,传递独立的 context 和参数,而不是 gin.Context // 启动协程,传递独立的 context 和参数,而不是 gin.Context
go func(ctx context.Context, params service.RequestParams, goodsIds []string, ch chan sync.Map) { go func(ctx context.Context, params service.RequestParams, skuStr, goods_id string, ch chan sync.Map) {
defer wg.Done() defer wg.Done()
defer func() { <-semaphore }() // 释放信号量 defer func() { <-semaphore }() // 释放信号量
zyService.ZyGoodsDetail(ctx, params, goodsIds, ch) zyService.ZyGoodsDetail(ctx, params, skuStr, goods_id, ch)
}(ctx, requestParams, idsToProcess, ch) }(ctx, requestParams, skuStr, goodsId, ch)
zyGoodsId = zyGoodsId[:0]
}
} else { //联营 } else { //联营
common.PrintDebugHtml(ctx, "ly增加协程:"+goodsId) common.PrintDebugHtml(ctx, "ly增加协程:"+goodsId)
...@@ -170,7 +177,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -170,7 +177,10 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
// 启动协程,传递独立的 context 和参数,而不是 gin.Context // 启动协程,传递独立的 context 和参数,而不是 gin.Context
go func(ctx context.Context, params service.RequestParams, skuStr, goodsId, spuStr string, ch chan sync.Map, tag int) { go func(ctx context.Context, params service.RequestParams, skuStr, goodsId, spuStr string, ch chan sync.Map, tag int) {
defer wg.Done() defer wg.Done()
defer func() { <-semaphore }() // 释放信号量 defer func() {
//println("释放信号:" + gconv.String(sd) + " time" + gconv.String(time.Now().UnixMilli()))
<-semaphore
}() // 释放信号量
lyService.LyGoodsDetail(ctx, params, skuStr, goodsId, spuStr, ch, tag) lyService.LyGoodsDetail(ctx, params, skuStr, goodsId, spuStr, ch, tag)
}(ctx, requestParams, skuStr, goodsId, redisLySpuArr[spuId], ch, sd) }(ctx, requestParams, skuStr, goodsId, redisLySpuArr[spuId], ch, sd)
......
...@@ -674,3 +674,14 @@ func CheckIntSliceContains(target int, slice []int) bool { ...@@ -674,3 +674,14 @@ func CheckIntSliceContains(target int, slice []int) bool {
} }
return false return false
} }
// 返回带小数点的时间戳,字符串
func ProTime() string {
now := time.Now()
// 获取秒级时间戳
seconds := now.Unix()
// 获取毫秒部分
milliseconds := now.UnixNano()/1e6 - seconds*1000
// 格式化为 "秒.毫秒" 格式
return fmt.Sprintf("%d.%03d", seconds, milliseconds)
}
...@@ -2,6 +2,9 @@ package service ...@@ -2,6 +2,9 @@ package service
import ( import (
"context" "context"
"github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
"go_sku_server/model" "go_sku_server/model"
"go_sku_server/pkg/common" "go_sku_server/pkg/common"
"go_sku_server/pkg/gredis" "go_sku_server/pkg/gredis"
...@@ -12,10 +15,6 @@ import ( ...@@ -12,10 +15,6 @@ import (
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"sort" "sort"
"sync" "sync"
"github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
) )
type LyService struct { type LyService struct {
...@@ -37,7 +36,7 @@ type Power struct { ...@@ -37,7 +36,7 @@ type Power struct {
*/ */
func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, skuStr, goodsId, spuStr string, ch chan sync.Map, tag int) { func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, skuStr, goodsId, spuStr string, ch chan sync.Map, tag int) {
//start_time := gconv.String(time.Now().UnixMilli()) start_time := common.ProTime()
redisConn := gredis.Conn("search_r") redisConn := gredis.Conn("search_r")
redisConnSpu := gredis.Conn("spu") redisConnSpu := gredis.Conn("spu")
// 连接prev_sku MongoDB // 连接prev_sku MongoDB
...@@ -273,7 +272,10 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk ...@@ -273,7 +272,10 @@ func (ls *LyService) LyGoodsDetail(ctx context.Context, params RequestParams, sk
//退出通道 //退出通道
ch <- GoodsRes ch <- GoodsRes
//println("---tag:" + gconv.String(tag) + "-----goodsId:" + goodsId + "--start_time:" + start_time + " ---end_time:" + gconv.String(time.Now().UnixMilli())) if (gconv.Int64(start_time) - gconv.Int64(common.ProTime())) > 2 {
println("---超时:" + gconv.String(tag) + "-----goodsId:" + goodsId + "--start_time:" + start_time + " ---end_time:" + common.ProTime())
}
} }
// 获取活动 // 获取活动
......
...@@ -75,14 +75,14 @@ func (ss *SampleService) GetSampleList(ctx *gin.Context) (data map[string]interf ...@@ -75,14 +75,14 @@ func (ss *SampleService) GetSampleList(ctx *gin.Context) (data map[string]interf
func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[string]interface{} { func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[string]interface{} {
// 提取请求参数 // 提取请求参数
params := ExtractRequestParams(ctx) //params := ExtractRequestParams(ctx)
// 创建带超时的 context // 创建带超时的 context
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 20*time.Second) ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel() defer cancel()
//抽取自营 或者联营 goods_id //抽取自营 或者联营 goods_id
zyService := ZiyingService{} //实例化自营查询 //zyService := ZiyingService{} //实例化自营查询
//lyService := LyService{} //实例化联营查询 //lyService := LyService{} //实例化联营查询
ch := make(chan sync.Map) //管道 ch := make(chan sync.Map) //管道
p := 0 //总共协程 p := 0 //总共协程
...@@ -95,7 +95,7 @@ func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[strin ...@@ -95,7 +95,7 @@ func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[strin
if len(goodsId) < 19 { //自营 if len(goodsId) < 19 { //自营
zyGoodsId = append(zyGoodsId, goodsId) zyGoodsId = append(zyGoodsId, goodsId)
if len(zyGoodsId) >= 10 { if len(zyGoodsId) >= 10 {
go zyService.ZyGoodsDetail(ctxWithTimeout, params, zyGoodsId, ch) // go zyService.ZyGoodsDetail(ctxWithTimeout, params, zyGoodsId, ch)
zyGoodsId = zyGoodsId[:0:0] zyGoodsId = zyGoodsId[:0:0]
p++ p++
} }
...@@ -109,7 +109,7 @@ func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[strin ...@@ -109,7 +109,7 @@ func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[strin
} }
} }
if len(zyGoodsId) > 0 { if len(zyGoodsId) > 0 {
go zyService.ZyGoodsDetail(ctxWithTimeout, params, zyGoodsId, ch) //go zyService.ZyGoodsDetail(ctxWithTimeout, params, zyGoodsId, ch)
p++ p++
} }
if len(lyGoodsId) > 0 { if len(lyGoodsId) > 0 {
......
...@@ -34,7 +34,7 @@ type ZiyingService struct { ...@@ -34,7 +34,7 @@ type ZiyingService struct {
@param power[special_invoice] 增值税专用发票公司名字,活动价时需要,否则可能导致用户无法享受活动价 : 深圳是猎芯科技有限公司 @param power[special_invoice] 增值税专用发票公司名字,活动价时需要,否则可能导致用户无法享受活动价 : 深圳是猎芯科技有限公司
@param power[verify_blacklist] 是否验证黑名单,用于折扣活动提交订单页面与后台下单 :true @param power[verify_blacklist] 是否验证黑名单,用于折扣活动提交订单页面与后台下单 :true
*/ */
func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams, goodsIds []string, ch chan sync.Map) { func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams, info, goodsId string, ch chan sync.Map) {
// 检查 context 是否已取消(超时或主动取消) // 检查 context 是否已取消(超时或主动取消)
select { select {
case <-ctx.Done(): case <-ctx.Done():
...@@ -51,317 +51,294 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams ...@@ -51,317 +51,294 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx context.Context, params RequestParams
redisConnSpu.Close() redisConnSpu.Close()
}() }()
skuArr := gredis.Hmget("search_r", "Self_SelfGoods", goodsIds) //批量获取商品详情 fast := params.Fast // 从参数中获取,而不是从 gin.Context
fast := params.Fast // 从参数中获取,而不是从 gin.Context
GoodsRes := sync.Map{} GoodsRes := sync.Map{}
for goodsId, info := range skuArr { if gjson.Get(info, "goods_name").String() == "" {
// 在处理每个商品前检查 context 是否已取消 //fmt.Print("zy goods_name为空-----",goods_id,skuArr)
select { GoodsRes.Store(goodsId, false)
case <-ctx.Done(): ch <- GoodsRes
logger.Log("ZyGoodsDetail: 处理过程中context被取消", "sku", 1) return
// 不要在这里发送,直接返回 }
return
default: //拼接属性
} attrJsonArr := gjson.Parse(gjson.Get(info, "attrs").String()).Array()
if gjson.Get(info, "goods_name").String() == "" { attrs := make([]model.Attrs, 0)
//fmt.Print("zy goods_name为空-----",goods_id,skuArr) for _, d := range attrJsonArr {
GoodsRes.Store(goodsId, false) if d.Get("attr_name").String() == "" {
continue continue
} }
attrs = append(attrs, model.Attrs{
AttrName: d.Get("attr_name").String(),
AttrValue: d.Get("attr_value").String(),
})
}
//拼接属性 //数组
attrJsonArr := gjson.Parse(gjson.Get(info, "attrs").String()).Array() A := orderedmap.New() //初始化有序map,拼接data 数据
attrs := make([]model.Attrs, 0) A.Set("goods_id", common.MyInt64(goodsId))
for _, d := range attrJsonArr { A.Set("goods_type", gjson.Get(info, "goods_type").Int()) //查询总条数
if d.Get("attr_name").String() == "" { A.Set("supplier_id", gjson.Get(info, "supplier_id").Int()) //查询总条数
continue // A.Set("brand_id", gjson.Get(info, "brand_id").Int()) //
}
attrs = append(attrs, model.Attrs{ otherAttrs := gjson.Get(info, "other_attrs").String()
AttrName: d.Get("attr_name").String(), if otherAttrs == "" {
AttrValue: d.Get("attr_value").String(), A.Set("other_attrs", "")
}) } else {
B := make(map[string]string)
pickType := gjson.Get(otherAttrs, "pick_type").String()
length := gjson.Get(otherAttrs, "length").String()
grossWegiht := gjson.Get(otherAttrs, "gross_wegiht").String()
if pickType != "" {
B["pick_type"] = pickType
} }
B["length"] = length
//数组 if grossWegiht != "" {
A := orderedmap.New() //初始化有序map,拼接data 数据 B["gross_wegiht"] = grossWegiht
A.Set("goods_id", common.MyInt64(goodsId))
A.Set("goods_type", gjson.Get(info, "goods_type").Int()) //查询总条数
A.Set("supplier_id", gjson.Get(info, "supplier_id").Int()) //查询总条数
// A.Set("brand_id", gjson.Get(info, "brand_id").Int()) //
otherAttrs := gjson.Get(info, "other_attrs").String()
if otherAttrs == "" {
A.Set("other_attrs", "")
} else {
B := make(map[string]string)
pickType := gjson.Get(otherAttrs, "pick_type").String()
length := gjson.Get(otherAttrs, "length").String()
grossWegiht := gjson.Get(otherAttrs, "gross_wegiht").String()
if pickType != "" {
B["pick_type"] = pickType
}
B["length"] = length
if grossWegiht != "" {
B["gross_wegiht"] = grossWegiht
}
A.Set("other_attrs", B)
} }
A.Set("other_attrs", B)
}
classId1 := gjson.Get(info, "class_id1").Int() classId1 := gjson.Get(info, "class_id1").Int()
classId2 := gjson.Get(info, "class_id2").Int() classId2 := gjson.Get(info, "class_id2").Int()
classId1Name := "" classId1Name := ""
if classId1 > 0 { if classId1 > 0 {
classId1Info, _ := gredis.String(redisConn.Do("HGET", "Self_SelfClassInfo", classId1)) classId1Info, _ := gredis.String(redisConn.Do("HGET", "Self_SelfClassInfo", classId1))
classId1Name = gjson.Get(classId1Info, "class_name").String() classId1Name = gjson.Get(classId1Info, "class_name").String()
} }
classId2Name := "" classId2Name := ""
if classId2 > 0 { if classId2 > 0 {
classId2Info, _ := gredis.String(redisConn.Do("HGET", "Self_SelfClassInfo", classId2)) classId2Info, _ := gredis.String(redisConn.Do("HGET", "Self_SelfClassInfo", classId2))
classId2Name = gjson.Get(classId2Info, "class_name").String() classId2Name = gjson.Get(classId2Info, "class_name").String()
} }
//迁移品牌获取,以前是读的自营品牌库,现在改成了读spu的品牌(其实就是联营品牌)以及获取标准品牌信息 //迁移品牌获取,以前是读的自营品牌库,现在改成了读spu的品牌(其实就是联营品牌)以及获取标准品牌信息
spuId := gjson.Get(info, "spu_id").Int() spuId := gjson.Get(info, "spu_id").Int()
var standardBrand model.StandardBrand var standardBrand model.StandardBrand
var brandId int64 var brandId int64
var brandName string var brandName string
if spuId != 0 { if spuId != 0 {
spuStr, _ := gredis.String(redisConnSpu.Do("HGET", "spu", spuId)) spuStr, _ := gredis.String(redisConnSpu.Do("HGET", "spu", spuId))
brandId = gjson.Get(spuStr, "brand_id").Int() brandId = gjson.Get(spuStr, "brand_id").Int()
brandName, _ = gredis.String(redisConn.Do("HGET", "brand", brandId)) brandName, _ = gredis.String(redisConn.Do("HGET", "brand", brandId))
var ly LyService var ly LyService
standardBrand = ly.GetStandardBrand(brandId) standardBrand = ly.GetStandardBrand(brandId)
} }
A.Set("brand_id_spu", brandId) A.Set("brand_id_spu", brandId)
A.Set("brand_name_spu", brandName) A.Set("brand_name_spu", brandName)
A.Set("standard_brand", standardBrand) A.Set("standard_brand", standardBrand)
A.Set("class_id1", classId1) // A.Set("class_id1", classId1) //
A.Set("class_id2", classId2) // A.Set("class_id2", classId2) //
A.Set("class_id1_name", classId1Name) // A.Set("class_id1_name", classId1Name) //
A.Set("class_id2_name", classId2Name) // A.Set("class_id2_name", classId2Name) //
A.Set("goods_name", gjson.Get(info, "goods_name").String()) // A.Set("goods_name", gjson.Get(info, "goods_name").String()) //
A.Set("status", gjson.Get(info, "status").Int()) // A.Set("status", gjson.Get(info, "status").Int()) //
A.Set("encoded", gjson.Get(info, "encoded").String()) // A.Set("encoded", gjson.Get(info, "encoded").String()) //
A.Set("encap", gjson.Get(info, "encap").String()) // A.Set("encap", gjson.Get(info, "encap").String()) //
A.Set("packing", gjson.Get(info, "packing").Int()) // A.Set("packing", gjson.Get(info, "packing").Int()) //
A.Set("goods_unit", gjson.Get(info, "goods_unit").Int()) // A.Set("goods_unit", gjson.Get(info, "goods_unit").Int()) //
A.Set("goods_brief", gjson.Get(info, "goods_brief").String()) // A.Set("goods_brief", gjson.Get(info, "goods_brief").String()) //
A.Set("ability_level", 2) A.Set("ability_level", 2)
moq := gjson.Get(info, "moq").Int() moq := gjson.Get(info, "moq").Int()
mpq := gjson.Get(info, "mpq").Int() mpq := gjson.Get(info, "mpq").Int()
//自营递增量 //自营递增量
Multiple := gjson.Get(info, "multiple").Int() Multiple := gjson.Get(info, "multiple").Int()
if Multiple > 0 { if Multiple > 0 {
Multiple = Multiple Multiple = Multiple
} else {
if mpq > moq {
Multiple = moq
} else { } else {
if mpq > moq { Multiple = mpq
Multiple = moq
} else {
Multiple = mpq
}
} }
A.Set("moq", moq) // }
A.Set("mpq", mpq) // A.Set("moq", moq) //
A.Set("multiple", Multiple) // A.Set("mpq", mpq) //
A.Set("multiple", Multiple) //
A.Set("update_time", gjson.Get(info, "update_time").Int()) //
A.Set("sku_name", strings.Trim(gjson.Get(info, "sku_name").String(), " ")) // A.Set("update_time", gjson.Get(info, "update_time").Int()) //
A.Set("mpl", gjson.Get(info, "mpl").Int()) // A.Set("sku_name", strings.Trim(gjson.Get(info, "sku_name").String(), " ")) //
A.Set("spu_id", spuId) // A.Set("mpl", gjson.Get(info, "mpl").Int()) //
A.Set("spu_id", spuId) //
//处理库存
A.Set("stock", 0) //默认库存为0 //处理库存
dbStock := gjson.Get(info, "stock").Int() //当前db库存 A.Set("stock", 0) //默认库存为0
if dbStock > 0 { dbStock := gjson.Get(info, "stock").Int() //当前db库存
lockStock := qs.skuLockNum(&redisConn, goodsId) //当前锁库库存 if dbStock > 0 {
stockG := dbStock - lockStock //当前可购买库存 lockStock := qs.skuLockNum(&redisConn, goodsId) //当前锁库库存
if stockG > 0 { stockG := dbStock - lockStock //当前可购买库存
A.Set("actual_stock", dbStock) //锁定库存 if stockG > 0 {
A.Set("stock", stockG) A.Set("actual_stock", dbStock) //锁定库存
} A.Set("stock", stockG)
A.Set("saler_atio", "") //
} }
A.Set("saler_atio", "") //
}
A.Set("attrs", attrs) // A.Set("attrs", attrs) //
A.Set("cost", gjson.Get(info, "cost").String()) // A.Set("cost", gjson.Get(info, "cost").String()) //
A.Set("new_cost", gjson.Get(info, "new_cost").String()) // A.Set("new_cost", gjson.Get(info, "new_cost").String()) //
A.Set("supplier_stock", gjson.Get(info, "supplier_stock").Int()) // A.Set("supplier_stock", gjson.Get(info, "supplier_stock").Int()) //
A.Set("self_supplier_type", gjson.Get(info, "self_supplier_type").Int()) // A.Set("self_supplier_type", gjson.Get(info, "self_supplier_type").Int()) //
A.Set("cn_delivery_time", "1-3工作日") // 货期
//处理货期
//cnDeliveryTime := gjson.Get(info, "cn_delivery_time").String() //查询品牌名称(作废,现在改成读联营的品牌库,之前有关联自营商品到spu_id)
//if cnDeliveryTime == "" { brand_id := gjson.Get(info, "brand_id").Int()
// cnDeliveryTime = "3-7工作日" brand_info, _ := gredis.String(redisConn.Do("HGET", "Self_Brand", brand_id))
//} A.Set("brand_name", gjson.Get(brand_info, "brand_name").String())
A.Set("cn_delivery_time", "1-3工作日") // 货期 A.Set("brand_id", brand_id)
//查询品牌名称(作废,现在改成读联营的品牌库,之前有关联自营商品到spu_id) if fast != "1" { //不是快速查询
brand_id := gjson.Get(info, "brand_id").Int() // 供应商名称
brand_info, _ := gredis.String(redisConn.Do("HGET", "Self_Brand", brand_id)) supplier_id := gjson.Get(info, "supplier_id").String()
A.Set("brand_name", gjson.Get(brand_info, "brand_name").String()) supplier_name := ""
A.Set("brand_id", brand_id) if supplier_id != "" {
supplierInfo, _ := gredis.String(redisConn.Do("HGET", "Self_SelfSupplierInfo", supplier_id))
if fast != "1" { //不是快速查询 supplier_name = gjson.Get(supplierInfo, "supplier_name").String()
// 供应商名称 }
supplier_id := gjson.Get(info, "supplier_id").String() A.Set("supplier_name", supplier_name)
supplier_name := ""
if supplier_id != "" {
supplierInfo, _ := gredis.String(redisConn.Do("HGET", "Self_SelfSupplierInfo", supplier_id))
supplier_name = gjson.Get(supplierInfo, "supplier_name").String()
}
A.Set("supplier_name", supplier_name)
//商品单位
goodsUnit := gjson.Get(info, "goods_unit").String()
goodsUnitName := ""
if goodsUnit != "" {
goodsUnitName, _ = gredis.String(redisConn.Do("HGET", "Self_Unit", goodsUnit))
}
A.Set("goods_unit_name", goodsUnitName)
//包装名称
packing := gjson.Get(info, "packing").String()
packingName := ""
if packing != "" {
packingName, _ = gredis.String(redisConn.Do("HGET", "Self_Unit", packing))
}
A.Set("packing_name", packingName) //
//mpq包装名称 //商品单位
mpqUnitName := "" goodsUnit := gjson.Get(info, "goods_unit").String()
if packing != "" { goodsUnitName := ""
mpqUnitName, _ = gredis.String(redisConn.Do("HGET", "Self_UnitAlias", packing)) if goodsUnit != "" {
} goodsUnitName, _ = gredis.String(redisConn.Do("HGET", "Self_Unit", goodsUnit))
A.Set("mpq_unit_name", mpqUnitName) // }
A.Set("goods_unit_name", goodsUnitName)
//图片 //包装名称
img := gjson.Get(info, "goods_images").String() packing := gjson.Get(info, "packing").String()
goodsImages := "" packingName := ""
if img != "" { if packing != "" {
goodsImages, _ = gredis.String(redisConn.Do("HGET", "Self_SelfGoodsSource", img)) packingName, _ = gredis.String(redisConn.Do("HGET", "Self_Unit", packing))
}
image := gjson.Get(goodsImages, "url").String()
image = strings.Replace(image, "http://img.ichunt.com", "https://img.ichunt.com", 1)
image = strings.Replace(image, "http://image.ichunt.net", "https://image.ichunt.net", 1)
image = strings.Replace(image, "https://image.ichunt.net", "https://imgscdn.ichunt.com", 1)
A.Set("goods_images", image)
pdf := gjson.Get(info, "pdf").String()
if pdf != "" {
pdf, _ = gredis.String(redisConn.Do("HGET", "Self_SelfGoodsSource", pdf))
}
pdf = gjson.Get(pdf, "url").String()
pdf = strings.Replace(pdf, "http://img.ichunt.com", "https://img.ichunt.com", 1)
A.Set("pdf", pdf)
} }
A.Set("packing_name", packingName) //
A.Set("scm_brand_name", gjson.Get(info, "scm_brand_name").String()) // //mpq包装名称
mpqUnitName := ""
//处理系数 if packing != "" {
ratio, _ := gredis.String(redisConn.Do("HGET", "zy_ratio_sku", goodsId)) mpqUnitName, _ = gredis.String(redisConn.Do("HGET", "Self_UnitAlias", packing))
var PriceAcXi float64 = 0 //系数 }
if ratio != "" { A.Set("mpq_unit_name", mpqUnitName) //
PriceAcXi = gjson.Get(ratio, "price_ac").Float()
//获取立创价格
if gjson.Parse("szlc_price").String() == "" {
A.Set("szlc_price", nil)
} else {
ladderPriceLc := make([]model.LadderPriceLc, 0)
for _, v := range gjson.Parse("szlc_price").Array() {
ladderPriceLc = append(ladderPriceLc, model.LadderPriceLc{
Purchases: v.Get("Purchases").Int(),
Price: v.Get("price").Float(),
})
}
A.Set("szlc_price", ladderPriceLc)
}
A.Set("allow_coupon", gjson.Get(ratio, "allow_coupon").String()) //图片
A.Set("allow_presale", gjson.Get(ratio, "allow_presale").String()) img := gjson.Get(info, "goods_images").String()
A.Set("ac_type", 6) //系数 goodsImages := ""
if img != "" {
goodsImages, _ = gredis.String(redisConn.Do("HGET", "Self_SelfGoodsSource", img))
}
image := gjson.Get(goodsImages, "url").String()
image = strings.Replace(image, "http://img.ichunt.com", "https://img.ichunt.com", 1)
image = strings.Replace(image, "http://image.ichunt.net", "https://image.ichunt.net", 1)
image = strings.Replace(image, "https://image.ichunt.net", "https://imgscdn.ichunt.com", 1)
A.Set("goods_images", image)
pdf := gjson.Get(info, "pdf").String()
if pdf != "" {
pdf, _ = gredis.String(redisConn.Do("HGET", "Self_SelfGoodsSource", pdf))
} }
pdf = gjson.Get(pdf, "url").String()
pdf = strings.Replace(pdf, "http://img.ichunt.com", "https://img.ichunt.com", 1)
A.Set("pdf", pdf)
}
//拼接梯度价格 A.Set("scm_brand_name", gjson.Get(info, "scm_brand_name").String()) //
ladderPriceArr := gjson.Get(info, "ladder_price").Array()
ladderPrice := make([]model.LadderPrice, 0) //处理系数
for _, v := range ladderPriceArr { ratio, _ := gredis.String(redisConn.Do("HGET", "zy_ratio_sku", goodsId))
if v.Get("purchases").String() == "" { var PriceAcXi float64 = 0 //系数
continue if ratio != "" {
} PriceAcXi = gjson.Get(ratio, "price_ac").Float()
priceCn := php2go.Round(v.Get("price_cn").Float()*10000) / 10000 //获取立创价格
if PriceAcXi == 0 { if gjson.Parse("szlc_price").String() == "" {
ladderPrice = append(ladderPrice, model.LadderPrice{ A.Set("szlc_price", nil)
Purchases: v.Get("purchases").Int(), } else {
PriceCn: priceCn, ladderPriceLc := make([]model.LadderPriceLc, 0)
}) for _, v := range gjson.Parse("szlc_price").Array() {
} else { ladderPriceLc = append(ladderPriceLc, model.LadderPriceLc{
ladderPrice = append(ladderPrice, model.LadderPrice{ Purchases: v.Get("Purchases").Int(),
Purchases: v.Get("purchases").Int(), Price: v.Get("price").Float(),
PriceCn: priceCn,
PriceAc: PriceAcXi,
}) })
} }
A.Set("szlc_price", ladderPriceLc)
} }
if len(ladderPrice) == 0 { A.Set("allow_coupon", gjson.Get(ratio, "allow_coupon").String())
A.Set("ladder_price", "") // A.Set("allow_presale", gjson.Get(ratio, "allow_presale").String())
} else { A.Set("ac_type", 6) //系数
A.Set("ladder_price", ladderPrice) // }
}
//处理限额 //拼接梯度价格
goodsQuota, _ := gredis.String(redisConn.Do("HGET", "Self_goods_quota", goodsId)) ladderPriceArr := gjson.Get(info, "ladder_price").Array()
if goodsQuota != "" { ladderPrice := make([]model.LadderPrice, 0)
A.Set("is_quota", 1) for _, v := range ladderPriceArr {
A.Set("quota_num", gjson.Get(goodsQuota, "num").Int()) if v.Get("purchases").String() == "" {
continue
} }
//处理是否能购买 priceCn := php2go.Round(v.Get("price_cn").Float()*10000) / 10000
if gjson.Get(info, "status").String() != "1" || len(ladderPriceArr) == 0 { if PriceAcXi == 0 {
A.Set("is_buy", 0) ladderPrice = append(ladderPrice, model.LadderPrice{
Purchases: v.Get("purchases").Int(),
PriceCn: priceCn,
})
} else { } else {
A.Set("is_buy", 1) ladderPrice = append(ladderPrice, model.LadderPrice{
Purchases: v.Get("purchases").Int(),
PriceCn: priceCn,
PriceAc: PriceAcXi,
})
} }
}
if len(ladderPrice) == 0 {
A.Set("ladder_price", "") //
} else {
A.Set("ladder_price", ladderPrice) //
}
//处理限额
goodsQuota, _ := gredis.String(redisConn.Do("HGET", "Self_goods_quota", goodsId))
if goodsQuota != "" {
A.Set("is_quota", 1)
A.Set("quota_num", gjson.Get(goodsQuota, "num").Int())
}
//处理是否能购买
if gjson.Get(info, "status").String() != "1" || len(ladderPriceArr) == 0 {
A.Set("is_buy", 0)
} else {
A.Set("is_buy", 1)
}
//提前设置活动信息的默认值 //提前设置活动信息的默认值
A.Set("activity_info", map[string]interface{}{}) A.Set("activity_info", map[string]interface{}{})
A.Set("has_gift_activity", 0) A.Set("has_gift_activity", 0)
A.Set("gift_activity", map[string]interface{}{}) A.Set("gift_activity", map[string]interface{}{})
if PriceAcXi == 0 { //没有系数价格才处理活动价 if PriceAcXi == 0 { //没有系数价格才处理活动价
//处理活动价 //处理活动价
A.Set("ac_type", 0) A.Set("ac_type", 0)
A.Set("allow_coupon", 1) A.Set("allow_coupon", 1)
AcPrice := qs.ActivityPrice(info) AcPrice := qs.ActivityPrice(info)
if AcPrice != nil { if AcPrice != nil {
keys := AcPrice.Keys() keys := AcPrice.Keys()
for _, k := range keys { for _, k := range keys {
v, _ := AcPrice.Get(k) v, _ := AcPrice.Get(k)
A.Set(k, v) //活动价格覆盖 A.Set(k, v) //活动价格覆盖
}
} }
} }
}
//获取标签信息 //获取标签信息
var tagService TagsService var tagService TagsService
A.Set("goods_tag", tagService.GetTags(goodsId, gjson.Get(info, "self_supplier_type").Int())) A.Set("goods_tag", tagService.GetTags(goodsId, gjson.Get(info, "self_supplier_type").Int()))
A.Set("canal", "L0003270") //自营写死编码 A.Set("canal", "L0003270") //自营写死编码
//最后写入sync map //最后写入sync map
(GoodsRes).Store(goodsId, A) (GoodsRes).Store(goodsId, A)
} ch <- GoodsRes
// 发送结果时也要检查 context,避免在超时后阻塞
select {
case <-ctx.Done():
logger.Log("ZyGoodsDetail: 发送结果前context已取消,直接返回", "sku", 1)
return
case ch <- GoodsRes:
// 成功发送
}
} }
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