<?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.xinyilu.system.mapper.testItemLevel.TestItemLevelMapper">

    <resultMap type="TestItemLevel" id="TestItemLevelResult">
        <result property="id" column="id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemCategoryId" column="item_category_id"/>
        <result property="levelName" column="level_name"/>
        <result property="levelMinScore" column="level_min_score"/>
        <result property="levelMaxScore" column="level_max_score"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="standardFlag" column="standard_flag"/>
    </resultMap>

    <sql id="selectTestItemLevelVo">
        select id, item_id, item_category_id, level_name, level_min_score, level_max_score, del_flag, create_by, create_time, update_by, update_time, standard_flag from test_item_level
    </sql>

    <select id="selectTestItemLevelList" parameterType="TestItemLevel" resultMap="TestItemLevelResult">
        <include refid="selectTestItemLevelVo"/>
        <where>
            <if test="itemId != null ">
                and item_id = #{itemId}
            </if>
            <if test="levelName != null  and levelName != ''">
                and level_name like concat('%', #{levelName}, '%')
            </if>
            <if test="levelMinScore != null ">
                and level_min_score = #{levelMinScore}
            </if>
            <if test="levelMaxScore != null ">
                and level_max_score = #{levelMaxScore}
            </if>
        </where>
        and del_flag = 0
    </select>

    <select id="selectTestItemLevelById" parameterType="Long"
            resultMap="TestItemLevelResult">
        <include refid="selectTestItemLevelVo"/>
        where id = #{id}
    </select>

    <insert id="insertTestItemLevel" parameterType="TestItemLevel" useGeneratedKeys="true"
            keyProperty="id">
        insert into test_item_level
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,
            </if>
            <if test="itemCategoryId != null">item_category_id,
            </if>
            <if test="levelName != null">level_name,
            </if>
            <if test="levelMinScore != null">level_min_score,
            </if>
            <if test="levelMaxScore != null">level_max_score,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="standardFlag != null">standard_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemId != null">#{itemId},
            </if>
            <if test="itemCategoryId != null">#{itemCategoryId},
            </if>
            <if test="levelName != null">#{levelName},
            </if>
            <if test="levelMinScore != null">#{levelMinScore},
            </if>
            <if test="levelMaxScore != null">#{levelMaxScore},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="standardFlag != null">#{standardFlag},
            </if>
        </trim>
    </insert>

    <update id="updateTestItemLevel" parameterType="TestItemLevel">
        update test_item_level
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id =
                #{itemId},
            </if>
            <if test="itemCategoryId != null">item_category_id =
                #{itemCategoryId},
            </if>
            <if test="levelName != null">level_name =
                #{levelName},
            </if>
            <if test="levelMinScore != null">level_min_score =
                #{levelMinScore},
            </if>
            <if test="levelMaxScore != null">level_max_score =
                #{levelMaxScore},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="standardFlag != null">standard_flag =
                #{standardFlag},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestItemLevelById" parameterType="Long">
        delete from test_item_level where item_id = #{id}
    </delete>

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

    <update id="updateTestItemLevelByIds" parameterType="java.lang.Long">
        update test_item_level set del_flag = 1, update_time = now() where item_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getTestItemLevelByItemId" parameterType="long" resultType="int">
        select count(1) as levelCount from test_item_level where item_id = #{itemId}
    </select>

    <select id="getTestItemLevelByCategoryId" parameterType="long" resultType="int">
        select count(1) as levelCount from test_item_level where item_category_id = #{itemCategoryId}
    </select>

    <delete id="deleteTestItemLevelByCategory" parameterType="Long">
        delete from test_item_level where item_category_id = #{itemCategoryId}
    </delete>

    <update id="updateTestItemLevelByCategoryIds" parameterType="java.lang.Long">
        update test_item_level set del_flag = 1, update_time = now() where item_category_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getTestItemLevelByItemIds" parameterType="long" resultType="int">
        select count(1) as levelCount from test_item_level where item_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getTestItemLevelByCategoryIds" parameterType="long" resultType="int">
        select count(1) as levelCount from test_item_level where item_category_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getTestItemLevel" parameterType="long" resultMap="TestItemLevelResult">
        <include refid="selectTestItemLevelVo"/>
        where item_category_id = #{itemCategoryId}
    </select>
</mapper>
