package jdapi

import (
	"errors"

	"git.rosy.net.cn/baseapi/utils"
)

func (a *API) FakeOrderQueryRaw(jdParams map[string]interface{}) (retVal []map[string]interface{}, totalCount int, err error) {
	orderList, totalCount, err := a.AccessAPIHavePage("order/orderQuery", jdParams, nil, nil, nil)
	if err == nil {
		for _, v := range orderList {
			retVal = append(retVal, v.(map[string]interface{}))
		}
	}
	return retVal, totalCount, err
}

func (a *API) FakeQuerySingleOrderRaw(orderId string) (map[string]interface{}, error) {
	jdParams := make(map[string]interface{})
	jdParams["orderId"] = orderId
	result, _, err := a.FakeOrderQueryRaw(jdParams)
	if err != nil {
		return nil, err
	}
	if len(result) == 0 {
		return nil, ErrCanNotFindOrder
	}
	return result[0], nil
}

func (a *API) FakeOrderQuery(jdParams map[string]interface{}) (retVal []*OrderInfo, totalCount int, err error) {
	orderList, totalCount, err := a.AccessAPIHavePage("order/orderQuery", jdParams, nil, nil, nil)
	if err == nil {
		err = JdMap2StructByJson(orderList, &retVal, true)
	}
	return retVal, totalCount, err
}

func (a *API) FakeQuerySingleOrder(orderId string) (*OrderInfo, error) {
	jdParams := make(map[string]interface{})
	jdParams["orderId"] = orderId
	result, _, err := a.FakeOrderQuery(jdParams)
	if err != nil {
		return nil, err
	}
	if len(result) == 0 {
		return nil, ErrCanNotFindOrder
	}
	return result[0], nil
}

func (a *API) FakeBatchUpdateCurrentQtys(trackInfo, outStationNo, stationNo string, skuStockList []*SkuStock, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) {
	if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") {
		return nil, errors.New("outStationNo and stationNo can not all be empty or have value")
	}
	jdParams := map[string]interface{}{
		"skuStockList": skuStockList,
		"userPin":      utils.GetAPIOperator(userPin),
	}
	if outStationNo != "" {
		jdParams["outStationNo"] = outStationNo
	} else {
		jdParams["stationNo"] = stationNo
	}
	result, err := a.AccessAPINoPage2("stock/batchUpdateCurrentQtys", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo)
	if result != nil {
		var err2 error
		if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(skuStockList), err, result, ""); err2 != nil && err == nil {
			err = err2
		}
	}
	return responseList, err
}

func (a *API) FakeBatchUpdateVendibility(trackInfo, outStationNo, stationNo string, stockVendibilityList []*StockVendibility, userPin string) (responseList []*StoreSkuBatchUpdateResponse, err error) {
	if (outStationNo == "" && stationNo == "") || (outStationNo != "" && stationNo != "") {
		return nil, errors.New("outStationNo and stationNo can not all be empty or have value")
	}
	jdParams := map[string]interface{}{
		"stockVendibilityList": stockVendibilityList,
		"userPin":              utils.GetAPIOperator(userPin),
	}
	if outStationNo != "" {
		jdParams["outStationNo"] = outStationNo
	} else {
		jdParams["stationNo"] = stationNo
	}
	// 此函数在全部失败时，err仍然返回成功
	result, err := a.AccessAPINoPage2("stock/batchUpdateVendibility", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0"), trackInfo)
	if result != nil {
		var err2 error
		if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(stockVendibilityList), err, result, ""); err2 != nil && err == nil {
			err = err2
		}
	}
	return responseList, err
}

func (a *API) FakeUpdateVendorStationPrice(trackInfo string, outStationNo, stationNo string, skuPriceInfoList []*SkuPriceInfo) (responseList []*StoreSkuBatchUpdateResponse, err error) {
	jdParams := map[string]interface{}{
		"skuPriceInfoList": skuPriceInfoList,
	}
	if outStationNo != "" {
		jdParams["outStationNo"] = outStationNo
	} else {
		jdParams["stationNo"] = stationNo
	}
	result, err := a.AccessAPINoPage2("price/batchUpdateStationPrice", jdParams, nil, nil, genNoPageResultParser("code", "msg", "result", "0"), trackInfo)
	if result != nil {
		var err2 error
		if responseList, err2 = a.handleBatchOpResult(outStationNo, stationNo, len(skuPriceInfoList), err, result, "json2"); err2 != nil && err == nil {
			err = err2
		}
	}
	return responseList, err
}
