- jdapi.BatchAddSku

This commit is contained in:
gazebo
2018-12-12 19:09:43 +08:00
parent 3d041588bc
commit c17d7d8030
2 changed files with 65 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import (
"math"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
)
const (
@@ -100,6 +101,19 @@ type CategoryInfo struct {
FullPath string `json:"fullPath"` // 到家分类独有
}
type CreateByUpcParam struct {
Upc string
OutSkuId string
Price int // 单位为分
ShopCategoryId int64
IsSale bool
}
type CreateByUpcPair struct {
Upc string
SkuId int64
}
// 分页查询商品品牌信息接口
// https://opendj.jd.com/staticnew/widgets/resources.html?groupid=180&apiid=1ca07a3e767649a7a44fc6ea7e9ed8dd
func (a *API) QueryPageBrandInfo(pageNo, pageSize, brandId int, brandName string) (brandList []*BrandInfo, totalCount int, err error) {
@@ -513,6 +527,40 @@ func (a *API) GetSpuSaleAttr(outSpuId string) (attrList []map[string]interface{}
return nil, err
}
func (a *API) BatchAddSku(paramList []*CreateByUpcParam) (pairs []*CreateByUpcPair, err error) {
batchSkuRequestList := make([]map[string]interface{}, len(paramList))
for k, v := range paramList {
batchSkuRequestList[k] = map[string]interface{}{
"uniqueCode": v.Upc,
"outSku": v.OutSkuId,
"jdPrice": fmt.Sprintf("%.2f", float32(v.Price)/100),
"shopCategoryId": v.ShopCategoryId,
"isSale": v.IsSale,
}
}
result, err := a.AccessAPINoPage("PmsSkuMainService/batchAddSku", map[string]interface{}{
"batchSkuRequestList": batchSkuRequestList,
}, nil, nil, genNoPageResultParser("code", "result", "detail", "0"))
if err == nil {
globals.SugarLogger.Debug(utils.Format4Output(result, false))
// todo 这个API在找不到UPC创建失败时code也是0底层不能判断失败
if result2, ok := result.([]interface{}); ok && len(result2) > 0 {
detail := utils.Slice2MapSlice(result2)
pairs = make([]*CreateByUpcPair, len(detail))
for k, v := range detail {
pairs[k] = &CreateByUpcPair{
Upc: utils.Interface2String(v["uniqueCode"]),
SkuId: utils.MustInterface2Int64(v["skuId"]),
}
}
return pairs, nil
} else {
return nil, utils.NewErrorIntCode("BatchAddSku失败", -1)
}
}
return nil, err
}
///////////////////////////
// 私有辅助函数
func interface2Cat(data interface{}, level int) (cat *CategoryInfo) {

View File

@@ -148,3 +148,20 @@ func TestGetSpuSaleAttr(t *testing.T) {
}
t.Log(utils.Format4Output(result, false))
}
func TestBatchAddSku(t *testing.T) {
paramList := []*CreateByUpcParam{
&CreateByUpcParam{
Upc: "6948939649102",
OutSkuId: "50001",
Price: 213,
ShopCategoryId: 4247719,
IsSale: true,
},
}
result, err := jdapi.BatchAddSku(paramList)
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}