/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2021-04-12
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */

package pposRequest

import (
	"PPOSUtils/utils"
	"encoding/json"
	"errors"
	"net/url"
)

//new...
func (that *PPOSClient) NewTrainingInstitution() *TrainingInstitutionRequest {
	return &TrainingInstitutionRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/class/trainingInstitution/list",
		},
	}
}

//Request...
type TrainingInstitutionRequest struct {
	Request
}

//设置商户ids
func (that *TrainingInstitutionRequest) SetBusinessIds(value string) *TrainingInstitutionRequest {
	that.initParams()
	that.params.Set("business_ids", value)
	return that
}

func (that *TrainingInstitutionRequest) SetPage(value int64) *TrainingInstitutionRequest {
	that.initParams()
	that.params.Set("page", utils.ToString(value)) //页码
	return that
}

func (that *TrainingInstitutionRequest) SetSize(value int64) *TrainingInstitutionRequest {
	that.initParams()
	that.params.Set("size", utils.ToString(value)) //页码
	return that
}

func (that *TrainingInstitutionRequest) SetStatus(value int64) *TrainingInstitutionRequest {
	that.initParams()
	that.params.Set("status", utils.ToString(value)) //状态
	return that
}

//Params ...
func (that *TrainingInstitutionRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	return _params, nil
}

type TrainingInstitutionResponse struct {
	List []TrainingInstitutionList `json:"list"` //list
}

type TrainingInstitutionList struct {
	BusinessId   int64  `json:"business_id"`
	TrainingId   int64  `json:"training_id"`
	TrainingName string `json:"training_name"`
}

//Do ...
func (that *TrainingInstitutionRequest) Do() (TrainingInstitutionResponse, error) {
	_rs := TrainingInstitutionResponse{}
	//
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return _rs, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_sliceData, _ok := _data.(map[string]interface{})
	if !_ok {
		return _rs, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	//
	for _, _val := range _slice {
		if _, _ok := _sliceData[_val]; !_ok {
			return _rs, errors.New(that.apiAddress + "请求响应参数不正确2")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + _err.Error())
	}
	return _rs, nil
}
