<?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.teachcloud.report.business.mapper.TBExamStudentMapper">

    <sql id="conditionSql">
        <if test="schoolId != null and schoolId != ''">
            and tbes.school_id = #{schoolId}
        </if>
        <if test="examId != null and examId != ''">
            and tbes.exam_id = #{examId}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and tbes.course_code = #{courseCode}
        </if>
        <if test="absent != null">
            and tbes.absent = #{absent}
        </if>
        <if test="current != null">
            and tbes.student_current = #{current}
        </if>
        <if test="status != null and status != ''">
            and tbec.publish_status = #{status}
        </if>
    </sql>

    <select id="selectTeachCollegeList"
            resultType="com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult">
        select
        DISTINCT tbes.teach_college_id as teachCollegeId,
        so.name as teachCollegeName,
        so.code as teachCollegeCode
        from
        t_b_exam_student tbes
        join sys_org so on
        so.id = tbes.teach_college_id
        join t_b_exam_course tbec on
            tbec.course_code = tbes.course_code
        <where>
            <include refid="conditionSql"/>
        </where>
    </select>

    <select id="selectInspectCollegeList"
            resultType="com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult">
        select
        DISTINCT tbes.inspect_college_id as inspectCollegeId,
        so.name as inspectCollegeName,
        so.code as inspectCollegeCode
        from
        t_b_exam_student tbes
        join sys_org so on
        so.id = tbes.inspect_college_id
        join t_b_exam_course tbec on
            tbec.course_code = tbes.course_code
        <where>
            <include refid="conditionSql"/>
        </where>
    </select>

    <select id="selectInspectCollegeListByTeacher"
            resultType="com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult">
        select
        DISTINCT tbes.inspect_college_id as inspectCollegeId,
        so.name as inspectCollegeName,
        so.code as inspectCollegeCode
        from
        t_b_exam_student tbes
        join sys_org so on
        so.id = tbes.inspect_college_id
        join t_b_exam_course tbec on
            tbec.course_code = tbes.course_code
        <where>
            <include refid="conditionSql"/>
            <if test="collegeIds != null and collegeIds != '' and collegeIds.size > 0">
                and tbes.teach_college_id in
                <foreach collection="collegeIds" item="collegeId" index="index" open="(" separator="," close=")">
                    #{collegeId}
                </foreach>
            </if>
            and tbes.teacher_id = #{teacherId}
        </where>
    </select>

    <select id="selectTeachList" resultType="com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult">
        select
        DISTINCT tbes.teacher_id as teacherId,
        su.real_name as teacherName
        from
        t_b_exam_student tbes
        join sys_user su on
        su.id = tbes.teacher_id
        join t_b_exam_course tbec on
            tbec.course_code = tbes.course_code
        <where>
            <include refid="conditionSql"/>
            <if test="inspectCollegeId != null and inspectCollegeId != ''">
                and tbes.inspect_college_id = #{inspectCollegeId}
            </if>
            <if test="teachCollegeId != null and teachCollegeId != ''">
                and tbes.teach_college_id = #{teachCollegeId}
            </if>
        </where>
    </select>

    <select id="selectClassList" resultType="com.qmth.teachcloud.report.business.bean.result.TBExamStudentResult">
        select
        DISTINCT tbes.clazz_id as clazzId,
        tbsc.clazz_name as clazzName,
        tbsc.clazz_code as clazzCode
        from
        t_b_exam_student tbes
        join t_b_school_clazz tbsc on
            tbsc.id = tbes.clazz_id
        join t_b_exam_course tbec on
            tbec.course_code = tbes.course_code
        <where>
            <include refid="conditionSql"/>
            <if test="teacherId != null and teacherId != ''">
                and tbes.teacher_id = #{teacherId}
            </if>
            <if test="inspectCollegeIdList != null and inspectCollegeIdList != '' and inspectCollegeIdList.size > 0">
                and tbes.inspect_college_id in
                <foreach collection="inspectCollegeIdList" item="inspectCollegeId" index="index" open="(" separator="," close=")">
                    #{inspectCollegeId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listTeachCollegeResult"
            resultType="com.qmth.teachcloud.report.business.bean.result.TeachCollegeResult">
        select * from(SELECT
        bs.name as semester,
        d.exam_name examName,
        a.course_code courseCode,
        a.course_name courseName,
        a.ticket_number ticketNumber,
        a.student_code studentCode,
        a.name,
        a.student_current `current`,
        case a.student_current when true then '应届' else '非应届' end as currentStr,
        round(b.objective_score, 1) objectiveScore,
        round(b.subjective_score, 1) subjectiveScore,
        round(b.total_score, 1) totalScore,
        round(c.assigned_score, 1) assignedScore,
        e.clazz_code clazzCode,
        e.clazz_name clazzName,
        f.name teachCollegeName,
        g.name inspectCollegeName,
        h.real_name teacherName
        FROM
        t_b_exam_student a
        LEFT JOIN
        t_b_exam_record b ON a.id = b.exam_student_id
        LEFT JOIN
        t_a_exam_course_record c ON b.id = c.exam_record_id
        LEFT JOIN
        t_b_exam d ON a.exam_id = d.id
        LEFT JOIN
        t_b_school_clazz e ON a.clazz_id = e.id
        LEFT JOIN
        sys_org f ON a.teach_college_id = f.id
        LEFT JOIN
        sys_org g ON a.inspect_college_id = g.id
        LEFT JOIN
        sys_user h ON a.teacher_id = h.id
        join basic_semester bs on bs.id = d.semester_id
        <where>
            and d.school_id = #{schoolId}
            and a.absent = false
            <if test="semesterId != null and semesterId != ''">
                and d.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="current != null">
                and a.student_current = #{current}
            </if>
            <if test="inspectCollegeId != null">
                and a.inspect_college_id = #{inspectCollegeId}
            </if>
            <if test="teacherId != null">
                and a.teacher_id = #{teacherId}
            </if>
            <if test="studentParam != null and studentParam != ''">
                and (a.ticket_number like concat('%' ,#{studentParam}, '%')
                or a.student_code like concat('%' ,#{studentParam}, '%')
                or a.name like concat('%' ,#{studentParam}, '%'))
            </if>
            <if test="collegeIds != null and collegeIds != '' and collegeIds.size > 0">
                and a.teach_college_id in
                <foreach collection="collegeIds" item="collegeId" index="index" open="(" separator="," close=")">
                    #{collegeId}
                </foreach>
            </if>
        </where> ) t
<!--        <if test="column != null and column != '' and order != null and order != ''">-->
<!--            order by t.${column} ${order}-->
<!--        </if>-->
        <choose>
            <when test="column != null and column != '' and order != null and order != ''">
                order by t.${column} ${order}
            </when>
            <otherwise>
                order by t.courseCode ASC,t.current DESC,t.studentCode ASC
            </otherwise>
        </choose>
    </select>

    <select id="listInspectCollegeResult"
            resultType="com.qmth.teachcloud.report.business.bean.result.InspectCollegeResult">
        select * from(SELECT
        bs.name as semester,
        d.exam_name examName,
        a.course_code courseCode,
        a.course_name courseName,
        a.ticket_number ticketNumber,
        a.student_code studentCode,
        a.name,
        a.student_current `current`,
        case a.student_current when true then '应届' else '非应届' end as currentStr,
        round(c.assigned_score, 1) assignedScore,
        e.clazz_code clazzCode,
        e.clazz_name clazzName,
        f.name teachCollegeName,
        g.name inspectCollegeName,
        h.real_name teacherName
        FROM
        t_b_exam_student a
        LEFT JOIN
        t_b_exam_record b ON a.id = b.exam_student_id
        LEFT JOIN
        t_a_exam_course_record c ON b.id = c.exam_record_id
        LEFT JOIN
        t_b_exam d ON a.exam_id = d.id
        LEFT JOIN
        t_b_school_clazz e ON a.clazz_id = e.id
        LEFT JOIN
        sys_org f ON a.teach_college_id = f.id
        LEFT JOIN
        sys_org g ON a.inspect_college_id = g.id
        LEFT JOIN
        sys_user h ON a.teacher_id = h.id
        join t_b_exam_course tbec on
            tbec.course_code = a.course_code
        join basic_semester bs on bs.id = d.semester_id
        <where>
            and d.school_id = #{schoolId}
            <if test="semesterId != null and semesterId != ''">
                and d.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="current != null">
                and a.student_current = #{current}
            </if>
            <if test="inspectCollegeId != null">
                and a.inspect_college_id = #{inspectCollegeId}
            </if>
            <if test="teacherId != null">
                and a.teacher_id = #{teacherId}
            </if>
            <if test="clazzId != null">
                and a.clazz_id = #{clazzId}
            </if>
            <if test="studentParam != null and studentParam != ''">
                and (a.ticket_number like concat('%' ,#{studentParam}, '%')
                or a.student_code like concat('%' ,#{studentParam}, '%')
                or a.name like concat('%' ,#{studentParam}, '%'))
            </if>
            and tbec.publish_status = 'PUBLISH'
            and a.absent = false
        </where> ) t
        <if test="column != null and column != '' and order != null and order != ''">
            order by t.${column} ${order}
        </if>
    </select>

    <select id="reportList" resultType="com.qmth.teachcloud.report.business.bean.result.TBStudentReportResult">
        SELECT
            c.name collegeName,
            b.exam_code examCode,
            a.course_code courseCode,
            a.student_code studentCode,
            a.course_name courseName,
            b.school_id schoolId,
            b.exam_name examName,
            a.name studentName,
            d.clazz_name clazzName,
            a.absent
        FROM
            t_b_exam_student a
                LEFT JOIN
            t_b_exam b ON a.exam_id = b.id
                LEFT JOIN
            sys_org c ON a.inspect_college_id = c.id
                LEFT JOIN
            t_b_school_clazz d ON a.clazz_id = d.id
        <where>
            and b.school_id = #{schoolId}
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="collegeId != null">
                and a.inspect_college_id = #{collegeId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="clazzId != null">
                and a.clazz_id = #{clazzId}
            </if>
            <if test="absent != null">
                and a.absent = #{absent}
            </if>
            <if test="absent != null">
                and a.absent = #{absent}
            </if>
            <if test="studentCode != null and studentCode.length() > 0">
                and a.student_code = #{studentCode}
            </if>
        </where>
        order by b.school_id desc, b.id desc,a.course_code,c.id,d.id,a.student_code desc
    </select>
    <select id="listExamBySchoolIdAndStudentCode"
            resultType="com.qmth.teachcloud.report.business.bean.result.ExamInfo">
        SELECT
            a.exam_id examId,
            b.exam_code examCode,
            b.exam_name examName,
            b.exam_time createTime,
            bs.name as semester
        FROM
            t_b_exam_student a
                LEFT JOIN
            t_b_exam b ON a.exam_id = b.id
            join basic_semester bs on bs.id = b.semester_id
        <where>
            and b.school_id = #{schoolId}
            and a.student_code = #{studentCode}
        </where>
        GROUP BY a.exam_id , b.exam_code , b.exam_name , b.exam_time
        order by b.exam_time desc
    </select>
    <select id="listCourseByExamIdAndStudentCode"
            resultType="com.qmth.teachcloud.report.business.bean.result.CourseInfo">
        SELECT
            c.exam_code examCode,
            a.course_code courseCode,
            a.course_name courseName,
            a.absent,
            a.student_current as studentCurrent,
            b.publish_status publishStatus
        FROM
            t_b_exam_student a
                LEFT JOIN
            t_b_exam_course b ON a.exam_id = b.exam_id
                AND a.course_code = b.course_code
                LEFT JOIN
            t_b_exam c ON a.exam_id = c.id
        <where>
            and b.school_id = #{schoolId}
            and a.exam_id = #{examId}
            and a.student_code = #{studentCode}
        </where>
    </select>
</mapper>
