Commit ce2e459c by hcy001
parents b3910cdb 5f8e2f63
......@@ -53,6 +53,7 @@ type Api struct {
type Oss struct {
UploadKey string `ini:"upload_key"`
IsDebug int `ini:"is_debug"`
}
var DatabaseSetting = &Database{}
......
......@@ -36,7 +36,7 @@ func GetDBCms() DbInfo {
"mysql",
"ichuntcms",
"ichuntcms#zsyM",
"192.168.2.232",
"192.168.1.252",
"3306",
"ichuntcms",
}
......@@ -47,7 +47,7 @@ func GetDBLiexin() DbInfo {
"mysql",
"liexin",
"liexin#zsyM",
"192.168.2.232",
"192.168.1.252",
"3306",
"liexin",
}
......
package common
import (
"bom_server/configs"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/shopspring/decimal"
"github.com/syyongx/php2go"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
/*
提取字符串的字母数字并去掉空格
@param string target_str 目标字符串
@return 返回格式化后字符串
*/
func DrawLetterNum(target_str string) (str string) {
if target_str == "" {
return ""
}
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
return strings.ToUpper(replace.ReplaceAllString(target_str, ""))
}
/*
定时任务打印输出
@parm jsonStr 需要json输出的内容
*/
func PrintDebug(str ...interface{}) {
flag := configs.OssSetting.IsDebug
if flag == 0 {
return
}
temp := "---"+time.Now().Format("2006-01-02 15:04:05");
for _,jsonStr := range str {
if v, p := jsonStr.(string); p {
temp = temp +"---"+ v
}else{
jsonData,err := json.Marshal(jsonStr)
if err != nil {
fmt.Println("错误:-----",err)
}
temp = temp +"---"+ string(jsonData)
}
}
fmt.Println(temp)
}
// Md5 md5()
func Md5(str string) string {
hash := md5.New()
......
......@@ -2,6 +2,7 @@ package logic
import (
"bom_server/configs"
"bom_server/internal/common"
"bom_server/internal/model"
"encoding/json"
"errors"
......@@ -94,11 +95,20 @@ func UpdateBomItem(bomId, bomItemId int) (err error) {
bomItems = GetStandardAttrs(bomItems)
//第一次去精确匹配,没有再去模糊匹配
var goodsMapList []GoodsMap
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, true)
itemsStatus := 2
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, true,false)
common.PrintDebug("精确匹配:",goodsMapList)
if len(goodsMapList) == 0 {
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, false)
if err != nil {
return
//替代型号匹配
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, false,true)
common.PrintDebug("替代型号匹配:",goodsMapList)
itemsStatus = 5; //替代物料匹配
if len(goodsMapList) == 0 {
goodsMapList, err = getUpdateGoodsData(bomId, bomItems, where.DeliveryType, where.Sort, client, false,false)
common.PrintDebug("模糊匹配:",goodsMapList)
if len(goodsMapList) != 0 {
itemsStatus = 6; //模糊匹配
}
}
}
//fmt.Println(goodsMapList)
......@@ -111,7 +121,7 @@ func UpdateBomItem(bomId, bomItemId int) (err error) {
return
}
} else {
err = UpdateGoodsData(goodsMapList)
err = UpdateGoodsData(goodsMapList,itemsStatus)
if err != nil {
return
}
......
package logic
import (
"bom_server/internal/common"
"bom_server/internal/model"
"github.com/prometheus/common/log"
"math"
......@@ -28,7 +29,7 @@ func GetBomMatching(bomId, bomItemId int) (bomMatching model.BomItemMatching) {
}
//根据matchingList去修改
func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err error) {
func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching,status ...int) (err error) {
bomIdStr := strconv.FormatInt(int64(bomId), 10)
tableEnd := string(bomIdStr[len(bomIdStr)-1])
tableName := "lie_bom_item_matching_" + tableEnd
......@@ -56,9 +57,34 @@ func BatchSaveMatchings(bomId int, matchingList []model.BomItemMatching) (err er
} else {
itemStatus = 3
}
is_select := 1;
if len(status) > 0 { //todo 增加类型: 5 替代型号匹配 6 模糊匹配
itemStatus = status[0]
if itemStatus == 4 || itemStatus == 5 || itemStatus == 6 {
is_select = 0
}
}
//查询需求数量 跟 当前库存比较
var bomInfo model.BomItem
model.Db.Table("lie_bom_item_"+ tableEnd).Where("bom_item_id = ?", matching.BomItemID).First(&bomInfo)
if bomInfo.Number > matching.Stock {
common.PrintDebug("精确匹配算出来库存不足")
is_select = 0;
itemStatus = 3
}
if itemStatus == 3 {
is_select = 0;
}
err = model.Db.Table("lie_bom_item_"+tableEnd).Where("bom_item_id = ?", matching.BomItemID).
Updates(map[string]interface{}{"item_status": itemStatus, "update_time": now, "is_select": 1}).Error
Updates(map[string]interface{}{"item_status": itemStatus, "update_time": now, "is_select": is_select}).Error
if err != nil {
common.PrintDebug(err)
return
}
//更新商品价格和库存的队列
......
......@@ -2,6 +2,7 @@ package logic
import (
"bom_server/configs"
"bom_server/internal/common"
"bom_server/internal/model"
"encoding/json"
"github.com/ichunt2019/go-rabbitmq/utils/rabbitmq"
......@@ -22,8 +23,12 @@ type GoodsMap struct {
DeliveryType int
}
func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
func UpdateGoodsData(goodsMapList []GoodsMap,status ...int) (err error) {
//根据goodsIds去商品服务获取对应的商品信息
if len(goodsMapList) == 0 {
return
}
var (
goodsIds []string
bomId int
......@@ -61,15 +66,25 @@ func UpdateGoodsData(goodsMapList []GoodsMap) (err error) {
}
}
}
//先找出没有匹配到商品的进行批量修改状态
err = batchUpdateItemStatus(bomId, noGoodsInfoItemIdList)
if err != nil {
return
}
err = BatchSaveMatchings(bomId, bomMatchingList)
if err != nil {
return
if len(status) > 0 { //todo 增加类型: 5 替代型号匹配 6 模糊匹配
err = BatchSaveMatchings(bomId, bomMatchingList,status[0])
if err != nil {
return
}
}else{
err = BatchSaveMatchings(bomId, bomMatchingList)
if err != nil {
return
}
}
return
}
......@@ -89,6 +104,9 @@ func GetGoodsInfo(goodsIdsStr string) (goodsList []model.ApiGoods, err error) {
}
//fmt.Println(goodsIdsStr)
resp, err := req.Post(goodsServerUrl+"/synchronization", params)
common.PrintDebug("请求商品详情:",goodsServerUrl+"/synchronization"," 参数:",params , "结果:",len(resp.String()))
if err != nil {
return
}
......@@ -194,6 +212,8 @@ func MatchGoodsInfo(goods model.ApiGoods, goodsMap GoodsMap) (bomMatching model.
//阶梯价处理
if len(goods.LadderPrice) > 0 {
ladderPriceStr, err := json.Marshal(goods.LadderPrice)
common.PrintDebug(string(ladderPriceStr))
if err != nil {
return model.BomItemMatching{}, err
}
......
......@@ -91,42 +91,80 @@ func SearchGoods(bomId int, bomItems []model.BomItem, deliveryType, sort int, wg
} else {
needDealBomItems = bomItems[i : i+perNumber]
}
//第一次先去精确匹配
goodsMapList, err := getUpdateGoodsData(bomId, needDealBomItems, deliveryType, sort, client, true)
//todo 精确匹配: 第一次先去精确匹配
common.PrintDebug("精确匹配型号:",needDealBomItems)
exactList, err := getUpdateGoodsData(bomId, needDealBomItems, deliveryType, sort, client, true,false)
if err != nil {
return err
}
common.PrintDebug("精确匹配结果:",exactList)
//要删除已经精确匹配过的bomItem,得到需要去模糊匹配的商品
var fuzzyBomItems []model.BomItem
for _, bomItem := range needDealBomItems {
//不在精确匹配结果里面的,同时商品名称不能为空的
if !checkInGoodsMap(bomItem, goodsMapList) && bomItem.GoodsName != "" {
if !checkInGoodsMap(bomItem, exactList) {
fuzzyBomItems = append(fuzzyBomItems, bomItem)
}
}
//第二次去模糊匹配
fuzzyGoodsMapList, err := getUpdateGoodsData(bomId, fuzzyBomItems, deliveryType, sort, client, false)
//todo 替代型号匹配 2021.4.17 替代型号参与匹配,优先取原始型号,原始型号无法购买再推荐替代型号
common.PrintDebug("替代型号匹配型号:",fuzzyBomItems)
AlikeGoodsMapList, err := getUpdateGoodsData(bomId, fuzzyBomItems, deliveryType, sort, client, false,true)
if err != nil {
return err
}
common.PrintDebug("替代型号匹配结果:",AlikeGoodsMapList)
//删除已经类似料匹配的型号
var fuzzyBomItems2 []model.BomItem
for _, bomItem := range fuzzyBomItems {
//不在精确匹配结果里面的,同时商品名称不能为空的
if !checkInGoodsMap(bomItem, AlikeGoodsMapList) {
fuzzyBomItems2 = append(fuzzyBomItems2, bomItem)
}
}
//todo 模糊匹配
common.PrintDebug("模糊匹配型号:",fuzzyBomItems2)
fuzzyGoodsMapList, err := getUpdateGoodsData(bomId, fuzzyBomItems2, deliveryType, sort, client, false,false)
if err != nil {
return err
}
common.PrintDebug("模糊匹配结果",fuzzyGoodsMapList)
//再删除模糊匹配到的数据,就得到完全没有匹配的数据了
var notMatchBomItems []model.BomItem
var needMatchGoodsMapList = append(goodsMapList, fuzzyGoodsMapList...)
for _, bomItem := range needDealBomItems {
if !checkInGoodsMap(bomItem, needMatchGoodsMapList) {
for _, bomItem := range fuzzyBomItems2 {
if !checkInGoodsMap(bomItem, fuzzyGoodsMapList) {
notMatchBomItems = append(notMatchBomItems, bomItem)
}
}
common.PrintDebug("没有匹配到的bom 明细",notMatchBomItems)
//先去处理没有匹配到的数据
err = UpdateNoMatchBomItem(notMatchBomItems, true)
if err != nil {
return err
}
//然后去处理匹配到的数据
err = UpdateGoodsData(needMatchGoodsMapList)
//精确匹配数据
err = UpdateGoodsData(exactList)
if err != nil {
return err
}
//替代物料数据
err = UpdateGoodsData(AlikeGoodsMapList,5)
if err != nil {
return err
}
//模糊匹配
err = UpdateGoodsData(fuzzyGoodsMapList,6)
if err != nil {
return err
}
}
return
}
......@@ -180,14 +218,14 @@ func checkInGoodsMap(bom model.BomItem, goodsMapList []GoodsMap) bool {
return false
}
func getUpdateGoodsData(bomId int, bomItems []model.BomItem, deliveryType, sort int, client *es.Client, rawSearch bool) (goodsMapList []GoodsMap, err error) {
func getUpdateGoodsData(bomId int, bomItems []model.BomItem, deliveryType, sort int, client *es.Client, rawSearch bool,isAlike bool) (goodsMapList []GoodsMap, err error) {
if len(bomItems) == 0 {
return
}
index := configs.ESSetting.GoodsIndexName
//如果是大陆收货(type=1),就要包含专卖,其中自营单独查
index = index + ",zhuanmai,liexin_ziying"
goodsMapList, err = search(index, bomId, bomItems, deliveryType, sort, client, rawSearch)
goodsMapList, err = search(index, bomId, bomItems, deliveryType, sort, client, rawSearch,isAlike)
if err != nil {
return nil, err
}
......
......@@ -7,11 +7,10 @@ import (
"context"
"encoding/json"
es "gopkg.in/olivere/elastic.v5"
"regexp"
"strings"
)
func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sort int, client *es.Client, rawSearch bool) (goodsMapList []GoodsMap, err error) {
func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sort int, client *es.Client, rawSearch bool,isAlike bool) (goodsMapList []GoodsMap, err error) {
//先去自营查一遍
search := client.MultiSearch().Index(index)
//是否已经搜索过标签
......@@ -33,8 +32,12 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
paramsRawSearch = true
}
//构建一个goods_name对应的bomItems列表
searchRequest := getSearchParams(index, bom, sort, paramsRawSearch)
searchRequest := getSearchParams(index, bom, sort, paramsRawSearch,rawSearch,isAlike)
searchFlag = true
common.PrintDebug("查询es索引,","是否原生rawSearch:",rawSearch," 是否类似:",isAlike,index)
common.PrintDebug(searchRequest.Body())
//randInt := rand.Intn(10)
//time.Sleep(10 * time.Millisecond)
search.Add(searchRequest)
......@@ -44,6 +47,10 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
return nil, err
}
res, err := search.Do(context.Background())
common.PrintDebug(res.Responses)
if err != nil {
return
}
......@@ -80,12 +87,12 @@ func search(index string, bomId int, bomItems []model.BomItem, deliveryType, sor
}
//构建请求参数
func getSearchParams(index string, bomItem model.BomItem, sort int, flag bool) (searchRequest *es.SearchRequest) {
func getSearchParams(index string, bomItem model.BomItem, sort int, flag bool,rawSearch bool,isAlike bool) (searchRequest *es.SearchRequest) {
//fmt.Println("商品名称 : ", bomItem.GoodsName)
//fmt.Println("参数列表 : ", bomItem.AttrList)
bomItem.Encap = TransformEncap(bomItem.Encap)
//fmt.Println("封装 : ", bomItem.Encap)
query := getTermQuery(bomItem, sort, flag)
query := getTermQuery(bomItem, sort, flag,rawSearch,isAlike)
source := es.NewSearchSource().Query(query)
if sort == 1 {
//source.Sort("_score", false)
......@@ -100,19 +107,17 @@ func getSearchParams(index string, bomItem model.BomItem, sort int, flag bool) (
//source.Sort("_score", false)
source.Sort("stock", false)
}
source = source.From(0).Size(1)
searchRequest = es.NewSearchRequest().Source(source)
return searchRequest
}
//构建term条件
func getTermQuery(bomItem model.BomItem, sort int, flag bool) (query *es.BoolQuery) {
func getTermQuery(bomItem model.BomItem, sort int, flag bool,rawSearch bool,isAlike bool) (query *es.BoolQuery) {
query = es.NewBoolQuery()
goodsName := common.DrawLetterNum(bomItem.GoodsName)
if flag {
field := "auto_goods_name.raw"
replace, _ := regexp.Compile("[^A-Za-z0-9]+")
goodsName := replace.ReplaceAllString(bomItem.GoodsName, "")
goodsName = strings.ToUpper(goodsName)
//用于判断下面的miniShould,因为下面的参数里面,如果出现阻值的,用的是should语句
//为了保证所有参数都必须参与搜索,所以要用miniShould去做限制
//var shouldNumber int
......@@ -145,9 +150,12 @@ func getTermQuery(bomItem model.BomItem, sort int, flag bool) (query *es.BoolQue
} else {
query = query.Must(es.NewTermsQuery("attr_bom", "$_$"))
}
} else {
query = query.Filter(es.NewTermQuery(field, goodsName))
}
if goodsName != "" {
query = query.Filter(es.NewTermQuery("auto_goods_name.raw", goodsName))
}
//判断是否存在brandName并且匹配不到对应的标准品牌
if bomItem.BrandName != "" {
bomItem.BrandName = strings.ToUpper(bomItem.BrandName)
......@@ -157,19 +165,33 @@ func getTermQuery(bomItem model.BomItem, sort int, flag bool) (query *es.BoolQue
if bomItem.Encap != "" {
query = query.Must(es.NewTermQuery("attr_bom", "FZ€"+bomItem.Encap))
}
//搜索库存
query = query.Should(es.NewRangeQuery("stock").Gte(bomItem.Number * bomItem.Amount))
} else {
query = query.Must(es.NewTermQuery("auto_goods_name", bomItem.GoodsName))
//精确匹配
if rawSearch {
query = query.Must(es.NewRangeQuery("stock").Gte(bomItem.Number * bomItem.Amount)) //搜索库存
}else{
query = query.Should(es.NewRangeQuery("stock").Gte(bomItem.Number * bomItem.Amount)) //搜索库存
}
} else if isAlike && goodsName != "" { //todo 推荐型号搜索
query = query.Must(es.NewTermsQuery("alike_spu_name.raw", goodsName))
} else if goodsName != "" {
query = query.Must(es.NewTermQuery("goods_name", goodsName))
}
common.PrintDebug("isalike:",isAlike)
//只显示库存大于0的数据
query.Must(es.NewRangeQuery("stock").Gt(0))
if rawSearch == false { //不是精确匹配
query.Must(es.NewRangeQuery("stock").Gt(0))
}
//按价格排序
//if sort == 2 || sort == 1 {
// query = query.Filter(es.NewRangeQuery("single_price").Gt(0))
//}
//只显示起订量大于0
query.Must(es.NewRangeQuery("moq").Gt(0))
query.Must(es.NewRangeQuery("moq").Gt(0).Lte(bomItem.Number*bomItem.Amount)) //todo 2021.4.17 起订量高于需求数量的排除;
if configs.ApiSetting.Mode != "debug" {
query = query.Filter(es.NewTermQuery("status", 1))
}
......
#适用于更新GOSKU代码服务,并且重启代码
#!/bin/bash
Cur_Dir=$(pwd)
echo $Cur_Dir
cd $Cur_Dir
git reset --hard HEAD
git pull
rm -f ${Cur_Dir}"/cmd/main"
export GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
branch_name=$(git name-rev --name-only HEAD)
echo $branch_name
git pull origin $branch_name
go build -o ${Cur_Dir}"/cmd/main" ${Cur_Dir}"/cmd/main.go"
go build -o ${Cur_Dir}"/server/main" ${Cur_Dir}"/server/main.go"
chmod +x ${Cur_Dir}"/cmd/main"
chmod +x ${Cur_Dir}"/server/main"
chmod +x ${Cur_Dir}"/update.sh"
supervisorctl restart bom_server_cmd:*
supervisorctl restart bom_server_http_60010:*
echo "更新执行成功"
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