package pposRequest

import (
	"encoding/json"
	"errors"
	"net/url"
)

//场地：根据订单号获取其是否可开发票
func (that *PPOSClient) NewFieldOrderDetailGetOrderInvoiceStatus() *FieldOrderDetailGetOrderInvoiceStatusRequest {
	return &FieldOrderDetailGetOrderInvoiceStatusRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/fieldOrderDetail/getOrderInvoiceStatus",
			publicParamsSlice: []string{
				"order_nos",
			},
		}}
}

type FieldOrderDetailGetOrderInvoiceStatusRequest struct {
	Request
}
type FieldOrderDetailGetOrderInvoiceStatusResponse struct {
	OrderNo   string `json:"order_no"`
	IsInvoice int64  `json:"is_invoice"`
}

func (that *FieldOrderDetailGetOrderInvoiceStatusRequest) SetOrderNos(value string) *FieldOrderDetailGetOrderInvoiceStatusRequest {
	that.initParams()
	that.params.Set("order_nos", value) //订单号，多个用逗号隔开
	return that
}

//Params ...
func (that *FieldOrderDetailGetOrderInvoiceStatusRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.publicParamsSlice != nil {
		for _, _v := range that.publicParamsSlice {
			if _params.Get(_v) == "" {
				return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
			}
		}
	}
	return _params, nil

}

//Do ...
func (that *FieldOrderDetailGetOrderInvoiceStatusRequest) Do() ([]*FieldOrderDetailGetOrderInvoiceStatusResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_str, _err := json.Marshal(_data)
	if _err != nil {
		return nil, errors.New(that.apiAddress + _err.Error())
	}
	//返回结果
	_rs := make([]*FieldOrderDetailGetOrderInvoiceStatusResponse, 0)
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	//
	return _rs, nil
}
