- 清理三个平台的门店商品批处理操作,部分失败返回失败条目

This commit is contained in:
gazebo
2019-07-21 16:08:37 +08:00
parent 0298f7de71
commit 414c359200
12 changed files with 597 additions and 179 deletions

View File

@@ -1,6 +1,7 @@
package mtwmapi
import (
"regexp"
"strings"
"git.rosy.net.cn/baseapi/utils"
@@ -14,6 +15,15 @@ const (
MaxBatchDeleteSize = 100 // retailCat/batchdelete/catandretail这个接口的批量最大值
)
const (
SellStatusOnline = 0 // 上架
SellStatusOffline = 1 // 下架
)
var (
retailBatchFailedSkuReg = regexp.MustCompile(`((?:\d+;)+)`)
)
type RetailCategoryInfo struct {
Name string `json:"name"`
Sequence int `json:"sequence"`
@@ -91,6 +101,11 @@ type AppFood struct {
ZhName string `json:"zh_name"`
}
type AppFoodResult struct {
AppFoodCode string `json:"app_food_code"`
ErrorMsg string `json:"error_msg"`
}
// 美团分类没有ID就以名字为唯一标识不论级别都必须不能重名
// name和originName的长度不能超过10个字符字符不是字节
// 创建一级分类originName为空name为新分类名secondaryName为空
@@ -168,28 +183,78 @@ func (a *API) RetailList(poiCode string, offset, limit int) (foodList []*AppFood
return foodList, err
}
func (a *API) RetailBatchInitData(poiCode string, foodDataList []map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/batchinitdata", false, map[string]interface{}{
func handleRetailBatchResult(result interface{}) (failedFoodList []*AppFoodResult, err error) {
if msg, ok := result.(string); ok && msg != "" {
err = utils.UnmarshalUseNumber([]byte(msg), &failedFoodList)
}
return failedFoodList, err
}
func handleRetailBatchResultByRegexp(result interface{}) (failedFoodList []*AppFoodResult, err error) {
if msg, ok := result.(string); ok && msg != "" {
findList := retailBatchFailedSkuReg.FindStringSubmatch(msg)
if len(findList) == 2 {
ids := strings.Split(strings.Trim(findList[1], ";"), ";")
if len(ids) > 0 {
failedFoodList = make([]*AppFoodResult, len(ids))
for k, v := range ids {
failedFoodList[k] = &AppFoodResult{
AppFoodCode: v,
ErrorMsg: "",
}
}
}
}
}
return failedFoodList, err
}
func (a *API) RetailBatchInitData(poiCode string, foodDataList []map[string]interface{}) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/batchinitdata", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodDataList)),
})
return err
}, resultKeyMsg)
if err == nil {
failedFoodList, err = handleRetailBatchResult(result)
}
return failedFoodList, err
}
func (a *API) RetailSkuPrice(poiCode string, foodData []*BareStoreFoodInfo) (err error) {
_, err = a.AccessAPI("retail/sku/price", false, map[string]interface{}{
func (a *API) RetailSkuPrice(poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/sku/price", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)),
})
return err
}, resultKeyMsg)
if err == nil {
failedFoodList, err = handleRetailBatchResult(result)
}
return failedFoodList, err
}
func (a *API) RetailSkuStock(poiCode string, foodData []*BareStoreFoodInfo) (err error) {
_, err = a.AccessAPI("retail/sku/stock", false, map[string]interface{}{
func (a *API) RetailSkuStock(poiCode string, foodData []*BareStoreFoodInfo) (failedFoodList []*AppFoodResult, err error) {
result, err := a.AccessAPI2("retail/sku/stock", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)),
})
return err
}, resultKeyMsg)
if err == nil {
failedFoodList, err = handleRetailBatchResult(result)
}
return failedFoodList, err
}
// retail/sku/sellStatus在部分失败时会返回错误其它相应的批处理函数则会返回成功
func (a *API) RetailSkuSellStatus(poiCode string, foodData []*BareStoreFoodInfo, sellStatus int) (failedFoodList []*AppFoodResult, err error) {
_, err = a.AccessAPI2("retail/sku/sellStatus", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)),
"sell_status": sellStatus,
}, resultKeyMsg)
if err != nil {
if errExt, ok := err.(*utils.ErrorWithCode); ok {
failedFoodList, _ = handleRetailBatchResultByRegexp(errExt.ErrMsg())
}
}
return failedFoodList, err
}
func (a *API) RetailGet(poiCode, foodCode string) (food *AppFood, err error) {
@@ -216,24 +281,6 @@ func (a *API) RetailSkuSave(poiCode, foodCode string, standardSkus, unstandardSk
return err
}
func (a *API) RetailSkuSellStatus(poiCode string, foodData []map[string]interface{}, sellStatus int) (err error) {
_, err = a.AccessAPI("retail/sku/sellStatus", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)),
"sell_status": sellStatus,
})
return err
}
func (a *API) RetailSkuSellStatus2(poiCode string, foodData []*BareStoreFoodInfo, sellStatus int) (err error) {
_, err = a.AccessAPI("retail/sku/sellStatus", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodData)),
"sell_status": sellStatus,
})
return err
}
func (a *API) RetailDelete(poiCode, foodCode string) (err error) {
_, err = a.AccessAPI("retail/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode,