<?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.TEExamStudentMapper">

    <select id="examStudentQuery"
            resultType="com.qmth.themis.business.dto.response.TEExamStudentDto">
        select
        tees.id,
        tee.id as examId,
        tee.name as examName,
        tees.name,
        tees.`identity`,
        teea.code,
        tees.course_code as courseCode,
        tees.course_name as courseName,
        IF(tees.enable = 1, '启用', '停用') as enableText,
        tees.enable,
        tees.room_code as roomCode,
        tees.room_name as roomName,
        tees.grade,
        tees.class_no as classNo,
        teea.id as examActivityId,
        teea.code as activityCode,
        stu.base_photo_path basePhotoUrl
        from
        t_e_exam_student tees
        left join t_e_student stu on tees.student_id=stu.id
        left join t_e_exam tee on
        tees.exam_id = tee.id
        left join t_e_exam_activity teea on
        tees.exam_activity_id = teea.id
        <where>
            <if test="examId != null and examId != ''">
                and tees.exam_id = #{examId}
            </if>
            <if test="activityId != null and activityId != ''">
                and tees.exam_activity_id = #{activityId}
            </if>
            <if test="identity != null and identity != ''">
                and tees.identity like concat('%', #{identity}, '%')
            </if>
            <if test="name != null and name != ''">
                and tees.name like concat('%', #{name}, '%')
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tees.room_code like concat('%', #{roomCode}, '%')
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tees.course_code = #{courseCode}
            </if>
            <if test="enable != null and enable != '' or enable == 0">
                and tees.enable = #{enable}
            </if>
            <if test="grade != null and grade != ''">
                and tees.grade like concat('%', #{grade}, '%')
            </if>
            <if test="classNo != null and classNo != ''">
                and tees.class_no like concat('%', #{classNo}, '%')
            </if>
            <if test="hasPhoto != null">
                <if test="hasPhoto == 0">
                    and stu.base_photo_path is null
                </if>
                <if test="hasPhoto == 1">
                    and stu.base_photo_path is not null
                </if>
            </if>
        </where>
        order by tees.id
    </select>

    <select id="getTotalCount" resultType="java.util.Map">
        select t.exam_activity_id activityId,count(1) cc from t_e_exam_student
        t left join t_e_student f on t.student_id=f.id
        where t.exam_id = #{examId}
        <if test="orgId != null">
            and f.org_id=#{orgId}
        </if>
        <if test="activityId != null and activityId != ''">
            and t.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and t.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        group by t.exam_activity_id
    </select>

    <select id="getTotalCountInfo"
            resultType="com.qmth.themis.business.bean.admin.ExamViewCountListBean">
        select
        t.*,
        if(t.monitorStatus = 'FINISHED',
        t.examTotal - t.actualExamTotal,
        0) as deficiencyExamTotal
        from
        (
        select
        tee.name as examName,
        t.exam_id examId,
        t.exam_activity_id examActivityId,
        t.room_code roomCode,
        t.room_name roomName,
        count(1) examTotal,
        teea.code as examActivityCode,
        tee.monitor_status as monitorStatus,
        (
        select
        count(distinct toer.exam_student_id)
        from
        t_oe_exam_record toer
        join t_e_exam_student tees on
        toer.exam_student_id = tees.id
        where
        toer.exam_id = t.exam_id
        and toer.exam_activity_id = t.exam_activity_id
        and tees.room_code = t.room_code and tees.room_name = t.room_name and toer.first_start_time is not null) as actualExamTotal
        from
        t_e_exam_student t
        left join t_e_student h on
        t.student_id = h.id
        left join t_e_exam_activity teea on
        teea.id = t.exam_activity_id
        join t_e_exam tee on
        tee.id = t.exam_id
        where t.exam_id = #{examId}
        <if test="orgId != null">
            and h.org_id=#{orgId}
        </if>
        <if test="activityId != null and activityId != ''">
            and t.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and t.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        group by
        t.exam_id,
        t.exam_activity_id,
        t.room_code,
        t.room_name) t
        order by
        t.examId
    </select>

    <select id="getDoneCountByActivityIds"
            resultType="java.util.Map">
        select t.exam_activity_id activityId,t.room_code roomCode,
        count(distinct(toer.exam_student_id)) cc
        from t_e_exam_student t
        left join t_oe_exam_record toer on
        toer.exam_student_id = t.id
        where t.exam_id = #{examId} and t.current_record_id is not null
        <if test="activityIds != null and activityIds != '' and activityIds.size > 0">
            and t.exam_activity_id in
            <foreach collection="activityIds" item="acid" index="index"
                     open="(" close=")" separator=",">
                #{acid}
            </foreach>
        </if>
        group by t.exam_activity_id, t.room_code
        order by t.exam_id
    </select>

    <select id="getAbsentCountByActivityIds"
            resultType="java.util.Map">
        select t.exam_activity_id activityId,t.room_code roomCode,count(1) cc
        from t_e_exam_student t
        where t.exam_id = #{examId} and t.current_record_id is null
        <if test="activityIds != null and activityIds != '' and activityIds.size > 0">
            and t.exam_activity_id in
            <foreach collection="activityIds" item="acid" index="index"
                     open="(" close=")" separator=",">
                #{acid}
            </foreach>
        </if>
        group by t.exam_activity_id, t.room_code
        order by t.exam_id
    </select>

    <select id="examRoomQuery"
            resultType="com.qmth.themis.business.dto.response.RoomCodeQueryDto">
        select
        DISTINCT
        tees.room_code as roomCode,
        tees.room_name as roomName
        from
        t_e_exam_student tees
        <where>
            <if test="roomName != null and roomName != ''">
                and tees.room_name like concat('%', #{roomName}, '%')
            </if>
        </where>
        order by tees.room_code
    </select>

    <select id="getExamDeficiencyPage"
            resultType="com.qmth.themis.business.bean.admin.ExamDeficiencyListBean">
        select
        *
        from
        (
        select
        distinct tee.name as examName,
        t.exam_id examId,
        t.exam_activity_id examActivityId,
        t.room_code roomCode,
        t.course_code courseCode,
        t.course_name courseName,
        t.name,
        t.identity ,
        t.room_name roomName,
        f.code as examActivityCode,
        t.id
        from
        t_e_exam_student t
        join t_e_exam_activity f on
        t.exam_activity_id = f.id
        join t_e_exam tee on
        tee.id = t.exam_id
        where
        NOT EXISTS(
        select distinct toer.exam_student_id
        from
        t_oe_exam_record toer
        where t.id = toer.exam_student_id)
        and t.exam_id = #{examId}
        and t.exam_activity_id in
        <foreach collection="activityIds" item="acid" index="index"
                 open="(" close=")" separator=",">
            #{acid}
        </foreach>
        <if test="activityId != null and activityId != ''">
            and t.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and t.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        <if test="identity != null and identity != ''">
            and t.identity like concat('%',#{identity},'%')
        </if>
        <if test="name != null and name != ''">
            and t.name like concat('%',#{name},'%')
        </if>
        union all
        select
        distinct tee.name as examName,
        t.exam_id examId,
        t.exam_activity_id examActivityId,
        t.room_code roomCode,
        t.course_code courseCode,
        t.course_name courseName,
        t.name,
        t.identity ,
        t.room_name roomName,
        f.code as examActivityCode,
        t.id
        from
        t_oe_exam_record toer
        join t_e_exam_student t on
        t.id = toer.exam_student_id
        and t.exam_id = toer.exam_id
        and t.exam_activity_id = toer.exam_activity_id
        join t_e_exam_activity f on
        t.exam_activity_id = f.id
        join t_e_exam tee on
        tee.id = t.exam_id
        where
        toer.exam_id = #{examId}
        and toer.exam_activity_id in
        <foreach collection="activityIds" item="acid" index="index"
                 open="(" close=")" separator=",">
            #{acid}
        </foreach>
        <if test="activityId != null and activityId != ''">
            and t.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and t.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        <if test="identity != null and identity != ''">
            and t.identity like concat('%',#{identity},'%')
        </if>
        <if test="name != null and name != ''">
            and t.name like concat('%',#{name},'%')
        </if>
        and toer.first_start_time is null
        and not exists(
        select
        *
        from
        (
        select
        distinct t.exam_student_id as id
        from
        t_oe_exam_record t
        join t_e_exam_student s on
        t.exam_student_id = s.id
        where
        t.exam_id = #{examId}
        and t.first_start_time is not null
        and t.exam_activity_id in
        <foreach collection="activityIds" item="acid" index="index"
                 open="(" close=")" separator=",">
            #{acid}
        </foreach>
        <if test="activityId != null and activityId != ''">
            and s.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and s.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and s.course_code = #{courseCode}
        </if>
        <if test="identity != null and identity != ''">
            and s.identity like concat('%',#{identity},'%')
        </if>
        <if test="name != null and name != ''">
            and s.name like concat('%',#{name},'%')
        </if>
        ) temp
        where
        temp.id = toer.exam_student_id)
            ) t
        order by
        t.examActivityId,
        t.roomCode
    </select>

    <select id="getPageForStudentLog"
            resultType="com.qmth.themis.business.bean.admin.ExamStudentLogListBean">
        select tee.name as examName,t.id examStudentId,t.exam_id examId,t.exam_activity_id
        examActivityId,t.room_code
        roomCode,t.course_code courseCode,t.course_name
        courseName,t.name,t.identity
        ,t.room_name roomName,teea.code as examActivityCode from
        t_e_exam_student t
        left join t_e_student h on t.student_id=h.id
        left join t_e_exam_activity teea on teea.id = t.exam_activity_id
        join t_e_exam tee on tee.id = t.exam_id
        where t.exam_id = #{examId}
        <if test="orgId != null">
            and h.org_id=#{orgId}
        </if>
        <if test="activityId != null and activityId != ''">
            and t.exam_activity_id = #{activityId}
        </if>
        <if test="roomCode != null and roomCode != ''">
            and t.room_code =#{roomCode}
        </if>
        <if test="courseCode != null and courseCode != ''">
            and t.course_code = #{courseCode}
        </if>
        <if test="identity != null and identity != ''">
            and t.identity like concat('%',#{identity},'%')
        </if>
        <if test="name != null and name != ''">
            and t.name like concat('%',#{name},'%')
        </if>
        order by t.id
    </select>

    <update id="updateAlreadyExamCount">
	update t_e_exam_student set already_exam_count=#{alreadyExamCount} where id=#{examStudentId} and (already_exam_count is null or already_exam_count <![CDATA[ < ]]> #{alreadyExamCount})
	</update>

    <update id="updateCurrentRecordId">
	update t_e_exam_student set current_record_id=#{currentRecordId} where id=#{examStudentId} and (current_record_id is null or current_record_id <![CDATA[ < ]]> #{currentRecordId})
	</update>

    <sql id="markResultCommon">
        select
        t.examActivityCode,
        t.name,
        t.identity,
        t.courseCode,
        t.courseName,
        t.roomCode,
        t.roomName,
        t.grade,
        t.classNo,
        if(t.examCount <![CDATA[ <= ]]> 0,'是','否') as examAbsent,
        t.examCount,
        t.examName,
        if(t.breachCount is null,0,t.breachCount) as breachCount,
        ifnull(t.objectiveScore,0) as objectiveScore,
        t.examId,
        t.examActivityId,
        t.subjectiveScore,
        (t.objectiveScore + t.subjectiveScore) as sumScore,
        t.examStudentId
        from
        (
        select tees.id as examStudentId,tee.id as examId,tee.name as examName,teea.id as examActivityId, teea.code as examActivityCode, tes.name, tes.`identity`, tees.course_code as courseCode, tees.course_name as courseName, tees.room_code as roomCode, tees.room_name as roomName, tees.grade, tees.class_no as classNo, (
        select
        count(1)
        from
        t_oe_exam_record toer
        where
        toer.exam_student_id = tees.id and toer.first_start_time is not null) as examCount,
        (select count(toer.id) from t_oe_exam_record toer
        where toer.exam_student_id = tees.id and toer.breach_status = 0) as breachCount,
        0 as objectiveScore,
        0 as subjectiveScore
        from
        t_e_student tes
        left join t_e_exam_student tees on
        tees.student_id = tes.id
        left join t_e_exam tee on
        tee.id = tees.exam_id
        left join t_e_exam_activity teea on
        teea.id = tees.exam_activity_id
        <where>
            <if test="examId != null and examId != ''">
                and tee.id = #{examId}
            </if>
            <if test="activityId != null and activityId != ''">
                and teea.id = #{activityId}
            </if>
            <if test="identity != null and identity != ''">
                and tes.identity like concat('%', #{identity}, '%')
            </if>
            <if test="name != null and name != ''">
                and tes.name like concat('%', #{name}, '%')
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tees.course_code = #{courseCode}
            </if>
        </where> ) t order by t.examStudentId
    </sql>

    <select id="markResultQuery" resultType="com.qmth.themis.business.dto.response.MarkResultDto">
        <include refid="markResultCommon" />
    </select>

    <select id="markResultQueryExportIpage" resultType="com.qmth.themis.business.dto.MarkResultSimpleExportDto">
        <include refid="markResultCommon" />
    </select>

    <select id="findExamStudentNeedMark" resultType="com.qmth.themis.business.dto.response.TEExamStudentDto">
        select
        tees.id,
        tees.name,
        tees.`identity`,
        tees.course_code as courseCode,
        tees.course_name as courseName,
        teea.code as activityCode
        from t_e_exam_student tees
        inner join t_e_exam_activity teea
        on tees.exam_activity_id = teea.id
        and teea.enable = 1
        <where>
            tees.exam_id = #{examId}
            and tees.enable = 1
            and tees.id <![CDATA[ > ]]> #{idGt}
            and tees.already_exam_count is not null
            and tees.already_exam_count <![CDATA[ > ]]> 0
            <if test="courseCode != null and courseCode != ''">
                and tees.course_code = #{courseCode}
            </if>
        </where>
        order by tees.id
        limit #{count}
    </select>

    <select id="findExamStudentNeedMarkAll" resultType="com.qmth.themis.business.entity.TEExamStudent">
        select tees.* from t_e_exam_student tees
            join t_oe_exam_record toer
                on toer.exam_id = tees.exam_id
                and toer.exam_activity_id = tees.exam_activity_id
                and toer.exam_student_id = tees.id
        <where>
            <if test="examId != null and examId != ''">
                and tees.exam_id = #{examId}
            </if>
          and tees.enable = 1
          and tees.already_exam_count is not null
          and tees.already_exam_count <![CDATA[ > ]]> 0
            <if test="examPaperIdSet != null and examPaperIdSet != '' and examPaperIdSet.size > 0">
                and toer.paper_id in
                <foreach collection="examPaperIdSet" item="paperId" index="index" open="(" close=")" separator=",">
                    #{paperId}
                </foreach>
            </if>
          and (toer.status = 'FINISHED' or toer.status = 'PERSISTED')
          and toer.first_start_time is not null
          and toer.finish_time is not null
          and (toer.breach_status is null or toer.breach_status = 1) order by tees.id
            <!--limit 510-->
        </where>
    </select>

    <select id="findAlreadyExamCountZero" resultType="com.qmth.themis.business.entity.TEExamStudent">
        select tees.* from t_e_exam_student tees
            join t_oe_exam_record toer on toer.exam_student_id = tees.id
        <where>
            tees.already_exam_count = 0
          and toer.first_start_time is not null
          and toer.finish_time is not null
          and toer.objective_score <![CDATA[ > ]]> 0
            <if test="examId != null and examId != ''">
                and tees.exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and tees.exam_activity_id = #{examActivityId}
            </if>
            <if test="id != null and id != ''">
                and tees.id = #{id}
            </if>
            <if test="examRecordId != null and examRecordId != ''">
                and toer.id = #{examRecordId}
            </if>
        </where>
    </select>
</mapper>