<?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.question.mapper.QuestionMapper">

    <select id="selectPageList" resultType="com.ynfy.buss.exam.question.entity.Question">
        SELECT
        q.*,
        p.title as repositoryName
        FROM question q
        LEFT JOIN repository p on q.repository_id = p.id
        <where>
            <if test="question.content != null and question.content != ''">
                AND q.content like concat('%', #{question.content}, '%')
            </if>
            <if test="question.repositoryId != null and question.repositoryId != ''">
                AND q.repository_id = #{question.repositoryId}
            </if>
            <if test="question.type != null">
                AND q.type = #{question.type}
            </if>
            <if test="question.level != null">
                AND q.level = #{question.level}
            </if>
            <if test="question.child != null">
                AND q.child = #{question.child}
            </if>
        </where>
        ORDER BY q.create_time DESC
    </select>

    <resultMap id="QuestionMap" type="com.ynfy.buss.exam.question.entity.Question">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="repository_id" property="repositoryId" jdbcType="VARCHAR"/>
        <result column="repository_name" property="repositoryName" jdbcType="VARCHAR"/>
        <result column="analysis" property="analysis" jdbcType="VARCHAR"/>
        <result column="answer_order" property="answerOrder" jdbcType="BOOLEAN"/>
        <result column="child" property="child" jdbcType="BOOLEAN"/>
        <result column="parent_id" property="parentId" jdbcType="VARCHAR"/>
        <result column="sort" property="sort" jdbcType="INTEGER"/>

        <collection property="answerList" ofType="com.ynfy.buss.exam.questionanswer.entity.QuestionAnswer">
            <id column="qa_id" property="id" jdbcType="VARCHAR"/>
            <result column="qa_content" property="content" jdbcType="VARCHAR"/>
            <result column="qa_analysis" property="analysis" jdbcType="VARCHAR"/>
            <result column="is_right" property="isRight" jdbcType="BOOLEAN"/>
            <result column="tag" property="tag" jdbcType="VARCHAR"/>
            <result column="qa_sort" property="sort" jdbcType="INTEGER"/>
            <result column="path_score" property="pathScore" jdbcType="DOUBLE"/>
        </collection>
    </resultMap>

    <select id="selectQuestionList" resultMap="QuestionMap">
        SELECT
            q.id,
            q.type,
            q.level,
            q.content,
            q.repository_id,
            q.answer_order,
            q.child,
            q.parent_id,
            q.sort,
            r.title as repository_name,
            qa.id as qa_id,
            qa.content as qa_content ,
            <choose>
                <when test="needAnalysis">
                    q.analysis,
                    qa.analysis as qa_analysis,
                </when>
            </choose>
            <choose>
                <when test="needAnswerFlag">
                    qa.is_right,
                </when>
            </choose>
            qa.tag,
            qa.sort as qa_sort
        FROM question q
        LEFT JOIN repository r on q.repository_id=r.id
        LEFT JOIN question_answer qa on q.id=qa.question_id
        <where>
            <if test="idList !=null and idList.size()>0">
                AND q.id IN
                <foreach collection="idList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by q.id,qa.sort
    </select>

    <select id="listUserExamQuestionAnswer" resultMap="QuestionMap">
        SELECT
            q.id,
            q.type,
            q.level,
            q.content,
            q.repository_id,
            q.answer_order,
            q.child,
            q.parent_id,
            qa.id as qa_id,
            qa.content as qa_content ,
            <choose>
                <when test="needAnalysis">
                    q.analysis,
                    qa.analysis as qa_analysis,
                </when>
            </choose>
            <choose>
                <when test="needAnswerFlag">
                    ueqa.is_right,
                </when>
            </choose>
            ueqa.tag,
            <choose>
                <when test="needPathScore">
                    pqa.path_score,
                </when>
            </choose>
            ueqa.sort as qa_sort
        FROM question q
        LEFT JOIN user_exam_question_answer ueqa on q.id = ueqa.question_id
        LEFT JOIN question_answer qa on ueqa.answer_id = qa.id
        <choose>
            <when test="needPathScore">
                LEFT JOIN user_exam ue ON ueqa.user_exam_id = ue.id
                LEFT JOIN paper_question_answer pqa
                ON pqa.paper_id = ue.paper_id AND pqa.question_id = q.id AND pqa.answer_id = qa.id
            </when>
        </choose>
        <where>
            <if test="userExamId !=null and userExamId !=''">
                AND ueqa.user_exam_id = #{userExamId}
            </if>
            <if test="objectiveList !=null and objectiveList.size()>0">
                AND q.id IN
                <foreach collection="objectiveList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        <choose>
            <when test="needSubjective and subjectiveList !=null and subjectiveList.size()>0">
                UNION ALL
                SELECT
                    q.ID,
                    q.TYPE,
                    q.LEVEL,
                    q.CONTENT,
                    q.repository_id,
                    q.answer_order,
                    q.child,
                    q.parent_id,
                    null as qa_id,
                    null as qa_content ,
                    <choose>
                        <when test="needAnalysis">
                            null as analysis,
                            null as qa_analysis,
                        </when>
                    </choose>
                    <choose>
                        <when test="needAnswerFlag">
                            null as is_right,
                        </when>
                    </choose>
                    null as tag,
                    <choose>
                        <when test="needPathScore">
                            null as path_score,
                        </when>
                    </choose>
                    null as qa_sort
                FROM
                    question q
                    <where>
                        <if test="subjectiveList !=null and subjectiveList.size()>0">
                            AND q.id IN
                            <foreach collection="subjectiveList" item="item" open="(" separator="," close=")">
                                #{item}
                            </foreach>
                        </if>
                    </where>
            </when>
        </choose>
    </select>

    <select id="groupLevelById" resultType="com.ynfy.buss.exam.question.dto.QuestionDTO">
        SELECT
            tmp.*,
            r.title as repositoryName
        FROM (
            SELECT
                repository_id,
                type,
                level,
                COUNT ( * ) AS categoryCount
            FROM
                question
            <where>
                <if test="repositoryIds !=null and repositoryIds.size()>0">
                    AND repository_id IN
                    <foreach collection="repositoryIds" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="type !=null">
                    AND type = #{type,jdbcType=INTEGER}
                </if>
                <if test="questionIds !=null and questionIds.size()>0">
                    AND id NOT IN
                    <foreach collection="questionIds" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                AND child = 0
            </where>
            GROUP BY
                repository_id,
                type,
                level
        ) tmp
        LEFT JOIN repository r on tmp.repository_id=r.id
        order by tmp.repository_id,tmp.level
    </select>

    <select id="listByPaperId" resultType="com.ynfy.buss.exam.question.entity.Question">
        SELECT
            q.*
        FROM question q
        LEFT JOIN paper_question pq on q.id=pq.question_id
        <where>
            <if test="paperId !=null and paperId!=''">
                pq.paper_id = #{paperId}
            </if>
        </where>
    </select>

    <select id="groupCount" resultType="com.ynfy.buss.exam.question.entity.Question">
        SELECT
            repository_id,
            TYPE,
            LEVEL,
            COUNT ( * ) as questionNum
        FROM
            question
        <where>
            <if test="repositoryIds !=null and repositoryIds.size()>0">
                AND repository_id IN
                <foreach collection="repositoryIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="type !=null">
                AND type = #{type,jdbcType=INTEGER}
            </if>
        </where>
        GROUP BY
            repository_id,
            TYPE,
            LEVEL
        ORDER BY
            repository_id,
            TYPE,
            LEVEL
    </select>

    <select id="listQuestionByCondition" parameterType="java.util.List"
            resultType="com.ynfy.buss.exam.question.entity.Question">
        <foreach collection="list" item="item" index="index" separator="union">
            SELECT
                *
            FROM
                question
            <where>
                <if test="item.repositoryId !=null and item.repositoryId!='' ">
                    AND repository_id = #{item.repositoryId}
                </if>
                <if test="item.questionType !=null">
                    AND type = #{item.questionType,jdbcType=INTEGER}
                </if>
                <if test="item.level !=null">
                    AND level = #{item.level,jdbcType=INTEGER}
                </if>
                AND child = 0
            </where>
        </foreach>
    </select>
    <select id="countQuestionByRepository" resultType="com.ynfy.buss.exam.question.dto.QuestionDTO">
        SELECT
            repository_id as repositoryId,
            COUNT(*) as num
        FROM
            question
        <where>
            <if test="repositoryIds !=null and repositoryIds.size()>0">
                AND repository_id IN
                <foreach collection="repositoryIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            AND child = 0
        </where>
        GROUP BY
            repository_id
    </select>

    <select id="countQuestionNumByType" resultType="com.ynfy.buss.exam.question.dto.QuestionDTO">
        SELECT
            type,
            COUNT(*) as num
        FROM
            question
        <where>
            repository_id = #{repositoryId}
            AND child = 0 and type!=4 and type!=6
        </where>
        GROUP BY
            type
    </select>

    <select id="countQuestionType" resultType="com.ynfy.buss.exam.question.dto.QuestionDTO">
        SELECT
            repository_id as repositoryId,
            type,
            COUNT(*) as num
        FROM
            question
        <where>
            <if test="repositoryIds !=null and repositoryIds.size()>0">
                AND repository_id IN
                <foreach collection="repositoryIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            AND child = 0
        </where>
        GROUP BY
            repository_id,
            type
    </select>
</mapper>