Files
baseapi/platformapi/mtwmapi/retail.go
2018-12-07 11:19:31 +08:00

216 lines
6.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package mtwmapi
import (
"strings"
"git.rosy.net.cn/baseapi/utils"
)
type RetailCategoryInfo struct {
Name string `json:"name"`
Sequence int `json:"sequence"`
Level int `json:"level"`
Children []*RetailCategoryInfo `json:"children"`
}
// 美团分类没有ID就以名字为唯一标识不论级别都必须不能重名
// name和originName的长度不能超过10个字符字符不是字节
// 创建一级分类originName为空name为新分类名secondaryName为空
// 修改一级分类originName为分类名name为分类新名secondaryName为空
// 创建二级分类secondaryName为二级分类名
// 如果originName为空同时创建一级分类所以如果只是创建二级分类originName与name要填一样的此时sequence指的二级分类的sequence一级分类的sequence为缺省值
// 修改二级分类originName为二级分类名name为二级分类新名secondaryName为空
func (a *API) RetailCatUpdate(poiCode, originName, name, secondaryName string, sequence int) (err error) {
params := map[string]interface{}{
KeyAppPoiCode: poiCode,
"category_name": name,
"sequence": sequence,
}
if originName != "" {
params["category_name_origin"] = originName
}
if secondaryName != "" {
params["secondary_category_name"] = secondaryName
}
_, err = a.AccessAPI("retailCat/update", false, params)
if err != nil {
if err2, ok := err.(*utils.ErrorWithCode); ok {
if err2.IntCode() == 833 {
return nil
}
}
}
return err
}
func (a *API) RetailCatDelete(poiCode, name string) (err error) {
_, err = a.AccessAPI("retailCat/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"category_name": name,
})
return err
}
func (a *API) RetailCatList(poiCode string) (retailCatList []*RetailCategoryInfo, err error) {
result, err := a.AccessAPI("retailCat/list", true, map[string]interface{}{
KeyAppPoiCode: poiCode,
})
if err == nil {
return interface2CatList(result, 1, nil), nil
}
return nil, err
}
// 商品名最长30个字符非字节
// 不能包含敏感词:特级
func (a *API) RetailInitData(poiCode, foodCode string, params map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/initdata", false, utils.MergeMaps(map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
}, params))
return err
}
// offset 从0开始limit最大不能超过200
func (a *API) RetailList(poiCode string, offset, limit int) (foodList []map[string]interface{}, err error) {
result, err := a.AccessAPI("retail/list", true, map[string]interface{}{
KeyAppPoiCode: poiCode,
"offset": offset,
"limit": limit,
})
if err == nil {
return utils.Slice2MapSlice(result.([]interface{})), nil
}
return nil, err
}
func (a *API) RetailBatchInitData(poiCode string, foodDataList []map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/batchinitdata", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": string(utils.MustMarshal(foodDataList)),
})
return err
}
func (a *API) RetailSkuPrice(poiCode string, foodData []map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/sku/price", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": foodData,
})
return err
}
func (a *API) RetailSkuStock(poiCode string, foodData []map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/sku/stock", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": foodData,
})
return err
}
func (a *API) RetailGet(poiCode, foodCode string) (food map[string]interface{}, err error) {
result, err := a.AccessAPI("retail/get", true, map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
})
if err == nil {
return result.(map[string]interface{}), nil
}
return nil, err
}
func (a *API) RetailSkuSave(poiCode, foodCode string, standardSkus, unstandardSkus []map[string]interface{}) (err error) {
_, err = a.AccessAPI("retail/sku/save", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
"standard_skus": standardSkus,
"unstandard_skus": unstandardSkus,
})
return err
}
func (a *API) RetailSkuSellStatus(poiCode string, foodData []map[string]interface{}, status int) (err error) {
_, err = a.AccessAPI("retail/sku/sellStatus", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
"food_data": foodData,
"status": status,
})
return err
}
func (a *API) RetailDelete(poiCode, foodCode string) (err error) {
_, err = a.AccessAPI("retail/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
})
return err
}
func (a *API) RetailSkuDelete(poiCode, foodCode, skuID string) (err error) {
_, err = a.AccessAPI("retail/sku/delete", false, map[string]interface{}{
KeyAppPoiCode: poiCode,
KeyAppFoodCode: foodCode,
"sku_id": skuID,
})
return err
}
// 就是厂商商品类别
func (a *API) RetailGetSpTagIds() (tagIds []map[string]interface{}, err error) {
result, err := a.AccessAPI("retail/getSpTagIds", true, nil)
if err == nil {
return utils.Slice2MapSlice(result.([]interface{})), nil
}
return nil, err
}
// 此接口将申请授权后方可接入
func (a *API) RetailCatSkuBatchDelete(poiCode string, catNames []string, secondaryCatNames []string, foodCodes []string) (err error) {
params := map[string]interface{}{
KeyAppPoiCode: poiCode,
}
if len(catNames) > 0 {
params["category_names"] = strings.Join(catNames, ",")
}
if len(secondaryCatNames) > 0 {
params["secondary_category_names"] = strings.Join(secondaryCatNames, ",")
}
if len(foodCodes) > 0 {
params["app_food_codes"] = strings.Join(foodCodes, ",")
}
_, err = a.AccessAPI("retailCat/batchdelete/catandretail", false, params)
return err
}
//////////////////////
// 私有辅助函数
func interface2Cat(data interface{}, level int) (cat *RetailCategoryInfo) {
mapData, ok := data.(map[string]interface{})
if ok {
cat = &RetailCategoryInfo{
Name: utils.Interface2String(mapData["name"]),
Sequence: int(utils.Interface2Int64WithDefault(mapData["sequence"], 0)),
Level: level,
}
children := mapData["children"]
if children != nil {
cat.Children = interface2CatList(children, level+1, nil)
}
}
return cat
}
func interface2CatList(data interface{}, level int, interface2CatHandler func(data interface{}, level int) (cat *RetailCategoryInfo)) (cats []*RetailCategoryInfo) {
if interface2CatHandler == nil {
interface2CatHandler = interface2Cat
}
maps, ok := data.([]interface{})
if ok {
cats = make([]*RetailCategoryInfo, len(maps))
for index, v := range maps {
cats[index] = interface2CatHandler(v, level)
}
}
return cats
}