package order

import (
	"sort"
	"strconv"
)

type CreatedOrder struct {
	PayKey      string  `json:"payKey" description:"用户key" valid:"AlphaNumeric;"`
	OrderNo     string  `json:"orderNo" description:"订单号" valid:"AlphaNumeric;"`
	OrderPrice  float64 `json:"orderPrice" description:"订单金额" valid:"Required;"`
	OrderPeriod int     `json:"orderPeriod" description:"订单周期" valid:"Required;"`
	NotifyUrl   string  `json:"notifyUrl" description:"回调地址" valid:"Required;"`
	Sign        string  `json:"sign" description:"签名" valid:"Required;"`
	ProductCode string  `json:"productCode" description:"产品编码" valid:"Required;"`
	Timestamp   int64   `json:"timestamp" description:"时间戳" valid:"Required;"`
}

// ToMap 转换为map类型
func (c *CreatedOrder) ToMap() map[string]interface{} {
	data := map[string]interface{}{
		"payKey":      c.PayKey,
		"orderNo":     c.OrderNo,
		"orderPrice":  c.OrderPrice,
		"orderPeriod": c.OrderPeriod,
		"notifyUrl":   c.NotifyUrl,
		"productCode": c.ProductCode,
		"timestamp":   c.Timestamp,
		"sign":        c.Sign,
	}
	// 按照键的字母顺序排序
	keyList := make([]string, 0)
	for key := range data {
		keyList = append(keyList, key)
	}
	sort.Strings(keyList)
	sortedData := map[string]interface{}{}
	for _, key := range keyList {
		sortedData[key] = data[key]
	}
	return data
}

// ToStrMap 转换为map类型
func (c *CreatedOrder) ToStrMap() map[string]string {
	data := map[string]string{
		"productCode": c.ProductCode,
		"notifyUrl":   c.NotifyUrl,
		"orderPeriod": strconv.Itoa(c.OrderPeriod),
		"orderPrice":  strconv.FormatFloat(c.OrderPrice, 'g', -1, 64),
		"orderNo":     c.OrderNo,
		"payKey":      c.PayKey,
		"timestamp":   strconv.FormatInt(c.Timestamp, 10),
	}
	// 按照键的字母顺序排序
	keyList := make([]string, 0)
	for key := range data {
		keyList = append(keyList, key)
	}
	sort.Strings(keyList)
	sortedData := map[string]string{}
	for _, key := range keyList {
		sortedData[key] = data[key]
	}
	return data
}

type CreateOrderResponse struct {
	ProductCode     string `json:"productCode"`
	PaymentName     string `json:"paymentName"`
	TransactionType string `json:"transactionType"`
	PayUrl          string `json:"PayUrl"`
	OrderNo         string `json:"OrderNo"`
	MerchantOrderNo string `json:"MerchantOrderNo"`
	Sign            string `json:"sign"`
}

type SubmitOrder struct {
	OrderPeriod int    `json:"orderPeriod"`
	NotifyUrl   string `json:"notifyUrl"`
	OrderPrice  string `json:"orderPrice"`
	OrderNo     string `json:"orderNo"`
	ProductCode string `json:"productCode"`
	ExValue     string `json:"exValue"`
	Ip          string `json:"ip"`
	PayKey      string `json:"payKey"`
	PaySecret   string `json:"paySecret"`
}

func (s *SubmitOrder) ToStrMap() map[string]string {
	data := map[string]string{
		"orderPeriod": strconv.Itoa(s.OrderPeriod),
		"notifyUrl":   s.NotifyUrl,
		"orderPrice":  s.OrderPrice,
		"orderNo":     s.OrderNo,
		"productCode": s.ProductCode,
		"exValue":     s.ExValue,
		"ip":          s.Ip,
		"payKey":      s.PayKey,
	}
	return data
}

type SubmitOrderResponse struct {
	PayKey     string `json:"payKey"`
	StatusCode string `json:"statusCode"`
	Msg        string `json:"msg"`
	Code       int    `json:"code"`
}
