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

    <resultMap type="com.ximai.mes.tm.domain.TmToolWarehouseItem" id="TmToolWarehouseItemResult">
        <result property="toolWarehouseItemId" column="tool_warehouse_item_id"/>
        <result property="toolWarehouseId" column="tool_warehouse_id"/>
        <result property="warehouse" column="warehouse"/>
        <result property="local" column="local"/>
        <result property="warehouseDesc" column="warehouse_desc"/>
        <result property="toolCode" column="tool_code"/>
        <result property="toolName" column="tool_name"/>
        <result property="itemCode" column="item_code"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectTmToolWarehouseItemVo">
        SELECT
            tm.tool_warehouse_item_id,
            tm.tool_warehouse_item_code,
            tm.tool_warehouse_id,
            tm.warehouse,
            tm.local,
            tm.warehouse_desc,
            tm.tool_code,
            tm.tool_name,
            tm.item_code,
            tm.item_name,
            tm.create_by,
            tm.update_by,
            tm.update_time,
            tm.create_time,
            md.sap_item_code
        FROM
            tm_tool_warehouse_item tm
        left join md_item md on md.item_code=tm.item_code

    </sql>

    <select id="selectTmToolWarehouseItemList"
            resultMap="TmToolWarehouseItemResult">
        <include refid="selectTmToolWarehouseItemVo"/>
        <where>
            <if test="toolWarehouseId != null ">and tm.tool_warehouse_id = #{toolWarehouseId}</if>
            <if test="warehouse != null ">and tm.warehouse = #{warehouse}</if>
            <if test="local != null  and local != ''">and tm.local = #{local}</if>
            <if test="warehouseDesc != null  and warehouseDesc != ''">and tm.warehouse_desc = #{warehouseDesc}</if>
            <if test="toolCode != null  and toolCode != ''">and tm.tool_code = #{toolCode}</if>
            <if test="toolName != null  and toolName != ''">and tm.tool_name like concat('%', #{toolName}, '%')</if>
            <if test="itemCode != null  and itemCode != ''">and tm.item_code = #{itemCode}</if>
        </where>
        order by tm.create_time
    </select>

    <select id="selectTmToolWarehouseItemByToolWarehouseItemId" parameterType="Long"
            resultMap="TmToolWarehouseItemResult">
        <include refid="selectTmToolWarehouseItemVo"/>
        where tool_warehouse_item_id = #{toolWarehouseItemId}
    </select>

    <insert id="insertTmToolWarehouseItem"  useGeneratedKeys="true"
            keyProperty="toolWarehouseItemId">
        insert into tm_tool_warehouse_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolWarehouseId != null">tool_warehouse_id,</if>
            <if test="toolWarehouseItemCode != null">tool_warehouse_item_code,</if>
            <if test="warehouse != null">warehouse,</if>
            <if test="local != null">local,</if>
            <if test="warehouseDesc != null">warehouse_desc,</if>
            <if test="toolCode != null">tool_code,</if>
            <if test="toolName != null">tool_name,</if>
            <if test="itemCode != null">item_code,</if>
            <if test="itemName != null">item_name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="toolWarehouseId != null">#{toolWarehouseId},</if>
            <if test="toolWarehouseItemCode != null">#{toolWarehouseItemCode},</if>
            <if test="warehouse != null">#{warehouse},</if>
            <if test="local != null">#{local},</if>
            <if test="warehouseDesc != null">#{warehouseDesc},</if>
            <if test="toolCode != null">#{toolCode},</if>
            <if test="toolName != null">#{toolName},</if>
            <if test="itemCode != null">#{itemCode},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateTmToolWarehouseItem" parameterType="TmToolWarehouseItem">
        update tm_tool_warehouse_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolWarehouseId != null">tool_warehouse_id = #{toolWarehouseId},</if>
            <if test="warehouse != null">warehouse = #{warehouse},</if>
            <if test="local != null">local = #{local},</if>
            <if test="warehouseDesc != null">warehouse_desc = #{warehouseDesc},</if>
            <if test="toolCode != null">tool_code = #{toolCode},</if>
            <if test="toolName != null">tool_name = #{toolName},</if>
            <if test="itemCode != null">item_code = #{itemCode},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where tool_warehouse_item_id = #{toolWarehouseItemId}
    </update>

    <delete id="deleteTmToolWarehouseItemByToolWarehouseItemId" parameterType="Long">
        delete
        from tm_tool_warehouse_item
        where tool_warehouse_item_id = #{toolWarehouseItemId}
    </delete>

    <delete id="deleteTmToolWarehouseItemByToolWarehouseItemIds" parameterType="String">
        delete from tm_tool_warehouse_item where tool_warehouse_item_id in
        <foreach item="toolWarehouseItemId" collection="array" open="(" separator="," close=")">
            #{toolWarehouseItemId}
        </foreach>
    </delete>
</mapper>