<?php

// 根据课程id获取课程详细信息
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$courseId = ParamUtil::getRequestNumber("courseId", 0);


//参数检查
if(empty($unionId) || empty($courseId)) {
	$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);
}

$userMst = $userList[0];

$result = array();

//每页显示条数
$courseMst = CourseMst::getById($courseId);
if(empty($courseMst)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//获取对应的媒体列表
$courseMediaList = array();
$param = array();
$param['course_id'] = $courseId;
$param['delete_flg'] = false;
$tmpList = CourseMediaDat::getList($param,'id','asc');
if(!empty($tmpList)) {
	foreach($tmpList as $tmp) {
		$tmp->haveBuy = false;
		if(!$tmp->is_free) {
			//收费课程的情况下，判断用户是否已经购买本课程
			$param = array();
			$param['user_id'] = $$userMst->id;
			$param['course_id'] = $courseId;
			$param['media_id'] = $tmp->id;
			$param['is_free'] = false;
			$param['status'] = 'SUCCESS';
			$param['delete_flg'] = false;
			$userBuyList = UserMediaDat::getList($param, "id", "desc", 0, 1);
			if(!empty($userBuyList)) {
				$tmp->haveBuy = true;
			}
			
		}
		$courseMediaList[] = $tmp;
	}
}


//接口返回数据
$result["teacherName"] = $courseMst->teacher_name;
$result["teacherProfile"] = $courseMst->teacher_profile;
$result["mediaCount"] = $courseMst->media_count;
$result["list"] = $courseMediaList;

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


//返回结果
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;
}
?>