Commit bfdb8c21 by wang

验证和查询商品的查询

parent 542e0f5b
......@@ -17,4 +17,5 @@ cmd.exe~
/vm.sh
/cmd/http
/doc/spuTest
/doc/test3
......@@ -53,22 +53,43 @@ func SaveSku(ctx *gin.Context) {
serviceErr,skuId:=LySave.SaveSku(lySaveRequest,ctx)
//错误处理
if(serviceErr!=nil){
code:=10001
errMsg:=serviceErr.Error()
if err,ok:=serviceErr.(*e.ApiError);ok{
code=err.Code
errMsg=err.ErrMsg
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
return
}else{
common.NResponse(serviceErr.Error()).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
}
common.NResponse(errMsg,code).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
}else {//执行成功
res:=saveModel.LySaveResponse{0,"ok",gconv.Int64(skuId)}
fmt.Print("执行成功")
common.NResponse(&res).OutPut(ctx)
}
}
func SaveZySku(ctx *gin.Context) {
//参数验证
var zySaveRequest saveModel.ZySaveRequest
if err := ctx.ShouldBindBodyWith(&zySaveRequest,binding.JSON); err != nil {
common.NResponse(err.Error()).SetLogHandel(saveLogHandle).OutPut(ctx)
return
}
zySave:=service.ZySaveService{}
serviceErr:=zySave.SaveZySku(zySaveRequest,ctx)
//错误处理
if(serviceErr!=nil){
if err,ok:=serviceErr.(*e.ApiError);ok{
common.NResponse(err.ErrMsg,err.Code).SetLogHandel(saveLogHandle).OpenParamLog().OutPut(ctx)
return
}
}
common.NResponse().OutPut(ctx)
return
}
//修改(暂时废弃,大数据那边没调用)
//修改(暂时废弃,爬虫那边没调用)
func SkuEdit(ctx *gin.Context) {
//参数验证
......
......@@ -6,6 +6,7 @@ require (
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd // indirect
github.com/gin-gonic/gin v1.6.3
github.com/go-ini/ini v1.57.0
github.com/go-playground/validator/v10 v10.2.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-xorm/xorm v0.7.9
github.com/gogf/gf v1.14.5
......
package saveModel
import (
"fmt"
"github.com/go-playground/validator/v10"
"math"
)
/**
return [
'cn'=>[
-1 => '系统错误',
0 => 'ok',
108001 => '缺少参数',
108002 => '未建立品牌的映射关系',
108003 => '存在相同的型号,无法新增',
108004 => '品牌ID不存在,无法新增#brand_id',
108005 => '型号不存在,无法新增#goods_name',
508001 => '更新商品信息失败',
508002 => '新增商品失败'
]
];
/**
Array
(
[other_attrs] =>
[goods_id] => 187042
[goods_type] => 0
[supplier_id] => 10000
[brand_id] => 4492
[class_id1] => 10177
[class_id2] => 10192
[goods_name] => C0201C0G2R0C500NTA1
[status] =>
[encoded] => 10142
[encap] => 0201(0603)
[packing] => 5
[goods_unit] => 1
[moq] => 50
[mpq] => 15000
[ladder_price] => [{"purchases":50,"price_cn":"0.0048"},{"purchases":1000,"price_cn":"0.0048"},{"purchases":15000,"price_cn":"0.0048"}]
[update_time] => 1608621241
[sku_name] => C0201C0G2R0C500NTA1 编带 涤纶电容
[mpl] =>
[supplier_stock] => 0
[self_supplier_type] => 4
[self_supplier_id] => 04SUP000001
[barcode] => C0201C0G2R0C500NTA1
[pick_type] => 1
[is_perfect] => 2
[unique_md5] => a2063d3a5551b87efd95fdcacd98d01a
)
*/
/**
@author wangsong
新增自营sku请求参数结构
*/
/*type ZySaveRequest struct {
R1 string `json:"r1" form:"r1" binding:"required"`
R2 string `json:"r2" form:"r2" binding:"required"`
GoodsId int //一个标志,判断是否是新增
AddFiled1 string `json:"add_filed1" form:"add_filed1" validate:"required_with=GoodsId"`
AddFiled2 string `json:"add_filed2" form:"add_filed2" validate:"required_with=GoodsId"`
}*/
/**
验证错误返回格式
*/
var ZyCodes =ZySaveErrcodeCn{}
type ZySaveRequest struct {
SupplierId int `json:"supplier_id" form:"supplier_id" binding:"required"`//供应商ID
SelfSupplierType int `json:"self_supplier_type" form:"self_supplier_type" binding:"required"`//自采标记:寄售填1,京北3,立创2,油柑4(商品类别是猎芯自采)
SelfSupplierId string `json:"self_supplier_id" form:"self_supplier_id" binding:"required"`
GoodsId int `json:"goods_id" form:"goods_id"`
IsAdd bool //是否新增,默认为false
SupplierBrandId int `json:"supplier_brand_id" form:"supplier_brand_id" binding:"required_with=IsAdd"`// 新增时必填,必须要是建立了映射关系的品牌ID
GoodsName string `json:"goods_name" form:"goods_name" validate:"required_with=IsAdd"`//新增时必填
Encap string `json:"encap" form:"encap" validate:"required_with=IsAdd"`//新增时必填
Packing string `json:"packing" form:"packing" validate:"required_with=IsAdd"`//新增时必填
GoodsUnit string `json:"goods_unit" form:"goods_unit" validate:"required_with=IsAdd"`//新增时必填
Moq int `json:"moq" form:"moq" validate:"required_with=IsAdd"`//新增时必填
Mpq int `json:"mpq" form:"mpq" validate:"required_with=IsAdd"`//新增时必填
SupplierClassId int `json:"supplier_class_id" form:"supplier_class_id" validate:"required_with=IsAdd"`//新增时必填,供应商分类ID,必须是已经做了映射的ID
Cost float64 `json:"cost" form:"cost" `//成本(用于生成阶梯价的价钱),新增时,与ladder_price必填一个,取决于商品价格生成规则
LadderPrice []LadderPrice `json:"ladder_price" form:"ladder_price" `
Mpl int `json:"mpl" form:"mpl" `//倍数
Barcode string `json:"barcode" form:"barcode" `//原厂条码,新增时默认goods_name
SupplierStock string `json:"supplier_stock" form:"supplier_stock" `//供应商库存
OtherAttrs interface{} `json:"other_attrs" form:"other_attrs" `//其他参数,长度(cm暂时未用到)和重量(kg)
}
type ZySaveErrcodeCn map[int]string
/**
新增自营错误状态码(原php项目有这个),后续放到配置里面去
*/
func (ZySaveErrcodeCn)GetCnErr(codeCn int) string {
var staticErrcodePoll = map[int]string{
108001 : "缺少参数",
108002 : "未建立品牌的映射关系",
108003 : "存在相同的型号,无法新增",
108004 : "品牌ID不存在,无法新增#brand_id",
108005 : "型号不存在,无法新增#goods_name",
508001 : "更新商品信息失败",
508002 : "新增商品失败",
}
if errCns, ok := staticErrcodePoll[codeCn];ok{
return errCns
}
return ""
}
//自营sku梯度
type LadderPrice struct {
Purchases int64 `json:"purchases"` //购买数量
PriceCn float64 `json:"price_cn"` //数量对应的中文价格
}
//新增sku数据验证,ladder_price 和 cost必填一个,有些字段必填
func (Z *ZySaveRequest)AddDataAlidator() error{
validate:= validator.New()
if(Z.LadderPrice==nil && math.Float64bits(Z.Cost) == 0){
return fmt.Errorf("新增sku时,LadderPrice 和 Cost必填一个")
}
err:= validate.Struct(Z)
if(err!=nil){
if fieldError,ok:=err.(validator.ValidationErrors);ok{
if(fieldError[0].Tag()=="required_with"){
return fmt.Errorf("新增sku缺少必填字段:"+fieldError[0].Field())
}
}
}
return nil
}
/*func (L * LySaveRequest)GetAttrsJson() map[string]interface{} {
if AttrsMap,ok:=L.Attrs.(map[string]interface{});ok{
if(len(AttrsMap)>0){
return AttrsMap
}
}
return nil
}
func (L * LySaveRequest)GetLadderPrice() string {
json_str:="";
if AttrsMap,ok:=L.LadderPrice.([]interface{});ok{
if(len(AttrsMap)>0){
s, _ :=json.Marshal(L.LadderPrice)
json_str=fmt.Sprintf("%s",s)
}
}
return json_str
}*/
package model
//@author wangsong
type ZySkuEntity struct {
//tod……
SupplierId int `json:"supplier_id"`
}
......@@ -9,16 +9,16 @@ import (
@author wangsong
输出工具,输出json 和输出日志(链式调用)
一、输出json
1 输出json支持自定义传struct 和默认的输出格式
common.NResponse(err.Error()).OutPut(ctx)
1 默认的输出格式
common.NResponse().OutPut(ctx) //输出正确
common.NResponse("错了",100).SetLogFilePre("sku").OutPut(ctx) //输出错误,并存日志到文件 ******_sku.txt
2.输出 自定义 struct
common.NResponse(&struct).SetLogFilePre("sku").OutPut(ctx)
二、单独输出日志
common.NResponse().SetLogContent("进来了").OutPutLog()
尚未完善的地方
1.输出日志渠道 文件,数据库,oss之类
2.可以多次设置输出对象的 code 和 errMsg,目前只有第一次设置
common.NResponse("错了",100).OutPut(ctx) //输出错误
2.输出 自定义 struct struct 必须要继承 NewOutPut 接口
common.NResponse(&struct).OutPut(ctx)
二、输出日志(如果不是要记录传入进来的参数,不需要用这个)
SetLogHandel 回调函数传错误字符串
common.NResponse(err.Error()).SetLogHandel(saveLogHandle).OutPut(ctx)
*/
//接口
type NewOutPut interface {
......@@ -26,8 +26,6 @@ type NewOutPut interface {
GetErrorMsg() string
}
//输出类
type NewResponse struct {
logTool
......@@ -133,7 +131,7 @@ func (NR *NewResponse) OutPut(ctx *gin.Context) {
if(reflect.ValueOf(NR.loghandleFunc).IsZero()!=true){
NR.loghandleFunc(NR.getLogErrorStr())//日志处理
}
if(reflect.ValueOf(NR.outPutData).IsZero()==true){//没有输出对象,就用默认的
if(NR.outPutData==nil){//没有输出对象,就用默认的
NR.outPutData=&defaultOutPutDate{0,"ok",nil}
}
NR.ctx.JSONP(200,NR.outPutData)
......
......@@ -23,9 +23,15 @@ func InitRouter() *gin.Engine {
r.GET("hbsdata", controller.Hbsdata)
r.POST("hbsdata",controller.Hbsdata)
//营SaveSku
//营SaveSku
r.POST("SaveSku",controller.Error_Middleware(),controller.SaveSku)
r.POST("SkuEdit",controller.Error_Middleware(),controller.SkuEdit)
//自营
r.POST("SaveZySku",controller.Error_Middleware(),controller.SaveZySku)
return r
}
......@@ -49,6 +49,7 @@ var skuCache SkuCacheService
func (S *LySaveService)SaveSku(lySaveRequest saveModel.LySaveRequest,ctx *gin.Context) (error,string) {
lySkuEntity:=lySaveRequest.ToLySkuEntity()//sku实体
//供应商处理(处理后,将[新增/修改sku]所需供应商相关字段 赋值给 &lySkuEntity)
err:=S.supplierHandle(lySaveRequest.SupplierId,lySkuEntity.Canal,&lySkuEntity)
if(err!=nil){
......
package service
import (
"github.com/gin-gonic/gin"
"go_sku_server/model"
"go_sku_server/model/saveModel"
"go_sku_server/pkg/e"
)
type ZySaveService struct {
}
func (S *ZySaveService)SaveZySku(zySaveRequest saveModel.ZySaveRequest,ctx *gin.Context) (error) {
return nil
zySaveRequest.IsAdd=true
err:=zySaveRequest.AddDataAlidator()
if err != nil {
return e.NewApiError("缺少参数:"+err.Error(),108001)
}
return nil
}
/**
先读redis,读不到就读数据库
@param isUpdate 如果redis没有,是否直接更新redis
*/
func (S *ZySaveService)GetSelfGoodsInfo(isUpdate bool)(model.ZySkuEntity){
zySkuEntity:=model.ZySkuEntity{}
//经过redis 或者数据库查询
getRedisGoodsIinfo:=false
if(getRedisGoodsIinfo==false){//redis没查到
if(zySkuEntity!=model.ZySkuEntity{}){//查数据库
//TOD……
//更新redis
}
}
return zySkuEntity
}
\ No newline at end of file
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