// 增加邮费模板
//  NOTE: 无需指定 Id 字段
func (c *Client) MerchantExpressAddDeliveryTemplate(template *express.DeliveryTemplate) (templateId int64, err error) {
	if template == nil {
		err = errors.New("template == nil")
		return
	}

	template.Id = 0

	var request = struct {
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		DeliveryTemplate: template,
	}

	var result struct {
		Error
		TemplateId int64 `json:"template_id"`
	}

	hasRetry := false
RETRY:
	token, err := c.Token()
	if err != nil {
		return
	}
	_url := merchantExpressAddURL(token)
	if err = c.postJSON(_url, request, &result); err != nil {
		return
	}

	switch result.ErrCode {
	case errCodeOK:
		templateId = result.TemplateId
		return

	case errCodeTimeout:
		if !hasRetry {
			hasRetry = true
			timeoutRetryWait()
			goto RETRY
		}
		fallthrough

	default:
		err = &result.Error
		return
	}
}
// 修改邮费模板
//  NOTE: 需要指定 template.Id 字段
func (c *Client) MerchantExpressUpdateDeliveryTemplate(template *express.DeliveryTemplate) (err error) {
	if template == nil {
		return errors.New("template == nil")
	}

	var request = struct {
		TemplateId       int64                     `json:"template_id"`
		DeliveryTemplate *express.DeliveryTemplate `json:"delivery_template"`
	}{
		TemplateId:       template.Id,
		DeliveryTemplate: template,
	}

	template.Id = 0 // 请求的时候不携带这个参数

	var result Error

	hasRetry := false
RETRY:
	token, err := c.Token()
	if err != nil {
		return
	}
	_url := merchantExpressUpdateURL(token)
	if err = c.postJSON(_url, request, &result); err != nil {
		return
	}

	switch result.ErrCode {
	case errCodeOK:
		return

	case errCodeTimeout:
		if !hasRetry {
			hasRetry = true
			timeoutRetryWait()
			goto RETRY
		}
		fallthrough

	default:
		err = &result
		return
	}
}