package tiktok_api

import (
	"errors"
	superm_applyPlatformPickUp_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_applyPlatformPickUp/request"
	superm_cancelPlatformPickUp_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_cancelPlatformPickUp/request"
	superm_createVirtualMobile_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_createVirtualMobile/request"
	superm_createVirtualMobile_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_createVirtualMobile/response"
	superm_getDispatcherInfo_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getDispatcherInfo/request"
	superm_getPlatformPickUpEstimatedCharge_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getPlatformPickUpEstimatedCharge/request"
	superm_getShipmentInfo_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getShipmentInfo/request"
	superm_getShipmentInfo_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_getShipmentInfo/response"
	superm_orderDispatcher_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/superm_orderDispatcher/request"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
)

// /superm/getDispatcherInfo 运力呼叫询价/运力取消询价(正向)
func (a *API) GetDispatcherInfo(storeID, shopOrderID int64, dispatcherType int32) (int64, error) {
	request := superm_getDispatcherInfo_request.New()
	request.Param = &superm_getDispatcherInfo_request.SupermGetDispatcherInfoParam{
		StoreID:        storeID,
		ShopOrderID:    shopOrderID,
		DispatcherType: dispatcherType,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("运力呼叫询价/运力取消询价(正向)=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("运力呼叫询价/运力取消询价(正向):=%s", utils.Format4Output(response, false))
	if err != nil {
		return 0, err
	}
	if response.Code != RequestSuccessCode {
		return 0, errors.New(response.SubMsg)
	}
	return response.Data.DispatcherFee, nil
}

// /superm/orderDispatcher 呼叫运力并发货/重新发货、取消运力
func (a *API) OrderDispatcher(storeID, shopOrderID int64, dispatcherType int32) error {
	request := superm_orderDispatcher_request.New()
	request.Param = &superm_orderDispatcher_request.SupermOrderDispatcherParam{
		StoreID:        storeID,
		ShopOrderID:    shopOrderID,
		DispatcherType: dispatcherType,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("呼叫运力并发货/重新发货、取消运力=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("呼叫运力并发货/重新发货、取消运力:=%s", utils.Format4Output(response, false))
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.SubMsg)
	}
	return nil
}

// /superm/createVirtualMobile 隐私小号获取
func (a *API) CreateVirtualMobile(shopOrderID int64) (*superm_createVirtualMobile_response.SupermCreateVirtualMobileData, error) {
	request := superm_createVirtualMobile_request.New()
	request.Param = &superm_createVirtualMobile_request.SupermOrderDispatcherParam{
		ShopOrderID: shopOrderID,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("隐私小号获取=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("隐私小号获取:=%s", utils.Format4Output(response, false))
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg)
	}
	return response.Data, nil
}

// /superm/getPlatformPickUpEstimate]dCharge 运力呼叫询价/运力取消询价(逆向)
func (a *API) GetPlatformPickUpEstimatedCharge(afterSaleID int64, inquiryType int32) (int64, error) {
	request := superm_getPlatformPickUpEstimatedCharge_request.New()
	request.Param = &superm_getPlatformPickUpEstimatedCharge_request.SupermGetPlatformPickUpEstimatedChargeParam{
		AfterSaleID: afterSaleID,
		InquiryType: inquiryType,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("运力呼叫询价/运力取消询价=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("运力呼叫询价/运力取消询价:=%s", utils.Format4Output(response, false))
	if err != nil {
		return 0, err
	}
	if response.Code != RequestSuccessCode {
		return 0, errors.New(response.SubMsg)
	}
	return response.Data.PlatformPickupEstimatedCharge, err
}

// /superm/applyPlatformPickUp 呼叫运力售后取件
func (a *API) ApplyPlatformPickUp(afterSaleID int64) (string, error) {
	request := superm_applyPlatformPickUp_request.New()
	request.Param = &superm_applyPlatformPickUp_request.SupermApplyPlatformPickUpParam{
		AfterSaleID: afterSaleID,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("呼叫运力售后取件=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("呼叫运力售后取件:=%s", utils.Format4Output(response, false))
	if err != nil {
		return "", err
	}
	if response.Code != RequestSuccessCode {
		return "", errors.New(response.SubMsg)
	}
	return response.Data.LogisticsID, err
}

// /superm/getShipmentInfo 查询运力订单信息
func (a *API) GetShipmentInfo(shopOrderID, afterSaleID, shipmentType int64) (*superm_getShipmentInfo_response.ShipmentInfo, error) {
	request := superm_getShipmentInfo_request.New()
	request.Param = &superm_getShipmentInfo_request.SupermGetShipmentInfoParam{
		ShopOrderID:  shopOrderID,
		ShipmentType: shipmentType,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("查询运力订单信息=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("查询运力订单信息:=%s", utils.Format4Output(response, false))
	if err != nil {
		return nil, err
	}
	if response.Code != RequestSuccessCode {
		return nil, errors.New(response.SubMsg)
	}
	return response.Data.ShipmentInfo, err
}

// /superm/cancelPlatformPickUp 取消运力 (逆向)
func (a *API) CancelPlatformPickUp(afterSaleID int64) error {
	request := superm_cancelPlatformPickUp_request.New()
	request.Param = &superm_cancelPlatformPickUp_request.SupermCancelPlatformPickUpParam{
		AfterSaleID: afterSaleID,
	}
	response, err := request.Execute(a.accessTokenObj)
	globals.SugarLogger.Debugf("取消运力=========:%s", request.GetUrlPath())
	globals.SugarLogger.Debugf("取消运力:=%s", utils.Format4Output(response, false))
	if err != nil {
		return err
	}
	if response.Code != RequestSuccessCode {
		return errors.New(response.SubMsg)
	}
	return err
}
