package quick_recharge

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

// CreateOrderByGoods 根据商品下单
func (a *API) CreateOrderByGoods(param *CreateOrderByGoodsReq) (result *CreateOrderByGoodsResp, err error) {
	param.Sign = a.Md5Sign(AppKey, UserID, utils.Int2Str(param.Id), utils.Int2Str(param.Count), utils.Int2Str(param.PayType))
	data, err := a.AccessInfo(UrlLink, "api/v2/addOrder", http.MethodPost, "", utils.Struct2Map(param, "", false))
	if err != nil {
		return nil, err
	}

	orderByte, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}
	if err = json.Unmarshal(orderByte, &result); err != nil {
		return nil, err
	}

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

	return result, nil
}

type CreateOrderByGoodsResp struct {
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
	Id     int    `json:"id"`     // 订单ID
	Number string `json:"number"` // 订单编号
	Cards  []struct {
		Number string `json:"number"` // 卡号密码
		Pwd    string `json:"pwd"`
	} `json:"cards"`
}
