<?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.testcategoryitem.TestCategoryItemMapper">

    <resultMap type="TestCategoryItem" id="TestCategoryItemResult">
        <result property="id" column="id"/>
        <result property="itemCategoryId" column="item_category_id"/>
        <result property="createBy" column="create_by"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectTestCategoryItemVo">
        select id, item_category_id, item_id, del_flag, create_by, create_time, update_by, update_time from test_category_item
    </sql>

    <select id="selectTestCategoryItemList" parameterType="TestCategoryItem" resultMap="TestCategoryItemResult">
        <include refid="selectTestCategoryItemVo"/>
        <where>
            del_flag = 0
            <if test="itemCategoryId != null ">
                and item_category_id = #{itemCategoryId}
            </if>
            <if test="itemId != null ">
                and item_id = #{itemId}
            </if>
        </where>
    </select>

    <select id="selectTestCategoryItemById" parameterType="Long"
            resultMap="TestCategoryItemResult">
        <include refid="selectTestCategoryItemVo"/>
        where id = #{id} and del_flag = 0
    </select>

    <insert id="insertTestCategoryItem" parameterType="TestCategoryItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into test_category_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemCategoryId != null">item_category_id,
            </if>
            <if test="itemId != null">item_id,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemCategoryId != null">#{itemCategoryId},
            </if>
            <if test="itemId != null">#{itemId},
            </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>
        </trim>
    </insert>

    <update id="updateTestCategoryItem" parameterType="TestCategoryItem">
        update test_category_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemCategoryId != null">item_category_id =
                #{itemCategoryId},
            </if>
            <if test="itemId != null">item_id =
                #{itemId},
            </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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestCategoryItemById" parameterType="Long">
        delete from test_category_item where id = #{id}
    </delete>

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

    <insert id="insertBatchTestCategoryInfo" parameterType="list">
        insert into test_category_item(
        item_category_id,
        item_id,
        del_flag,
        create_by,
        create_time
        ) values
        <foreach item="categoryItem" index="index" collection="categoryItemList" separator=",">
            (
            #{categoryItem.itemCategoryId},
            #{categoryItem.itemId},
            0,
            #{categoryItem.createBy},
            #{categoryItem.createTime}
            )
        </foreach>
    </insert>

    <delete id="deleteCategoryItemByCategoryIds">
        delete from test_category_item where item_id = #{itemId} and item_category_id in
        <foreach item="itemCategoryId" collection="categoryIdList" open="(" separator="," close=")">
            #{itemCategoryId}
        </foreach>
    </delete>

    <update id="updateTestCategoryItems">
        update test_category_item set del_flag = 1 where item_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getItemCategoryIds" parameterType="long" resultType="long">
        select item_category_id from test_category_item where item_id = #{itemId} and del_flag = 0
    </select>
</mapper>