<?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.ximai.mes.tm.mapper.TmToolRequestItemMapper">

    <resultMap type="TmToolRequestItem" id="TmToolRequestItemResult">
        <result property="toolRequestItemId" column="tool_request_item_id"/>
        <result property="toolRequestId" column="tool_request_id"/>
        <result property="itemId" column="item_id"/>
        <result property="quantity" column="quantity"/>
        <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="itemName" column="item_name"/>
    </resultMap>

    <sql id="selectTmToolRequestItemVo">
        select ttri.tool_request_item_id,
               ttri.tool_request_id,
               ttri.item_id,
               ttri.quantity,
               ttr.estimated_life_time,
               ttri.create_by,
               ttri.create_time,
               ttri.update_by,
               ttri.update_time,
               im.item_name as item_name
        from tm_tool_request_item ttri
                 left join tm_tool_request ttr on ttri.tool_request_id = ttr.tool_request_id
                 left join md_item im on ttri.item_id = im.item_id
                 left join tm_tool tt on ttri.item_id = tt.tool_id
    </sql>

    <select id="selectTmToolRequestItemList" parameterType="TmToolRequestItem" resultMap="TmToolRequestItemResult">
        <include refid="selectTmToolRequestItemVo"/>
        <where>
            <if test="toolRequestId != null ">and ttri.tool_request_id = #{toolRequestId}</if>
            <if test="itemId != null ">and ttri.item_id = #{itemId}</if>
            <if test="quantity != null ">and ttri.quantity = #{quantity}</if>
        </where>
    </select>

    <select id="selectTmToolRequestItemByToolRequestItemId" parameterType="Long" resultMap="TmToolRequestItemResult">
        <include refid="selectTmToolRequestItemVo"/>
        where ttri.tool_request_item_id = #{toolRequestItemId}
    </select>

    <insert id="insertTmToolRequestItem" parameterType="TmToolRequestItem" useGeneratedKeys="true"
            keyProperty="toolRequestItemId">
        insert into tm_tool_request_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolRequestId != null">tool_request_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="quantity != null">quantity,</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="toolRequestId != null">#{toolRequestId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="quantity != null">#{quantity},</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="updateTmToolRequestItem" parameterType="TmToolRequestItem">
        update tm_tool_request_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolRequestId != null">tool_request_id = #{toolRequestId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="quantity != null">quantity = #{quantity},</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 tool_request_item_id = #{toolRequestItemId}
    </update>

    <delete id="deleteTmToolRequestItemByToolRequestItemId" parameterType="Long">
        delete
        from tm_tool_request_item
        where tool_request_item_id = #{toolRequestItemId}
    </delete>

    <delete id="deleteTmToolRequestItemByToolRequestItemIds" parameterType="String">
        delete from tm_tool_request_item where tool_request_item_id in
        <foreach item="toolRequestItemId" collection="array" open="(" separator="," close=")">
            #{toolRequestItemId}
        </foreach>
    </delete>
</mapper>
