package pposRequest

import (
	"PPOSUtils/utils"
	"errors"
	"log"
	"net/url"
	"time"
)

//NewOrderDiscountPriceRequest 优惠券询价/改价 （优惠券）
func (that *PPOSClient) NewOrderDiscountPriceRequest() *DiscountPriceRequest {
	return &DiscountPriceRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/coupon/orderDiscount/discountPriceGet",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"discount_info",
			"payment_way",
		},
		bT: time.Now(),
	}
}

//
type DiscountPriceRequest struct {
	Request
	publicParamsSlice []string  //必传参数名
	bT                time.Time //耗时时间
}

func (that *DiscountPriceRequest) SetParentOrderNo(value int64) *DiscountPriceRequest {
	that.initParams()
	that.params.Set("parent_order_no", utils.ToString(value))
	return that
}
func (that *DiscountPriceRequest) SetDiscountInfo(value string) *DiscountPriceRequest {
	that.initParams()
	that.params.Set("discount_info", value)
	return that
}
func (that *DiscountPriceRequest) SetPaymentWay(value int64) *DiscountPriceRequest {
	that.initParams()
	that.params.Set("payment_way", utils.ToString(value))
	return that
}
func (that *DiscountPriceRequest) SetMemberCardId(value int64) *DiscountPriceRequest {
	that.initParams()
	that.params.Set("member_card_id", utils.ToString(value))
	return that
}
func (that *DiscountPriceRequest) SetOrderFrom(value int64) *DiscountPriceRequest {
	that.initParams()
	that.params.Set("order_from", utils.ToString(value))
	return that
}

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

//
type OrderDiscountResponse struct {
	Order *DiscountOrder
	Item  []*DiscountItem
}
type DiscountOrder struct {
	BusinessId int64  `json:"business_id"`
	CompanyId  int64  `json:"company_id"`
	OrderNo    string `json:"order_no"`
	OrderPrice int64  `json:"order_price"`
	PayPrice   int64  `json:"pay_price"`
	SalePrice  int64  `json:"sale_price"`
	StadiumId  int64  `json:"stadium_id"`
}
type DiscountItem struct {
	ItemPrice   int64  `json:"item_price"`
	PayPrice    int64  `json:"pay_price"`
	SalePrice   int64  `json:"sale_price"`
	OrderNo     string `json:"order_no"`
	OrderItemNo string `json:"order_item_no"`
	Sku         string `json:"sku"`
	SkuNum      int64  `json:"sku_num"`
	SkuPrice    int64  `json:"sku_price"`
}

//Do ...
func (that *DiscountPriceRequest) Do() ([]*OrderDiscountResponse, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return nil, _err
	}
	_mp, _ok := _data.([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	//
	_dataSlice := []*OrderDiscountResponse{}
	for _, _v := range _mp {
		_v1, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
		}
		//
		_orderInfo := new(DiscountOrder)
		_itemSlice := []*DiscountItem{}
		//
		if _err := that.ParseResponeMp(_orderInfo, _v1["order"]); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		//
		_itemData, _ok := _v1["item"].([]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
		}
		for _, _v := range _itemData {
			_item := new(DiscountItem)
			if _err := that.ParseResponeMp(_item, _v); _err != nil {
				return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
			}
			_itemSlice = append(_itemSlice, _item)
		}
		//
		_dataSlice = append(_dataSlice, &OrderDiscountResponse{
			_orderInfo,
			_itemSlice,
		})
	}
	//返回结果
	return _dataSlice, nil
}
