<?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,
        stu.mobile_number as mobileNumber
        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 = #{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 CAST(teea.code AS UNSIGNED),CAST(tees.room_code AS UNSIGNED)
    </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>

    <sql id="getTotalCountInfoSql">
        select
        tee.name as examName,
        t.exam_id examId,
        t.exam_activity_id examActivityId,
        t.room_code roomCode,
        t.room_name roomName,
        count(distinct t.id) examTotal,
        teea.code as examActivityCode,
        tee.monitor_status as monitorStatus,
        count(distinct toer.exam_student_id) as actualExamTotal,
        if(teea.finish_time <![CDATA[ <= ]]> (unix_timestamp(current_timestamp()) * 1000),count(distinct t.id) - count(distinct toer.exam_student_id),0) as deficiencyExamTotal,
        CONCAT(t.room_name,'(',t.room_code,')') as roomNameCode
        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
        left join t_oe_exam_record toer on toer.exam_student_id = t.id and toer.first_start_time is not null
        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
        order by
        CAST(teea.code AS UNSIGNED),CAST(t.room_code AS UNSIGNED)
    </sql>

    <select id="getTotalCountInfo" resultType="com.qmth.themis.business.bean.admin.ExamViewCountListBean">
        <include refid="getTotalCountInfoSql" />
    </select>

    <select id="getTotalCountInfoExport" resultType="com.qmth.themis.business.bean.admin.ExamViewCountListBean">
        <include refid="getTotalCountInfoSql" />
    </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>

    <sql id="getExamDeficiencyPageSql">
        select
        *,
        CONCAT(t.roomName,'(',t.roomCode,')') as roomNameCode,
        CONCAT(t.courseName,'(',t.courseCode,')') as courseNameCode
        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
        left join t_oe_exam_record toer on t.id = toer.exam_student_id
        where 1 = 1
        and t.exam_id = #{examId}
        <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>
        <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.id is null
        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
        join t_e_exam_activity f on t.exam_activity_id = f.id
        join t_e_exam tee on tee.id = t.exam_id
        left join t_oe_exam_record toer1 on t.id = toer1.exam_student_id and toer1.first_start_time is not null
        where 1 = 1
        and toer.exam_id = #{examId}
        <if test="activityIds != null and activityIds != '' and activityIds.size > 0">
            and toer.exam_activity_id in
            <foreach collection="activityIds" item="acid" index="index"
                     open="(" close=")" separator=",">
                #{acid}
            </foreach>
        </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>
        and toer.first_start_time is null
        and toer1.id is null) t
        order by CAST(t.examActivityCode AS UNSIGNED),CAST(t.roomCode AS UNSIGNED)
    </sql>

    <select id="getExamDeficiencyPage" resultType="com.qmth.themis.business.bean.admin.ExamDeficiencyListBean">
        <include refid="getExamDeficiencyPageSql" />
    </select>

    <select id="getExamDeficiencyPageExport" resultType="com.qmth.themis.business.bean.admin.ExamDeficiencyListBean">
        <include refid="getExamDeficiencyPageSql" />
    </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 CAST(teea.code AS UNSIGNED),CAST(t.room_code AS UNSIGNED)
    </select>

    <update id="updateAlreadyExamCount">
	    update t_e_exam_student set already_exam_count = #{alreadyExamCount} where id = #{examStudentId}
	</update>

    <update id="updateCurrentRecordId">
	    update t_e_exam_student set current_record_id = #{currentRecordId} where id = #{examStudentId}
	</update>

    <update id="updateAlreadyExamCountAndCurrentRecordId">
        update t_e_exam_student set current_record_id = #{currentRecordId},already_exam_count = #{alreadyExamCount} where id = #{examStudentId}
    </update>

    <sql id="markResultCommon">
        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,
        count(toer1.id) as examCount,
        count(toer2.id) as breachCount,
        if(count(toer1.id) <![CDATA[ <= ]]> 0,'是','否') as examAbsent
        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
        left join t_oe_exam_record toer1 on toer1.exam_student_id = tees.id and toer1.first_start_time is not null
        left join t_oe_exam_record toer2 on toer2.exam_student_id = tees.id and toer2.breach_status = 0
        <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>
        group by
        tees.id,
        tee.id,
        tee.name,
        teea.id,
        teea.code,
        tes.name,
        tes.`identity`,
        tees.course_code,
        tees.course_name,
        tees.room_code,
        tees.room_name,
        tees.grade,
        tees.class_no
        order by CAST(teea.code AS UNSIGNED),CAST(tees.room_code AS UNSIGNED)
    </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 distinct 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 in ('FINISHED','PERSISTED')
          and toer.first_start_time is not null
          and toer.finish_time is not null
          <if test="examStudentBreachPush != null and (examStudentBreachPush == false or examStudentBreachPush == 0)">
              and (toer.breach_status is null or toer.breach_status = 1)
          </if>
            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>

    <select id="getAbsentCount" resultType="java.lang.Integer">
        select count(distinct t.id) as c
        from t_e_exam_student t
        left join t_oe_exam_record toer on
        toer.exam_student_id = t.id
        <where> 1 = 1
            <if test="examId != null and examId != ''">
                and t.exam_id = #{examId}
            </if>
            <if test="examActivityId != null and examActivityId != ''">
                and t.exam_activity_id = #{examActivityId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and t.room_code = #{roomCode}
            </if>
            and (toer.id is null or (toer.status in ('FIRST_PREPARE', 'JUNK') and toer.client_websocket_status = 'OFF_LINE')
            or (toer.id is not null and toer.status in ('FIRST_PREPARE', 'JUNK') and toer.client_websocket_status is null))
            and t.already_exam_count = 0
        </where>
    </select>

    <select id="getExamInfo" resultType="com.qmth.themis.business.bean.admin.ExamInfoBean">
        select distinct tees.exam_id as examId,
                     tees.exam_activity_id as examActivityId,
                     group_concat(distinct tees.room_code) as roomCodes
        from t_e_exam_student tees
            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> 1 = 1
            <if test="orgId != null and orgId != ''">
                and tee.org_id = #{orgId}
            </if>
            <if test="examId != null and examId != ''">
                and tees.exam_id = #{examId}
            </if>
            <if test="roomCode != null and roomCode != ''">
                and tees.room_code = #{roomCode}
            </if>
            and tee.enable = 1
            and teea.enable = 1
        </where>
        group by tees.exam_id,tees.exam_activity_id
    </select>
</mapper>