package tao_vegetable

import (
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability585"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability585/domain"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability585/request"
	request2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability587/request"
	request589 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability589/request"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"

	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability587"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability589"
)

// AddStoreSku 新增门店商品
func (a *API) AddStoreSku(param *request.AlibabaWdkSkuAddRequest) (*[]VegetableResultList, error) {
	globals.SugarLogger.Debugf("进入 AddStoreSku")
	client := ability585.NewAbility585(&a.client)

	data, _ := client.AlibabaWdkSkuAdd(param, a.token)

	if !*data.Result.Success {
		globals.SugarLogger.Debugf("requestId[%s],err[%s]", data.RequestId, utils.Format4Output(data.Result, false))
		return nil, fmt.Errorf(*data.Result.ErrMsg)
	}

	var foodList = make([]VegetableResultList, 0, len(*data.Result.Models))
	for _, v := range *data.Result.Models {
		foodList = append(foodList, VegetableResultList{
			ProductID: *v.ProductId,
			SkuID:     *v.Model,
			ErrMsg:    *v.ErrMsg,
		})
	}

	return &foodList, nil
}

// UpdateStoreSku 更新门店商品
func (a *API) UpdateStoreSku(param *request.AlibabaWdkSkuUpdateRequest) (*[]VegetableResultList, error) {
	globals.SugarLogger.Debugf("进入 UpdateStoreSku")
	client := ability585.NewAbility585(&a.client)

	data, _ := client.AlibabaWdkSkuUpdate(param, a.token)

	if !*data.Result.Success {
		globals.SugarLogger.Debugf("requestId[%s],err[%s]", data.RequestId, utils.Format4Output(data.Result, false))
		return nil, fmt.Errorf(*data.Result.ErrMsg)
	}

	var foodList = make([]VegetableResultList, 0, len(*data.Result.Models))
	for _, v := range *data.Result.Models {
		model := VegetableResultList{
			ProductID: *v.Model,
			SkuID:     *v.Model,
		}
		if !*v.Success {
			model.ErrMsg = *v.ErrMsg
		}
		foodList = append(foodList, model)
	}

	return &foodList, nil
}

// QueryStoreSKu 查询门店商品
func (a *API) QueryStoreSKu(param *request.AlibabaWdkSkuQueryRequest) (*[]domain.AlibabaWdkSkuQueryApiResult, error) {
	globals.SugarLogger.Debugf("进入 QueryStoreSKu")
	client := ability585.NewAbility585(&a.client)

	data, _ := client.AlibabaWdkSkuQuery(param, a.token)

	if !*data.Result.Success {
		globals.SugarLogger.Debugf("requestId[%s],err[%s]", data.RequestId, utils.Format4Output(data.Result, false))
		return nil, fmt.Errorf(*data.Result.ErrMsg)
	}

	return data.Result.Models, nil
}

// DeleteStoreSku 删除门店商品
func (a *API) DeleteStoreSku() {

}

// StoreSkuUpdateOffShelf 门店商品上下架
func (a *API) StoreSkuUpdateOffShelf(param *request2.AlibabaAxChannelSkuStatusUpdateRequest) error {
	globals.SugarLogger.Debugf("进入 StoreSkuUpdateOffShelf")
	client := ability587.NewAbility587(&a.client)

	data, _ := client.AlibabaAxChannelSkuStatusUpdate(param, a.token)

	if !*data.ApiResult.Success {
		globals.SugarLogger.Debugf("requestId[%s],err[%s]", data.RequestId, utils.Format4Output(data.ApiResult, false))
		return fmt.Errorf(*data.ApiResult.ErrMsg)
	}

	return nil
}

// StoreSkuStock 商品库存同步
func (a *API) StoreSkuStock(param *request589.AlibabaWdkStockPublishRequest) error {
	globals.SugarLogger.Debugf("进入 StoreSkuStock")
	client := ability589.NewAbility589(&a.client)

	data, _ := client.AlibabaWdkStockPublish(param)

	if !data.IsSuccess {
		globals.SugarLogger.Debugf("requestId[%s],err[%s]", data.RequestId, utils.Format4Output(data, false))
		return fmt.Errorf(data.Message)
	}

	return nil
}
