<?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.md.mapper.MdItemWarehouseMapper">

    <resultMap type="MdItemWarehouse" id="MdItemWarehouseResult">
        <result property="itemWarehouseId"    column="item_warehouse_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="itemCode"    column="item_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseCode"    column="warehouse_code"    />
        <result property="location"    column="location"    />
        <result property="safetyStock"    column="safety_stock"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="com.ximai.mes.md.dto.MdItemWarehouseDto" id="MdItemWarehouseResult2">
        <result property="itemWarehouseId"    column="item_warehouse_id"    />
        <result property="itemId"    column="item_id"    />
        <result property="itemCode"    column="item_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseCode"    column="warehouse_code"    />
        <result property="location"    column="location"    />
        <result property="safetyStock"    column="safety_stock"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectMdItemWarehouseVo">
        select item_warehouse_id, item_id, item_code, warehouse_id, warehouse_code, location, safety_stock, remark, create_by, create_time, update_by, update_time from md_item_warehouse
    </sql>

    <select id="selectMdItemWarehouseList" parameterType="MdItemWarehouse" resultMap="MdItemWarehouseResult2">
        <include refid="selectMdItemWarehouseVo"/>
        <where>  
            <if test="itemId != null "> and item_id = #{itemId}</if>
            <if test="itemCode != null  and itemCode != ''"> and item_code = #{itemCode}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="warehouseCode != null  and warehouseCode != ''"> and warehouse_code = #{warehouseCode}</if>
            <if test="location != null  and location != ''"> and location = #{location}</if>
            <if test="safetyStock != null "> and safety_stock = #{safetyStock}</if>
        </where>
    </select>
    
    <select id="selectMdItemWarehouseByItemWarehouseId" parameterType="Long" resultMap="MdItemWarehouseResult2">
        <include refid="selectMdItemWarehouseVo"/>
        where item_warehouse_id = #{itemWarehouseId}
    </select>
        
    <insert id="insertMdItemWarehouse" parameterType="MdItemWarehouse" useGeneratedKeys="true" keyProperty="itemWarehouseId">
        insert into md_item_warehouse
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemId != null">item_id,</if>
            <if test="itemCode != null and itemCode != ''">item_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseCode != null">warehouse_code,</if>
            <if test="location != null">location,</if>
            <if test="safetyStock != null">safety_stock,</if>
            <if test="remark != null">remark,</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="itemId != null">#{itemId},</if>
            <if test="itemCode != null and itemCode != ''">#{itemCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseCode != null">#{warehouseCode},</if>
            <if test="location != null">#{location},</if>
            <if test="safetyStock != null">#{safetyStock},</if>
            <if test="remark != null">#{remark},</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="updateMdItemWarehouse" parameterType="MdItemWarehouse">
        update md_item_warehouse
        <trim prefix="SET" suffixOverrides=",">
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemCode != null and itemCode != ''">item_code = #{itemCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="warehouseCode != null">warehouse_code = #{warehouseCode},</if>
            <if test="location != null">location = #{location},</if>
            <if test="safetyStock != null">safety_stock = #{safetyStock},</if>
            <if test="remark != null">remark = #{remark},</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 item_warehouse_id = #{itemWarehouseId}
    </update>

    <delete id="deleteMdItemWarehouseByItemWarehouseId" parameterType="Long">
        delete from md_item_warehouse where item_warehouse_id = #{itemWarehouseId}
    </delete>

    <delete id="deleteMdItemWarehouseByItemWarehouseIds" parameterType="String">
        delete from md_item_warehouse where item_warehouse_id in 
        <foreach item="itemWarehouseId" collection="array" open="(" separator="," close=")">
            #{itemWarehouseId}
        </foreach>
    </delete>
</mapper>