Commit e691f13f by huangchengyi

1.0

parent 037cda3f
package controller package controller
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/syyongx/php2go" "github.com/syyongx/php2go"
"go_sku_server/pkg/common" "go_sku_server/pkg/common"
"go_sku_server/service" "go_sku_server/service"
"sync" "sync"
"time"
) )
const goods_slice_count = 10 //每多少个型号id开启一个协程 const goods_slice_count = 10 //每多少个型号id开启一个协程
...@@ -39,12 +41,11 @@ func Synchronization(ctx *gin.Context) { ...@@ -39,12 +41,11 @@ func Synchronization(ctx *gin.Context) {
common.Output(ctx, 1001, "查询型号ID不得为空", "") common.Output(ctx, 1001, "查询型号ID不得为空", "")
return return
} }
//gjson.Parse(string).Array()
GoodsRes := sync.Map{}
goodsIdArr := php2go.Explode(",", GoodsIdStr) goodsIdArr := php2go.Explode(",", GoodsIdStr)
wg := sync.WaitGroup{} //协程 //wg := sync.WaitGroup{} //协程
ch := make(chan sync.Map) //管道
p := 0; //总共协程
zyGoodsId := make([]string, 0) zyGoodsId := make([]string, 0)
lyGoodsId := make([]string, 0) lyGoodsId := make([]string, 0)
...@@ -57,18 +58,20 @@ func Synchronization(ctx *gin.Context) { ...@@ -57,18 +58,20 @@ func Synchronization(ctx *gin.Context) {
if len(zyGoodsId) >= goods_slice_count { if len(zyGoodsId) >= goods_slice_count {
common.PrintDebugHtml(ctx, "zy增加协程1001:") common.PrintDebugHtml(ctx, "zy增加协程1001:")
common.PrintDebugHtml(ctx, zyGoodsId) common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //协程计数一 //wg.Add(1) //协程计数一
go zyService.ZyGoodsDetail(ctx, zyGoodsId, &GoodsRes, &wg) go zyService.ZyGoodsDetail(ctx, zyGoodsId, ch)
zyGoodsId = zyGoodsId[:0:0] zyGoodsId = zyGoodsId[:0:0]
p ++;
} }
} else { //联营 } else { //联营
lyGoodsId = append(lyGoodsId, goods_id) lyGoodsId = append(lyGoodsId, goods_id)
if len(lyGoodsId) >= goods_slice_count { if len(lyGoodsId) >= goods_slice_count {
common.PrintDebugHtml(ctx, "ly增加协程1002:") common.PrintDebugHtml(ctx, "ly增加协程1002:")
common.PrintDebugHtml(ctx, zyGoodsId) common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //wg.Add(1)
go lyService.LyGoodsDetail(ctx, lyGoodsId, &GoodsRes, &wg) go lyService.LyGoodsDetail(ctx, lyGoodsId, ch)
lyGoodsId = lyGoodsId[:0:0] lyGoodsId = lyGoodsId[:0:0]
p ++;
} }
} }
} }
...@@ -76,28 +79,37 @@ func Synchronization(ctx *gin.Context) { ...@@ -76,28 +79,37 @@ func Synchronization(ctx *gin.Context) {
if len(zyGoodsId) > 0 { if len(zyGoodsId) > 0 {
common.PrintDebugHtml(ctx, "zy增加协程1003:") common.PrintDebugHtml(ctx, "zy增加协程1003:")
common.PrintDebugHtml(ctx, zyGoodsId) common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //协程计数一 //wg.Add(1) //协程计数一
go zyService.ZyGoodsDetail(ctx, zyGoodsId, &GoodsRes, &wg) go zyService.ZyGoodsDetail(ctx, zyGoodsId, ch)
p ++;
} }
if len(lyGoodsId) > 0 { if len(lyGoodsId) > 0 {
common.PrintDebugHtml(ctx, "ly增加协程1004:") common.PrintDebugHtml(ctx, "ly增加协程1004:")
common.PrintDebugHtml(ctx, zyGoodsId) common.PrintDebugHtml(ctx, zyGoodsId)
wg.Add(1) //wg.Add(1)
go lyService.LyGoodsDetail(ctx, lyGoodsId, &GoodsRes, &wg) go lyService.LyGoodsDetail(ctx, lyGoodsId, ch)
p ++;
} }
wg.Wait() //wg.Wait()
//异步map最后转成map //异步map最后转成map
temp := make(map[string]interface{}) temp := make(map[string]interface{})
GoodsRes.Range(func(k, v interface{}) bool { for i := 0; i < p; i++ {
s, _ := k.(string) select {
temp[s] = v case GoodsRes := <- ch:
return true GoodsRes.Range(func(k, v interface{}) bool {
}) s, _ := k.(string)
temp[s] = v
return true
})
case <- time.After(time.Second *2):
fmt.Println("read time out")
}
}
common.Output(ctx, 0, "success", temp) common.Output(ctx, 0, "success", temp)
} }
......
...@@ -27,10 +27,10 @@ type Power struct { ...@@ -27,10 +27,10 @@ type Power struct {
/* /*
联营数据详情 联营数据详情
*/ */
func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, goodsRes *sync.Map, wg *sync.WaitGroup) (results model.LyResponse) { func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, ch chan sync.Map) {
redisConn := gredis.Conn("search_r") redisConn := gredis.Conn("search_r")
defer func() { defer func() {
wg.Done() //wg.Done()
redisConn.Close() redisConn.Close()
}() }()
...@@ -50,6 +50,8 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, goodsRes ...@@ -50,6 +50,8 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, goodsRes
skuArr := gredis.Hmget("default_r", "sku", goodsIds) skuArr := gredis.Hmget("default_r", "sku", goodsIds)
//为了性能着想,这边也先去批量获取spu的信息 //为了性能着想,这边也先去批量获取spu的信息
spuList := ls.getSpuList(skuArr) spuList := ls.getSpuList(skuArr)
GoodsRes := sync.Map{}
for goodsId, skuStr := range skuArr { for goodsId, skuStr := range skuArr {
//初始化有序map,拼接data 数据 //初始化有序map,拼接data 数据
//A := orderedmap.New() //A := orderedmap.New()
...@@ -181,12 +183,11 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, goodsRes ...@@ -181,12 +183,11 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, goodsRes
//最后一步,将sku的全部信息放到有序map里面 //最后一步,将sku的全部信息放到有序map里面
(*goodsRes).Store(goodsId, sku) GoodsRes.Store(goodsId, sku)
//(*goodsRes)[goodsId] = A //(*goodsRes)[goodsId] = A
} }
ch <- GoodsRes
return
} }
func (ls *LyService) getSpuList(skuArr map[string]string) (spuList map[string]string) { func (ls *LyService) getSpuList(skuArr map[string]string) (spuList map[string]string) {
......
package service package service
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/iancoleman/orderedmap" "github.com/iancoleman/orderedmap"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
...@@ -31,19 +30,22 @@ type ZiyingService struct { ...@@ -31,19 +30,22 @@ 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 *gin.Context,goodsIds []string,goodsRes *sync.Map, wg *sync.WaitGroup) { func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string, ch chan sync.Map) {
redisConn := gredis.Conn("search_r") redisConn := gredis.Conn("search_r")
defer func() { defer func() {
wg.Done(); //wg.Done();
redisConn.Close(); redisConn.Close();
}() }()
skuArr := gredis.Hmget("search_r","Self_SelfGoods",goodsIds) //批量获取商品详情 skuArr := gredis.Hmget("search_r","Self_SelfGoods",goodsIds) //批量获取商品详情
fast := ctx.Request.FormValue("power[fast]") fast := ctx.Request.FormValue("power[fast]")
GoodsRes := sync.Map{}
for goods_id,info := range skuArr { for goods_id,info := range skuArr {
if gjson.Get(info, "goods_name").String() == "" { if gjson.Get(info, "goods_name").String() == "" {
fmt.Print("zy goods_name为空-----",goods_id,skuArr) //fmt.Print("zy goods_name为空-----",goods_id,skuArr)
GoodsRes.Store(goods_id,false)
continue;
} }
//拼接梯度价格 //拼接梯度价格
...@@ -79,8 +81,35 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR ...@@ -79,8 +81,35 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR
A.Set("goods_type", gjson.Get(info, "goods_type").Int()) //查询总条数 A.Set("goods_type", gjson.Get(info, "goods_type").Int()) //查询总条数
A.Set("supplier_id", gjson.Get(info, "supplier_id").Int()) //查询总条数 A.Set("supplier_id", gjson.Get(info, "supplier_id").Int()) //查询总条数
A.Set("brand_id", gjson.Get(info, "brand_id").Int()) // A.Set("brand_id", gjson.Get(info, "brand_id").Int()) //
A.Set("class_id1", gjson.Get(info, "class_id1").Int()) //
A.Set("class_id2", gjson.Get(info, "class_id2").Int()) // other_attrs := gjson.Get(info,"other_attrs").String()
if other_attrs == "" {
A.Set("other_attrs","")
}else{
A.Set("other_attrs",map[string]string{
"length":gjson.Get(other_attrs,"length").String(),
"gross_wegiht":gjson.Get(other_attrs,"gross_wegiht").String(),
})
}
class_id1 := gjson.Get(info, "class_id1").Int()
class_id2 := gjson.Get(info, "class_id2").Int()
class_id1_name := "";
if class_id1 >0 {
class_id1_info,_ := gredis.String(redisConn.Do("HGET","Self_SelfClassInfo",class_id1))
class_id1_name = gjson.Get(class_id1_info,"class_name").String()
}
class_id2_name := "";
if class_id2 >0 {
class_id2_info,_ := gredis.String(redisConn.Do("HGET","Self_SelfClassInfo",class_id2))
class_id2_name = gjson.Get(class_id2_info,"class_name").String()
}
A.Set("class_id1", class_id1) //
A.Set("class_id2", class_id2) //
A.Set("class_id1_name", class_id1_name) //
A.Set("class_id2_name", class_id2_name) //
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()) //
...@@ -126,7 +155,10 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR ...@@ -126,7 +155,10 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR
} }
A.Set("cn_delivery_time", cn_delivery_time) // 货期 A.Set("cn_delivery_time", cn_delivery_time) // 货期
A.Set("brand_name", gjson.Get(info, "brand_name").String()) // //查询品牌名称
brand_id := gjson.Get(info, "brand_id").Int()
brand_info,_ := gredis.String(redisConn.Do("HGET","Self_Brand",brand_id))
A.Set("brand_name", gjson.Get(brand_info, "brand_name").String()) //
if fast != "1" { //不是快速查询 if fast != "1" { //不是快速查询
// 供应商名称 // 供应商名称
...@@ -167,22 +199,20 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR ...@@ -167,22 +199,20 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR
if img != "" { if img != "" {
goods_images,_ = gredis.String(redisConn.Do("HGET","Self_SelfGoodsSource",img)) goods_images,_ = gredis.String(redisConn.Do("HGET","Self_SelfGoodsSource",img))
} }
A.Set("goods_images", goods_images) // A.Set("goods_images", gjson.Get(goods_images,"url").String())
//图片
pdf := gjson.Get(info, "pdf").String() pdf := gjson.Get(info, "pdf").String()
if pdf != "" { if pdf != "" {
pdf,_ = gredis.String(redisConn.Do("HGET","Self_SelfGoodsSource",pdf)) pdf,_ = gredis.String(redisConn.Do("HGET","Self_SelfGoodsSource",pdf))
} }
A.Set("pdf", gjson.Get(info, "pdf").String()) A.Set("pdf", gjson.Get(pdf, "url").String())
} }
A.Set("scm_brand_name", gjson.Get(info, "scm_brand_name").String()) // A.Set("scm_brand_name", gjson.Get(info, "scm_brand_name").String()) //
A.Set("actual_stock", gjson.Get(info, "actual_stock").Int()) // A.Set("actual_stock", gjson.Get(info, "actual_stock").Int()) //
A.Set("ac_type", gjson.Get(info, "ac_type").Int()) // A.Set("ac_type", gjson.Get(info, "ac_type").Int()) //
A.Set("saler_atio", gjson.Get(info, "saler_atio").String()) // A.Set("saler_atio", gjson.Get(info, "saler_atio").String()) //
A.Set("class_id1_name", gjson.Get(info, "class_id1_name").String()) //
A.Set("class_id2_name", gjson.Get(info, "class_id2_name").String()) //
//处理活动价 //处理活动价
A.Set("ac_type",0) A.Set("ac_type",0)
...@@ -232,9 +262,9 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR ...@@ -232,9 +262,9 @@ func (qs *ZiyingService) ZyGoodsDetail(ctx *gin.Context,goodsIds []string,goodsR
} }
//最后写入sync map //最后写入sync map
(*goodsRes).Store(goods_id,A) (GoodsRes).Store(goods_id,A)
} }
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