Commit 8fed8855 by mushishixian

多线程请求更新库存和价格

parent d5fe2068
The file could not be displayed because it is too large.
......@@ -22,7 +22,7 @@ func init() {
"amqp://huntadmin:jy2y2900@192.168.1.237:5672/",
}
str := `{"bom_id":168,"delivery_type":1,"sort":1}`
str := `{"bom_id":175,"delivery_type":1,"sort":1}`
rabbitmq.Send(queueExchange, str)
}
......
......@@ -12,6 +12,7 @@ require (
github.com/jinzhu/gorm v1.9.12
github.com/jmoiron/sqlx v1.2.0
github.com/pkg/errors v0.9.1
github.com/prometheus/common v0.9.1
github.com/shopspring/decimal v1.2.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/syyongx/php2go v0.9.4
......
......@@ -9,8 +9,10 @@ github.com/Shopify/sarama v1.23.1/go.mod h1:XLH1GYJnLVE0XCr6KdJGVJRTwY30moWNJ4sE
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
github.com/aristanetworks/fsnotify v1.4.2/go.mod h1:D/rtu7LpjYM8tRJphJ0hUBYpjai8SfX+aSNsWDTq/Ks=
......@@ -469,6 +471,7 @@ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.28.1 h1:C1QC6KzgSiLyBabDi87BbjaGreoRgGUF5nOyvfrAZ1k=
google.golang.org/grpc v1.28.1/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a/go.mod h1:KF9sEfUPAXdG8Oev9e99iLGnl2uJMjc5B+4y3O7x610=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
......
......@@ -3,6 +3,7 @@ package common
import (
"crypto/md5"
"encoding/hex"
"github.com/shopspring/decimal"
"github.com/syyongx/php2go"
"reflect"
"strconv"
......@@ -45,7 +46,7 @@ func Assert(input interface{}, needType string) (result interface{}) {
return
}
if value == "float64" {
result = strconv.FormatFloat(input.(float64), 'E', -1, 64)
result = decimal.NewFromFloat(input.(float64)).String()
return
}
if value == "int" {
......
......@@ -2,8 +2,11 @@ package logic
import (
"bom_server/internal/model"
"github.com/prometheus/common/log"
"math"
"strconv"
"strings"
"sync"
"time"
)
......@@ -69,6 +72,73 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
//刷新bom匹配的商品信息
func RefreshBomMatchingGoods(bomId int) (err error) {
//先找出bom相关信息
bomMatchings := GetBomMatchings(bomId)
perGoDealNumber := 40
//开启协程处理搜索.每50个开启一个协程
var wg sync.WaitGroup
//判断是否有余数
extraNumber := len(bomMatchings) % perGoDealNumber
var number int
if extraNumber > 0 {
number = (len(bomMatchings) / perGoDealNumber) + 1
} else {
number = len(bomMatchings) / perGoDealNumber
}
wgNumber := int(math.Ceil(float64(number)))
wg.Add(wgNumber)
for i := 0; i < len(bomMatchings); i = i + perGoDealNumber {
i := i
var bomData []model.BomItemMatching
if i+perGoDealNumber > len(bomMatchings) {
bomData = bomMatchings[i:]
} else {
bomData = bomMatchings[i : i+perGoDealNumber]
}
go func() {
defer wg.Done()
var goodsIds []string
for _, matching := range bomData {
goodsIds = append(goodsIds, matching.GoodsID)
}
goodsIdsStr := strings.Join(goodsIds, ",")
//去请求商品服务
responseData, err := GetGoodsInfo(goodsIdsStr)
if err != nil {
return
}
var bomMatchingList []model.BomItemMatching
for _, matching := range bomMatchings {
for _, goods := range responseData.Data {
if matching.GoodsID == goods.GoodsID {
var goodsMap GoodsMap
goodsMap.Number = matching.Number
goodsMap.GoodsName = matching.GoodsName
goodsMap.DeliveryType = matching.DeliveryType
goodsMap.BomId = matching.BomID
goodsMap.BomItemId = matching.BomItemID
goodsMap.GoodsId = matching.GoodsID
//组装需要去更新的商品信息
bomMatching, _ := MatchGoodsInfo(goods, goodsMap)
if err != nil {
log.Error(err)
}
bomMatchingList = append(bomMatchingList, bomMatching)
}
}
}
err = BatchSaveMatchings(bomId, bomMatchingList)
if err != nil {
return
}
}()
}
wg.Wait()
return
}
//刷新bom匹配的商品信息
func RefreshBomMatchingGoodsAbandon(bomId int) (err error) {
//先找出bom相关信息
var goodsIds []string
bomMatchings := GetBomMatchings(bomId)
for _, matching := range bomMatchings {
......
......@@ -39,7 +39,8 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
var bomMatchingList []model.BomItemMatching
for _, goodsMap := range goodsMapList {
for _, goods := range responseData.Data {
if goodsMap.GoodsId == goods.GoodsID {
goodsId := common.Assert(goods.GoodsID, "string")
if goodsMap.GoodsId == goodsId.(string) {
bomId = goodsMap.BomId
//组装需要去更新的商品信息
bomMatching, err := MatchGoodsInfo(goods, goodsMap)
......@@ -62,7 +63,6 @@ func GetGoodsInfo(goodsIdsStr string) (responseData model.ApiGoodsResponse, err
params := req.Param{
"goods_id": goodsIdsStr,
}
//req.Debug = true
resp, err := req.Post(goodsServerUrl+"/synchronization", params)
if err != nil {
return
......@@ -105,10 +105,11 @@ func MatchGoodsInfo(goods model.ApiGoods, goodsMap GoodsMap) (bomMatching model.
}
goodsIdStr := common.Assert(goods.GoodsID, "string")
pdf := common.Assert(goods.Pdf, "string")
brandId := common.Assert(goods.BrandID, "int")
bomMatching = model.BomItemMatching{
BomID: goodsMap.BomId,
BomItemID: goodsMap.BomItemId,
BrandId: goods.BrandID,
BrandId: brandId.(int),
GoodsID: goodsIdStr.(string),
GoodsName: goods.GoodsName,
BrandName: goods.BrandName,
......
......@@ -252,7 +252,7 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es
field = "auto_goods_name"
query = query.Must(es.NewTermQuery(field, goodsName))
}
//query = query.Filter(es.NewTermQuery("status", 1))
query = query.Filter(es.NewTermQuery("status", 1))
//src, err := query.Source()
//if err != nil {
// panic(err)
......
......@@ -48,51 +48,27 @@ type ApiGoods struct {
GoodsID interface{} `json:"goods_id"`
GoodsSn string `json:"goods_sn"`
//SpuID string `json:"spu_id"`
UpdateTime int `json:"update_time"`
GoodsStatus int `json:"goods_status"`
GoodsName string `json:"goods_name"`
GoodsType int `json:"goods_type"`
SupplierID int `json:"supplier_id"`
Encoded string `json:"encoded"`
BatchSn string `json:"batch_sn"`
Moq int `json:"moq"`
Mpq int `json:"mpq"`
Stock int `json:"stock"`
HkDeliveryTime string `json:"hk_delivery_time"`
CnDeliveryTime string `json:"cn_delivery_time"`
GoodsDetails string `json:"goods_details"`
LadderPrice []struct {
Purchases interface{} `json:"purchases"`
PriceUs interface{} `json:"price_us"`
PriceCn interface{} `json:"price_cn"`
PriceAc interface{} `json:"price_ac"`
} `json:"ladder_price"`
Canal string `json:"canal"`
CpTime int `json:"cp_time"`
BrandName string `json:"brand_name"`
SupplierName string `json:"supplier_name"`
Attrs interface{} `json:"attrs"`
AcType int `json:"ac_type"`
AllowCoupon int `json:"allow_coupon"`
BrandID int `json:"brand_id"`
Coefficient struct {
Cn string `json:"cn"`
Hk string `json:"hk"`
ExtraRatio string `json:"extra_ratio"`
Ratio string `json:"ratio"`
Tax float64 `json:"tax"`
} `json:"coefficient"`
BrandID interface{} `json:"brand_id"`
IsBuy int `json:"is_buy"`
ClassID1 int `json:"class_id1"`
ClassID2 int `json:"class_id2"`
ClassID3 int `json:"class_id3"`
SpuName string `json:"spu_name"`
Status int `json:"status"`
ImagesL string `json:"images_l"`
Encap string `json:"encap"`
Pdf interface{} `json:"pdf"`
SpuBrief string `json:"spu_brief"`
SpuDetail string `json:"spu_detail"`
ClassName string `json:"class_name"`
Encap string `json:"encap"`
//ErpTax bool `json:"erp_tax"`
}
......@@ -37,8 +37,10 @@ func Export(bomId int) (ossPath string, err error) {
f.SetCellValue("Sheet1", "J1", "货期(工作日)")
f.SetCellValue("Sheet1", "K1", "单价")
f.SetCellValue("Sheet1", "L1", "小计")
var totalPrice float64
var keyStr string
for k, xlsxData := range bomItemXlsxData {
keyStr := strconv.FormatInt(int64(k+2), 10)
keyStr = strconv.FormatInt(int64(k+2), 10)
f.SetCellValue("Sheet1", "A"+keyStr, xlsxData["no"])
f.SetCellValue("Sheet1", "B"+keyStr, xlsxData["goods_name"])
f.SetCellValue("Sheet1", "C"+keyStr, xlsxData["number"])
......@@ -52,12 +54,25 @@ func Export(bomId int) (ossPath string, err error) {
f.SetCellValue("Sheet1", "J"+keyStr, xlsxData["delivery"])
f.SetCellValue("Sheet1", "K"+keyStr, xlsxData["price"])
f.SetCellValue("Sheet1", "L"+keyStr, xlsxData["total"])
if xlsxData["price"] != nil {
totalPrice += xlsxData["price"].(float64)
}
} else {
f.MergeCell("Sheet1", "E"+keyStr, "L"+keyStr)
style, _ := f.NewStyle(`{"font":{"color":"#D3281E"}}`)
f.SetCellStyle("Sheet1", "E"+keyStr, "E"+keyStr, style)
f.SetCellValue("Sheet1", "E"+keyStr, "参数可能不完整,客服 将与您进一步确认,或调整商品")
}
////最后还要加一个总计
//key, _ := strconv.Atoi(keyStr)
//key = key + 2
//keyStr = strconv.Itoa(key)
//f.MergeCell("Sheet1", "A"+keyStr, "L"+keyStr)
//style, _ := f.NewStyle(`{"font":{"color":"#D3281E"}}`)
//f.SetCellStyle("Sheet1", "A"+keyStr, "A"+keyStr, style)
//f.SetCellValue("Sheet1", "A"+keyStr, "总计 : "+strconv.Itoa(int(totalPrice)))
err = f.SaveAs(filePath)
if err != nil {
return
......@@ -81,7 +96,7 @@ func getBomData(bomId int) (bomItemXlsxData []map[string]interface{}, err error)
}
bomItems := bom.BomItems
bomMatchings := bom.BomIMatchings
bomItemXlsxData = make([]map[string]interface{}, 200)
bomItemXlsxData = make([]map[string]interface{}, len(bomItems))
//组装数据
if len(bomItems) == 0 {
err = errors.New("导出bom单不存在商品")
......
......@@ -21,8 +21,8 @@ func InitApiServer() (err error) {
s := &http.Server{
Addr: ":" + configs.ApiSetting.ApiPort,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return s.ListenAndServe()
......
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