<?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.mark.mapper.MarkUserGroupMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.mark.entity.MarkUserGroup">
        <id column="id" property="id" />
        <result column="exam_id" property="examId" />
        <result column="paper_number" property="paperNumber" />
        <result column="group_number" property="groupNumber" />
        <result column="user_id" property="userId" />
        <result column="mode" property="mode" />
        <result column="top_count" property="topCount" />
        <result column="finish_count" property="finishCount" />
        <result column="valid_count" property="validCount" />
        <result column="header_finish_count" property="headerFinishCount" />
        <result column="avg_score" property="avgScore" />
        <result column="max_score" property="maxScore" />
        <result column="min_score" property="minScore" />
        <result column="avg_speed" property="avgSpeed" />
        <result column="stdev_score" property="stdevScore" />
    </resultMap>
    <select id="listEntranceGroup" resultType="com.qmth.teachcloud.mark.dto.mark.entrance.MarkEntranceDto">
        SELECT
            mg.exam_id examId,
            mg.number groupNumber,
            mg.course_code courseCode,
            mg.course_name courseName,
            mg.paper_number paperNumber,
            ifnull(mg.task_count, 0) taskCount,
            ifnull(mg.marked_count, 0) markedCount
        FROM
            mark_user_group mug
                LEFT JOIN
            mark_group mg ON mug.exam_id = mg.exam_id
                AND mug.paper_number = mg.paper_number
                AND mug.group_number = mg.number
        <where>
            mg.exam_id = #{examId}
            <if test="courseCode != null and courseCode != ''">
                and mg.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and mg.paper_number = #{paperNumber}
            </if>
            and mug.user_id = #{userId} and mg.task_count > 0
            <if test="status != null and status != ''">
                and exists (select 1 from mark_paper mp where mp.exam_id = mg.exam_id and mp.paper_number = mg.paper_number and mp.status = #{status})
            </if>
        </where>
            order by mug.id desc
    </select>
    <select id="listGroupUserByExamIdAndPaperNumberAndGroupNumber"
            resultType="com.qmth.teachcloud.common.bean.dto.mark.MarkUser">
        SELECT
            distinct su.id userId,
            su.login_name loginName,
            su.real_name name,
            so.name orgName
        FROM
            mark_user_group mug
                LEFT JOIN
            sys_user su ON mug.user_id = su.id
                LEFT JOIN
            sys_org so ON su.org_id = so.id
        where
            mug.exam_id = #{examId}
            AND mug.paper_number = #{paperNumber}
            <if test="groupNumber != null">
                AND mug.group_number = #{groupNumber}
            </if>
            <if test="className != null and className != ''">
                AND EXISTS (select 1 from mark_user_class muc
                            where mug.exam_id = muc.exam_id
                              and mug.paper_number = muc.paper_number
                              and mug.user_id = muc.user_id
                              and muc.class_name = #{className})
            </if>
    </select>
    <select id="pageGroupUserByExamIdAndPaperNumberAndGroupNumber"
            resultType="com.qmth.teachcloud.mark.dto.mark.manage.MarkUserGroupProgressDto">
        SELECT
            mug.id markUserGroupId,
            mug.group_number groupNumber,
            mug.top_count topCount,
            su.id userId,
            su.login_name loginName,
            su.real_name name,
            so.name orgName
        FROM
            mark_user_group mug
                LEFT JOIN
            sys_user su ON mug.user_id = su.id
                LEFT JOIN
            sys_org so ON su.org_id = so.id
        <where>
            mug.exam_id = #{examId}
            AND mug.paper_number = #{paperNumber}
            <if test="groupNumber != null and groupNumber != ''">
                AND mug.group_number = #{groupNumber}
            </if>
            <if test="loginName != null and loginName != ''">
                AND su.real_name like concat(#{loginName},'%')
            </if>
            <if test="className != null and className != ''">
                AND EXISTS (select 1 from mark_user_class muc
                    where mug.exam_id = muc.exam_id
                        and mug.paper_number = muc.paper_number
                        and mug.user_id = muc.user_id
                        and muc.class_name = #{className})
            </if>
        </where>
    </select>
    <select id="pageQuality" resultType="com.qmth.teachcloud.mark.dto.mark.manage.MarkQualityDto">
        SELECT
            mug.user_id userId,
            mug.group_number groupNumber,
            ifnull(mug.finish_count, 0) finishCount,
            ifnull(mug.valid_count, 0) validCount,
            ifnull(mug.header_finish_count, 0) headerFinishCount,
            mug.avg_score avgScore,
            mug.max_score maxScore,
            mug.min_score minScore,
            mug.avg_speed avgSpeed
        FROM
            mark_user_group mug
                LEFT JOIN sys_user su ON mug.user_id = su.id
        where
            mug.exam_id = #{examId}
            AND mug.paper_number = #{paperNumber}
            <if test="groupNumber != null and groupNumber != ''">
                AND mug.group_number = #{groupNumber}
            </if>
            <if test="loginName != null and loginName != ''">
                AND su.real_name like concat(#{loginName}, '%')
            </if>
    </select>

</mapper>
