<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
	namespace="com.qmth.themis.business.dao.TEExamCourseMapper">

	<select id="examCourseQuery" resultType="com.qmth.themis.business.dto.response.TEExamCourseDto">
		select
		t.id,
		t.examId,
		t.courseCode,
		t.courseName,
		t.paperCount,
		t.hasPaper,
		t.objectiveShuffle,
		t.optionShuffle,
		t.audioPlayCount,
		t.hasAnswer,
		t.hasAudio,
		if(t.updateName is not null, t.updateName, t.createName) as updateName,
		if(t.updateTime is not null, t.updateTime, t.createTime) as updateTime
		from
		(
		select
		teec.exam_id as examId,teec.id,teec.course_code as courseCode, teec.course_name as courseName, teec.paper_count as paperCount,
		if(teec.paper_count > 0, 1, 0) as hasPaper, teec.objective_shuffle as objectiveShuffle, teec.option_shuffle as
		optionShuffle, teec.audio_play_count as audioPlayCount, teec.has_answer as hasAnswer, teec.has_audio as
		hasAudio, teec.create_time as createTime, teec.update_time as updateTime, (
		select
		t.name
		from
		t_b_user t
		where
		t.id = teec.create_id) as createName, (
		select
		t.name
		from
		t_b_user t
		where
		t.id = teec.update_id) as updateName
		from
		t_e_exam_course teec
		left join t_e_exam tee on teec.exam_id = tee.id
		<where>
			<if test="id != null and id != ''">
				and teec.id = #{id}
			</if>
			<if test="examId != null and examId != ''">
				and teec.exam_id = #{examId}
			</if>
			<if test="courseCode != null and courseCode != ''">
				and teec.course_code like concat('%', #{courseCode}, '%')
			</if>
			<if test="courseName != null and courseName != ''">
				and teec.course_name like concat('%', #{courseName}, '%')
			</if>
			<if test="hasPaper != null and hasPaper != '' or hasPaper == 0">
				<choose>
					<when test="hasPaper == 0">
						and (teec.paper_count = #{hasPaper} or teec.paper_count is null)
					</when>
					<otherwise>
						and teec.paper_count <![CDATA[ > ]]> 0
					</otherwise>
				</choose>
			</if>
		</where>
		) t order by t.courseCode,t.updateTime desc
	</select>

	<select id="findByExamIdAndCourseCode"
		resultType="com.qmth.themis.business.entity.TEExamCourse">
		select * from t_e_exam_course t
		where t.exam_id=#{examId} and t.course_code =#{courseCode}
	</select>
	
	<select id="examCourseQueryForOpen"
		resultType="com.qmth.themis.business.bean.admin.OpenExamCourseBean">
		SELECT
			t.course_code courseCode,
			t.course_name courseName,
			t.paper_count paperCount,
			t.objective_shuffle objectiveShuffle,
			t.option_shuffle optionShuffle
		FROM
			t_e_exam_course t left join t_e_exam f on t.exam_id=f.id
		WHERE
			f.enable=1
		AND	t.exam_id = #{examId}
		<if test="courseCode != null and courseCode !='' ">
		AND t.course_code = #{courseCode}
		</if>
		<if test="hasPaper != null">
			<if test="hasPaper">
			AND t.paper_count>0
			</if>
			<if test="!hasPaper">
			AND (t.paper_count is null or t.paper_count=0)
			</if>
		</if>
	</select>
	
	<select id="findByExamId"
		resultType="com.qmth.themis.business.entity.TEExamCourse">
		select * from t_e_exam_course t
		where t.exam_id=#{examId}
	</select>
	
</mapper>
