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"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability587"
	domain587 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability587/domain"
	request2 "git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability587/request"
	"git.rosy.net.cn/baseapi/platformapi/tao_vegetable/sdk/ability589"
	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"
	"time"
)

// AddStoreSku 新增门店商品
func (a *API) AddStoreSku(param *request.AlibabaWdkSkuAddRequest) (*[]VegetableResultList, error) {
	client := ability585.NewAbility585(&a.client)
	data, _ := client.AlibabaWdkSkuAdd(param, a.token)
	var foodList = make([]VegetableResultList, 0, len(*data.Result.Models))
	for _, v := range *data.Result.Models {
		if *v.Success {
			foodList = append(foodList, VegetableResultList{
				ProductID: *v.Model,
				SkuID:     *v.Model,
				ErrMsg:    "",
			})
		} else {
			skuData := VegetableResultList{
				ProductID: "",
				SkuID:     *v.Model,
				ErrMsg:    *v.ErrMsg,
			}
			if *v.ErrCode == "F_SKU_S1_BIZ_011" {
				skuData.ProductID = *v.Model
				skuData.ErrMsg = ""
			}
			foodList = append(foodList, skuData)
		}

	}

	return &foodList, nil
}

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

	data, _ := client.AlibabaWdkSkuUpdate(param, a.token)
	if !*data.Result.Success {
		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) {
	client := ability585.NewAbility585(&a.client)

	data, err := client.AlibabaWdkSkuQuery(param, a.token)
	if err != nil {
		return nil, err
	}
	if !*data.Result.Success {
		return nil, fmt.Errorf(*data.Result.ErrMsg)
	}

	return data.Result.Models, nil
}

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

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

	if !*data.ApiResult.Success {
		globals.SugarLogger.Debugf("storeID:%d,skuId:%d,requestId[%s],err[%s]", param.ChannelSkuUpdateStatusReq.StoreId, param.ChannelSkuUpdateStatusReq.SkuCode, data.RequestId, utils.Format4Output(data, false))
		//return fmt.Errorf(*data.ApiResult.ErrMsg)
	}

	return nil
}

// StoreSkuUpdateOffShelfList 门店商品上下架
func (a *API) StoreSkuUpdateOffShelfList(storeID int64, skuList []int64) []string {
	client := ability587.NewAbility587(&a.client)

	errs := make([]string, 0, 0)
	param := &request2.AlibabaAxChannelSkuStatusUpdateRequest{ChannelSkuUpdateStatusReq: &domain587.AlibabaAxChannelSkuStatusUpdateChannelSkuUpdateStatusReq{
		StoreId:        utils.String2Pointer(fmt.Sprintf("JX%d", storeID)),
		ChannelCode:    utils.String2Pointer("31"),
		OnlineSaleFlag: utils.Int64ToPointer(1),
	}}
	for i := 0; i < len(skuList); i++ {
		param.ChannelSkuUpdateStatusReq.SkuCode = utils.String2Pointer(utils.Int64ToStr(skuList[i]))
		data, _ := client.AlibabaAxChannelSkuStatusUpdate(param, a.token)
		if !*data.ApiResult.Success {
			errs = append(errs, *data.ApiResult.ErrMsg)
		}
		if i%5 == 0 {
			time.Sleep(1 * time.Second)
		}
	}

	return errs
}

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

	data, _ := client.AlibabaWdkStockPublish(param)
	if !data.IsSuccess {
		return fmt.Errorf(data.Message)
	}

	return nil
}
