<?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="ink.wgink.module.examine.dao.paper.IPaperDao">

    <resultMap id="paperDTO" type="ink.wgink.module.examine.pojo.dtos.paper.PaperDTO">
        <id column="paper_id" property="paperId"/>
        <result column="paper_name" property="paperName"/>
        <result column="paper_number" property="paperNumber"/>
        <result column="paper_type" property="paperType"/>
        <result column="pass_score" property="passScore"/>
        <result column="total_score" property="totalScore"/>
        <result column="choice_single_count" property="choiceSingleCount"/>
        <result column="choice_single_each_score" property="choiceSingleEachScore"/>
        <result column="choice_multiple_count" property="choiceMultipleCount"/>
        <result column="choice_multiple_each_score" property="choiceMultipleEachScore"/>
        <result column="ture_or_false_count" property="tureOrFalseCount"/>
        <result column="ture_or_false_each_score" property="tureOrFalseEachScore"/>
        <result column="fill_in_the_blanks_count" property="fillInTheBlanksCount"/>
        <result column="fill_in_the_blanks_each_score" property="fillInTheBlanksEachScore"/>
        <result column="answer_count" property="answerCount"/>
        <result column="answer_each_score" property="answerEachScore"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="paper_status" property="paperStatus"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `exam_paper` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
            `paper_id` char(36) NOT NULL COMMENT '试卷主键',
            `paper_name` varchar(255) DEFAULT NULL COMMENT '试卷名称',
            `paper_number` varchar(255) DEFAULT NULL COMMENT '试卷编码',
            `paper_type` varchar(255) DEFAULT NULL COMMENT '试卷类型',
            `pass_score` double(11,2) DEFAULT NULL COMMENT '及格分数',
            `total_score` double(11,2) DEFAULT NULL COMMENT '总分',
            `choice_single_count` int(11) DEFAULT NULL COMMENT '单选数量',
            `choice_single_each_score` double(11,2) DEFAULT NULL COMMENT '单选每题得分',
            `choice_multiple_count` int(11) DEFAULT NULL COMMENT '多选数量',
            `choice_multiple_each_score` double(11,2) DEFAULT NULL COMMENT '多选每题得分',
            `ture_or_false_count` int(11) DEFAULT NULL COMMENT '判断数量',
            `ture_or_false_each_score` double(11,2) DEFAULT NULL COMMENT '判断每题得分',
            `fill_in_the_blanks_count` int(11) DEFAULT NULL COMMENT '填空数量',
            `fill_in_the_blanks_each_score` double(11,2) DEFAULT NULL COMMENT '填空每题得分',
            `answer_count` int(11) DEFAULT NULL COMMENT '解答数量',
            `answer_each_score` double(11,2) DEFAULT NULL COMMENT '解答每题得分',
            `paper_status` int(1) DEFAULT '0' COMMENT '试卷状态，0：未就绪，1：已就绪',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(1) DEFAULT '0',
            PRIMARY KEY (`id`,`paper_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='试卷';
    </update>

    <!-- 新增试卷 -->
    <insert id="save" parameterType="map">
        INSERT INTO exam_paper(
            paper_id,
            paper_name,
            paper_number,
            paper_type,
            pass_score,
            total_score,
            choice_single_count,
            choice_single_each_score,
            choice_multiple_count,
            choice_multiple_each_score,
            ture_or_false_count,
            ture_or_false_each_score,
            fill_in_the_blanks_count,
            fill_in_the_blanks_each_score,
            answer_count,
            answer_each_score,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{paperId},
            #{paperName},
            #{paperNumber},
            #{paperType},
            #{passScore},
            #{totalScore},
            #{choiceSingleCount},
            #{choiceSingleEachScore},
            #{choiceMultipleCount},
            #{choiceMultipleEachScore},
            #{tureOrFalseCount},
            #{tureOrFalseEachScore},
            #{fillInTheBlanksCount},
            #{fillInTheBlanksEachScore},
            #{answerCount},
            #{answerEachScore},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除试卷 -->
    <update id="remove" parameterType="map">
        UPDATE
            exam_paper
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            paper_id IN
        <foreach collection="paperIds" index="index" open="(" separator="," close=")">
            #{paperIds[${index}]}
        </foreach>
    </update>

    <!-- 删除试卷（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            exam_paper
        WHERE
            paper_id IN
        <foreach collection="paperIds" index="index" open="(" separator="," close=")">
            #{paperIds[${index}]}
        </foreach>
    </update>

    <!-- 修改试卷 -->
    <update id="update" parameterType="map">
        UPDATE
            exam_paper
        SET
        <if test="paperName != null and paperName != ''">
            paper_name = #{paperName},
        </if>
        <if test="paperNumber != null and paperNumber != ''">
            paper_number = #{paperNumber},
        </if>
        <if test="paperType != null and paperType != ''">
            paper_type = #{paperType},
        </if>
        <if test="passScore != null">
            pass_score = #{passScore},
        </if>
        <if test="totalScore != null">
            total_score = #{totalScore},
        </if>
        <if test="choiceSingleCount != null">
            choice_single_count = #{choiceSingleCount},
        </if>
        <if test="choiceSingleEachScore != null">
            choice_single_each_score = #{choiceSingleEachScore},
        </if>
        <if test="choiceMultipleCount != null">
            choice_multiple_count = #{choiceMultipleCount},
        </if>
        <if test="choiceMultipleEachScore != null">
            choice_multiple_each_score = #{choiceMultipleEachScore},
        </if>
        <if test="tureOrFalseCount != null">
            ture_or_false_count = #{tureOrFalseCount},
        </if>
        <if test="tureOrFalseEachScore != null">
            ture_or_false_each_score = #{tureOrFalseEachScore},
        </if>
        <if test="fillInTheBlanksCount != null">
            fill_in_the_blanks_count = #{fillInTheBlanksCount},
        </if>
        <if test="fillInTheBlanksEachScore != null">
            fill_in_the_blanks_each_score = #{fillInTheBlanksEachScore},
        </if>
        <if test="answerCount != null">
            answer_count = #{answerCount},
        </if>
        <if test="answerEachScore != null">
            answer_each_score = #{answerEachScore},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            paper_id = #{paperId}
    </update>

    <!-- 修改试卷状态 -->
    <update id="updatePaperStatus" parameterType="map">
        UPDATE
            exam_paper
        SET
            paper_status = #{paperStatus}
        WHERE
            paper_id = #{paperId}
    </update>

    <!-- 试卷详情 -->
    <select id="get" parameterType="map" resultMap="paperDTO">
        SELECT
            t1.paper_id,
            t1.paper_name,
            t1.paper_type,
            t1.pass_score,
            t1.total_score,
            t1.choice_single_count,
            t1.choice_single_each_score,
            t1.choice_multiple_count,
            t1.choice_multiple_each_score,
            t1.ture_or_false_count,
            t1.ture_or_false_each_score,
            t1.fill_in_the_blanks_count,
            t1.fill_in_the_blanks_each_score,
            t1.answer_count,
            t1.answer_each_score,
            t1.paper_status
        FROM
            exam_paper t1
        WHERE
            t1.is_delete = 0
        <if test="paperId != null and paperId != ''">
        AND
            t1.paper_id = #{paperId}
        </if>
    </select>

    <!-- 试卷列表 -->
    <select id="list" parameterType="map" resultMap="paperDTO">
        SELECT
            t1.paper_id,
            t1.paper_name,
            t1.paper_number,
            t1.paper_type,
            t1.pass_score,
            t1.total_score,
            t1.choice_single_count,
            t1.choice_single_each_score,
            t1.choice_multiple_count,
            t1.choice_multiple_each_score,
            t1.ture_or_false_count,
            t1.ture_or_false_each_score,
            t1.fill_in_the_blanks_count,
            t1.fill_in_the_blanks_each_score,
            t1.answer_count,
            t1.answer_each_score,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.paper_status
        FROM
            exam_paper t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.paper_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="paperIds != null and paperIds.size > 0">
        AND
            t1.paper_id IN
            <foreach collection="paperIds" index="index" open="(" separator="," close=")">
                #{paperIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 试卷统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            exam_paper t1
        WHERE
            t1.is_delete = 0
        <if test="creator != null and creator != ''">
        AND
            t1.creator = #{creator}
        </if>
    </select>

</mapper>