Commit 9aa0e50d by mushishixian

样片接口

parent 3b61fdee
package controller
import (
"github.com/gin-gonic/gin"
"go_sku_server/pkg/common"
"go_sku_server/service"
)
//获取样片分类列表
func SampleClassList(ctx *gin.Context) {
var sampleClassService service.SampleClassService
classList, err := sampleClassService.GetSampleClassList()
if err != nil {
common.Output(ctx, 0, "success", err.Error())
return
}
common.Output(ctx, 0, "success", classList)
return
}
//获取样片列表
func SampleList(ctx *gin.Context) {
var sampleClassService service.SampleService
sampleList, err := sampleClassService.GetSampleList(ctx)
if err != nil {
common.Output(ctx, 0, "success", err.Error())
return
}
common.Output(ctx, 0, "success", sampleList)
return
}
...@@ -33,12 +33,12 @@ const goodsSliceCount = 10 //每多少个型号id开启一个协程 ...@@ -33,12 +33,12 @@ const goodsSliceCount = 10 //每多少个型号id开启一个协程
*/ */
func CommonController(ctx *gin.Context) map[string]interface{} { func CommonController(ctx *gin.Context) map[string]interface{} {
GoodsIdStr := ctx.Request.FormValue("goods_id")
common.PrintDebugHeader(ctx) //开启debug调试 common.PrintDebugHeader(ctx) //开启debug调试
//抽取自营 或者联营 goods_id
zyService := service.ZiyingService{} //实例化自营查询 zyService := service.ZiyingService{} //实例化自营查询
lyService := service.LyService{} //实例化自营查询 lyService := service.LyService{} //实例化自营查询
var goodsIdArr []string var goodsIdArr []string
//抽取自营 或者联营 goods_id
GoodsIdStr := ctx.Request.FormValue("goods_id")
if GoodsIdStr == "" { if GoodsIdStr == "" {
goodsIdMap := ctx.PostFormMap("goods_id") goodsIdMap := ctx.PostFormMap("goods_id")
for _, goodsId := range goodsIdMap { for _, goodsId := range goodsIdMap {
...@@ -52,7 +52,6 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -52,7 +52,6 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
return nil return nil
} }
//wg := sync.WaitGroup{} //协程
ch := make(chan sync.Map) //管道 ch := make(chan sync.Map) //管道
p := 0 //总共协程 p := 0 //总共协程
zyGoodsId := make([]string, 0) zyGoodsId := make([]string, 0)
...@@ -95,11 +94,9 @@ func CommonController(ctx *gin.Context) map[string]interface{} { ...@@ -95,11 +94,9 @@ func CommonController(ctx *gin.Context) map[string]interface{} {
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)
go lyService.LyGoodsDetail(ctx, lyGoodsId, ch) go lyService.LyGoodsDetail(ctx, lyGoodsId, ch)
p++ p++
} }
//wg.Wait()
//异步map最后转成map //异步map最后转成map
temp := make(map[string]interface{}) temp := make(map[string]interface{})
...@@ -124,11 +121,6 @@ func Synchronization(ctx *gin.Context) { ...@@ -124,11 +121,6 @@ func Synchronization(ctx *gin.Context) {
res := CommonController(ctx) res := CommonController(ctx)
common.Output(ctx, 0, "success", res) common.Output(ctx, 0, "success", res)
} }
func Synchronization1(ctx *gin.Context) {
res := CommonController(ctx)
common.Output(ctx, 0, "success", res)
}
/* /*
健康监测 健康监测
......
...@@ -13,7 +13,7 @@ func PushMsg(listName string, data string) { ...@@ -13,7 +13,7 @@ func PushMsg(listName string, data string) {
listName, listName,
"", "",
"direct", "direct",
"amqp://guest:guest@192.168.2.232:5672/", "amqp://guest:guest@192.168.1.252:5672/",
} }
rabbitmq.Send(queueExchange, data) rabbitmq.Send(queueExchange, data)
} }
\ No newline at end of file
package model
type Sample struct {
GoodsId int `json:"goods_id"`
Id int `json:"id"`
ClassId int `json:"class_id"`
MaxNumber int `json:"max_number"`
Quota int `json:"quota"`
ClassName string `xorm:"-" json:"class_name"`
GoodsInfo interface{} `xorm:"-" json:"goods_info"`
}
package model
type SampleClass struct {
Id int `json:"id"`
ClassName string `json:"class_name"`
ParentId int `json:"parent_id"`
Sort int `json:"sort"`
ClassIcon string `json:"class_icon"`
Status int `json:"status"`
}
...@@ -31,6 +31,10 @@ func InitRouter() *gin.Engine { ...@@ -31,6 +31,10 @@ func InitRouter() *gin.Engine {
r.POST("SaveSku",controller.Error_Middleware(),controller.SaveSku) r.POST("SaveSku",controller.Error_Middleware(),controller.SaveSku)
r.POST("SkuEdit",controller.Error_Middleware(),controller.SkuEdit) r.POST("SkuEdit",controller.Error_Middleware(),controller.SkuEdit)
//样片相关
r.POST("/self/sample/class/list",controller.SampleClassList)
r.POST("/self/sample/list",controller.SampleList)
return r return r
} }
...@@ -35,16 +35,6 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, ch chan ...@@ -35,16 +35,6 @@ func (ls *LyService) LyGoodsDetail(ctx *gin.Context, goodsIds []string, ch chan
redisConn.Close() redisConn.Close()
}() }()
fast := ctx.Request.FormValue("power[fast]") fast := ctx.Request.FormValue("power[fast]")
//isNewCustomer := ctx.Request.FormValue("power[newCustomer]")
//power := Power{
// UserId: ctx.Request.FormValue("power[user_id]"),
// Mobile: ctx.Request.FormValue("power[mobile]"),
// Email: ctx.Request.FormValue("power[email]"),
// Member: ctx.Request.FormValue("power[member]"),
// Invoice: ctx.Request.FormValue("power[invoice]"),
// SpecialInvoice: ctx.Request.FormValue("power[special_invoice]"),
// VerifyBlacklist: ctx.Request.FormValue("power[verify_blacklist]"),
//}
//批量获取商品详情 //批量获取商品详情
skuArr := gredis.Hmget("default_r", "sku", goodsIds) skuArr := gredis.Hmget("default_r", "sku", goodsIds)
//为了性能着想,这边也先去批量获取spu的信息 //为了性能着想,这边也先去批量获取spu的信息
......
package service
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-xorm/xorm"
"github.com/gogf/gf/util/gconv"
"go_sku_server/model"
"go_sku_server/pkg/mysql"
"sync"
"time"
)
type SampleService struct {
}
//获取分类列表
func (ss *SampleService) GetSampleList(ctx *gin.Context) (data map[string]interface{}, err error) {
id := gconv.Int(ctx.Request.FormValue("id"))
goodsId := gconv.Int(ctx.Request.FormValue("goods_id"))
classId := gconv.Int(ctx.Request.FormValue("class_id"))
page := gconv.Int(ctx.Request.FormValue("page"))
limit := gconv.Int(ctx.Request.FormValue("limit"))
if limit == 0 {
limit = 10
}
var start int
if page == 0 || page == 1 {
page = 1
start = 0
} else {
start = (page - 1) * limit
}
query := mysql.Conn("liexin_data").Table("lie_sample").Where("status = ?", 1)
if id != 0 {
query.Where("id = ?", id)
}
if goodsId != 0 {
query.Where("goods_id = ?", goodsId)
}
if classId != 0 {
query.Where("class_id = ?", classId)
}
var sampleList []model.Sample
query.OrderBy("update_time DESC").Limit(limit, start)
err = query.Find(&sampleList)
if err != nil && err != xorm.ErrNotExist {
return nil, err
}
//构建和之前接口的数据一样的格式,有点操蛋
data = make(map[string]interface{})
goodsMap := make(map[string]interface{})
var goodsIdsSlice []string
sampleGoodsMap := make(map[string]model.Sample)
for _, sample := range sampleList {
goodsIdStr := gconv.String(sample.GoodsId)
goodsIdsSlice = append(goodsIdsSlice, goodsIdStr)
}
goodsMap = ss.GetGoods(ctx, goodsIdsSlice)
for _, sample := range sampleList {
classService := SampleClassService{}
class, _ := classService.GetSampleClass(sample.ClassId)
sample.ClassName = class.ClassName
sample.GoodsInfo = goodsMap[gconv.String(sample.GoodsId)]
sampleGoodsMap[gconv.String(sample.GoodsId)] = sample
}
data["data"] = sampleGoodsMap
return data, err
}
func (ss *SampleService) GetGoods(ctx *gin.Context, goodsIds []string) map[string]interface{} {
//抽取自营 或者联营 goods_id
zyService := ZiyingService{} //实例化自营查询
lyService := LyService{} //实例化自营查询
ch := make(chan sync.Map) //管道
p := 0 //总共协程
zyGoodsId := make([]string, 0)
lyGoodsId := make([]string, 0)
for _, goodsId := range goodsIds {
if goodsId == "" {
continue
}
if len(goodsId) < 19 { //自营
zyGoodsId = append(zyGoodsId, goodsId)
if len(zyGoodsId) >= 10 {
go zyService.ZyGoodsDetail(ctx, zyGoodsId, ch)
zyGoodsId = zyGoodsId[:0:0]
p++
}
} else { //联营
lyGoodsId = append(lyGoodsId, goodsId)
if len(lyGoodsId) >= 10 {
go lyService.LyGoodsDetail(ctx, lyGoodsId, ch)
lyGoodsId = lyGoodsId[:0:0]
p++
}
}
}
if len(zyGoodsId) > 0 {
go zyService.ZyGoodsDetail(ctx, zyGoodsId, ch)
p++
}
if len(lyGoodsId) > 0 {
go lyService.LyGoodsDetail(ctx, lyGoodsId, ch)
p++
}
//异步map最后转成map
temp := make(map[string]interface{})
for i := 0; i < p; i++ {
select {
case GoodsRes := <-ch:
GoodsRes.Range(func(k, v interface{}) bool {
s, _ := k.(string)
temp[s] = v
return true
})
case <-time.After(time.Second * 20):
fmt.Println("协程超时", "sku", 1)
}
}
return temp
}
package service
import (
"encoding/json"
"github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"github.com/tidwall/gjson"
"go_sku_server/model"
"go_sku_server/pkg/gredis"
"go_sku_server/pkg/mysql"
)
type SampleClassService struct {
}
//获取分类列表
func (ls *SampleClassService) GetSampleClassList() (classList []model.SampleClass, err error) {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
//var class model.SampleClass
classListStr, err := redis.String(redisCon.Do("GET", "Self_sample_class_list_web"))
if classListStr == "" {
err = mysql.Conn("liexin_data").Table("lie_sample_class").
Select("id,class_name,class_icon,parent_id,status,sort").
Where("status = ?", 1).OrderBy("sort DESC").Find(&classList)
if err != nil {
return
}
str, err := json.Marshal(&classList)
if err != nil {
return nil, err
}
_, err = redisCon.Do("SET", "Self_sample_class_list_web", str)
if err != nil {
return nil, err
}
_, err = redisCon.Do("EXPIRE", "Self_sample_class_list_web", 60)
if err != nil {
return nil, err
}
} else {
err := json.Unmarshal([]byte(classListStr), &classList)
if err != nil {
return nil, err
}
}
return classList, err
}
//获取分类信息
func (ls *SampleClassService) GetSampleClass(classId int) (class model.SampleClass, err error) {
redisCon := gredis.Conn("default_r")
defer redisCon.Close()
classStr, err := redis.String(redisCon.Do("HGET", "Self_sample_class_list", classId))
if err != nil {
return
}
class.Id = gconv.Int(classId)
class.ClassName = gjson.Get(classStr, "class_name").String()
class.Status = int(gjson.Get(classStr, "status").Int())
class.Sort = int(gjson.Get(classStr, "sort").Int())
return class, err
}
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