package gameServerMgr

import (
	"encoding/json"
	"fmt"
	"strconv"

	. "Framework/managecenterModel"
	"goutil/logUtil"
	"goutil/webUtil"
)

//修改区服注册人数
func UpdateRegisterCount(serverGroupId, registerCount int32) (err error, success bool) {
	success = false

	//定义参数
	requestParamMap := make(map[string]string, 0)
	requestParamMap["ServerGroupId"] = strconv.Itoa(int(serverGroupId))
	requestParamMap["Registrations"] = strconv.Itoa(int(registerCount))

	//构造请求url
	url := fmt.Sprintf("%s/%s", mManageCenterServerAPIUrl, "/API/RegistrationUpdate.ashx")

	//请求url,请求头
	header := webUtil.GetFormHeader()
	transport := webUtil.NewTransport()
	transport.DisableKeepAlives = true
	transport = webUtil.GetTimeoutTransport(transport, 30)

	statusCode, returnBytes, err := webUtil.PostMapData(url, requestParamMap, header, transport)
	//statusCode, returnBytes, err := webUtil.PostMapData(url, requestParamMap, header, nil)
	if err != nil {
		return
	}
	if statusCode != 200 {
		err = fmt.Errorf("StatusCode:%d", statusCode)
		return
	}

	// 解析返回值
	returnObj := new(ReturnObject)
	if err = json.Unmarshal(returnBytes, &returnObj); err != nil {
		logUtil.ErrorLog(fmt.Sprintf("更新区服注册人数出错，反序列化返回值出错，错误信息为：%s, str:%s", err, string(returnBytes)))
		return
	}

	// 判断返回状态是否为成功
	if returnObj.Code != 0 {
		err = fmt.Errorf("更新区服注册人数出错，返回状态：%d，信息为：%s", returnObj.Code, returnObj.Message)
		logUtil.ErrorLog(fmt.Sprintf("更新区服注册人数出错，返回状态：%d，信息为：%s", returnObj.Code, returnObj.Message))
		return
	}

	success = true
	return
}
