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

package pposRequest

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

//NewCfgCustomerTypeListByCp 客户类型配置列表(商户端)
func (that *PPOSClient) NewCfgCustomerTypeListByCp() *CfgCustomerTypeRequest {
	return &CfgCustomerTypeRequest{
		Request: Request{
			Client:     that,
			apiAddress: "cfg/customerType/listByCp",
		},
	}
}

//CfgCustomerTypeRequest 客户类型配置列表
type CfgCustomerTypeRequest struct {
	Request
	page int64
	size int64
}

func (that *CfgCustomerTypeRequest) SetPage(value int64) *CfgCustomerTypeRequest {
	that.initParams()
	that.page = value
	that.params.Set("page", utils.ToString(value))
	return that
}

func (that *CfgCustomerTypeRequest) SetSize(value int64) *CfgCustomerTypeRequest {
	that.initParams()
	that.size = value
	that.params.Set("size", utils.ToString(value))
	return that
}

//Params ...
func (that *CfgCustomerTypeRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	if that.page == 0 && that.size == 0 {
		that.SetPage(1)
		that.SetSize(100)
	}
	return _params, nil
}

//CfgCustomerTypeResponse 响应
type CfgCustomerTypeResponse struct {
	Count int64          `json:"count"` //count
	List  []CustomerType `json:"list"`  //list
	Page  int64          `json:"page"`  //page
	Size  int64          `json:"size"`  //size
}

type CustomerType struct {
	Id             int64  `json:"id"`
	CustomerTypeId int64  `json:"customer_type_id"`
	CompanyId      int64  `json:"company_id"`
	Title          string `json:"title"`
	IsEnable       int64  `json:"is_enable"`
	IsEnableStr    string `json:"is_enable_str"`
	Pid            int64  `json:"pid"`
}

//Do ...
func (that *CfgCustomerTypeRequest) Do() (*CfgCustomerTypeResponse, error) {
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	_slice := []string{
		"count",
		"list",
		"page",
		"size",
	}
	//
	_rs := &CfgCustomerTypeResponse{}
	_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 + "请求响应参数不正确")
		}
	}
	//
	_str, _err := json.Marshal(_sliceData)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed!" + _err.Error())
	}
	_err = json.Unmarshal(_str, &_rs)
	if _err != nil {
		return _rs, errors.New(that.apiAddress + "Umarshal failed2!" + _err.Error())
	}
	return _rs, nil
}
