package quick_recharge

import (
	"encoding/json"
	"fmt"
	"git.rosy.net.cn/baseapi/utils"
	"net/http"
)

// GetRechargeGoods 获取平台商品列表
func (a *API) GetRechargeGoods(param map[string]interface{}) (result []*RechargeGoodsRespData, err error) {
	if param["type"] != nil {
		param["sign"] = a.Md5Sign(AppKey, UserID, utils.Int64ToStr(utils.ForceInterface2Int64(param["type"])))
	} else {
		param["sign"] = a.Md5Sign(AppKey, UserID)
	}
	goods, err := a.AccessInfo(UrlLink, "api/getGoods.htm", http.MethodPost, "", param)
	if err != nil {
		return nil, err
	}

	var goodList *GetRechargeGoodsResp
	goodsByte, err := json.Marshal(goods)
	if err != nil {
		return nil, err
	}
	if err = json.Unmarshal(goodsByte, &goodList); err != nil {
		return nil, err
	}

	if goodList.Code != ResponseCodeSuccess {
		return nil, fmt.Errorf("%s", goodList.Msg)
	}

	return goodList.Data, nil
}

// GetGoodsDetail 获取商品详情
func (a *API) GetGoodsDetail(goodsId int64) (detail []*GetGoodsDetailData, err error) {
	param := map[string]interface{}{
		"userNo": a.userID,
		"id":     goodsId,
	}
	param["sign"] = a.Md5Sign(AppKey, UserID, utils.Int64ToStr(goodsId))
	data, err := a.AccessInfo(UrlLink, "api/getGood.htm", http.MethodPost, "", param)
	if err != nil {
		return nil, err
	}

	var result *GetGoodsDetailResp
	goodsByte, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	if err = json.Unmarshal(goodsByte, &result); err != nil {
		return nil, err
	}
	if result.Code != ResponseCodeSuccess {
		return nil, fmt.Errorf("%s", result.Msg)
	}

	return result.Data, nil
}
