<?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.paperrulegroup.mapper.PaperRuleGroupMapper">

    <resultMap id="groupMap" type="com.ynfy.buss.exam.paperrulegroup.entity.PaperRuleGroup">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="paper_id" property="paperId" jdbcType="VARCHAR"/>
        <result column="question_type" property="questionType" jdbcType="INTEGER"/>
        <result column="per_score" property="perScore" jdbcType="DOUBLE"/>
        <result column="question_count" property="questionCount" jdbcType="INTEGER"/>
        <result column="total_score" property="totalScore" jdbcType="DOUBLE"/>
        <result column="sort" property="sort" jdbcType="INTEGER"/>
        <result column="can_miss_option" property="canMissOption" jdbcType="BOOLEAN"/>
        <result column="can_blank_option" property="canBlankOption" jdbcType="BOOLEAN"/>

        <collection property="ruleList" ofType="com.ynfy.buss.exam.paperruledetail.entity.PaperRuleDetail">
            <id column="prd_id" property="id" jdbcType="VARCHAR"/>
            <result column="repository_id" property="repositoryId" jdbcType="VARCHAR"/>
            <result column="level" property="level" jdbcType="INTEGER"/>
            <result column="group_id" property="groupId" jdbcType="VARCHAR"/>
            <result column="prd_paper_id" property="paperId" jdbcType="VARCHAR"/>
            <result column="num" property="num" jdbcType="INTEGER"/>
            <result column="prd_sort" property="sort" jdbcType="INTEGER"/>
            <result column="prd_question_type" property="questionType" jdbcType="INTEGER"/>
        </collection>
    </resultMap>
    <select id="listGroupAndDetail" resultMap="groupMap">
        SELECT prg.id,
               prg.title,
               prg.paper_id,
               prg.question_type,
               prg.per_score,
               prg.question_count,
               prg.total_score,
               prg.sort,
               prg.can_miss_option,
               prg.can_blank_option,
               prd.id            AS prd_id,
               prd.repository_id,
               prd.level,
               prd.group_id,
               prd.paper_id      AS prd_paper_id,
               prd.num,
               prd.sort          AS prd_sort,
               prd.question_type AS prd_question_type
        FROM paper_rule_group prg
                 LEFT JOIN paper_rule_detail prd ON prg.ID = prd.group_id
        WHERE prg.paper_id = #{paperId}
        ORDER BY prg.sort ASC
    </select>

    <select id="sumQuestionCount" resultType="com.ynfy.buss.exam.exam.dto.QuestionTypeCountDTO">
        SELECT question_type,
               SUM(question_count) as question_count
        FROM paper_rule_group
        WHERE paper_id = #{paperId}
        GROUP BY question_type
        ORDER BY question_type
    </select>
</mapper>