package pposRequest

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

//参数：子系统ID
func (that *PPOSClient) NewAuditNoticeRequest(sysID int64, businessType int64) (*AuditNoticeRequest, error) {
	switch sysID {
	case 11:
		return that.NewMemberAuditNoticeRequest(), nil //会员
	case 12:
		return that.NewTicketAuditNoticeRequest(), nil //门票
	case 13:
		return that.NewFieldAuditNoticeRequest(), nil //场地
	case 14:
		return that.NewGoodsAuditNoticeRequest(), nil //商品
	case 15:
		return that.NewCosAuditNoticeRequest(), nil //通用收款
	case 16:
		return that.NewEventsAuditNoticeRequest(), nil //赛事
	case 17:
		if businessType == 1702 {
			return that.NewCourseAuditNoticeRequest(), nil //课程
		}
		if businessType == 1704 {
			return that.NewClassAuditNoticeRequest(), nil //课时
		}
		return nil, errors.New("not this businessType")
	case 18:
		return that.NewLeaseAuditNoticeRequest(), nil //租赁
	default:
		return nil, errors.New("not this type request")
	}
}

//订单支付审批回调（门票订单）
func (that *PPOSClient) NewTicketAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/ticket/order/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调（通用收款订单）
func (that *PPOSClient) NewCosAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cos/order/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调 (商品订单)
func (that *PPOSClient) NewGoodsAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/goods/goodsOrder/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调（场地订单）
func (that *PPOSClient) NewFieldAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/field/order/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调（会员订单）
func (that *PPOSClient) NewMemberAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/cardWork/memberCard/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调 (赛事订单)
func (that *PPOSClient) NewEventsAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/events/order/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调 (培训课时订单)
func (that *PPOSClient) NewClassAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/classOrder/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调 (培训课程订单)
func (that *PPOSClient) NewCourseAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/courseOrder/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

//订单支付审批回调 (租赁订单)
func (that *PPOSClient) NewLeaseAuditNoticeRequest() *AuditNoticeRequest {
	return &AuditNoticeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/lease/order/auditNotice",
		},
		publicParamsSlice: []string{
			"parent_order_no",
			"order_no",
		},
		bT: time.Now(),
	}
}

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

func (that *AuditNoticeRequest) SetParentOrderNo(value string) *AuditNoticeRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //订单中心的总订单号，唯一
	return that
}
func (that *AuditNoticeRequest) SetOrderNo(value string) *AuditNoticeRequest {
	that.initParams()
	that.params.Set("order_no", value) //订单订单号(业务子系统的订单号)，唯一
	return that
}

//Params ...
func (that *AuditNoticeRequest) 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
}

//Do ...
func (that *AuditNoticeRequest) Do() (interface{}, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return nil, _err
	}
	_str, _ok := _data.(string)
	if _ok {
		return _str, nil
	}
	//
	return _data, nil
}
