<?
// 共通类库
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_leader_user_register.php....Start.", 0);

$openId = ParamUtil::getRequestString("openId");
$unionId = ParamUtil::getRequestString("unionId");
$qrId = ParamUtil::getRequestNumber("qrId", 0);
$headerImg = ParamUtil::getRequestString("headerImg");

ErrorLogger::doOutput("Compass...ajax_leader_user_register.php....openId=" . $openId, 0);
ErrorLogger::doOutput("Compass...ajax_leader_user_register.php....unionId=" . $unionId, 0);
ErrorLogger::doOutput("Compass...ajax_leader_user_register.php....qrId=" . $qrId, 0);

$result = array();

//参数验证
if(empty($qrId)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否已经存在
$param = array();
$param['openid'] = $openId;
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(!empty($userList)) {
	$result["message"] = "用户已注册！";
	responseOK($result);
}

//Qr数据
$governmentQrDat = GovernmentQrDat::getById($qrId);
if(empty($governmentQrDat)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//检查使用次数
if($governmentQrDat->use_count >= $governmentQrDat->max_count) {
	$result["message"] = "已超过注册次数！";
	responseNG($result);
}

//检查日期限制
if(date("Y-m-d")> $governmentQrDat->limit_date) {
	$result["message"] = "已超过可注册时间！";
	responseNG($result);
}

//注册教育局领导账户
$userMst = new UserMst();
$userMst->openid = $openId;
$userMst->unionid = $unionId;
$userMst->name = "教育局领导";//4：教育局领导
$userMst->role = 4;//4：教育局领导
$userMst->header_img = $headerImg;
$userMst->government_id = $governmentQrDat->government_id;
$userMst->province = $governmentQrDat->province;
$userMst->city = $governmentQrDat->city;
$userMst->district = $governmentQrDat->district;
$userMst->save();

//减少使用次数
$governmentQrDat->use_count++;
$governmentQrDat->save();


ErrorLogger::doOutput("Compass...ajax_leader_user_register.php....End.", 0);

$result["message"] = "注册成功！";
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}

?>