<?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.ynfy.buss.exam.examreview.mapper.ExamReviewMapper">
    <select id="selectPageList"  resultType="com.ynfy.buss.exam.examreview.entity.ExamReview">
        SELECT
                er.*
        FROM
        (

            SELECT
                    *
            FROM exam
            WHERE
                    reviewer_type = 1
                    AND reviewer like concat('%', #{user.orgCode}, '%')
            UNION
            SELECT
                    *
            FROM exam
            WHERE
                    reviewer_type = 2
                    AND reviewer like concat('%', #{user.username}, '%')
        ) tmp
        INNER JOIN exam_review er on tmp.id=er.id
        <where>
            <if test="examReview.title!=null and examReview.title!=''">
                AND er.title like concat('%', #{examReview.title}, '%')
            </if>
        </where>
    </select>

    <select id="calcReview" resultType="com.ynfy.buss.exam.examreview.entity.ExamReview">
        SELECT
            em.ID as id,
            em.title,
            em.open_type,
            em.start_time,
            em.end_time,
            tmp1.exam_user_count,
            tmp1.exam_review_times
        FROM
            (
                SELECT
                    tmp.ID,
                    COUNT ( * ) AS exam_user_count,
                    SUM ( exam_review_times ) AS exam_review_times
                FROM
                    (
                        SELECT
                            e.ID,
                            ue.user_id,
                            COUNT ( * ) AS exam_review_times
                        FROM
                            exam e
                                LEFT JOIN user_exam ue ON ue.exam_id = e.ID
                        WHERE
                            ue.STATE = 1
                        GROUP BY
                            e.ID,
                            ue.user_id
                        ORDER BY
                            e.ID,
                            ue.user_id
                    ) tmp
                GROUP BY
                    tmp.ID
            ) tmp1
                LEFT JOIN exam em ON tmp1.ID = em.ID
    </select>
</mapper>