<?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.ruoyi.mingxue.mapper.QuestionCollectionMapper">
    
    <resultMap type="QuestionCollection" id="QuestionCollectionResult">
        <result property="questionId"    column="question_id"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap type="com.ruoyi.mingxue.dto.AppQuestionCollectionResDto" id="AppQuestionCollectionResResult">
        <result property="questionId"    column="question_id"    />
        <result property="title"    column="title"    />
        <result property="type"    column="type"    />
        <result property="difficulty"    column="difficulty"    />
        <result property="picUrl"    column="pic_url"    />
        <result property="subjectName"    column="subjectName"    />
    </resultMap>


    <select id="getQuestionCollectionList" parameterType="Long" resultMap="AppQuestionCollectionResResult">
        SELECT id question_id, title, qtpye type, diff difficulty ,
        (SELECT pic_url from test_question_pic WHERE question_id = questions.id) pic_url
        from questions WHERE id IN
        (SELECT question_id from mxjb_question_collection WHERE user_id = #{userId})
    </select>

    <sql id="selectQuestionCollectionVo">
        select question_id, user_id, create_time from mxjb_question_collection
    </sql>

    <select id="selectQuestionCollectionList" parameterType="QuestionCollection" resultMap="QuestionCollectionResult">
        <include refid="selectQuestionCollectionVo"/>
        <where>
            <if test="questionId != null "> and question_id = #{questionId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
        </where>
    </select>
    
    <select id="selectSubjects" parameterType="QuestionCollection" resultMap="QuestionCollectionResult">
		select distinct s.subjectName,s.subjectId from mxjb_question_collection c
		left join questions q on c.question_id = q.id
		left join subject s on q.subjectId = s.subjectId 
		where s.subjectName is not null and c.user_id = #{userId}
		order by s.subjectId asc
    </select>
    
    <select id="selectQtypes" parameterType="QuestionCollection" resultMap="QuestionCollectionResult">
		select distinct q.qtpye as subjectName from mxjb_question_collection c
		left join questions q on c.question_id = q.id
		left join subject s on q.subjectId = s.subjectId 
		where c.user_id = #{userId} and q.qtpye is not null
		and q.subjectId = #{subjectId}
    </select>
    
    
    <select id="selectQuestionCollectionById" parameterType="Long" resultMap="QuestionCollectionResult">
        <include refid="selectQuestionCollectionVo"/>
        where question_id = #{questionId}
    </select>
        
    <insert id="insertQuestionCollection" parameterType="QuestionCollection">
        insert into mxjb_question_collection
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="questionId != null">question_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="questionId != null">#{questionId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateQuestionCollection" parameterType="QuestionCollection">
        update mxjb_question_collection
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where question_id = #{questionId}
    </update>

    <delete id="deleteQuestionCollectionById">
        delete from mxjb_question_collection where question_id = #{questionId} and user_id = #{userId}
    </delete>

    <delete id="deleteQuestionCollectionByIds" parameterType="String">
        delete from mxjb_question_collection where question_id in 
        <foreach item="questionId" collection="array" open="(" separator="," close=")">
            #{questionId}
        </foreach>
    </delete>
</mapper>