Commit ababccc9 by 孙龙

Merge branch 'dev' of http://119.23.72.7/mushishixian/search_server into dev

# Conflicts:
#	pkg/common/function.go
parents aabd44c2 0116ced2
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/tidwall/gjson"
"os"
"search_server/boot"
"search_server/service"
)
func main() {
......@@ -33,15 +34,39 @@ func main() {
//fmt.Println(attrName)
//os.Exit(1)
ss := "LM358"
mouserRes := service.OutLink(&ss,"-1")
for k, mouseItem := range mouserRes {
fmt.Println(k)
fmt.Println(mouseItem.PriceTemp)
//ss := "LM358"
//mouserRes := service.OutLink(&ss,"-1")
//for k, mouseItem := range mouserRes {
// fmt.Println(k)
// fmt.Println(mouseItem.PriceTemp)
// os.Exit(1)
//}
//fmt.Errorf()
jsonstr := `{"goods_id":1156147221172418504,"goods_sn":"","spu_id":"2156147221162468806","update_time":1561472211,"goods_status":3,"goods_name":"RC0603FR-07100KL","goods_type":2,"supplier_id":17,"encoded":"10059","batch_sn":"1148","moq":27961,"mpq":1,"stock":1765,"hk_delivery_time":"","cn_delivery_time":"4\u4e2a\u5de5\u4f5c\u65e5","goods_details":"","ladder_price":[{"purchases":2765,"price_cn":0.01072951425,"price_us":0},{"purchases":45654,"price_cn":0.343434,"price_us":6666}],"goods_images":"","canal":"L0000110","cp_time":0}`
m, ok := gjson.Parse(jsonstr).Value().(map[string]interface{})
if !ok {
fmt.Println(ok)
os.Exit(1)
}
//fmt.Errorf()
goods_id := gjson.Get(jsonstr, "goods_id").String()
ladder := gjson.Get(jsonstr, "ladder_price").Array()
temp1 := []map[string]float64{};
for _,value:=range ladder {
b := value.Map()
temp1 = append(temp1,map[string]float64{
"purchases":b["purchases"].Float(),
"price_cn":b["price_cn"].Float(),
"price_us":b["price_us"].Float(),
})
}
//fmt.Println(ladder)
//
m["goods_id"] = goods_id
m["ladder_price"] = temp1
dd,_ := json.Marshal(m)
fmt.Println(string(dd))
//lines := []string{
// `{"index":{"_index":"hcy1","_type":"goods","_id":"s1"} }`,
......@@ -73,7 +98,6 @@ func main() {
os.Exit(1)
}
//xml := strings.NewReader(resp)
//json, err := xj.Convert(xml)
//if err != nil {
......@@ -83,4 +107,4 @@ func main() {
//json_str := json.String();
////ss := gjson.Get(json_str,"hello").String()
//
//fmt.Println(json_str)
\ No newline at end of file
//fmt.Println(json_str)
......@@ -6,8 +6,10 @@ url = amqp://guest:guest@192.168.2.232:5672/
MQ_BOM_ITEMS_LIST=bom_items_list
; bom更新数据队列
MQ_BOM_SKU_LIST=bom_sku_list
;联营数据推送入队列-》go后台任务消费(斌哥脚本)
;联营数据更新-推送入队列-》go后台任务消费(斌哥脚本)
SEARCH_SKU_UPDATE_LIST=search_sku_update_list
;联营数据新增-推送入队列-》go后台任务消费(斌哥脚本)
SEARCH_SKU_LIST=search_sku_list
; bom任务id
ZIYING_MQ_BOM_ITEMS_LIST=ziying_bom_items_list
; bom更新数据队列
......
......@@ -22,6 +22,8 @@ SEARCH_API_ERORR_PRE=search_api_overtime_
SEARCH_SHOW_SKU_TIME=search_show_sku_time
;获取关税信息,根据goods_name,brand_name获取
TAX_CUSTOMS_INFO=tax_customs_info
;存放联营外链原始映射
SKU_RAW_MAP=sku_raw_map
[redis_ziyin]
AGG_TABLE=sz_ziying_class_aggs
......
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/ichunt2019/logger"
"search_server/pkg/common"
"search_server/service"
)
......@@ -28,17 +28,13 @@ func Recommend(c *gin.Context) {
req := &common.RecommendRequest{}
err := c.Bind(&req) //使用的是post参数
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
if req.Flag == 0 {
res := common.BomResponse{
ErrCode: 101,
ErrMsg: "参数不全",
Data: []string{},
}
c.JSON(200, res)
common.Output(101, "参数不全", nil)
return
}
data := service.Recommend(req)
c.JSON(200, data)
common.Output(req.Flag, "", data)
return
}
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/pkg/common"
......@@ -41,14 +41,13 @@ func GetDataIndex(c *gin.Context) {
defer redisCon.Close()
//获取stockFlag
res, err := redis.Bool(redisCon.Do("SADD", "search_keyword_member", r.Keyword+"_"+r.SupplierName))
fmt.Println(r.Keyword + "_" + r.SupplierName)
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
if res {
_, err := redis.Bool(redisCon.Do("INCR", config.Get("redis_all.SEARCH_API_TOTAL_PRE").String()+r.SupplierName))
if err != nil {
fmt.Println(err)
logger.Error("%s", err.Error())
}
}
r.FirstSearchPage = 2
......@@ -71,7 +70,8 @@ func GetDataIndex(c *gin.Context) {
result := service.GetReturnSupplierData(dullData)
result["total"] = gjson.Get(esResult, "hits.total").Int()
result["took"] = gjson.Get(esResult, "took").Int()
c.JSON(200, common.SuccessResponse(0, "", result))
common.Output(0, "", result)
return
}
//获取查询es的条件语句
......
......@@ -4,29 +4,29 @@ import (
"github.com/gin-gonic/gin"
"search_server/model"
"search_server/pkg/common"
"search_server/service"
"search_server/service/ly"
)
//搜索型号
func GetMouserData(c *gin.Context) {
goodsName, _ := c.GetPostForm("goods_name")
lycon := new(service.CommonLyService)
lycon := new(ly.CommonLyService)
//调外链拿数据
apiData := service.OutLink(&goodsName,"1")
apiData := ly.OutLinkMouser(&goodsName,"1")
//供应商详情
supplierInfo := model.SUPPLIER_REDIS_INFO_{
SupplierId:14,
SupplierNickname: "mouser",
}
goods := lycon.GetSkuByGoodsSn(apiData,&supplierInfo)
err := lycon.GetSkuByGoodsSn(apiData,&supplierInfo)
var errCode int
if len(goods) == 0 {
if !err {
errCode = 1
}
res := common.BomResponse{
ErrCode: errCode,
ErrMsg: "",
Data: goods,
Data: "",
}
c.JSON(200,res)
}
package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
......@@ -12,6 +11,7 @@ import (
"search_server/pkg/gredis"
"search_server/requests"
"search_server/service"
"search_server/service/ly"
"search_server/service/transformer"
"strings"
)
......@@ -65,7 +65,7 @@ func QuoteIndex(c *gin.Context) {
}
func mouserOrg(keyword, suppliedId string) (result map[string]interface{}, err error) {
mouserRes := service.OutLink(&keyword, "1")
mouserRes := ly.OutLinkMouser(&keyword, "1")
redisCon := gredis.Conn("search_r")
defer redisCon.Close()
skuMouser := make(map[string]string)
......@@ -117,13 +117,11 @@ func GetDataPur(c *gin.Context) {
if user.Name == "" && user.Email != "" {
userInfoData[user.UserId] = user.Email
} else {
fmt.Println(user.UserId)
userInfoData[user.UserId] = user.Name
}
}
temp2 := make(map[int]string)
for _, code := range intracode {
//fmt.Println(userInfoData[code.AdminId])
if _, exist := userInfoData[code.AdminId]; exist {
temp2[code.CodeId] = userInfoData[code.AdminId]
} else {
......@@ -135,10 +133,8 @@ func GetDataPur(c *gin.Context) {
defer redisCon.Close()
for _, supplier := range supplierData {
if value, exist := temp2[supplier.ChannelUid]; exist {
fmt.Println(value)
fmt.Println(temp2[supplier.ChannelUid])
res, err := redis.Bool(redisCon.Do("HSET", "search_supplier_canaltopurchase_test", supplier.SupplierCode, value))
if err != nil || !res {
_, err := redis.Bool(redisCon.Do("HSET", "search_supplier_canaltopurchase_test", supplier.SupplierCode, value))
if err != nil {
c.String(200, "失败")
return
}
......@@ -152,6 +148,5 @@ func GetDataPur(c *gin.Context) {
func GetData(keyword, supplierName string) (res interface{}) {
quoteService := service.QuoteService{}
res, _ = quoteService.GetData(keyword, supplierName)
fmt.Println(res)
return
}
......@@ -2,28 +2,35 @@ package model
//联营请求外链后格式化数据
type LyClearGoodsList struct {
GoodsId string `json:"goods_name"` //型号id
GoodsName string `json:"goods_name"` //型号名称
BrandName string `json:"brand_name"` //品牌名称
Desc string `json:"desc"` //描述
GoodsSn string `json:"goods_sn"` //api唯一编码
Docurl string `json:"docurl"` //sku对应供应商的文档路径
Url string `json:"url"` //sku对应供应商的商品详情
GoodsImg string `json:"goods_img"` //sku图片
Cat string `json:"cat"` //分类
Encoded string `json:"encoded"` //内部编码
RestrictionMessage string `json:"RestrictionMessage"` //额外购买限制内容,如 ‘当前商品不在本地区销售’
Increment int64 `json:"increment"` //最小包装量、倍数 =Mpq
SinglePrice float64 `json:"single_price"` //最小单价
Stock int64 `json:"increment"` //库存
Moq int64 `json:"moq"` //最小起订量、起订量
Tiered []*TierItem //价格梯度数量
PriceTemp []interface{} //拼接梯度,目前只有联营推送队列用
SkuId string `json:"sku_id"` //平台型号id (非必填)
SpuId string `json:"spu_id"` //平台spuId (非必填)
BrandId string `json:"brand_id"` //平台品牌id (非必填)
GoodsName string `json:"goods_name"` //型号名称
BrandName string `json:"brand_name"` //品牌名称
Desc string `json:"desc"` //描述
BatchSn string `json:"batch_sn"` //批次号
GoodsSn string `json:"goods_sn"` //api唯一编码
Docurl string `json:"docurl"` //sku对应供应商的文档路径
Url string `json:"url"` //sku对应供应商的商品详情
GoodsImg string `json:"goods_img"` //sku图片
Cat string `json:"cat"` //分类
Encap string `json:"encap"` //包装
Canal string `json:"canal"` //渠道标签
Encoded string `json:"encoded"` //内部编码
RestrictionMessage string `json:"RestrictionMessage"` //额外购买限制内容,如 ‘当前商品不在本地区销售’
Increment int64 `json:"increment"` //最小包装量、倍数 =Mpq
SinglePrice float64 `json:"single_price"` //最小单价
Stock int64 `json:"increment"` //库存
Moq int64 `json:"moq"` //最小起订量、起订量
RawGoodsId string `json:"raw_goods_id"` //外链原始goods_sn,如digikey:{'raw_goods_id': 'AT28C64X-25PC-ND', 'raw_brand_name': 'Microchip Technology'}
RawBrandName string `json:"raw_brand_name"` //外链原始品牌名称,如digikey:{'raw_goods_id': 'AT28C64X-25PC-ND', 'raw_brand_name': 'Microchip Technology'}
Tiered []*TierItem //价格梯度数量
PriceTemp []interface{} //拼接梯度,目前只有联营推送队列用
}
type TierItem struct {
Purchases uint64 //购买数量
PriceUs float64 //数量对应的英文价格
PriceCn float64 //数量对应的中文价格
Purchases uint64 `json:"purchases"` //购买数量
PriceUs float64 `json:"price_us"` //数量对应的英文价格
PriceCn float64 `json:"price_cn"` //数量对应的中文价格
PriceAc float64 `json:"price_ac"`
}
......
......@@ -269,9 +269,6 @@ func auth() bool {
// interface{}转为 []interface{}
//將任意類型的切片轉為 []interface{}
func CreateAnyTypeSlice(slice interface{}) ([]interface{}, bool) {
val, ok := isSlice(slice)
......@@ -301,6 +298,20 @@ func isSlice(arg interface{}) (val reflect.Value, ok bool) {
return
}
//向左补充字符串
func StrPadLeft(input string, padLength int, padString string) string {
output := padString
for padLength > len(output) {
output += output
}
if len(input) >= padLength {
return input
}
return output[:padLength-len(input)] + input
}
//移除字符串切片中某個元素
func RemoveSliceString(s []string, i string) []string {
b := make([]string,0)
......
package common
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
"github.com/ichunt2019/logger"
log "github.com/sirupsen/logrus"
"os"
"search_server/middleware"
"search_server/pkg/config"
"search_server/pkg/gredis"
"search_server/pkg/message"
"strconv"
"strings"
"time"
)
type SearchApiLog struct {
Msg string `json:"msg"`
MsgCode string `json:"msgCode"`
Ts int64 `json:"ts"`
DateStr string `json:"dateStr"`
App string `json:"app"`
ServerIp string `json:"serverIp"`
FileName string `json:"fileName"`
LineNo string `json:"lineNo"`
Method string `json:"method"`
}
func AnalyzeSearchError(code int, ip, errMsg, file, line, method string) (log SearchApiLog) {
//获取所有参数
request := make(map[string]string)
ctx := middleware.Context
ctx.MultipartForm()
for name, value := range ctx.Request.Form {
if value[0] != "" {
request[name] = strings.TrimSpace(value[0])
}
}
requestByte, _ := json.Marshal(request)
log.Msg = errMsg + string(requestByte)
codeStr := strconv.Itoa(code)
log.MsgCode = StrPadLeft(codeStr, 6, "0")
log.Ts = time.Now().Unix()
log.DateStr = time.Now().Format("2006-01-02 15:04:05")
log.App = "search_api"
log.ServerIp = ip
log.FileName = file
log.LineNo = line
log.Method = method
//还要判断是否要推送消息
unitSupplierLogCode := config.Cfg.Section("UNIT_SUPPLIER_LOG_CODE").KeysHash()
if unitSupplierLogCode[codeStr] == "" {
if !strings.Contains(strings.ToUpper(errMsg), "SOAP-ERROR") {
//推送到钉钉
result, err := message.DingDingPush(errMsg)
if err != nil || result.Errcode != 0 {
logger.Error("%s", err, result)
}
}
} else {
redisCon := gredis.Conn("search_w")
defer redisCon.Close()
key := config.Get("SEARCH_API_LOG.SEARCH_API_ERROR_PRE").String() + unitSupplierLogCode[codeStr]
_, err := redis.Bool(redisCon.Do("INCR", key))
if err != nil {
logger.Error("%s", err)
}
}
return
}
type LogFormatter struct{}
//格式详情
func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
msg := entry.Message + "\n"
return []byte(msg), nil
}
//写入到特定的文件夹给ELK使用的日志
func WriteSearchErrorLog(searchLog string) {
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
path := "../logs/search/"
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
//生成当天的日志
filePath := path + time.Now().Format("20060102") + ".log"
file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
log.SetOutput(file)
log.Error(searchLog)
}
package common
import (
"regexp"
"encoding/json"
"runtime"
"search_server/middleware"
"search_server/pkg/log_report"
"strconv"
"strings"
)
type Response struct {
......@@ -48,18 +48,16 @@ func Output(errCode int, errMsg string, data interface{}) {
ErrMsg: errMsg,
Data: data,
}
if errCode >= 100 {
SearchApiLogger(errCode, errMsg)
}
ctx := middleware.Context
if ctx.DefaultQuery("callback", "") != "" {
ctx.JSONP(200, response)
} else {
referer := ctx.Request.Referer()
r, _ := regexp.Compile(`/^(\w+:\/\/)?([^\/]+)/i`)
var match string
matches := r.FindStringSubmatch(referer)
if len(matches) > 0 {
match = matches[0]
}
ctx.Header("Access-Control-Allow-Origin", match)
referer = strings.TrimRight(referer, "/")
ctx.Header("Access-Control-Allow-Origin", referer)
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
//跨域请求头设置
......@@ -68,12 +66,13 @@ func Output(errCode int, errMsg string, data interface{}) {
}
//错误的搜索日志记录
func SearchApiLog(code int, msg string) {
pc, file, line, _ := runtime.Caller(0)
func SearchApiLogger(code int, msg string) {
pc, file, line, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc)
ctx := middleware.Context
errMsg := "提示信息:" + msg + ",请求url:" + ctx.Request.URL.String()
lineNo := strconv.Itoa(line)
log_report.AnalyzeSearchError(code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLog := AnalyzeSearchError(code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLogByte, _ := json.Marshal(searchLog)
WriteSearchErrorLog(string(searchLogByte))
}
package log_report
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"os"
)
type SearchApiLog struct {
Msg string
MsgCode int
Ts int64
DateStr string
App string
ServerIp string
FileName string
LineNo string
Method string
}
func AnalyzeSearchError(code int, ip, errMsg, file, line, method string) (log SearchApiLog) {
//log.Msg = "" + errMsg
//log.MsgCode = 1
//log.Ts = time.Now().Unix()
//log.DateStr = time.Now().Format("2006-01-02 15:04:05")
//log.App = "search_api"
//log.ServerIp = ip
//log.FileName = file
//log.LineNo = line
//log.Method = method
//
////还要判断是否要推送消息
//unitSupplierLogCode := config.Cfg.Section("UNIT_SUPPLIER_LOG_CODE").KeysHash()
//codeStr := strconv.Itoa(code)
//if unitSupplierLogCode[codeStr] == "" {
// if !strings.Contains(strings.ToUpper(errMsg), "SOAP-ERROR") {
// //推送到钉钉
// result, err := message.DingDingPush(errMsg)
// if err != nil || result.Errcode != 0 {
// //logger.Errorf("%s", err, result)
// }
// }
//} else {
// redisCon := gredis.Conn("search_w")
// defer redisCon.Close()
// key := config.Get("SEARCH_API_LOG.SEARCH_API_ERROR_PRE").String() + unitSupplierLogCode[codeStr]
// redisCon.Do("INCR", key)
//}
return
}
type LogFormatter struct{}
//格式详情
func (s *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
msg := entry.Message
return []byte(msg), nil
}
//写入到特定的文件夹给ELK使用的日志
func WriteSearchErrorLog(searchLog SearchApiLog) {
logJson, _ := json.Marshal(searchLog)
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
file, _ := os.OpenFile("1.log", os.O_CREATE|os.O_WRONLY, 0666)
log.SetOutput(file)
log.Error(logJson)
}
func WriteSearchErrorLogTest(searchLog string) {
// 设置日志格式为json格式
log.SetFormatter(new(LogFormatter))
//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.File
file, _ := os.OpenFile("1.log", os.O_CREATE|os.O_WRONLY, 0666)
log.SetOutput(file)
log.Error(searchLog)
}
......@@ -7,7 +7,7 @@ import (
func SetUp() (err error) {
logConfig := make(map[string]string)
logConfig["log_path"] = "logs"
logConfig["log_chan_size"] = "1000"
logConfig["log_chan_size"] = "5"
err = logger.InitLogger("file", logConfig)
if err != nil {
return err
......
package logger
import (
"io"
"os"
"time"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
/**
* 判断文件是否存在 存在返回 true 不存在返回false
*/
func checkFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}
/*
@param writeString 写入文件字符串
@param file_pre 附加文件前缀
eg: logic.Loginfo("逾期统计开始","_yuqilv_");
*/
func Log(writeString string,log_file_pre string) {
log_file_pre = "_"+log_file_pre
date := time.Now().Format("2006-01-02")
date2 := time.Now().Format("2006-01-02 15:04:05")
var filename = "./logs/"+date+log_file_pre+".txt"
var f *os.File
var err1 error
if checkFileIsExist(filename) { //如果文件存在
f, err1 = os.OpenFile(filename, os.O_APPEND, 0666) //打开文件
} else {
f, err1 = os.Create(filename) //创建文件
}
check(err1)
ss := StrReplace("\r\n","",writeString,1) //替换多余换行
io.WriteString(f,"\r\n"+date2+"----"+ss) //写入文件(字符串)
//fmt.Print(err)
}
// StrReplace str_replace()
func StrReplace(search, replace, subject string, count int) string {
return strings.Replace(subject, search, replace, count)
}
......@@ -3,9 +3,9 @@ package message
import (
"encoding/json"
"fmt"
"github.com/syyongx/php2go"
"net/http"
"net/url"
"search_server/pkg/common"
"search_server/pkg/config"
"strconv"
"time"
......@@ -25,7 +25,7 @@ func SendMessage(mobile int64, keyWord string, content map[string]interface{}) {
"touser": {string(requestTel)},
"keyword": {keyWord},
"k1": {strconv.FormatInt(timeNow, 10)},
"k2": {common.Md5(common.Md5(strconv.FormatInt(timeNow, 10)) + apiMd5Str)},
"k2": {php2go.Md5(php2go.Md5(strconv.FormatInt(timeNow, 10)) + apiMd5Str)},
"is_ignore": {},
})
......
package routes
import (
"fmt"
"github.com/gin-gonic/gin"
"search_server/controller"
"search_server/middleware"
"search_server/pkg/common"
)
//初始化路由
......@@ -33,7 +33,8 @@ func InitRouter() *gin.Engine {
r.POST("/search/get_data", controller.GetDataIndex)
r.GET("/test", func(c *gin.Context) {
fmt.Println(c.Query("test"))
common.Output(1000, "testest", nil)
})
return r
}
......@@ -6,6 +6,7 @@ import (
"github.com/tidwall/gjson"
"gopkg.in/olivere/elastic.v5"
"regexp"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
......@@ -64,7 +65,7 @@ func getTermQuery(goodsName string) (query *elastic.BoolQuery) {
}
//推荐商品搜索
func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
func Recommend(req *common.RecommendRequest) (goodsList []model.ApiGoods) {
var err error
//先去请求参数,看是否是参数,如果是参数的话,需要修改请求的商品名称
var goodsName string
......@@ -101,10 +102,8 @@ func Recommend(req *common.RecommendRequest) (rsp *common.BomResponse) {
goodsIdList = append(goodsIdList, goodsId)
}
goodsIdListStr = strings.Join(goodsIdList, ",")
goodsList, err := GetGoodsInfoByApi(goodsIdListStr)
response.Data = goodsList
response.Flag = req.Flag
return &response
goodsList, err = GetGoodsInfoByApi(goodsIdListStr)
return goodsList
}
//处理recommend接口的请求参数
......
package service
package ly
/*
等同于 php.search_server.middleBaseController
联营请求外链后 更新或者新增到平台 (es,redis,rabmq)
*/
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/middleware"
"search_server/model"
"search_server/pkg/common"
"search_server/pkg/config"
"search_server/pkg/es"
"search_server/pkg/gredis"
"search_server/pkg/logger"
"search_server/pkg/mq"
"search_server/pkg/mysql"
"strconv"
"strings"
"fmt"
"encoding/json"
)
//定义类名
type CommonLyService struct {
......@@ -51,52 +53,110 @@ func (t *CommonLyService) LyServerRun(){
}
/*
联营公共类
联营入口
联营大类,包括修改新增 sku es redis ,推送数据到队列让其他go服务更新数据到mysql
@param goods_list 请求联营格式化后的数据
@param supplier_info 供应商商品详情
*/
func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoodsList,supplierInfo *model.SUPPLIER_REDIS_INFO_) map[string]interface{}{
func (t *CommonLyService) GetSkuByGoodsSn(goodsList map[string]*model.LyClearGoodsList,supplierInfo *model.SUPPLIER_REDIS_INFO_) bool {
t.LyServerRun(); //初始化变量
redisRead := gredis.Conn("search_r")
defer redisRead.Close()
redisWrite := gredis.Conn("search_w")
defer redisWrite.Close()
t.supplierId = supplierInfo.SupplierId
t.supplierName = supplierInfo.SupplierNickname
_,haveLimitTime := t.supplier_over_time[supplierInfo.SupplierNickname] //获取当前供应商更新周期
originGoods := make(map[string]interface{},0)
for goodsSn, info := range goodsList {
for goods_sn, info := range goodsList {
snSku := php2go.Md5(strings.ToLower(goodsSn))
skuId,_ := gredis.String(redisWrite.Do("HGET",config.Get("redis_all.SKU_UNIQUE_JUDGE").String(),snSku)) //查询唯一值,反查sku_id
snSku := php2go.Md5(strings.ToLower(goods_sn))
skuId,_ := gredis.String(redisRead.Do("HGET",config.Get("redis_all.SKU_UNIQUE_JUDGE").String(),snSku)) //查询唯一值,反查sku_id
var sku_flag bool = false; //是否新增或者更新db+redis,为true则新增
if skuId == "" { //为空,先创建sku
/**********新增sku**************/
skuFlag := false; //是否新增或者更新db+redis,为true则新增
if skuId == "" { //redis没有此sku,创建
lock_key := "searchapi_"+snSku;
flag,_ := redis.Bool(redisRead.Do("SETNX", lock_key,php2go.Time()+2))
if flag { //不存在锁,直接新增
sku_flag = true
redisRead.Do("EXP",lock_key,2) //给锁有效时间2秒
//sku_id = t.writeSkuInfo(info)
}else{ //存在锁
flag,_ := redis.Bool(redisWrite.Do("SETNX", lock_key,php2go.Time()+2))
if !flag { //防止并发
continue;
}
//新增操作
skuFlag = true //新增标志
redisWrite.Do("EXP",lock_key,2) //给锁有效时间2秒
skuId = t.writeSkuInfo(info,"") //新增redis+sku+spu+brand
redisWrite.Do("del",lock_key) //删除锁
}
info.SkuId = skuId;
//获取sku存在redis的详情
redisData,_ := redis.String(redisWrite.Do("HGET",config.Get("redis_all.SKU").String(),skuId))
if redisData == "" {
logger.Log("此sku不存在redis" + skuId,"redis_get_sku_error")
continue;
}
redisArr, ok := gjson.Parse(redisData).Value().(map[string]interface{}) //redis取值转成map
if !ok {
logger.Log("此sku不是正确json格式" + redisData,"redis_get_sku_error")
continue;
}
//获取spu详情
spuId := gjson.Get(redisData,"spu_id").String();
spuStr,_ := redis.String(redisWrite.Do("HGET",config.Get("redis_all.SPU").String(),spuId))
info.SpuId = spuId //平台spuid
info.BrandId = gjson.Get(spuStr,"brand_id").String(); //平台品牌id
if skuFlag { //如果是新增
t.createSkuDB(info) //推入队列->(斌队列消费写redis+go+mongodb)
t.esSkuInfo(info) //新增es数据
}
/**********更新sku**************/
//计算更新周期
var isUpdate int64 //是否到时间更新sku
if haveLimitTime {
isUpdate = php2go.Time()-gjson.Get(redisData,"update_time").Int()-t.supplier_over_time[supplierInfo.SupplierNickname]
}
if isUpdate > 0 && !skuFlag { //需要更新
//更新redis->sku
redisArr["moq"] = info.Moq
redisArr["stock"] = info.Stock
redisArr["mpq"] = info.Moq
redisArr["goods_status"] = 1
redisArr["is_expire"] = 0
redisArr["ladder"] = info.Tiered
redisArr["update_time"] = php2go.Time()
if supplierInfo.SupplierId == 14 {
redisArr["cp_time"] = 0
}
redisJson,_ := json.Marshal(redisArr)
redisWrite.Do("HSET",config.Get("redis_all.SKU").String(),redisJson)
//更新redis->商品映射
if supplierInfo.SupplierId == 7 {
skuMapJson,_ := json.Marshal(map[string]string{
"raw_goods_id":info.RawGoodsId, //原始goods_sn
"raw_brand_name":info.RawBrandName, //原始品牌名称
})
redisWrite.Do("HSET",config.Get("redis_all.SKU_RAW_MAP").String(),skuMapJson)
}
//更新db
t.updateSkuDB(info)
//更新es
t.esSkuInfo(info)
}
print(info)
print(sku_flag)
//productList[goodsSn] = &LyClearGoodsList
}
fmt.Println(originGoods)
return originGoods
t.pushEs(); //最后推送ES
return true
}
/*
生成或者更新 sku信息
*/
func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId string,) {
func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId string) string {
redisWrite := gredis.Conn("search_w")
defer redisWrite.Close()
......@@ -139,7 +199,7 @@ func (t *CommonLyService) writeSkuInfo(skuInfo *model.LyClearGoodsList,skuId str
if t.supplierId == 7 {
redisWrite.Do("HSET",config.Get("redis_all.SKU_RAW_MAP").String(),skuKey,skuId)
}
return skuId
}
/*
......@@ -217,7 +277,7 @@ func (t *CommonLyService) writeBrandInfo(skuInfo *model.LyClearGoodsList) int64
/*
组装推送到es数据
*/
func (t * CommonLyService) EsSkuInfo(skuInfo *model.LyClearGoodsList,spuId * string,brandId *string) {
func (t * CommonLyService) esSkuInfo(skuInfo *model.LyClearGoodsList) {
sort := 1; //排序
if skuInfo.SinglePrice >0 && skuInfo.Stock >0 && skuInfo.Stock >= skuInfo.Increment && skuInfo.Stock >= skuInfo.Moq {
sort = 31;
......@@ -240,16 +300,16 @@ func (t * CommonLyService) EsSkuInfo(skuInfo *model.LyClearGoodsList,spuId * str
nowTime := php2go.Time()
param := map[string]interface{}{
"goods_id":skuInfo.GoodsId,
"goods_id":skuInfo.SkuId,
"goods_name":skuInfo.GoodsName,
"auto_goods_name":php2go.Strtoupper(skuInfo.GoodsName),
"brand_id":*brandId,
"brand_id":skuInfo.BrandId,
"brand_name":skuInfo.BrandName,
"supplier_name":t.supplierName,
"class_id1":0,
"class_id2":0,
"class_id3":0,
"spu_id":*spuId,
"spu_id":skuInfo.SpuId,
"sale_time":nowTime,
"create_time":nowTime,
"update_time":nowTime,
......@@ -266,7 +326,7 @@ func (t * CommonLyService) EsSkuInfo(skuInfo *model.LyClearGoodsList,spuId * str
}
paramjson,_ := json.Marshal(param)
t.skuEsUpdataList = append(t.skuEsUpdataList,`{"index":{"_index":"`+t.supplierName+`","_type":"goods","_id":"`+skuInfo.GoodsId+`"}}`)
t.skuEsUpdataList = append(t.skuEsUpdataList,`{"index":{"_index":"`+t.supplierName+`","_type":"goods","_id":"`+skuInfo.SkuId+`"}}`)
t.skuEsUpdataList = append(t.skuEsUpdataList,string(paramjson))
}
......@@ -275,7 +335,7 @@ func (t * CommonLyService) EsSkuInfo(skuInfo *model.LyClearGoodsList,spuId * str
推入队列=》(斌 go队列去消费更新db+mongodb..等操作)
*/
func (t * CommonLyService) updateSkuDB(skuInfo *model.LyClearGoodsList) {
if skuInfo.GoodsId == "" {
if skuInfo.SkuId == "" {
return
}
param := map[string]interface{}{
......@@ -283,7 +343,7 @@ func (t * CommonLyService) updateSkuDB(skuInfo *model.LyClearGoodsList) {
"stock":skuInfo.Stock,
"tiered":skuInfo.PriceTemp,
"increment":skuInfo.Increment,
"sku_id":skuInfo.GoodsId,
"sku_id":skuInfo.SkuId,
"brand_name":skuInfo.BrandName,
"goods_name":skuInfo.GoodsName,
"goods_sn":skuInfo.GoodsSn,
......@@ -317,3 +377,44 @@ func (t *CommonLyService) pushEs() {
t.flagStr += result
}
}
/*
新增sku,组装好数据推送到db(斌哥队列消费后 更新 mongodb+mysql 等)
*/
func (t * CommonLyService) createSkuDB(skuInfo *model.LyClearGoodsList) {
if skuInfo.SkuId == "" {
return
}
param := map[string]interface{}{
"desc":skuInfo.Desc,
"docurl":skuInfo.Docurl,
"goods_img":skuInfo.GoodsImg,
"url":skuInfo.Url,
"spu_id":skuInfo.SpuId,
"brand_id":skuInfo.BrandId,
"batch_sn":skuInfo.BatchSn,
"canal":skuInfo.Canal,
"encoded":skuInfo.Encoded,
"encap":skuInfo.Encap,
"pn":t.supplierName,
"stock":skuInfo.Stock,
"tiered":skuInfo.PriceTemp,
"increment":skuInfo.Increment,
"sku_id":skuInfo.SkuId,
"brand_name":skuInfo.BrandName,
"goods_name":skuInfo.GoodsName,
"goods_sn":skuInfo.GoodsSn,
}
paramjson,_ := json.Marshal(param)
paramStr := string(paramjson)
if t.flag == "101" {
t.flagStr = "<br/><br/>";
t.flagStr = "推送到go新增db:";
t.flagStr = paramStr;
t.flagStr = "<br/><br/>";
}
mq.PushMsg(config.Get("rabmq_all.SEARCH_SKU_LIST").String(),paramStr) //推送队列
//记录日志
logger.Log(paramStr,"sku_create")
}
package service
package ly
import (
"github.com/imroc/req"
......@@ -12,7 +12,7 @@ import (
const Mouser_api_url string = "https://api.mouser.com/api/v1/search/partnumber?apiKey=0a11fa6f-ddcb-4ddf-9947-e42b2f3b4723"
//mouser请求外链
func OutLink(goodsName *string,flag string) map[string]*model.LyClearGoodsList {
func OutLinkMouser(goodsName *string,flag string) map[string]*model.LyClearGoodsList {
var result string;
if flag == "-1" { //调试
......
......@@ -7,7 +7,7 @@ import (
"github.com/syyongx/php2go"
"reflect"
"search_server/boot"
"search_server/controller"
"search_server/pkg/common"
)
func StrRandom(lenNum int) string {
......@@ -47,8 +47,10 @@ func main() {
//fmt.Println(str)
//log.Error("sadsadsadsa")
//log.Error("sadsadsadsa")
//log_report.WriteSearchErrorLog("sdasdasdas")
common.Output(1000, "ceshiceshi", nil)
//log.Error("sadsadsadsa")
controller.GetData("LM358","arrow")
//controller.GetData("LM358","arrow")
}
......
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