<?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.zhongzheng.modules.collect.mapper.CollectQuestionMapper">

    <resultMap type="com.zhongzheng.modules.collect.domain.CollectQuestion" id="CollectQuestionResult">
        <result property="collectQuestionId" column="collect_question_id"/>
        <result property="questionId" column="question_id"/>
        <result property="userId" column="user_id"/>
        <result property="createTime" column="create_time"/>
        <result property="examId" column="exam_id"/>
        <result property="goodsId" column="goods_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.collect.vo.CollectQuestionVo" id="CollectQuestionVoResult">
        <result property="collectQuestionId" column="collect_question_id"/>
        <result property="questionId" column="question_id"/>
        <result property="userId" column="user_id"/>
        <result property="examId" column="exam_id"/>
        <result property="examName" column="exam_name"/>
        <result property="questionNum" column="question_num"/>
        <result property="content" column="content"/>
        <result property="goodsId" column="goods_id"/>
        <result property="type" column="type"/>
        <result property="num" column="num"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.bank.vo.QuestionVo" id="QuestionResultVo">
        <result property="questionId" column="question_id"/>
        <result property="content" column="content"/>
        <result property="type" column="type"/>
        <result property="answerQuestion" column="answer_question"/>
        <result property="status" column="status"/>
        <result property="analysisContent" column="analysis_content"/>
        <result property="imgUrl" column="img_url"/>
        <result property="jsonStr" column="json_str"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="knowledgeIds" column="knowledge_ids"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo"  resultMap="QuestionResultVo">
        SELECT
        DISTINCT q.*
        FROM
        collect_question cq
        LEFT JOIN question q ON cq.question_id = q.question_id
        WHERE
        1 = 1
        AND cq.user_id = #{userId}
        <if test="type != null and type != ''">
            AND q.type = #{type}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND cq.goods_id = #{goodsId}
        </if>
    </select>

    <select id="selectExamList" parameterType="com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo"  resultMap="CollectQuestionVoResult">
        SELECT
        e.*,
        COUNT( cq.question_id ) question_num
        FROM
        collect_question cq
        LEFT JOIN exam e ON cq.exam_id = e.exam_id
        LEFT JOIN exam_paper ep ON ep.paper_id = e.exam_paper_id
        WHERE
        1 = 1
        AND cq.user_id = #{userId}
        <if test="paperId != null and paperId != ''">
            AND e.exam_paper_id = #{paperId}
        </if>
        <if test="goodsId != null and goodsId != ''">
            AND cq.goods_id = #{goodsId}
        </if>
        GROUP BY
        e.exam_id

    </select>

    <select id="selectExamQuestionList" parameterType="com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo"  resultMap="QuestionResultVo">
        SELECT
            DISTINCT q.*
        FROM
        collect_question cq
        LEFT JOIN question q ON q.question_id = cq.question_id
        WHERE
        1 = 1
        AND cq.exam_id = #{examId}
        AND cq.user_id = #{userId}
        <if test="goodsId != null and goodsId != ''">
            AND cq.goods_id = #{goodsId}
        </if>
    </select>

    <select id="selectTypeNum" parameterType="com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo"  resultMap="CollectQuestionVoResult">
        SELECT
            q.type,
            count( q.question_id ) AS num
        FROM
                ( SELECT c.* FROM collect_question c
        LEFT JOIN exam e ON c.exam_id = e.exam_id
        LEFT JOIN exam_paper ep ON ep.paper_id = e.exam_paper_id
                WHERE user_id = #{userId}
        <if test="paperId != null and paperId != ''">
            AND e.exam_paper_id = #{paperId}
        </if>
                    ) cq
                    LEFT JOIN question q ON cq.question_id = q.question_id

        WHERE
            1 = 1
        <if test="goodsId != null and goodsId != ''">
            AND cq.goods_id = #{goodsId}
        </if>

        GROUP BY
            q.type
    </select>

</mapper>
