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个字符(字符,不是字节) // 首次创建分类时,如果secondaryName不为空,会把一级(如果不存在)及二级分类创建起来, // 如果一级分类已经存在时,要创建二级分类,则要求originName与name都不为空,且值是一样的 // 大概逻辑是: // 如果originName不存在,则是创建一级分类,如果secondaryName相应还要创建二级分类,此时的sequence指的是二级的,一级的sequence就没有指定了 // 如果originName存在,则指的是更新,一级分类就必须存在,如果不想改名,将name填成与originName一样的即可 // todo 但这个函数好像就无法实现变更二级分类的名字及sequence 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) 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 } 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, foodData map[string]interface{}) (err error) { _, err = a.AccessAPI("retail/batchinitdata", false, map[string]interface{}{ KeyAppPoiCode: poiCode, "food_data": string(utils.MustMarshal(foodData)), }) 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 }