<?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="cn.reghao.tnb.content.app.exam.db.mapper.QuestionMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into exam_question
        (`subject_id`,`type`,`level`,`content`,`analysis`,`create_by`,`pid`,`child`,`pos`)
        values 
        (#{subjectId},#{type},#{level},#{content},#{analysis},#{createBy},#{pid},#{child},#{pos})
    </insert>

    <delete id="deleteById">
        delete from exam_question
        where id=#{id}
    </delete>

    <select id="findAll" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where pid=0
    </select>
    <select id="findById" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where id=#{id}
    </select>
    <select id="findBySubjectId" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where subject_id=#{subjectId}
    </select>
    <select id="findByPid" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where pid=#{pid}
    </select>
    <select id="findByQuestionIds" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
    <select id="findGroupBySubjectId" resultType="cn.reghao.tnb.common.db.GroupCount">
        select subject_id as id, count(*) as total
        from exam_question
        group by subject_id
    </select>

    <select id="countByCriteria" resultType="java.lang.Integer">
        select count(*)
        from exam_question
        where 1=1 and pid=0
        <if test="subjectId != null">
            and subject_id=#{subjectId}
        </if>
        <if test="type != null">
            and type=#{type}
        </if>
        <if test="level != null">
            and `level`=#{level}
        </if>
    </select>
    <select id="findQuestionByPage" resultType="cn.reghao.tnb.content.app.exam.model.po.Question">
        select *
        from exam_question
        where 1=1 and pid=0
        <if test="questionQuery.subjectId != null">
            and subject_id=#{questionQuery.subjectId}
        </if>
        <if test="questionQuery.type != null">
            and type=#{questionQuery.type}
        </if>
        <if test="questionQuery.level != null">
            and `level`=#{questionQuery.level}
        </if>
    </select>
</mapper>