Commit a9715920 by wang

周五暂存

parent e570ea22
......@@ -7,6 +7,7 @@ import (
"go_sku_server/pkg/common"
"go_sku_server/pkg/config"
"go_sku_server/pkg/e"
"go_sku_server/pkg/logger"
"net/http"
)
......@@ -49,19 +50,35 @@ func Cors_Middleware() gin_.Middleware {
@author wangsong
捕获 系统内部 panic 异常,并加入日志
*/
func Error_Middleware() gin.HandlerFunc {
func Error_Middleware(logPaths ...string) gin.HandlerFunc {
logPath:="zysku_save"
if(len(logPaths)>0){
logPath=logPaths[0]
}
return func(ctx *gin.Context) {
defer func() {
if err:=recover(); err!=nil{
if apiError,ok:=err.(*e.ApiError);ok{
fmt.Println(apiError.ErrMsg)
common.NResponse(apiError.ErrMsg,apiError.Code).SetLogHandel(saveLogHandle).OutPut(ctx)
common.NResponse(apiError.ErrMsg,apiError.Code).SetLogHandel(errlogHandle(logPath)).OutPut(ctx)
}else{
errMsg:=fmt.Sprintf("%s",err)
common.NResponse(errMsg,500).SetLogHandel(saveLogHandle).OutPut(ctx)
common.NResponse(errMsg,500).SetLogHandel(errlogHandle(logPath)).OutPut(ctx)
}
}
}()
ctx.Next()
}
}
/**
错误日志处理,是给 NResponse的SetLogHandel 方法使用(其他同事用不到NResponse的SetLogHandel可以不用管它)
@param logPath 对应 log.ini的配置文件 比如 lysku_save
@return func
*/
func errlogHandle(logPath string) func(msg string){
return func(msg string) {
logger.Select(logPath).Error(msg)
}
}
\ No newline at end of file
......@@ -8,7 +8,6 @@ import (
"go_sku_server/model/saveModel"
"go_sku_server/pkg/common"
"go_sku_server/pkg/e"
"go_sku_server/pkg/logger"
"go_sku_server/service"
)
......@@ -45,7 +44,7 @@ func SaveSku(ctx *gin.Context) {
//参数验证
var lySaveRequest saveModel.LySaveRequest
if err := ctx.ShouldBindBodyWith(&lySaveRequest,binding.JSON); err != nil {
common.NResponse(err.Error()).SetLogHandel(saveLogHandle).OutPut(ctx)
common.NResponse(err.Error()).SetLogHandel(errlogHandle("lysku_save")).OutPut(ctx)
return
}
//执行skuSave
......@@ -54,10 +53,10 @@ func SaveSku(ctx *gin.Context) {
//错误处理
if(serviceErr!=nil){
if err,ok:=serviceErr.(*e.ApiError);ok{
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(errlogHandle("lysku_save")).OpenParamLog().OutPut(ctx)
return
}else{
common.NResponse(serviceErr.Error()).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
common.NResponse(serviceErr.Error()).SetLogHandel(errlogHandle("lysku_save")).OpenParamLog().OutPut(ctx)
}
}else {//执行成功
res:=saveModel.LySaveResponse{0,"ok",gconv.Int64(skuId)}
......@@ -72,7 +71,7 @@ func SaveZySku(ctx *gin.Context) {
//参数验证
var zySaveRequest saveModel.ZySaveRequest
if err := ctx.ShouldBindBodyWith(&zySaveRequest,binding.JSON); err != nil {
common.NResponse(err.Error()).SetLogHandel(zySaveLogHandle).OutPut(ctx)
common.NResponse(err.Error()).SetLogHandel(errlogHandle("zysku_save")).OutPut(ctx)
return
}
zySave:=service.ZySaveService{}
......@@ -80,8 +79,10 @@ func SaveZySku(ctx *gin.Context) {
//错误处理
if(serviceErr!=nil){
if err,ok:=serviceErr.(*e.ApiError);ok{
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(zySaveLogHandle).OpenParamLog().OutPut(ctx)
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(errlogHandle("zysku_save")).OpenParamLog().OutPut(ctx)
return
}else{
common.NResponse(serviceErr.Error()).SetLogHandel(errlogHandle("zysku_save")).OpenParamLog().OutPut(ctx)
}
}
common.NResponse().OutPut(ctx)
......@@ -94,7 +95,7 @@ func SkuEdit(ctx *gin.Context) {
//参数验证
var lyeditRequest saveModel.LyEditRequest
if err := ctx.ShouldBindBodyWith(&lyeditRequest,binding.JSON); err != nil {
common.NResponse(err.Error()).SetLogHandel(editLogHandle).OutPut(ctx)
common.NResponse(err.Error()).SetLogHandel(errlogHandle("lysku_save")).OutPut(ctx)
return
}
//执行skuSave
......@@ -103,9 +104,9 @@ func SkuEdit(ctx *gin.Context) {
//错误处理
if(serviceErr!=nil){
if err,ok:=serviceErr.(*e.ApiError);ok{
common.NResponse(err.Error()).SetLogHandel(editLogHandle).OutPut(ctx)
common.NResponse(err.Error()).SetLogHandel(errlogHandle("lysku_save")).OutPut(ctx)
}else{
common.NResponse(err.Error()).SetLogHandel(editLogHandle).OutPut(ctx)
common.NResponse(err.Error()).SetLogHandel(errlogHandle("lysku_save")).OutPut(ctx)
}
}else {//执行成功
......@@ -115,14 +116,4 @@ func SkuEdit(ctx *gin.Context) {
return
}
func saveLogHandle(errMsg string) {
logger.Select("lysku_save").Error(errMsg)
}
func zySaveLogHandle(errMsg string) {
logger.Select("zysku_save").Error(errMsg)
}
func editLogHandle(errMsg string) {
logger.Select("lysku_save").Error(errMsg)
}
......@@ -44,12 +44,12 @@ func GetZyGoodSInfoByCollect(supplierId int ,selfSupplierType int,selfSupplierId
@param self_supplier_type 自采标记
@param brandId 品牌Id
*/
func GetSimilarGoods(selfSupplierType int,brandId int64) (zySkuEntity []model.ZySkuEntity,err error) {
func GetSimilarGoods(selfSupplierType int,brandId int64,goodsName string) (zySkuEntity []model.ZySkuEntity,err error) {
dbSpu:= mysql.Conn("liexin_data") //spu实例化链接
zySkuEntityS := make([]model.ZySkuEntity, 0)
whereStr:="self_supplier_type =? and self_supplier_id ='' and brand_id=?"
err= dbSpu.Table("lie_goods").Where(whereStr,selfSupplierType,brandId).Find(&zySkuEntityS)
whereStr:="goods_name =? and self_supplier_type =? and self_supplier_id ='' and brand_id=?"
err= dbSpu.Table("lie_goods").Where(whereStr,goodsName,selfSupplierType,brandId).Find(&zySkuEntityS)
if(err!=nil){
return zySkuEntityS,fmt.Errorf("method:GetSimilarGoods,"+err.Error())
}
......@@ -90,4 +90,15 @@ func InsertDbGoods( zySkuEntity model.ZySkuEntity) error {
return nil
}
//推送ES队列
func PushGoodsQueue(goodsId int) error{
redisWriteConn := gredis.Conn("search_w")
defer redisWriteConn.Close()
_,err:=redisWriteConn.Do("Rpush","Self_PushQueue",goodsId)
if(err!=nil){
return fmt.Errorf("method:PushGoodsQueue,压入redis队列失败"+err.Error())
}
return nil
}
......@@ -17,7 +17,17 @@ func GetRedisUnit(key int) (err error,packingCn string){
return fmt.Errorf("method:GetRedisUnit"+err.Error()),packingCn
}
return
}
//
func GetDBUnit() {
}
//插入redis Unit
func HmsetRedisUnit() {
redisReadConn := gredis.Conn("search_r")
defer redisReadConn.Close()
//err:=redis.String(redisReadConn.Do("HMSET","Self_Unit",key))
}
\ No newline at end of file
......@@ -36,10 +36,10 @@ type ZySkuEntity struct {
Pdf string `json:"pdf" form:"pdf" ` //PDF地址
GoodsImages string `json:"goods_images" form:"goods_images" `
GoodsUnit int `json:"goods_unit" form:"goods_unit" validate:"required_with=IsAdd"`//SKU单位名称
Packing int `json:"packing" form:"packing" validate:"required_with=IsAdd"`//包装/方式
Packing int `json:"packing" form:"packing" validate:"required_with=IsAdd"`//包装/方式
Encap string `json:"encap" form:"encap" `//封装/规格
Status int `json:"status" `
Encoded string `json:"encoded" `//内部编码
Encoded string `json:"encoded" `//内部编码
GoodsName string `json:"goods_name" form:"goods_name" `
BrandId int `json:"brand_id" form:"brand_id" `// 品牌ID
SupplierId int `json:"supplier_id" form:"supplier_id" `//供应商ID
......
......@@ -13,6 +13,9 @@ import (
1 默认的输出格式
common.NResponse().OutPut(ctx) //输出正确
common.NResponse("错了",100).OutPut(ctx) //输出错误
common.NResponse(err.Error()).SetLogHandel(saveLogHandle).OutPut(ctx)//输出错误 并且记录错误
common.NResponse(err.Error()).SetLogHandel(saveLogHandle)..OpenParamLog().OutPut(ctx)//输出错误 并且记录错误,并且记录请求参数
2.输出 自定义 struct struct 必须要继承 NewOutPut 接口
common.NResponse(&struct).OutPut(ctx)
......@@ -118,6 +121,7 @@ func (NR *NewResponse) OpenLog() *NewResponse{
}
func (NR *NewResponse) SetLogHandel(a func(string)) *NewResponse {
NR.openLog=true
NR.loghandleFunc=a
return NR
}
......
......@@ -24,14 +24,10 @@ func InitRouter() *gin.Engine {
r.POST("hbsdata",controller.Hbsdata)
//联营SaveSku
r.POST("SaveSku",controller.Error_Middleware(),controller.SaveSku)
r.POST("SkuEdit",controller.Error_Middleware(),controller.SkuEdit)
r.POST("SaveSku",controller.Error_Middleware("lysku_save"),controller.SaveSku)
r.POST("SkuEdit",controller.Error_Middleware("lysku_save"),controller.SkuEdit)
//自营
r.POST("SaveZySku",controller.Error_Middleware(),controller.SaveZySku)
r.POST("SaveZySku",controller.Error_Middleware("zysku_save"),controller.SaveZySku)
return r
}
......@@ -5,7 +5,6 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/gogf/gf/util/gconv"
"github.com/tidwall/gjson"
"go_sku_server/dao"
"go_sku_server/model"
......@@ -29,52 +28,107 @@ func (S *ZySaveService)SaveZySku(zySaveRequest saveModel.ZySaveRequest,ctx *gin.
zySkuEntity,err:=dao.GetZyGoodSInfoByCollect(zySaveRequest.SupplierId,zySaveRequest.SelfSupplierType,zySaveRequest.SelfSupplierId)
e.CheckError(err)
if(zySkuEntity.GoodsId==0){
//相似商品
e.CheckError(S.brandHandle(&zySkuEntity,&zySaveRequest))//查找相似商品必须要先获取到brandID
SimilarGoods,err:=dao.GetSimilarGoods(zySaveRequest.SelfSupplierType,gconv.Int64(zySkuEntity.BrandId))
//相似商品处理
affected,err:=S.similarGoodsHandle(zySaveRequest)
e.CheckError(err)
if len(SimilarGoods)>0{//
return S.updateSimilarGoods(SimilarGoods,zySaveRequest)//修改相似商品
if(affected>0){
return nil
}else{
//没有相似商品要改 新增商品
e.CheckError(S.addGoods(zySkuEntity,zySaveRequest))
}
S.addGoods(zySkuEntity,zySaveRequest)//新增商品
}else{//修改
S.updateGoods(zySkuEntity,zySaveRequest)//修改此商品
}else{
//修改此商品
e.CheckError(S.updateGoods(zySkuEntity,zySaveRequest))
}
return nil
}
/**
修改相似商品
相似商品处理
*/
func (S *ZySaveService) updateSimilarGoods(SimilarGoods []model.ZySkuEntity,zySaveRequest saveModel.ZySaveRequest) error{
for _,zySkuEntity:= range SimilarGoods{
fmt.Printf("修改相似商品:%d",zySkuEntity.GoodsId)
err:=S.updateGoods(zySkuEntity,zySaveRequest)
if(err!=nil){
return err
func (S *ZySaveService)similarGoodsHandle(zySaveRequest saveModel.ZySaveRequest) (affected int,err error) {
//查询相似商品,有两个额外字段为必填,不多就手动验证
if(zySaveRequest.SupplierBrandId==0){
return 0,e.NewApiError("品牌ID不存在,无法新增#brand_id"+err.Error(),108004)
}
if(zySaveRequest.GoodsName==""){
return 0,e.NewApiError("型号不存在"+err.Error(),108005)
}
err,brandId:=S.getBrandId(&zySaveRequest)
if(err!=nil){
return 0,err
}
SimilarGoods,err:=dao.GetSimilarGoods(zySaveRequest.SelfSupplierType,brandId,zySaveRequest.GoodsName)
if(err!=nil){
return 0,e.NewApiError("获取相似商品报错 msg:"+err.Error(),108005)
}
if len(SimilarGoods)>0{//如果有相似商品
for _,zySkuEntity:= range SimilarGoods{
fmt.Printf("修改相似商品:%d",zySkuEntity.GoodsId)
err:=S.updateGoods(zySkuEntity,zySaveRequest)
if(err!=nil){
return 0,err
}
}
return len(SimilarGoods),err
}
return nil
return 0,nil
}
/**
新增商品
*/
func (S *ZySaveService)addGoods(zySkuEntity model.ZySkuEntity,zySaveRequest saveModel.ZySaveRequest) error{
//新增商品字段验证
err:=S.addDataAlidator(zySaveRequest)
if(err!=nil){
return err
}
S.GoodsDataHandle(&zySkuEntity,&zySaveRequest)
zySkuEntity.GoodsUnit=1
//新增到数据库
err=dao.InsertDbGoods(zySkuEntity)
if(err!=nil){
return e.NewApiError("新增sku 失败, msg:"+err.Error(),508002)
}
err=dao.InsertRedisGoods(zySkuEntity)
//mysql操作之后的处理 存redis 推es 推 金谷(mq)
err=S.afterUpdateHandle(zySkuEntity)
if(err!=nil){
return e.NewApiError("新增sku 失败, msg:"+err.Error(),508002)
return e.NewApiError("新增sku, msg:"+err.Error(),508002)
}
return nil
}
/**
新增修改到mysql之后的操作
1.存redis
2.推送es(redis)
3.推送金谷(mq)
*/
func (S *ZySaveService) afterUpdateHandle(zySkuEntity model.ZySkuEntity) error{
//新增到redis
err:=dao.InsertRedisGoods(zySkuEntity)
if(err!=nil){
return e.NewApiError("sku存redis Self_SelfGoods 失败, msg:"+err.Error(),508002)
}
//推送到es
err=dao.PushGoodsQueue(zySkuEntity.GoodsId)
if(err!=nil){
return e.NewApiError("推送到ES redis 失败, msg:"+err.Error(),508002)
}
err=S.pushWms(zySkuEntity)
if(err!=nil){
return e.NewApiError("推送到wms 失败, msg:"+err.Error(),508002)
}
return nil
}
......@@ -93,20 +147,30 @@ func (S *ZySaveService)updateGoods(zySkuEntity model.ZySkuEntity,zySaveRequest s
logger.Select("zysku_save").Info("不需要修改")
return nil
}else{
//存redis
err:=dao.InsertRedisGoods(zySkuEntity)
err:=S.afterUpdateHandle(zySkuEntity)
if(err!=nil){
return e.NewApiError("修改sku redis数据失败 msg:"+err.Error(),508001)
}
//存历史价格
S.historyPriceHandle(zySkuEntity.GoodsId,zySaveRequest.LadderPrice)
if(err!=nil){
return e.NewApiError("修改sku 历史价格出错 msg:"+err.Error(),508001)
return e.NewApiError("新增sku, msg:"+err.Error(),508002)
}
}
return nil
}
func (S *ZySaveService) pushWms(zySkuEntity model.ZySkuEntity) error {
info:=make(map[string]interface{})
info["goods_id"]=zySkuEntity.GoodsId
zyBrandService:=ZyBrandService{}
err,brandName:=zyBrandService.GetBrandName(zySkuEntity.BrandId)
if(err!=nil){
return nil
}
info["brand_name"]=brandName
info["class_id2"]=zySkuEntity.ClassId2
return nil
}
/**
商品信息处理,将需要修改的数据赋值到 zySkuEntity
......@@ -119,7 +183,9 @@ func (S *ZySaveService) GoodsDataHandle(zySkuEntity *model.ZySkuEntity,zySaveReq
zySkuEntity.IsPerfect=2
common.CopyStruct(zySaveRequest,zySkuEntity)
//品牌处理
e.CheckError(S.brandHandle(zySkuEntity,zySaveRequest))
err,brandId:=S.getBrandId(zySaveRequest)
e.CheckError(err)
zySkuEntity.BrandId=int(brandId)
//分类处理
e.CheckError(S.classHandle(zySkuEntity,zySaveRequest))
//包装处理
......@@ -145,21 +211,26 @@ func (S *ZySaveService) GoodsDataHandle(zySkuEntity *model.ZySkuEntity,zySaveReq
/**
品牌处理
*/
func (S *ZySaveService) brandHandle(zySkuEntity *model.ZySkuEntity, zySaveRequest *saveModel.ZySaveRequest) error {
func (S *ZySaveService) getBrandId(zySaveRequest *saveModel.ZySaveRequest) (error,int64) {
if(zySaveRequest.SupplierBrandId!=0){
err,brandId:=dao.GetBrandMappingId(zySaveRequest.SupplierId,zySaveRequest.SelfSupplierType,zySaveRequest.SupplierBrandId)
e.CheckError(err)
if(err!=nil){
return err,0
}
if(brandId==0){
return e.NewApiError("品牌映射不存在", 108002)
return e.NewApiError("品牌映射不存在", 108002),0
}
zySkuEntity.BrandId=int(brandId)
return nil,brandId
}
return nil
return nil,0
}
/**
分类处理
分类处理 5000
yh 1 2000
zj 1 1500
zj 2 1500
*/
func (S *ZySaveService) classHandle(zySkuEntity *model.ZySkuEntity, zySaveRequest *saveModel.ZySaveRequest) error {
......@@ -190,7 +261,7 @@ func (S *ZySaveService) classHandle(zySkuEntity *model.ZySkuEntity, zySaveReques
return nil
}
//包装处理
//包装处理王阶
func (S *ZySaveService) packHandle(zySkuEntity *model.ZySkuEntity, zySaveRequest *saveModel.ZySaveRequest) error {
packRule:=Rule.PackRule
......@@ -199,7 +270,7 @@ func (S *ZySaveService) packHandle(zySkuEntity *model.ZySkuEntity, zySaveRequest
packing:=packRule.Get(packName).Packing
pickType:=packRule.Get(packName).PickType
if(packing==0 || pickType==0){
return e.NewApiError("包装方式不存在#packing", 508001)
return e.NewApiError("包装方式不存在#packing:"+zySaveRequest.Packing, 508001)
}
zySkuEntity.Packing=packing
zySkuEntity.PickType=pickType
......@@ -226,7 +297,7 @@ func (S *ZySaveService) goodsUnitHandle(zySkuEntity *model.ZySkuEntity, zySaveR
if(goodsUnit!=""){
goodsUnit:=goodsUnitRule.Get(goodsUnit)
if(goodsUnit==0 ){
return e.NewApiError("商品单位不存在#goods_unit", 508001)
return e.NewApiError("商品单位不存在#goods_unit:"+zySaveRequest.GoodsUnit, 508001)
}
zySkuEntity.GoodsUnit=goodsUnit
}
......@@ -251,8 +322,8 @@ func (S *ZySaveService) statusHandle(zySkuEntity *model.ZySkuEntity, zySaveRequ
}
/**
价格库存处理
1 阶梯价处理
2.库存处理
1 阶梯价处理
2.库存处理
*/
func (S *ZySaveService) priceStockHandle (zySkuEntity *model.ZySkuEntity,zySaveRequest *saveModel.ZySaveRequest)error{
//阶梯价处理
......@@ -415,7 +486,10 @@ func (S *ZySaveService)md5GoodsUnique(zySkuEntity *model.ZySkuEntity, Z *saveMod
return nil
}
//新增sku数据验证,ladder_price 和 cost必填一个,有些字段必填
/**
新增sku数据验证,ladder_price 和 cost必填一个
有些字段必填
*/
func (S *ZySaveService)addDataAlidator(Z saveModel.ZySaveRequest) error{
Z.IsAdd=true
validate:= validator.New()
......@@ -434,8 +508,8 @@ func (S *ZySaveService)addDataAlidator(Z saveModel.ZySaveRequest) error{
return nil
}
//历史价格处理 参照 项目 self_sku HistoryPriceModel.php 的 AddHistoryPrice(感觉是有问题,暂时按照PHP的业务来)
//
func (S *ZySaveService) historyPriceHandle(goodsId int,LadderPrice []model.LadderPrice) error{
if(len(LadderPrice)>0){
sort.Sort(LadderPriceSorter(LadderPrice))
......
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