<?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.hw.wms.mapper.WmsStockTotalMapper">

    <resultMap type="WmsStockTotal" id="WmsStockTotalResult">
        <result property="stockTotalId"    column="stock_total_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseFloor"    column="warehouse_floor"    />
        <result property="stockType"    column="stock_type"    />
        <result property="materialId"    column="material_id"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="frozenAmount"    column="frozen_amount"    />
        <result property="occupyAmount"    column="occupy_amount"    />
        <result property="createBy"    column="create_by"    />
        <result property="createDate"    column="create_date"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateDate"    column="update_date"    />
        <result property="activeFlag"    column="active_flag"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <sql id="selectWmsStockTotalVo">
        select stock_total_id, warehouse_id, warehouse_floor, stock_type, material_id, total_amount, frozen_amount, occupy_amount, create_by, create_date, update_by, update_date, active_flag from wms_stock_total
    </sql>

    <select id="selectWmsStockTotalList" parameterType="WmsStockTotal" resultMap="WmsStockTotalResult">
        <include refid="selectWmsStockTotalVo"/>
        <where>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="warehouseFloor != null "> and warehouse_floor = #{warehouseFloor}</if>
            <if test="stockType != null  and stockType != ''"> and stock_type = #{stockType}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="totalAmount != null "> and total_amount = #{totalAmount}</if>
            <if test="frozenAmount != null "> and frozen_amount = #{frozenAmount}</if>
            <if test="occupyAmount != null "> and occupy_amount = #{occupyAmount}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
        </where>
    </select>

    <select id="selectWmsStockTotalByStockTotalId" parameterType="Long" resultMap="WmsStockTotalResult">
        <include refid="selectWmsStockTotalVo"/>
        where stock_total_id = #{stockTotalId}
    </select>

    <insert id="insertWmsStockTotal" parameterType="WmsStockTotal" useGeneratedKeys="true" keyProperty="stockTotalId">
        insert into wms_stock_total
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseFloor != null">warehouse_floor,</if>
            <if test="stockType != null and stockType != ''">stock_type,</if>
            <if test="materialId != null">material_id,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="frozenAmount != null">frozen_amount,</if>
            <if test="occupyAmount != null">occupy_amount,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createDate != null">create_date,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateDate != null">update_date,</if>
            <if test="activeFlag != null">active_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseFloor != null">#{warehouseFloor},</if>
            <if test="stockType != null and stockType != ''">#{stockType},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="frozenAmount != null">#{frozenAmount},</if>
            <if test="occupyAmount != null">#{occupyAmount},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateDate != null">#{updateDate},</if>
            <if test="activeFlag != null">#{activeFlag},</if>
         </trim>
    </insert>

    <update id="updateWmsStockTotal" parameterType="WmsStockTotal">
        update wms_stock_total
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="warehouseFloor != null">warehouse_floor = #{warehouseFloor},</if>
            <if test="stockType != null and stockType != ''">stock_type = #{stockType},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="totalAmount != null">total_amount = #{totalAmount},</if>
            <if test="frozenAmount != null">frozen_amount = #{frozenAmount},</if>
            <if test="occupyAmount != null">occupy_amount = #{occupyAmount},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateDate != null">update_date = #{updateDate},</if>
            <if test="activeFlag != null">active_flag = #{activeFlag},</if>
        </trim>
        where stock_total_id = #{stockTotalId}
    </update>

    <delete id="deleteWmsStockTotalByStockTotalId" parameterType="Long">
        delete from wms_stock_total where stock_total_id = #{stockTotalId}
    </delete>

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








    <select id="selectWmsStockTotalJoinList" parameterType="WmsStockTotal" resultMap="WmsStockTotalResult">
        select wst.stock_total_id, wst.warehouse_id, wst.warehouse_floor, wst.stock_type, wst.material_id,
               wst.total_amount, wst.frozen_amount, wst.occupy_amount,
               mbmi.material_code,mbmi.material_name
        from wms_stock_total wst left join mes_base_material_info mbmi on wst.material_id = mbmi.material_id

        <where>
            <if test="warehouseId != null "> and wst.warehouse_id = #{warehouseId}</if>
            <if test="warehouseFloor != null "> and wst.warehouse_floor = #{warehouseFloor}</if>
            <if test="stockType != null  and stockType != ''"> and wst.stock_type = #{stockType}</if>
            <if test="materialId != null "> and wst.material_id = #{materialId}</if>
            <if test="materialName != null  and materialName != ''">and mbmi.material_name like concat('%', #{materialName},
                '%')
            </if>
        </where>
    </select>

</mapper>
