package pposRequest

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

//退单审批
func (that *PPOSClient) NewOrderApprovalCheckRequest() *RefundApprovalRequest {
	return &RefundApprovalRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/pub/audit/check2",
		},
		bT: time.Now(),
	}
}

//创建审批工单
func (that *PPOSClient) NewOrderOrderApprovalCreateRequest() *RefundApprovalRequest {
	return &RefundApprovalRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/pub/audit/startOrderAudit",
		},
		bT: time.Now(),
	}
}

//验证退单是否需要审批（退单后）
func (that *PPOSClient) NewPubAuditRefundOrderCheckRequest() *RefundApprovalRequest {
	return &RefundApprovalRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/pub/audit/refundOrderCheck",
		},
		bT: time.Now(),
	}
}

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

func (that *RefundApprovalRequest) SetParentOrderNo(value string) *RefundApprovalRequest {
	that.initParams()
	that.params.Set("parent_order_no", value) //（订单中心的退单总单），唯一
	return that
}
func (that *RefundApprovalRequest) SetSkuItems(value string) *RefundApprovalRequest {
	that.initParams()
	that.params.Set("sku_items", value) //需要退单的sku,格式sku:num:code-code,若多个，用逗号拼接,即sku:num:code-code,sku:num:code-code,sku:num:code-code
	return that
}
func (that *RefundApprovalRequest) SetType(value int) *RefundApprovalRequest {
	that.initParams()
	that.params.Set("type", utils.ToString(value)) //审批订单类型（1：正向单，2:反向单）
	return that
}

//Params ...
func (that *RefundApprovalRequest) 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 RefundApprovalResponse struct {
	AuditType int64 `json:"audit_type"` // 审批验证结果类型（1：需要审批，且为批后生效；2：需要审批，且为事后审批；3：无需审批）
}

//Do ...
func (that *RefundApprovalRequest) Do() (*RefundApprovalResponse, 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.(map[string]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码01")
	}
	_slice := []string{"audit_type"}
	for _, _v := range _slice {
		if _, _ok := _mp[_v]; !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确,错误代码02")
		}
	}
	//返回结果
	_rs := new(RefundApprovalResponse)
	if _err := that.ParseResponeMp(_rs, _mp); _err != nil {
		return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
	}
	return _rs, nil
}

//Do ...
func (that *RefundApprovalRequest) Do2() (bool, error) {
	//返回data
	_, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return false, _err
	}
	return true, nil
}

//Do ...
func (that *RefundApprovalRequest) Do3() (int64, error) {
	//返回data
	_data, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return 0, _err
	}
	if _rs, _ok := _data.(map[string]interface{}); _ok {
		return utils.ToInt64(_rs["audit_type"]), nil
	}
	return 0, nil
}
