<?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.mxjb.mapper.MxjbWrongBookMapper">

    <resultMap type="MxjbWrongBook" id="MxjbWrongBookResult">
        <result property="wrongId"    column="wrong_id"    />
        <result property="customerCode"    column="customer_code"    />
        <result property="name"    column="name"    />
        <result property="subjectId"    column="subject_id"    />
        <result property="knownledgeId"    column="knownledge_id"    />
        <result property="knownledgeName"    column="knownledge_name"    />
        <result property="paperType"    column="paper_type"    />
        <result property="paperId"    column="paper_id"    />
        <result property="questionId"    column="question_id"    />
        <result property="source"    column="source"    />
        <result property="answers"    column="answers"    />
        <result property="answer1"    column="answer1"    />
        <result property="answer2"    column="answer2"    />
        <result property="wrongCount"    column="wrong_count"    />
        <result property="rightCount"    column="right_count"    />
        <result property="totalCount"    column="total_count"    />
        <result property="state"    column="state"    />
        <result property="scoreTotal"    column="score_total"    />
        <result property="score"    column="score"    />
        <result property="duration"    column="duration"    />
        <result property="comment"    column="comment"    />
        <result property="createdTime"    column="created_time"    />
        <result property="updatedTime"    column="updated_time"    />
        <result property="qtpye"    column="qtpye"    />
        <result property="title"    column="title"    />
        <result property="title0"    column="title0"    />
        <result property="optionA"    column="option_a"    />
        <result property="optionB"    column="option_b"    />
        <result property="optionC"    column="option_c"    />
        <result property="optionD"    column="option_d"    />
        <result property="optionE"    column="option_e"    />
        <result property="optionF"    column="option_f"    />
        <result property="optionG"    column="option_g"    />
        <result property="collect"    column="collect"    />
        <result property="examineeId"    column="examinee_id"    />
    </resultMap>

    <sql id="selectMxjbWrongBookVo">
        select wrong_id, customer_code, subject_id, knownledge_id, knownledge_name, paper_type, paper_id, question_id, source, answers, answer1, answer2, wrong_count, right_count, total_count, score_total, score, duration, comment, state, created_time, updated_time from mxjb_wrong_book
    </sql>

    <select id="findWrongExaminees" parameterType="Long" resultMap="MxjbWrongBookResult">
        SELECT wd.`examinee_id`, wd.paper_id, p.`paperName`, p.`paperType` paper_type, e.`begin_time` created_time
          FROM `mxjb_wrong_detail` wd
          JOIN `papers` p ON wd.`paper_id` = p.`id`
          JOIN `mxjb_paper_examinee` e ON e.`examinee_id` = wd.`examinee_id`
         WHERE wd.wrong_id = #{wrongId}
    </select>

    <select id="findWrongQuestions" parameterType="map" resultMap="MxjbWrongBookResult">
        SELECT wb.`wrong_id`, wb.`question_id`, q.`title`, q.parse, wb.`knownledge_id`, k.`name` knownledge_name, wb.`wrong_count`, q.qtpye
        ,q.option_a,q.option_b,q.option_c,q.option_d,q.option_e,q.option_f,q.option_g,q.answer1,q.answer2,q.title0,wb.answers, qc.question_id &gt; 0 collect
            FROM `mxjb_wrong_book` wb
          JOIN `questions` q ON wb.`question_id` = q.`id`
     LEFT JOIN `knowledge_tree` k ON wb.`knownledge_id` = k.`id`
        LEFT JOIN mxjb_question_collection qc on qc.question_id = q.id and qc.user_id = #{creatorId}
        <where> wb.`customer_code` = #{creatorId} AND q.`qtpye` IN ('判断题', '单选题', '选择题','单项选择','单项选择题','多选题')
            <if test="subjectId != null "> and wb.`subject_id` = #{subjectId}</if>
        </where>
    </select>
    <select id="selectMxjbWrongStudentKnownledgeStats" parameterType="map" resultMap="MxjbWrongBookResult">
        SELECT k.id knownledge_id, IF(k.knowledgeName IS NULL, t.knownledge_name, k.knowledgeName) knownledge_name, SUM(IF(t.state = 0, 1, 0)) wrong_count, COUNT(*) total_count
          FROM mxjb_wrong_book t
          LEFT JOIN knowledge_basic k ON k.id = t.knownledge_id
         WHERE t.customer_code = #{customerCode} AND t.subject_id = #{subjectId} AND t.updated_time &gt;= #{beginTime} AND t.updated_time &lt;= #{endTime}
               and (t.knownledge_name is not null and t.knownledge_name != '' or k.knowledgeName is not null and t.knownledge_name != '')
         GROUP BY k.id, IF(k.knowledgeName IS NULL, t.knownledge_name, k.knowledgeName)
         ORDER BY COUNT(*) DESC
    </select>

    <select id="selectMxjbWrongStudentQuestions" parameterType="map" resultMap="MxjbWrongBookResult">
    SELECT DISTINCT t.paper_type, t.question_id, q.title `name`, t.state, t.source, t.answers, t.answer1, t.answer2, t.score_total, t.score, t.duration, t.comment, t.created_time, t.updated_time, q.qtpye
      FROM mxjb_wrong_book t
      JOIN questions q ON q.id = t.question_id
      JOIN b_customer_basic b ON b.code = t.customer_code
      JOIN b_customer_class c ON c.customerCode = b.code
    <where> t.updated_time &gt;= #{beginTime} AND t.updated_time &lt;= #{endTime}
          <if test="studentGradeId != null "> and c.gradeId = #{studentGradeId}</if>
          <if test="subjectId != null "> and t.subject_id = #{subjectId}</if>
          <if test="customerCode != null "> and t.customer_code = #{customerCode}</if>
          <if test="knownledgeName != null ">AND t.knownledge_name = #{knownledgeName} </if>
          <if test="state != null "> and t.state = #{state}</if>
      </where>
    </select>

    <select id="selectMxjbWrongClassKnownledgeStats" parameterType="map" resultMap="MxjbWrongBookResult">
        SELECT k.id knownledge_id, IF(k.knowledgeName IS NULL, t.knownledge_name, k.knowledgeName) knownledge_name, SUM(IF(t.state = 0, 1, 0)) wrong_count, COUNT(*) total_count
          FROM mxjb_wrong_book t
          <if test="customerCode != null">
              JOIN b_customer_subject cs ON cs.subjectId = t.subject_id AND cs.customerCode = #{customerCode}
          </if>
		  JOIN b_customer_basic b ON b.code = t.customer_code
		  JOIN b_customer_class c ON c.customerCode = b.code
          LEFT JOIN knowledge_basic k ON k.id = t.knownledge_id
		 <where> t.updated_time &gt;= #{beginTime} AND t.updated_time &lt;= #{endTime}
		        and (t.knownledge_name is not null and t.knownledge_name != '' or k.knowledgeName is not null and t.knownledge_name != '')
             <if test="studentGradeId != null "> and c.gradeId = #{studentGradeId}</if>
             <if test="subjectId != null "> and t.subject_id = #{subjectId}</if>
             <if test="classId != null "> and c.classId = #{classId}</if>
             <if test="classIds != null "> and c.classId in <foreach item="id" collection="classIds" open="(" separator="," close=")">#{id}</foreach></if>
         </where>
		 GROUP BY k.id, IF(k.knowledgeName IS NULL, t.knownledge_name, k.knowledgeName)
		 ORDER BY COUNT(*) DESC
    </select>

    <select id="selectMxjbWrongClassStudentStats" parameterType="map" resultMap="MxjbWrongBookResult">
        SELECT t.customer_code, b.name name, SUM(IF(t.state = 0, 1, 0)) wrong_count
          FROM mxjb_wrong_book t
		  JOIN b_customer_basic b ON b.code = t.customer_code
		  JOIN b_customer_class c ON c.customerCode = b.code
        <where> t.updated_time &gt;= #{beginTime} AND t.updated_time &lt;= #{endTime}
            <if test="studentGradeId != null "> and c.gradeId = #{studentGradeId}</if>
            <if test="subjectId != null "> and t.subject_id = #{subjectId}</if>
            <if test="classId != null "> and c.classId = #{classId}</if>
            <if test="classIds != null "> and c.classId in <foreach item="id" collection="classIds" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="knownledgeName != null ">AND t.knownledge_name = #{knownledgeName} </if>
        </where>
		  GROUP BY t.customer_code, b.name
		  ORDER BY wrong_count DESC
    </select>

    <select id="selectMxjbWrongClassStudentQuestions" parameterType="map" resultMap="MxjbWrongBookResult">
        SELECT t.paper_type, t.question_id, q.title name, t.state, t.source, t.answers, t.answer1, t.answer2, t.score_total, t.score, t.duration, t.comment, t.created_time, t.updated_time
          FROM mxjb_wrong_book t
          JOIN b_customer_basic b ON b.code = t.customer_code
		  JOIN b_customer_class c ON c.customerCode = b.code
	      JOIN questions q ON q.id = t.question_id
	      <where> t.customer_code = #{customerCode} AND t.updated_time &gt;= #{beginTime} AND t.updated_time &lt;= #{endTime}
              <if test="classId != null "> and c.classId = #{classId}</if>
              <if test="studentGradeId != null "> and c.gradeId = #{studentGradeId}</if>
              <if test="subjectId != null "> and t.subject_id = #{subjectId}</if>
              <if test="classIds != null "> and c.classId in <foreach item="id" collection="classIds" open="(" separator="," close=")">#{id}</foreach></if>
              <if test="knownledgeName != null ">AND t.knownledge_name = #{knownledgeName} </if>
              <if test="state != null "> and t.state = #{state}</if>
          </where>
    </select>


	<select id="selectSubjects" parameterType="MxjbWrongBook" resultMap="MxjbWrongBookResult">
       select distinct s.subjectname subjectName,s.subjectId subject_id from mxjb_wrong_book b
       left join subject s on s.subjectid = b.subject_id
       where b.customer_code = #{customerCode} and s.subjectname is not null
       	order by s.subjectId asc
    </select>

    <select id="selectQTypes" parameterType="MxjbWrongBook" resultMap="MxjbWrongBookResult">
       select distinct q.qtpye subjectName from mxjb_wrong_book b
       left join questions q on q.id = b.question_id
       where b.customer_code = #{customerCode} and q.qtpye is not null
        <if test="subjectId != null "> and q.subjectId = #{subjectId}</if>
    </select>

    <select id="selectMxjbWrongBookList" parameterType="MxjbWrongBook" resultMap="MxjbWrongBookResult">
        <include refid="selectMxjbWrongBookVo"/>
        <where>
            <if test="customerCode != null  and customerCode != ''"> and customer_code = #{customerCode}</if>
            <if test="questionId != null "> and question_id = #{questionId}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="answers != null  and answers != ''"> and answers = #{answers}</if>
            <if test="answer1 != null  and answer1 != ''"> and answer1 = #{answer1}</if>
            <if test="answer2 != null  and answer2 != ''"> and answer2 = #{answer2}</if>
            <if test="wrongCount != null "> and wrong_count = #{wrongCount}</if>
            <if test="rightCount != null "> and right_count = #{rightCount}</if>
            <if test="totalCount != null "> and total_count = #{totalCount}</if>
            <if test="state != null "> and state = #{state}</if>
            <if test="duration != null "> and duration = #{duration}</if>
            <if test="comment != null "> and comment = #{comment}</if>
            <if test="createdTime != null "> and created_time = #{createdTime}</if>
            <if test="updatedTime != null "> and updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectMxjbWrongBookById" parameterType="Long" resultMap="MxjbWrongBookResult">
        <include refid="selectMxjbWrongBookVo"/>
        where wrong_id = #{wrongId}
    </select>

    <insert id="insertMxjbWrongBook" parameterType="MxjbWrongBook" useGeneratedKeys="true" keyProperty="wrongId" keyColumn="wrong_id">
        insert into mxjb_wrong_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="wrongId != null">wrong_id,</if>
            <if test="customerCode != null">customer_code,</if>
            <if test="subjectId != null">subject_id,</if>
            <if test="knownledgeId != null">knownledge_id,</if>
            <if test="knownledgeName != null">knownledge_name,</if>
            <if test="paperType != null">paper_type,</if>
            <if test="paperId != null">paper_id,</if>
            <if test="questionId != null">question_id,</if>
            <if test="source != null">source,</if>
            <if test="answers != null">answers,</if>
            <if test="answer1 != null">answer1,</if>
            <if test="answer2 != null">answer2,</if>
            <if test="wrongCount != null">wrong_count,</if>
            <if test="rightCount != null">right_count,</if>
            <if test="totalCount != null">total_count,</if>
            <if test="scoreTotal != null">score_total,</if>
            <if test="score != null">score,</if>
            <if test="state != null">state,</if>
            <if test="duration != null">duration,</if>
            <if test="comment != null">comment,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedTime != null">updated_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="wrongId != null">#{wrongId},</if>
            <if test="customerCode != null">#{customerCode},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="knownledgeId != null">#{knownledgeId},</if>
            <if test="knownledgeName != null">#{knownledgeName},</if>
            <if test="paperType != null">#{paperType},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="questionId != null">#{questionId},</if>
            <if test="source != null">#{source},</if>
            <if test="answers != null">#{answers},</if>
            <if test="answer1 != null">#{answer1},</if>
            <if test="answer2 != null">#{answer2},</if>
            <if test="wrongCount != null">#{wrongCount},</if>
            <if test="rightCount != null">#{rightCount},</if>
            <if test="totalCount != null">#{totalCount},</if>
            <if test="scoreTotal != null">#{scoreTotal},</if>
            <if test="score != null">#{score},</if>
            <if test="state != null">#{state},</if>
            <if test="duration != null">#{duration},</if>
            <if test="comment != null">#{comment},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
         </trim>
    </insert>

    <update id="updateMxjbWrongBook" parameterType="MxjbWrongBook">
        update mxjb_wrong_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerCode != null">customer_code = #{customerCode},</if>
            <if test="subjectId != null">subject_id = #{subjectId},</if>
            <if test="knownledgeId != null">knownledge_id = #{knownledgeId},</if>
            <if test="knownledgeName != null">knownledge_name = #{knownledgeName},</if>
            <if test="paperType != null">paper_type = #{paperType},</if>
            <if test="paperId != null">paper_id = #{paperId},</if>
            <if test="questionId != null">question_id = #{questionId},</if>
            <if test="source != null">source = #{source},</if>
            <if test="answers != null">answers = #{answers},</if>
            <if test="answer1 != null">answer1 = #{answer1},</if>
            <if test="answer2 != null">answer2 = #{answer2},</if>
            <if test="wrongCount != null">wrong_count = #{wrongCount},</if>
            <if test="rightCount != null">right_count = #{rightCount},</if>
            <if test="totalCount != null">total_count = #{totalCount},</if>
            <if test="scoreTotal != null">score_total = #{scoreTotal},</if>
            <if test="score != null">score = #{score},</if>
            <if test="state != null">state = #{state},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where wrong_id = #{wrongId}
    </update>

    <delete id="deleteMxjbWrongBookById" parameterType="Long">
        delete from mxjb_wrong_book where wrong_id = #{wrongId}
    </delete>

    <delete id="deleteMxjbWrongBookByIds" parameterType="String">
        delete from mxjb_wrong_book where wrong_id in
        <foreach item="wrongId" collection="array" open="(" separator="," close=")">
            #{wrongId}
        </foreach>
    </delete>

    <delete id="deleteByQuestion" parameterType="MxjbWrongBook">
        delete from mxjb_wrong_book where customer_code = #{customerCode} and question_id = #{questionId}
    </delete>

</mapper>
