package pposRequest

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

//NewBasePersonnelGet 获取员工详情
func (that *PPOSClient) NewBasePersonnelList() *PersonnelListRequest {
	return &PersonnelListRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/base/personnel/list",
		},
	}
}

//PersonnelListRequest ...
type PersonnelListRequest struct {
	Request
	Status int64 //状态
}

func (that *PersonnelListRequest) SetStatus(value int64) *PersonnelListRequest {
	that.Status = value
	return that
}

//func (that *PersonnelListRequest) SetIds(value string) {
//	if value != "" {
//		that.Ids = utils.NewSliceString(strings.Split(value, ",")...).ToInt64()
//	}
//}

//func (that *PersonnelListRequest) SetColumn(value string) *PersonnelListRequest {
//	that.Column = value
//	return that
//}

//Params ...
func (that *PersonnelListRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	_params.Set("status", utils.ToString(that.Status))
	_params.Set("page", "1")
	_params.Set("size", "10000")
	//_params.Set("column", utils.ToString(that.Column))
	return _params, nil
}

//Do ...
func (that *PersonnelListRequest) Do() ([]*BasePersonnelListResponse, error) {
	_data, _err := that.Client.GetData(that.apiAddress, that)
	if _err != nil {
		return nil, _err
	}
	//
	//_sliceData, _ok := _data.(map[string]interface{})
	//if !_ok {
	//	return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	//}
	//
	_rs := []*BasePersonnelListResponse{}
	_mp, _ok := _data.(map[string]interface{})
	_sliceData, _ok := _mp["list"].([]interface{})
	if !_ok {
		return nil, errors.New(that.apiAddress + "请求响应格式不正确")
	}
	for _, _v := range _sliceData {
		_vData, _ok := _v.(map[string]interface{})
		if !_ok {
			return nil, errors.New(that.apiAddress + "请求响应格式不正确2")
		}
		//
		_tmp := new(BasePersonnelListResponse)
		if _err := that.ParseResponeMp(_tmp, _vData); _err != nil {
			return nil, errors.New(that.apiAddress + "请求响应格式化异常:" + _err.Error())
		}
		_rs = append(_rs, _tmp)
	}
	return _rs, nil

	//
	//_str, _err := json.Marshal(_sliceData)
	//if _err != nil {
	//	return nil, errors.New(that.apiAddress + "Umarshal failed!")
	//}
	//_err = json.Unmarshal(_str, &_rs)
	//if _err != nil {
	//	return nil, errors.New(that.apiAddress + "Umarshal failed2!")
	//}
	////
	//return _rs, nil
}
