<?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.TmToolRequestUseItemMapper">

    <resultMap type="com.ximai.mes.tm.domain.TmToolRequestUseItem" id="TmToolRequestUseItemResult">
        <result property="toolRequestUseItemId" column="tool_request_use_item_id"/>
        <result property="toolRequestUseId" column="tool_request_use_id"/>
        <result property="itemId" column="item_id"/>
        <result property="toolId" column="tool_id"/>
        <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="location" column="location"/>
        <result property="itemName" column="item_name"/>
        <result property="toolName" column="tool_name"/>
        <result property="toolCode" column="tool_code"/>
        <result property="lifeTime" column="life_time"/>
        <result property="requestNum" column="request_num"/>
        <result property="quantity" column="quantity"/>
        <result property="toolRequestCode" column="tool_request_code"/>
    </resultMap>

    <sql id="selectTmToolRequestUseItemVo">
        select ttrui.tool_request_use_item_id,
               ttrui.tool_request_use_id,
               ttrui.item_id,
               ttrui.tool_id,
               ttrui.location,
               ttrui.create_by,
               ttrui.create_time,
               ttrui.update_by,
               ttrui.update_time,
               ttr.tool_request_code,
               ttr.task_workunit_id,
               tt.tool_name,
               tt.tool_code,
               tt.life_time,
               ttr.estimated_life_time,
               ttr.request_num,
               ttrui.quantity,
               im.item_code,
               im.item_name
        from tm_tool_request_use_item ttrui
                 inner join tm_tool_request_use tru on ttrui.tool_request_use_id = tru.tool_request_use_id
                 left join tm_tool_request ttr on ttr.tool_request_id = tru.tool_request_id
                 left join tm_tool tt on tt.tool_id = ttrui.tool_id
                 left join md_item im on im.item_id = ttrui.item_id
    </sql>

    <select id="selectTmToolRequestUseItemList" resultMap="TmToolRequestUseItemResult">
        <include refid="selectTmToolRequestUseItemVo"/>
        <where>
            <if test="toolRequestUseId != null ">and ttrui.tool_request_use_id = #{toolRequestUseId}</if>
            <if test="itemId != null ">and ttrui.item_id = #{itemId}</if>
            <if test="toolId != null ">and ttrui.tool_id = #{toolId}</if>
        </where>
    </select>

    <select id="selectTmToolRequestUseItemByToolRequestUseItemId"
            resultMap="TmToolRequestUseItemResult">
        <include refid="selectTmToolRequestUseItemVo"/>
        where ttrui.tool_request_use_item_id = #{toolRequestUseItemId}
    </select>

    <insert id="insertTmToolRequestUseItem" parameterType="TmToolRequestUseItem" useGeneratedKeys="true"
            keyProperty="toolRequestUseItemId">
        insert into tm_tool_request_use_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolRequestUseId != null">tool_request_use_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="toolId != null">tool_id,</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="location != null">location,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="toolRequestUseId != null">#{toolRequestUseId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="toolId != null">#{toolId},</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="location != null">#{location},</if>
        </trim>
    </insert>

    <update id="updateTmToolRequestUseItem" parameterType="TmToolRequestUseItem">
        update tm_tool_request_use_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolRequestUseId != null">tool_request_use_id = #{toolRequestUseId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="toolId != null">tool_id = #{toolId},</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="location != null">location = #{location},</if>
        </trim>
        where tool_request_use_item_id = #{toolRequestUseItemId}
    </update>

    <delete id="deleteTmToolRequestUseItemByToolRequestUseItemId" parameterType="Long">
        delete
        from tm_tool_request_use_item
        where tool_request_use_item_id = #{toolRequestUseItemId}
    </delete>


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

    <delete id="deleteTmToolRequestUseItemByToolRequestUseIds" parameterType="String">
        delete from tm_tool_request_use_item where tool_request_use_id in
        <foreach item="toolRequestUseId" collection="array" open="(" separator="," close=")">
            #{toolRequestUseId}
        </foreach>
    </delete>
</mapper>
