<?php

// 志愿者排名【学校】
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$province = ParamUtil::getRequestString("province");//省份
$city = ParamUtil::getRequestString("city");//市名称
$district = ParamUtil::getRequestString("district");//区县名称

$result = array();

//参数检查
if(empty($unionId) || empty($province) || empty($city)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

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

//查询志愿者排名
$rankList = array();
$sql = "select original_source, school_no, sum(ability_point) as ability_point from user_mst where delete_flg = false and province='{$province}' and city='{$city}' and district='{$district}' group by  original_source, school_no order by ability_point desc";
$db = &CompassDBManager::getInstance();
$tmpList = $db->executeQuery($sql);

if(!empty($tmpList)) {
	foreach($tmpList as $tmp) {
		//获取学校名称（TODO 以后从家校家园系统获取）
		$tmp["school"] = "测试学校";
		//获取该区县下辖学校数量
		$sql = "select count(*) as user_count from user_mst where delete_flg = false and province='{$province}' and city='{$city}' and original_source='{$tmp["original_source"]}' and school_no='{$tmp["school_no"]}'";
		$tmpList2 = $db->executeQuery($sql);
		$tmp["user_count"] = $tmpList2[0]['user_count'];
		$rankList[] = $tmp;
	}
}

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

//返回结果
$result["rankList"] = $rankList;
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;
}
?>