Commit 44b4402a by mushishixian

修复

parent 6d55a733
The file could not be displayed because it is too large.
......@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"github.com/ichunt2019/go-rabbitmq/utils/rabbitmq"
"github.com/prometheus/common/log"
"time"
)
......@@ -30,14 +31,14 @@ func (t *RecvPro) Consumer(dataByte []byte) (err error) {
start := time.Now()
var message model.BomMessage
if err = json.Unmarshal(dataByte, &message); err != nil {
fmt.Println(err)
log.Error(err)
}
if err != nil {
fmt.Println(err)
log.Error(err)
}
err = logic.MatchGoods(message)
if err != nil {
fmt.Println(err)
log.Error(err)
}
duration := time.Now().Sub(start)
fmt.Println(duration)
......
......@@ -16,6 +16,7 @@ require (
github.com/shopspring/decimal v1.2.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.0
gopkg.in/ini.v1 v1.56.0 // indirect
gopkg.in/olivere/elastic.v5 v5.0.85
)
......@@ -333,6 +333,12 @@ github.com/syyongx/php2go v0.9.4 h1:qUtETTHzqHzxZK8plkbkb0YawD8bpLpxNsbzHQmb22Y=
github.com/syyongx/php2go v0.9.4/go.mod h1:meN2eIhhUoxOd2nMxbpe8g6cFPXI5O9/UAAuz7oDdzw=
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20181023030647-4e92f724b73b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20200122045848-3419fae592fc/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
......
......@@ -5,7 +5,6 @@ import (
"bom_server/internal/model"
"encoding/json"
"errors"
"fmt"
es "gopkg.in/olivere/elastic.v5"
"strconv"
)
......@@ -27,7 +26,7 @@ func GetBomItem(bomId, bomItemId int) (bomItem model.BomItem) {
}
//更新没有匹配的到bom详情
func UpdateNoMatchBomItem(bomItems []model.BomItem) (err error) {
func UpdateNoMatchBomItem(bomItems []model.BomItem,isCancelMatch bool) (err error) {
if len(bomItems) == 0 {
return nil
}
......@@ -46,11 +45,13 @@ func UpdateNoMatchBomItem(bomItems []model.BomItem) (err error) {
if err != nil {
return
}
matchingDelStatus := 2
err = model.Db.Table("lie_bom_item_matching_"+tableEnd).Where("bom_item_id IN (?)", updateIdList).
Updates(map[string]interface{}{"status": matchingDelStatus}).Error
if err != nil {
return
if isCancelMatch {
matchingDelStatus := 2
err = model.Db.Table("lie_bom_item_matching_"+tableEnd).Where("bom_item_id IN (?)", updateIdList).
Updates(map[string]interface{}{"status": matchingDelStatus}).Error
if err != nil {
return
}
}
return nil
}
......@@ -84,13 +85,12 @@ func UpdateBomItem(bomId, bomItemId int) (err error) {
}
defer client.Stop()
//直接模糊匹配
goodsMapList, err := getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, false)
goodsMapList, err := getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, true)
if err != nil {
return
}
fmt.Println(goodsMapList)
if len(goodsMapList) == 0 {
err = UpdateNoMatchBomItem(bomItems)
err = UpdateNoMatchBomItem(bomItems,true)
if err != nil {
return
}
......
......@@ -32,7 +32,7 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
bomIdStr := strconv.FormatInt(int64(bomId), 10)
tableEnd := string(bomIdStr[len(bomIdStr)-1])
tableName := "lie_bom_item_matching_" + tableEnd
var isBuyBomItemIdList, noBuyBomItemIdList []int
//批量更新每一条记录之前,要去批量更新item表的item_status
for k, matching := range matchingList {
if k == 1 || k == 2 || k == 3 {
time.Sleep(2 * time.Second)
......@@ -50,7 +50,30 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
return
}
}
//先找出不同购买状态的bom_id列表,还要去修改bom_item表的item_status
for _, matching := range matchingList {
now := time.Now().Unix()
if matching.IsBuy == 1 {
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id = ?", matching.BomItemID).
Updates(map[string]interface{}{"item_status": 2, "update_time": now}).Error
if err != nil {
return
}
} else {
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id = ?", matching.BomItemID).
Updates(map[string]interface{}{"item_status": 3, "update_time": now}).Error
if err != nil {
return
}
}
}
}
return nil
}
func batchUpdateItemStatus(tableEnd string, matchingList []model.BomItemMatching) (err error) {
//先找出不同购买状态的bom_id列表,还要去修改bom_item表的item_status
var isBuyBomItemIdList, noBuyBomItemIdList []int
for _, matching := range matchingList {
if matching.IsBuy == 1 {
isBuyBomItemIdList = append(isBuyBomItemIdList, matching.BomItemID)
} else {
......@@ -69,7 +92,7 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
if err != nil {
return
}
return nil
return
}
//刷新bom匹配的商品信息
......@@ -105,13 +128,13 @@ func RefreshBomMatchingGoods(bomId int) (err error) {
}
goodsIdsStr := strings.Join(goodsIds, ",")
//去请求商品服务
responseData, err := GetGoodsInfo(goodsIdsStr)
goodsList, err := GetGoodsInfo(goodsIdsStr)
if err != nil {
return
}
var bomMatchingList []model.BomItemMatching
for _, matching := range bomMatchings {
for _, goods := range responseData.Data {
for _, goods := range goodsList {
if matching.GoodsID == goods.GoodsID {
var goodsMap GoodsMap
goodsMap.Number = matching.Number
......@@ -149,13 +172,13 @@ func RefreshBomMatchingGoodsAbandon(bomId int) (err error) {
}
goodsIdsStr := strings.Join(goodsIds, ",")
//去请求商品服务
responseData, err := GetGoodsInfo(goodsIdsStr)
goodsList, err := GetGoodsInfo(goodsIdsStr)
if err != nil {
return
}
var bomMatchingList []model.BomItemMatching
for _, matching := range bomMatchings {
for _, goods := range responseData.Data {
for _, goods := range goodsList {
if matching.GoodsID == goods.GoodsID {
var goodsMap GoodsMap
goodsMap.Number = matching.Number
......
......@@ -2,12 +2,10 @@ package logic
import (
"bom_server/configs"
"bom_server/internal/common"
"bom_server/internal/model"
"encoding/json"
"errors"
"github.com/imroc/req"
"reflect"
"github.com/tidwall/gjson"
"strings"
"time"
)
......@@ -31,16 +29,15 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
goodsIds = append(goodsIds, goodsMap.GoodsId)
}
goodsIdsStr := strings.Join(goodsIds, ",")
responseData, err := GetGoodsInfo(goodsIdsStr)
goodList, err := GetGoodsInfo(goodsIdsStr)
if err != nil {
return
}
//这是匹配到的数据
var bomMatchingList []model.BomItemMatching
for _, goodsMap := range goodsMapList {
for _, goods := range responseData.Data {
goodsId := common.Assert(goods.GoodsID, "string")
if goodsMap.GoodsId == goodsId.(string) {
for _, goods := range goodList {
if goodsMap.GoodsId == goods.GoodsID {
bomId = goodsMap.BomId
//组装需要去更新的商品信息
bomMatching, err := MatchGoodsInfo(goods, goodsMap)
......@@ -58,7 +55,7 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
return
}
func GetGoodsInfo(goodsIdsStr string) (responseData model.ApiGoodsResponse, err error) {
func GetGoodsInfo(goodsIdsStr string) (goodsList []model.ApiGoods, err error) {
goodsServerUrl := configs.ApiSetting.Goods
params := req.Param{
"goods_id": goodsIdsStr,
......@@ -67,30 +64,69 @@ func GetGoodsInfo(goodsIdsStr string) (responseData model.ApiGoodsResponse, err
if err != nil {
return
}
var responseDataTemp model.ApiGoodsResponseTemp
if err = resp.ToJSON(&responseDataTemp); err != nil {
err = errors.New(err.Error())
return
}
responseData.Data = make(map[string]model.ApiGoods, 200)
for goodsId, goods := range responseDataTemp.Data {
//不是bool值的,才去给数据,商品服务的商品信息有可能是bool值,蛋疼
if reflect.ValueOf(goods).Type().String() != "bool" {
var goodsStr []byte
goodsStr, err = json.Marshal(goods)
if err != nil {
return
}
var apiGoods model.ApiGoods
err = json.Unmarshal(goodsStr, &apiGoods)
if err != nil {
return
//先判断返回的data是不是字典,不是字典代表可能是返回字符串了
if gjson.Get(resp.String(), "data").IsObject() {
for _, data := range gjson.Get(resp.String(), "data").Map() {
//还要去判断是否是bool
if data.IsObject() {
var goods model.ApiGoods
goods.GoodsName = data.Get("goods_name").String()
goods.GoodsID = data.Get("goods_id").String()
goods.BrandID = int(data.Get("goods_id").Int())
goods.Pdf = data.Get("pdf").String()
goods.Stock = int(data.Get("stock").Int())
goods.Mpq = int(data.Get("mpq").Int())
goods.Moq = int(data.Get("moq").Int())
goods.SupplierName = data.Get("supplier_name").String()
goods.BrandName = data.Get("brand_name").String()
goods.HkDeliveryTime = data.Get("hk_delivery_time").String()
goods.CnDeliveryTime = data.Get("cn_delivery_time").String()
goods.IsBuy = int(data.Get("is_buy").Int())
goods.Encap = data.Get("encap").String()
goods.SupplierID = int(data.Get("supplier_id").Int())
goods.Status = int(data.Get("status").Int())
goods.GoodsType = int(data.Get("goods_type").Int())
var ladderPrice []model.LadderPrice
for _, price := range data.Get("ladder_price").Array() {
var ladder model.LadderPrice
ladder = model.LadderPrice{
Purchases: int(price.Get("purchases").Int()),
PriceUs: price.Get("price_us").Float(),
PriceCn: price.Get("price_cn").Float(),
PriceAc: price.Get("price_ac").Float(),
}
ladderPrice = append(ladderPrice, ladder)
}
goods.LadderPrice = ladderPrice
goodsList = append(goodsList, goods)
}
responseData.Data[goodsId] = apiGoods
}
}
responseData.Errmsg = responseDataTemp.Errmsg
responseData.Errcode = responseDataTemp.Errcode
//var responseDataTemp model.ApiGoodsResponseTemp
//if err = resp.ToJSON(&responseDataTemp); err != nil {
// err = errors.New(err.Error())
// return
//}
//responseData.Data = make(map[string]model.ApiGoods, 200)
//for goodsId, goods := range responseDataTemp.Data {
// //不是bool值的,才去给数据,商品服务的商品信息有可能是bool值,蛋疼
// if reflect.ValueOf(goods).Type().String() != "bool" {
// var goodsStr []byte
// goodsStr, err = json.Marshal(goods)
// if err != nil {
// return
// }
// var apiGoods model.ApiGoods
// err = json.Unmarshal(goodsStr, &apiGoods)
// if err != nil {
// return
// }
// responseData.Data[goodsId] = apiGoods
// }
//}
//responseData.Errmsg = responseDataTemp.Errmsg
//responseData.Errcode = responseDataTemp.Errcode
return
}
......@@ -103,19 +139,21 @@ func MatchGoodsInfo(goods model.ApiGoods, goodsMap GoodsMap) (bomMatching model.
} else {
delivery = goods.HkDeliveryTime
}
goodsIdStr := common.Assert(goods.GoodsID, "string")
pdf := common.Assert(goods.Pdf, "string")
brandId := common.Assert(goods.BrandID, "int")
var pdf string
pdf = goods.Pdf
if goods.Pdf == "false" {
pdf = ""
}
bomMatching = model.BomItemMatching{
BomID: goodsMap.BomId,
BomItemID: goodsMap.BomItemId,
BrandId: brandId.(int),
GoodsID: goodsIdStr.(string),
BrandId: goods.BrandID,
GoodsID: goods.GoodsID,
GoodsName: goods.GoodsName,
BrandName: goods.BrandName,
GoodsType: goods.GoodsType,
SupplierID: goods.SupplierID,
Pdf: pdf.(string),
Pdf: pdf,
SupplierName: goods.SupplierName,
Delivery: delivery,
DeliveryType: goodsMap.DeliveryType,
......@@ -149,23 +187,20 @@ func MatchGoodsInfo(goods model.ApiGoods, goodsMap GoodsMap) (bomMatching model.
}
bomMatching.LadderPrice = string(ladderPriceStr)
//获取美元和人民币的价格(需要去判断数量)
if len(goods.LadderPrice) > 0 {
for _, price := range goods.LadderPrice {
//要先去断言,因为商品服务返回的json的值类型不确定...
purchases := common.Assert(price.Purchases, "int")
if goodsMap.Number <= purchases.(int) {
for _, price := range goods.LadderPrice {
if goodsMap.Number <= price.Purchases {
//判断收货地,1是大陆,2是香港
if goodsMap.DeliveryType == 1 {
//判断是否有优惠价,有的话取优惠价
if price.PriceAc != nil {
pac := common.Assert(price.PriceAc, "float")
bomMatching.Price = pac.(float64)
if price.PriceAc != 0 {
bomMatching.Price = price.PriceAc
} else {
if price.PriceCn != nil {
p := common.Assert(price.PriceCn, "float")
bomMatching.Price = p.(float64)
}
bomMatching.Price = price.PriceCn
}
break
} else {
bomMatching.Price = price.PriceUs
}
break
}
}
}
......
......@@ -30,7 +30,7 @@ func MatchGoods(message model.BomMessage) (err error) {
return errors.New("没有商品的bom单")
}
bomItems := bom.BomItems
perGoDealNumber := 200
perGoDealNumber := 40
//开启协程处理搜索.每50个开启一个协程
var wg sync.WaitGroup
//判断是否有余数
......@@ -55,7 +55,7 @@ func MatchGoods(message model.BomMessage) (err error) {
if err := SearchGoods(bomId, bomData, message.DeliveryType, message.Sort, &wg); err != nil {
log.Error(err)
}
}()
}()
}
wg.Wait()
//全部处理完以后要去修改主表的同步状态
......@@ -100,12 +100,12 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg
notMatchBomItems = append(notMatchBomItems, bomItem)
}
}
err = UpdateGoodsData(append(goodsMapList, fuzzyGoodsMapList...))
//先去处理没有匹配到的数据
err = UpdateNoMatchBomItem(notMatchBomItems, false)
if err != nil {
return
}
//还要去处理没有匹配到的数据
err = UpdateNoMatchBomItem(notMatchBomItems)
err = UpdateGoodsData(append(goodsMapList, fuzzyGoodsMapList...))
if err != nil {
return
}
......@@ -114,7 +114,7 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg
//更新单个matching
func UpdateSingleBomMatching(bomSn string, bomItemId int, goodsId string) (bomItemInfo model.BomItem, err error) {
responseData, err := GetGoodsInfo(goodsId)
goodsList, err := GetGoodsInfo(goodsId)
if err != nil {
return
}
......@@ -122,8 +122,8 @@ func UpdateSingleBomMatching(bomSn string, bomItemId int, goodsId string) (bomIt
//先去获取对应的matching
bom := GetBomBySn(bomSn)
matching := GetBomMatching(bom.BomID, bomItemId)
if len(responseData.Data) > 0 {
for _, goods := range responseData.Data {
if len(goodsList) > 0 {
for _, goods := range goodsList {
var goodsMap GoodsMap
goodsMap.Number = matching.Number
goodsMap.GoodsName = matching.GoodsName
......@@ -175,6 +175,9 @@ func getUpdateGoodsData(bomId int, bomItems []model.BomItem, deliveryType, sort
//如果是模糊查询,还要去截取字符串
if !rawSearch {
bom.GoodsName = common.SubKeyWordStr(bom.GoodsName)
if bom.GoodsName == "" {
continue
}
}
//构建一个goods_name对应的bomItems列表
searchRequest := getSearchParams(bom.GoodsName, bom.BrandName, sort, bom.Number, rawSearch)
......@@ -190,13 +193,16 @@ func getUpdateGoodsData(bomId int, bomItems []model.BomItem, deliveryType, sort
//因为是多重查询,所以会有多套结果
for key, responses := range res.Responses {
//有数据进行转换
if responses.Hits.TotalHits > 0 {
if responses.Hits != nil {
for _, hit := range responses.Hits.Hits {
var goods model.Goods
err := json.Unmarshal(*hit.Source, &goods)
if err != nil {
return nil, err
}
if bomItems[key].GoodsName == "" {
break
}
var goodsMap GoodsMap
goodsMap.GoodsId = hit.Id
goodsMap.Number = bomItems[key].Number
......@@ -230,12 +236,9 @@ func getSearchParams(goodsName, brandName string, sort, number int, flag bool) (
//构建term条件
func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es.BoolQuery) {
var (
field string
)
query = es.NewBoolQuery()
if flag {
field = "auto_goods_name.raw"
field := "auto_goods_name.raw"
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName := replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
......@@ -249,7 +252,7 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es
//搜索库存
query = query.Should(es.NewConstantScoreQuery(es.NewRangeQuery("stock").Gte(number)))
} else {
field = "auto_goods_name"
field := "auto_goods_name"
query = query.Must(es.NewTermQuery(field, goodsName))
}
query = query.Filter(es.NewTermQuery("status", 1))
......
......@@ -45,30 +45,29 @@ type Data map[string]ApiGoods
type DataTemp map[string]interface{}
type ApiGoods struct {
GoodsID interface{} `json:"goods_id"`
GoodsSn string `json:"goods_sn"`
//SpuID string `json:"spu_id"`
GoodsStatus int `json:"goods_status"`
GoodsName string `json:"goods_name"`
GoodsType int `json:"goods_type"`
SupplierID int `json:"supplier_id"`
Moq int `json:"moq"`
Mpq int `json:"mpq"`
Stock int `json:"stock"`
HkDeliveryTime string `json:"hk_delivery_time"`
CnDeliveryTime string `json:"cn_delivery_time"`
LadderPrice []struct {
Purchases interface{} `json:"purchases"`
PriceUs interface{} `json:"price_us"`
PriceCn interface{} `json:"price_cn"`
PriceAc interface{} `json:"price_ac"`
} `json:"ladder_price"`
BrandName string `json:"brand_name"`
SupplierName string `json:"supplier_name"`
BrandID interface{} `json:"brand_id"`
IsBuy int `json:"is_buy"`
Status int `json:"status"`
Pdf interface{} `json:"pdf"`
Encap string `json:"encap"`
GoodsID string `json:"goods_id"`
GoodsName string `json:"goods_name"`
GoodsType int `json:"goods_type"`
SupplierID int `json:"supplier_id"`
Moq int `json:"moq"`
Mpq int `json:"mpq"`
Stock int `json:"stock"`
HkDeliveryTime string `json:"hk_delivery_time"`
CnDeliveryTime string `json:"cn_delivery_time"`
LadderPrice []LadderPrice `json:"ladder_price"`
BrandName string `json:"brand_name"`
SupplierName string `json:"supplier_name"`
BrandID int `json:"brand_id"`
IsBuy int `json:"is_buy"`
Status int `json:"status"`
Pdf string `json:"pdf"`
Encap string `json:"encap"`
//ErpTax bool `json:"erp_tax"`
}
type LadderPrice struct {
Purchases int `json:"purchases"`
PriceUs float64 `json:"price_us"`
PriceCn float64 `json:"price_cn"`
PriceAc float64 `json:"price_ac"`
}
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