<?php

// 2.家校/家园平台班级信息同步接口
require_once ("../user_include.inc");

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

//获取参数
$schoolNo = ParamUtil::getRequestString("schoolNo");
$classNo = ParamUtil::getRequestString("classNo");
$originalSource = ParamUtil::getRequestNumber("originalSource", 0);
$title = ParamUtil::getRequestString("title");
$gradeTitle = ParamUtil::getRequestString("gradeTitle");
$memberCount = ParamUtil::getRequestNumber("memberCount", 0);


//参数检查
if(empty($schoolNo) || empty($classNo) || empty($originalSource) || empty($gradeTitle) || empty($title)) {
	responseNG("参数错误！");
}

//originalSource为1或者2
if($originalSource <> 1 && $originalSource <> 2) {
	responseNG("参数错误！");
}

//判断学校是否已经登陆
$param = array();
$param['school_no'] = $schoolNo;
$param['original_source'] = $originalSource;
$param['delete_flg'] = false;
$schoolList = SchoolMst::getList($param,'id','desc', 0, 1);
if(empty($schoolList)) {
	responseNG("学校不存在！");
}

$schoolMst = $schoolList[0];

//年级数据登录
$param = array();
$param['school_no'] = $schoolNo;
$param['original_source'] = $originalSource;
$param['title'] = $gradeTitle;
$param['delete_flg'] = false;
$gradeList = GradeMst::getList($param,'id','desc', 0, 1);
$gradeMst = new GradeMst();
if(empty($gradeList)) {
	$gradeMst->school_id = $schoolMst->id;
	$gradeMst->school_no = $schoolNo;
	$gradeMst->original_source = $originalSource;
	$gradeMst->title = $gradeTitle;
	$gradeMst->save();
	//查找id
	$param = array();
	$param['school_no'] = $schoolNo;
	$param['original_source'] = $originalSource;
	$param['title'] = $gradeTitle;
	$param['delete_flg'] = false;
	$tmpGradeList = GradeMst::getList($param,'id','desc', 0, 1);
	if(!empty($tmpGradeList)) {
		$gradeMst = $tmpGradeList[0];
	}
} else {
	$gradeMst = $gradeList[0];
}

//班级数据登陆
$param = array();
$param['school_no'] = $schoolNo;
$param['class_no'] = $classNo;
$param['original_source'] = $originalSource;
$param['delete_flg'] = false;
$classList = ClassMst::getList($param,'id','desc', 0, 1);
if(empty($classList)) {
	$classMst = new ClassMst();
	$classMst->school_id = $schoolMst->id;
	$classMst->school_no = $schoolNo;
	$classMst->class_no = $classNo;
	$classMst->original_source = $originalSource;
	$classMst->grade_id = $gradeMst->id;
	$classMst->title = $title;
	$classMst->member_count = $memberCount;
	$classMst->save();
} else {
	$classMst = $classList[0];
	$classMst->grade_id = $gradeMst->id;
	$classMst->title = $title;
	$classMst->member_count = $memberCount;
	$classMst->save();
}

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

responseOK("更新成功！");

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