package tiktok_api

import (
	"errors"
	"fmt"
	address_create_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_create/request"
	address_create_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_create/response"
	address_getAreasByProvince_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_getAreasByProvince/request"
	address_getAreasByProvince_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_getAreasByProvince/response"
	address_getProvince_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_getProvince/request"
	address_getProvince_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_getProvince/response"
	address_list_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_list/request"
	address_list_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/address_list/response"
	freightTemplate_create_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_create/request"
	freightTemplate_create_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_create/response"
	freightTemplate_detail_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_detail/request"
	freightTemplate_detail_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_detail/response"
	freightTemplate_update_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_update/request"
	freightTemplate_update_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/freightTemplate_update/response"
	shop_batchCreateStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_batchCreateStore/request"
	shop_batchCreateStore_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_batchCreateStore/response"
	shop_bindStoreFreight_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_bindStoreFreight/request"
	shop_bindStoreSaleLimit_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_bindStoreSaleLimit/request"
	shop_editStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_editStore/request"
	shop_getStoreDetail_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreDetail/request"
	shop_getStoreDetail_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreDetail/response"
	shop_getStoreFreight_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreFreight/request"
	shop_getStoreList_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreList/request"
	shop_getStoreList_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreList/response"
	shop_getStorePackageFee_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStorePackageFee/request"
	shop_getStoreSaleLimit_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_getStoreSaleLimit/request"
	shop_setStorePackageFee_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_setStorePackageFee/request"
	shop_storeSuspend_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_storeSuspend/request"
	shop_storeSuspend_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_storeSuspend/response"
	shop_unsuspendStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_unsuspendStore/request"
	shop_unsuspendStore_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/shop_unsuspendStore/response"
	trade_UpdateTradeLimitTemplate_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_UpdateTradeLimitTemplate/request"
	trade_UpdateTradeLimitTemplate_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_UpdateTradeLimitTemplate/response"
	trade_batchGetTradeLimitTemplateList_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_batchGetTradeLimitTemplateList/request"
	trade_batchGetTradeLimitTemplateList_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_batchGetTradeLimitTemplateList/response"
	trade_createTradeLimitTemplate_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_createTradeLimitTemplate/request"
	trade_createTradeLimitTemplate_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/trade_createTradeLimitTemplate/response"
	warehouse_bindFences_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_bindFences/request"
	warehouse_bindFencesByStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_bindFencesByStore/request"
	warehouse_bindFencesByStore_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_bindFencesByStore/response"
	warehouse_bindStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_bindStore/request"
	warehouse_bindStore_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_bindStore/response"
	warehouse_create_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_create/request"
	warehouse_create_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_create/response"
	warehouse_createBatch_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_createBatch/request"
	warehouse_createBatch_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_createBatch/response"
	warehouse_createFence_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_createFence/request"
	warehouse_createFence_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_createFence/response"
	warehouse_getFences_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_getFences/request"
	warehouse_getFences_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_getFences/response"
	warehouse_getWarehouseByStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_getWarehouseByStore/request"
	warehouse_getWarehouseByStore_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_getWarehouseByStore/response"
	warehouse_list_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_list/request"
	warehouse_list_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_list/response"
	warehouse_setFence_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_setFence/request"
	warehouse_unbindStore_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/warehouse_unbindStore/request"
	"git.rosy.net.cn/jx-callback/globals"
)

//GetStoreDetail 单个获取门店信息
func (a *API) GetStoreDetail(param *shop_getStoreDetail_request.ShopGetStoreDetailParam) (*shop_getStoreDetail_response.ShopGetStoreDetailData, error) {
	request := shop_getStoreDetail_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

//BatchCreateStore 批量创建门店
func (a *API) BatchCreateStore(param *shop_batchCreateStore_request.ShopBatchCreateStoreParam) (*shop_batchCreateStore_response.ShopBatchCreateStoreData, error) {
	request := shop_batchCreateStore_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		//return nil, errors.New(response.Data.ResultList[0].Msg)
		return nil, errors.New(response.LogId + ":" + response.Msg + ":" + response.LogId)
	}
	//if response.Data.ResultList[0].Store.StoreId == 0 {
	//	return nil, errors.New(response.Msg + "," + response.SubMsg)
	//}
	return response.Data, nil
}

// EditStore 编辑门店
func (a *API) EditStore(param *shop_editStore_request.ShopEditStoreParam) error {
	request := shop_editStore_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return nil
}

//暂停营业
func (a *API) StoreSuspend(param *shop_storeSuspend_request.ShopStoreSuspendParam) (*shop_storeSuspend_response.ShopStoreSuspendData, error) {
	request := shop_storeSuspend_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

func (a *API) StoreSuspend2(storeID int64) (*shop_storeSuspend_response.ShopStoreSuspendData, error) {
	request := shop_storeSuspend_request.New()
	request.Param.StoreId = storeID
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

//门店恢复营业
func (a *API) UnsuspendStore(param *shop_unsuspendStore_request.ShopUnsuspendStoreParam) (*shop_unsuspendStore_response.ShopUnsuspendStoreData, error) {
	request := shop_unsuspendStore_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}
func (a *API) UnsuspendStore2(storeID int64) (*shop_unsuspendStore_response.ShopUnsuspendStoreData, error) {
	request := shop_unsuspendStore_request.New()
	request.Param.StoreId = storeID
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

type VendorAndStoreId struct {
	StoreId       string `json:"store_id"`        //京西本地ID
	VendorStoreID int64  `json:"vendor_store_id"` //抖店平台ID
}

//门店列表分页查询
func (a *API) GetStoreList(param *shop_getStoreList_request.ShopGetStoreListParam) (map[int64]string, error) {
	request := shop_getStoreList_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	//测试用
	resp := map[int64]string{}
	if len(response.Data.StoreDetailList) > 0 {
		for _, v := range response.Data.StoreDetailList {
			resp[v.Store.StoreId] = v.Store.StoreCode
		}
	}
	return resp, err
}

//门店列表分页查询
func (a *API) GetStoreList2(param *shop_getStoreList_request.ShopGetStoreListParam) (*shop_getStoreList_response.ShopGetStoreListResponse, error) {
	request := shop_getStoreList_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}

	return response, err
}

//创建限售规则模板
func (a *API) CreateTradeLimitTemplate(param *trade_createTradeLimitTemplate_request.TradeCreateTradeLimitTemplateParam) (*trade_createTradeLimitTemplate_response.TradeCreateTradeLimitTemplateData, error) {
	request := trade_createTradeLimitTemplate_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

//更新限售模板
func (a *API) UpdateTradeLimitTemplate(param *trade_UpdateTradeLimitTemplate_request.TradeUpdateTradeLimitTemplateParam) (*trade_UpdateTradeLimitTemplate_response.TradeUpdateTradeLimitTemplateData, error) {
	request := trade_UpdateTradeLimitTemplate_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return response.Data, nil
}

//门店绑定限售模板
func (a *API) BindStoreSaleLimit(param *shop_bindStoreSaleLimit_request.ShopBindStoreSaleLimitParam) error {
	request := shop_bindStoreSaleLimit_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.LogId + ":" + response.SubMsg + ":" + response.LogId)
	}
	return nil
}

// 门店查询限售模板id
func (a *API) StoreQuerySaleLimitTemp(storeId int64) (int64, error) {
	request := shop_getStoreSaleLimit_request.New()
	request.Param.StoreId = storeId
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, err
	}
	if result.Code != RequestSuccessCode {
		return 0, errors.New(result.LogId + ":" + result.SubMsg + ":" + result.LogId)
	}
	if result.Data == nil || len(result.Data.StoreSaleLimits) == 0 {
		return 0, errors.New("未绑定限售模板,请先绑定")
	}
	return result.Data.StoreSaleLimits[0].SaleLimitId, nil
}

//id 查询限售模板详情
func (a *API) GetSaleLimitDetail(saleLimitId *[]int64) (*trade_batchGetTradeLimitTemplateList_response.TradeBatchGetTradeLimitTemplateListData, error) {
	request := trade_batchGetTradeLimitTemplateList_request.New()
	request.Param.TradeLimitTemplateIdList = saleLimitId
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if result.Code != RequestSuccessCode {
		return nil, errors.New(result.LogId + ":" + result.SubMsg + ":" + result.LogId)
	}
	if result.Data == nil || len(result.Data.TradeLimitTemplateList) == 0 {
		return nil, errors.New("未查找到限售模板详情")
	}
	return result.Data, nil
}

// 创建运费模板
func (a *API) FreightTemplateCreate(param *freightTemplate_create_request.FreightTemplateCreateParam) (*freightTemplate_create_response.FreightTemplateCreateData, error) {
	request := freightTemplate_create_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.Msg + "," + response.SubMsg)
	}
	return response.Data, nil
}

//更新运费模板
func (a *API) FreightTemplateUpdate(param *freightTemplate_update_request.FreightTemplateUpdateParam) (*freightTemplate_update_response.FreightTemplateUpdateData, error) {
	request := freightTemplate_update_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.Msg + "," + response.SubMsg)
	}
	return nil, nil
}

// 门店绑定运费模板 1对1
func (a *API) BindFreightTemplate(param *shop_bindStoreFreight_request.ShopBindStoreFreightParam) error {
	request := shop_bindStoreFreight_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.LogId + ":" + response.SubMsg)
	}
	return nil
}

// GetStoreFreight 获取门店所有的运费模板
func (a *API) GetStoreFreight(storeId int64) ([]int64, error) {
	request := shop_getStoreFreight_request.New()
	request.Param.StoreId = storeId
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if result.Code != RequestSuccessCode {
		return nil, errors.New(result.LogId + ":" + result.SubMsg)
	}
	tempList := make([]int64, 0)
	if result.Data == nil {
		tempList = append(tempList, 0)
		return tempList, err
	}
	for _, v := range result.Data.StoreFreights {
		tempList = append(tempList, v.FreightId)
	}
	return tempList, err
}

//获取运费模板详情
func (a *API) GetStoreFreightDetail(freightId int64) (*freightTemplate_detail_response.Data, error) {
	request := freightTemplate_detail_request.New()
	request.Param.FreightId = freightId
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if result.Code != RequestSuccessCode {
		return nil, errors.New(result.LogId + ":" + result.SubMsg)
	}
	return result.Data, nil
}

// 获取门店绑定的运费模板
func (a *API) GetStoreBindTemp(storeId int64) (freightId int64, err error) {
	request := shop_getStoreFreight_request.New()
	request.Param.StoreId = storeId
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, err
	}
	if result.Data == nil || len(result.Data.StoreFreights) == 0 {
		return 0, nil
	}
	for _, v := range result.Data.StoreFreights {
		if v.StoreId == storeId {
			return v.FreightId, nil
		}
	}
	return 0, nil
}

// 批量创建区域仓
func (a *API) BatchCreateWarehouse(param *warehouse_createBatch_request.WarehouseCreateBatchParam) (*warehouse_createBatch_response.WarehouseCreateBatchData, error) {
	request := warehouse_createBatch_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//创建单个区域仓
func (a *API) CreateWarehouse(param *warehouse_create_request.WarehouseCreateParam) (*warehouse_create_response.WarehouseCreateData, error) {
	request := warehouse_create_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.Msg + "," + response.SubMsg)
	}
	return response.Data, nil
}

// 仓库绑定门店
func (a *API) StoreBindWarehouse(param *warehouse_bindStore_request.WarehouseBindStoreParam) (*warehouse_bindStore_response.WarehouseBindStoreData, error) {
	request := warehouse_bindStore_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//获取门店绑定的仓库信息
func (a *API) GetWarehouseByStore(storeID int64) (*warehouse_getWarehouseByStore_response.WarehouseGetWarehouseByStoreData, error) {
	request := warehouse_getWarehouseByStore_request.New()
	request.Param.StoreIds = append(request.Param.StoreIds, storeID)
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + response.Msg + "," + response.SubMsg)
	}
	if len(response.Data.WarehouseInfoMap) == 0 {
		return nil, errors.New(fmt.Sprintf("门店%d 暂时未绑定仓库，请进行绑定!", storeID))
	}
	return response.Data, nil
}

//创建电子围栏
func (a *API) CreateFence(param *warehouse_createFence_request.WarehouseCreateFenceParam) (*warehouse_createFence_response.WarehouseCreateFenceData, error) {
	request := warehouse_createFence_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		globals.SugarLogger.Debugf("CreateFence errMsg=%s", response.LogId+":"+response.SubMsg)
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//更新电子围栏
func (a *API) SetFence(param *warehouse_setFence_request.WarehouseSetFenceParam) error {
	request := warehouse_setFence_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		globals.SugarLogger.Debugf("SetFence errMsg=%s", response.LogId+":"+response.SubMsg)
		return errors.New(response.LogId + ":" + response.SubMsg)
	}
	return nil
}

// 门店绑定电子围栏
func (a *API) BindFenceByStore(param *warehouse_bindFencesByStore_request.WarehouseBindFencesByStoreParam) (*warehouse_bindFencesByStore_response.WarehouseBindFencesByStoreData, error) {
	request := warehouse_bindFencesByStore_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		globals.SugarLogger.Debugf("BindFenceByStore errMsg=%s", response.LogId+":"+response.SubMsg)
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//仓库绑定电子围栏
func (a *API) WarehouseBindFence(param *warehouse_bindFences_request.WarehouseBindFencesParam) error {
	request := warehouse_bindFences_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		globals.SugarLogger.Debugf("WarehouseBindFence errMsg=%s", response.LogId+":"+response.SubMsg)
		return errors.New(response.LogId + ":" + response.SubMsg)
	}
	return nil
}

// 获取仓库（即门店）绑定 电子围栏信息列表
func (a *API) GetFence(outFenceID []string) (*warehouse_getFences_response.WarehouseGetFencesData, error) {
	request := warehouse_getFences_request.New()
	request.Param.OutFenceIdList = outFenceID
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		globals.SugarLogger.Debugf("GetFence errMsg=%s", response.LogId+":"+response.SubMsg)
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//address
//门店新增查询发货地址
func (a *API) AddressCreate(param *address_create_request.AddressCreateParam) (*address_create_response.AddressCreateData, error) {
	request := address_create_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//分页获取商家地址列表
func (a *API) GetAddressList(param *address_list_request.AddressListParam) (*address_list_response.AddressListData, error) {
	request := address_list_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

//根据省份ID获取全量四级地址
func (a *API) GetAreasByProvince(param *address_getAreasByProvince_request.AddressGetAreasByProvinceParam) (*address_getAreasByProvince_response.AddressGetAreasByProvinceData, error) {
	request := address_getAreasByProvince_request.New()
	request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

// GetProvince 获取四级地址全量省份信息
func (a *API) GetProvince() (*address_getProvince_response.AddressGetProvinceData, error) {
	request := address_getProvince_request.New()
	//request.Param = param
	response, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.LogId + ":" + response.SubMsg)
	}
	return response.Data, nil
}

// QueryAllWarehouse 查询所有的仓库
func (a *API) QueryAllWarehouse(param *warehouse_list_request.WarehouseListParam) ([]warehouse_list_response.WarehousesItem, error) {
	request := warehouse_list_request.New()
	request.Param = param
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return nil, err
	}
	if result.Code != RequestSuccessCode {
		return nil, errors.New(result.LogId + ":" + result.SubMsg)
	}
	if result.Data.Total == 0 {
		return nil, errors.New("没有区域仓库")
	}
	return result.Data.Warehouses, nil
}

// WarehouseUnbindStore 仓库解绑门店接口 /warehouse/unbindStore
func (a *API) WarehouseUnbindStore(storeId []int64, outWarehouseId string) error {
	request := warehouse_unbindStore_request.New()
	request.Param.StoreIds = storeId
	request.Param.OutWarehouseId = outWarehouseId

	data, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if data.Code != RequestSuccessCode {
		return errors.New(data.LogId + ":" + data.SubMsg)
	}

	return nil
}

//  /shop/setStorePackageFee 设置门店打包费
func (a *API) SetStorePackageFee(storeID, packageFee int64) error {
	request := shop_setStorePackageFee_request.New()
	request.Param.StoreId = storeID
	request.Param.PackageFee = packageFee
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return err
	}
	if result.Code != RequestSuccessCode {
		return errors.New(result.SubMsg)
	}

	return nil
}

// /shop/getStorePackageFee 获取门店打包费
func (a *API) GetStorePackageFee(storeID int64) (int64, error) {
	request := shop_getStorePackageFee_request.New()
	request.Param.StoreId = storeID
	result, err := request.Execute(a.accessTokenObj)
	if err != nil {
		return 0, err
	}
	if result.Code != RequestSuccessCode {
		return 0, errors.New(result.LogId + ":" + result.SubMsg)
	}
	if result.Data.PackageFee == nil || result.Data == nil {
		return 0, err
	} else {
		return result.Data.PackageFee.Amount, nil
	}
}
