Commit 914a481b by mushishixian

添加修改

parent 8121d69d
The file could not be displayed because it is too large.
......@@ -12,7 +12,7 @@ import (
type RecvPro struct {
}
func init(){
func init() {
queueExchange := rabbitmq.QueueExchange{
"bom_match",
"bom_match",
......@@ -21,7 +21,7 @@ func init(){
"amqp://huntadmin:jy2y2900@192.168.1.237:5672/",
}
rabbitmq.Send(queueExchange, "1")
rabbitmq.Send(queueExchange, "48")
}
func (t *RecvPro) Consumer(dataByte []byte) error {
......
......@@ -12,3 +12,23 @@ func GetBomItems(bomId int) (bomItems []model.BomItem) {
model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_id = ?", bomId).Find(&bomItems)
return bomItems
}
//更新没有匹配的到bom详情
func UpdateNoMatchBomItem(bomItems []model.BomItem) (err error) {
var updateIdList []int
var bomIdStr string
for _, bomItem := range bomItems {
//组装需要修改的id
updateIdList = append(updateIdList, bomItem.BomItemID)
bomIdStr = strconv.FormatInt(int64(bomItem.BomID), 10)
}
tableEnd := string(bomIdStr[len(bomIdStr)-1])
//商品需要修正状态
reviseStatus := 4
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id IN (?)", updateIdList).
Updates(map[string]interface{}{"item_status": reviseStatus}).Error
if err != nil {
return
}
return nil
}
......@@ -11,6 +11,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
for _, matching := range matchingList {
//先去数据库查询是否存在该记录,有的话修改,没有就新增
var match model.BomItemMatching
......@@ -25,6 +26,23 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
return
}
}
//先找出不同购买状态的bom_id列表,还要去修改bom_item表的item_status
if matching.IsBuy == 1 {
isBuyBomItemIdList = append(isBuyBomItemIdList, matching.BomItemID)
} else {
noBuyBomItemIdList = append(noBuyBomItemIdList, matching.BomItemID)
}
}
//分别去更新
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id IN (?)", isBuyBomItemIdList).
Updates(map[string]interface{}{"item_status": 2}).Error
if err != nil {
return
}
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id IN (?)", noBuyBomItemIdList).
Updates(map[string]interface{}{"item_status": 3}).Error
if err != nil {
return
}
return nil
}
......@@ -4,6 +4,7 @@ import (
"bom_server/configs"
"bom_server/internal/model"
"encoding/json"
"fmt"
"github.com/imroc/req"
"reflect"
"strconv"
......@@ -42,6 +43,7 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
if err = resp.ToJSON(&responseData); err != nil {
return
}
//这是匹配到的数据
var bomMatchingList []model.BomItemMatching
for _, goodsMap := range goodsMapList {
for _, goods := range responseData.Data {
......@@ -51,7 +53,10 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
bomMatching := model.BomItemMatching{
BomID: goodsMap.BomId,
BomItemID: goodsMap.BomItemId,
BrandId: goods.BrandID,
GoodsID: goods.GoodsID,
GoodsName: goods.GoodsName,
BrandName: goods.BrandName,
GoodsType: goods.GoodsType,
SupplierID: goods.SupplierID,
SupplierName: goods.SupplierName,
......@@ -59,6 +64,7 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
HkDelivery: goods.HkDeliveryTime,
Number: goodsMap.Number,
Stock: goods.Stock,
IsBuy: goods.IsBuy,
Moq: goods.Moq,
Mpq: goods.Mpq,
Encap: goods.Encap,
......@@ -83,7 +89,7 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
purchases = int(tempNumber)
} else if value == "float64" {
purchases = int(price.Purchases.(float64))
}else{
} else {
purchases = price.Purchases.(int)
}
if goodsMap.Number <= purchases {
......@@ -103,7 +109,9 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
}
}
}
start := time.Now()
err = BatchSaveMatchings(bomId, bomMatchingList)
fmt.Println(time.Now().Sub(start))
if err != nil {
return
}
......
......@@ -6,14 +6,16 @@ import (
"bom_server/internal/model"
"context"
"encoding/json"
"errors"
"fmt"
es "gopkg.in/olivere/elastic.v5"
"math"
"regexp"
"strconv"
"strings"
"sync"
)
//匹配商品
/**
1.先去数据库查出所有需要匹配的数据
2.然后开协程去针对每个bom里的goods_name去请求搜索接口
......@@ -24,42 +26,47 @@ import (
func MatchGoods(bomId int) (err error) {
//获取bom
bom := GetBomInfoWithItems(bomId)
if len(bom.BomItems) == 0 {
return errors.New("没有商品的bom单")
}
bomItems := bom.BomItems
//bomItems := []model.BomItem{
// {
// BomID: 7,
// BomItemID: 1,
// BrandName: "ADVID",
// GoodsName: "ECC15DCKNS1562",
// Number: 1,
// },
// {
// BomID: 7,
// BomItemID: 2,
// BrandName: "ADVID",
// GoodsName: "ECC15DCKNS1562",
// Number: 1,
// },
// {
// BomID: 7,
// BomItemID: 3,
// BrandName: "ADVID",
// GoodsName: "ECC15DCKNS15621",
// Number: 1,
// },
// {
// BomID: 7,
// BomItemID: 4,
// BrandName: "ADVID",
// GoodsName: "ECC15DCKNS1562",
// Number: 1,
// },
//}
return SearchGoods(bomId, bomItems)
perGoDealNumber := 200
//开启协程处理搜索.每50个开启一个协程
var wg sync.WaitGroup
//判断是否有余数
extraNumber := len(bomItems) % perGoDealNumber
var number int
if extraNumber > 0 {
number = (len(bomItems) / perGoDealNumber) + 1
} else {
number = len(bomItems) / perGoDealNumber
}
wgNumber := int(math.Ceil(float64(number)))
wg.Add(wgNumber)
for i := 0; i < len(bomItems); i = i + perGoDealNumber {
i := i
var bomData []model.BomItem
if i+perGoDealNumber > len(bomItems) {
bomData = bomItems[i:]
} else {
bomData = bomItems[i : i+perGoDealNumber]
}
go func() {
if err := SearchGoods(bomId, bomData, &wg); err != nil {
fmt.Println(err)
}
}()
}
wg.Wait()
return nil
//return SearchGoods(bomId, bomItems)
}
//去es搜索商品,得到对应的商品对应关系
func SearchGoods(bomId int, bomItems []model.BomItem) (err error) {
func SearchGoods(bomId int, bomItems []model.BomItem, wg *sync.WaitGroup) (err error) {
defer func() {
wg.Done()
}()
if len(bomItems) == 0 {
return
}
......@@ -74,21 +81,50 @@ func SearchGoods(bomId int, bomItems []model.BomItem) (err error) {
fmt.Println(err)
}
//要删除已经精确匹配过的bomItem
//for _, bom := range bomItems {
// for _, goodsMap := range goodsMapList {
//
// }
//}
var fuzzyBomItems []model.BomItem
for _, bomItem := range bomItems {
if !checkInGoodsMap(bomItem, goodsMapList) {
fuzzyBomItems = append(fuzzyBomItems, bomItem)
}
}
//第二次去模糊匹配
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, client, true)
fuzzyGoodsMapList, err := getUpdateGoodsData(bomId, fuzzyBomItems, client, true)
if err != nil {
fmt.Println(err)
}
err = UpdateGoodsData(goodsMapList)
//再删除模糊匹配到的数据,就得到完全没有匹配的数据了
var notMatchBomItems []model.BomItem
for _, bomItem := range bomItems {
if !checkInGoodsMap(bomItem, append(goodsMapList, fuzzyGoodsMapList...)) {
notMatchBomItems = append(notMatchBomItems, bomItem)
}
}
err = UpdateGoodsData(append(goodsMapList, fuzzyGoodsMapList...))
if err != nil {
return
}
//还要去处理没有匹配到的数据
err = UpdateNoMatchBomItem(notMatchBomItems)
if err != nil {
return
}
return
}
//判断是否在里面
func checkInGoodsMap(bom model.BomItem, goodsMapList []GoodsMap) bool {
for _, goodsMap := range goodsMapList {
if bom.GoodsName == goodsMap.GoodsName {
return true
}
}
return false
}
func getUpdateGoodsData(bomId int, bomItems []model.BomItem, client *es.Client, rawSearch bool) (goodsMapList []GoodsMap, err error) {
if len(bomItems) == 0 {
return
}
index := configs.ESSetting.GoodsIndexName
search := client.MultiSearch().Index(index)
//多重搜索,第一次先去精确匹配
......@@ -103,9 +139,12 @@ func getUpdateGoodsData(bomId int, bomItems []model.BomItem, client *es.Client,
}
res, err := search.Do(context.Background())
if err != nil {
fmt.Println(err)
return
}
if len(res.Responses) == 0 {
return
}
//因为是多重查询,所以会有多套结果
for key, responses := range res.Responses {
//有数据进行转换
......@@ -119,7 +158,7 @@ func getUpdateGoodsData(bomId int, bomItems []model.BomItem, client *es.Client,
var goodsMap GoodsMap
goodsMap.GoodsId = strconv.FormatInt(int64(goods.GoodsID), 10)
goodsMap.Number = bomItems[key].Number
goodsMap.GoodsName = goods.GoodsName
goodsMap.GoodsName = bomItems[key].GoodsName
goodsMap.BomItemId = bomItems[key].BomItemID
goodsMap.BomId = bomId
goodsMapList = append(goodsMapList, goodsMap)
......@@ -132,16 +171,10 @@ func getUpdateGoodsData(bomId int, bomItems []model.BomItem, client *es.Client,
//构建请求参数
func getSearchParams(goodsName, brandName string, number int, flag bool) (searchRequest *es.SearchRequest) {
//var size int
//if flag {
// size = 10
//} else {
// size = 1
//}
query := getTermQuery(goodsName, brandName, number, flag)
source := es.NewSearchSource().IndexBoost("liexin_ziying", 2).Query(query).Size(1).
source := es.NewSearchSource().IndexBoost("liexin_ziying", 2).Query(query).
Sort("_score", false).Sort("sort", false).Sort("single_price", true).
From(0)
From(0).Size(1)
searchRequest = es.NewSearchRequest().Source(source)
return searchRequest
}
......@@ -151,16 +184,9 @@ func getTermQuery(goodsName, brandName string, number int, flag bool) (query *es
var (
field string
)
if number == 0 {
number = 0
}
query = es.NewBoolQuery()
if flag {
field = "auto_goods_name.raw"
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName := replace.ReplaceAllString(goodsName, "")
goodsName = strings.ToUpper(goodsName)
......@@ -174,9 +200,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"
query = query.Must(es.NewTermQuery(field, goodsName))
}
query = query.Filter(es.NewTermQuery("status", 0))
......
......@@ -4,6 +4,9 @@ type BomItemMatching struct {
// MatchingID 匹配ID
MatchingID int `json:"matching_id" gorm:"primary_key"`
// BomID bom主表Id(为了方便逆向查找bom_item表)
GoodsName string `json:"goods_name"`
BrandName string `json:"brand_name"`
BrandId int `json:"brand_id"`
BomID int `json:"bom_id"`
// BomItemID bom详情ID
BomItemID int `json:"bom_item_id"`
......@@ -38,6 +41,7 @@ type BomItemMatching struct {
AddTime int `json:"add_time"`
// UpdateTime 更新时间
UpdateTime int `json:"update_time"`
IsBuy int `json:"is_buy" gorm:"-"`
// LadderPrice 阶梯价
LadderPrice string `json:"ladder_price"`
}
......
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