package mtunionapi

import (
	"git.rosy.net.cn/baseapi/utils"
	"time"
)

const (
	MtUnionOrderTypeTG = 0 //团购订单
	MtUnionOrderTypeJD = 2 //酒店订单
	MtUnionOrderTypeWM = 4 //外卖订单
	MtUnionOrderTypeHF = 5 //话费订单
	MtUnionOrderTypeSG = 6 //闪购订单

	MtUnionOrderStatusPay      = 1 //已付款
	MtUnionOrderStatusFinished = 8 //已完成
	MtUnionOrderStatusCanceled = 9 //已退款或风控

	defaultPageSize = 50
)

type OrderListResult struct {
	Total    int `json:"total"`
	DataList []struct {
		Smstitle     string `json:"smstitle"`
		Refundprice  string `json:"refundprice"`
		Refundtime   string `json:"refundtime"`
		Orderid      string `json:"orderid"`
		Refundprofit string `json:"refundprofit"`
		Paytime      string `json:"paytime"`
		Appkey       string `json:"appkey"`
		Payprice     string `json:"payprice"`
		Profit       string `json:"profit"`
		Sid          string `json:"sid"`
		Status       int    `json:"status"`
	} `json:"dataList"`
}

func (a *API) OrderList(orderType int, startTime, endTime int64, page int) (orderListResult *OrderListResult, err error) {
	result, err := a.AccessAPI2("api/orderList", false, map[string]interface{}{
		"ts":        time.Now().Unix(),
		"type":      orderType,
		"startTime": startTime,
		"endTime":   endTime,
		"page":      page,
		"limit":     defaultPageSize,
	})
	if err == nil {
		utils.Map2StructByJson(result, &orderListResult, false)
	}
	return orderListResult, err
}

type CouponListResult struct {
	Total    int `json:"total"`
	DataList []struct {
		AppKey      string `json:"appKey"`
		Sid         string `json:"sid"`
		CouponTime  string `json:"couponTime"`
		Money       string `json:"money"`
		MinUseMoney string `json:"minUseMoney"`
		CouponName  string `json:"couponName"`
		CouponType  string `json:"couponType"`
		CouponCode  string `json:"couponCode"`
		BeginTime   int    `json:"beginTime"`
		EndTime     int    `json:"endTime"`
	} `json:"dataList"`
}

func (a *API) CouponList(orderType int, startTime, endTime int64, page int, sid string) (couponListResult *CouponListResult, err error) {
	result, err := a.AccessAPI2("api/couponList", false, map[string]interface{}{
		"ts":        time.Now().Unix(),
		"type":      orderType,
		"startTime": startTime,
		"endTime":   endTime,
		"page":      page,
		"limit":     defaultPageSize,
		"sid":       sid,
	})
	if err == nil {
		utils.Map2StructByJson(result, &couponListResult, false)
	}
	return couponListResult, err
}

type RtnotifyResult struct {
	Coupon []struct {
		Sequence string `json:"sequence"`
		Orderid  string `json:"orderid"`
		Price    string `json:"price"`
		Profit   string `json:"profit"`
		Usetime  string `json:"usetime"`
	} `json:"coupon"`
	Order struct {
		Smstitle string `json:"smstitle"`
		UID      string `json:"uid"`
		Total    string `json:"total"`
		Quantity string `json:"quantity"`
		Orderid  string `json:"orderid"`
		Dealid   string `json:"dealid"`
		Modtime  string `json:"modtime"`
		Direct   string `json:"direct"`
		Paytime  string `json:"paytime"`
		Sid      string `json:"sid"`
		Status   int    `json:"status"`
	} `json:"order"`
	Refund []struct {
		Quantity   string `json:"quantity"`
		Refundtime string `json:"refundtime"`
		Money      string `json:"money"`
		Orderid    string `json:"orderid"`
		Profit     string `json:"profit"`
	} `json:"refund"`
}

func (a *API) Rtnotify(oid, orderType string) (rtnotifyResult *RtnotifyResult, err error) {
	result, err := a.AccessAPI2("api/rtnotify", false, map[string]interface{}{
		"oid":  oid,
		"type": orderType,
		"full": 1,
	})
	if err == nil {
		utils.Map2StructByJson(result, &rtnotifyResult, false)
	}
	return rtnotifyResult, err
}
