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

    <resultMap type="WmsProductStock" id="WmsProductStockResult">
        <result property="productStockId"    column="product_stock_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseFloor"    column="warehouse_floor"    />
        <result property="locationCode"    column="location_code"    />
        <result property="stockType"    column="stock_type"    />
        <result property="productBatch"    column="product_batch"    />
        <result property="qualityStatus"    column="quality_status"    />
        <result property="productId"    column="product_id"    />
        <result property="planCode"    column="plan_code"    />
        <result property="planDetailCode"    column="plan_detail_code"    />
        <result property="saleorderCode"    column="saleorder_code"    />
        <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="instockDate"    column="instock_date"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
        <result property="orderAmount"    column="order_amount"    />
        <result property="warehouseName"    column="warehouse_name"    />
    </resultMap>

    <sql id="selectWmsProductStockVo">
        select product_stock_id, warehouse_id, location_code, stock_type, product_batch, quality_status, product_id, plan_code, plan_detail_code,saleorder_code, total_amount, frozen_amount, occupy_amount, create_by, create_date, update_by, update_date, active_flag, instock_date from wms_product_stock
    </sql>

    <select id="selectWmsProductStockList" parameterType="WmsProductStock" resultMap="WmsProductStockResult">
        <include refid="selectWmsProductStockVo"/>
        <where>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="stockType != null  and stockType != ''"> and stock_type = #{stockType}</if>
            <if test="productBatch != null  and productBatch != ''"> and product_batch = #{productBatch}</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="planCode != null "> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and plan_detail_code = #{planDetailCode}</if>
            <if test="saleorderCode != null "> and saleorder_code = #{saleorderCode}</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>
            <if test="instockDate != null "> and instock_date = #{instockDate}</if>
        </where>
    </select>

    <select id="selectWmsProductStockByProductStockId" parameterType="Long" resultMap="WmsProductStockResult">
        <include refid="selectWmsProductStockVo"/>
        where product_stock_id = #{productStockId}
    </select>

    <insert id="insertWmsProductStock" parameterType="WmsProductStock" useGeneratedKeys="true" keyProperty="productStockId">
        insert into wms_product_stock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="locationCode != null and locationCode != ''">location_code,</if>
            <if test="stockType != null and stockType != ''">stock_type,</if>
            <if test="productBatch != null">product_batch,</if>
            <if test="qualityStatus != null and qualityStatus != ''">quality_status,</if>
            <if test="productId != null">product_id,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="planDetailCode != null">plan_detail_code,</if>
            <if test="saleorderCode != null">saleorder_code,</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>
            <if test="instockDate != null">instock_date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationCode != null and locationCode != ''">#{locationCode},</if>
            <if test="stockType != null and stockType != ''">#{stockType},</if>
            <if test="productBatch != null">#{productBatch},</if>
            <if test="qualityStatus != null and qualityStatus != ''">#{qualityStatus},</if>
            <if test="productId != null">#{productId},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="planDetailCode != null">#{planDetailCode},</if>
            <if test="saleorderCode != null">#{saleorderCode},</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>
            <if test="instockDate != null">#{instockDate},</if>
         </trim>
    </insert>

    <update id="updateWmsProductStock" parameterType="WmsProductStock">
        update wms_product_stock
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="locationCode != null and locationCode != ''">location_code = #{locationCode},</if>
            <if test="stockType != null and stockType != ''">stock_type = #{stockType},</if>
            <if test="productBatch != null">product_batch = #{productBatch},</if>
            <if test="qualityStatus != null and qualityStatus != ''">quality_status = #{qualityStatus},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="planDetailCode != null">plan_detail_code = #{planDetailCode},</if>
            <if test="saleorderCode != null">saleorder_code = #{saleorderCode},</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>
            <if test="instockDate != null">instock_date = #{instockDate},</if>
        </trim>
        where product_stock_id = #{productStockId}
    </update>

    <delete id="deleteWmsProductStockByProductStockId" parameterType="Long">
        delete from wms_product_stock where product_stock_id = #{productStockId}
    </delete>

    <delete id="deleteWmsProductStockByProductStockIds" parameterType="String">
        delete from wms_product_stock where product_stock_id in
        <foreach item="productStockId" collection="array" open="(" separator="," close=")">
            #{productStockId}
        </foreach>
    </delete>









    <select id="getProductStockTotalAmount" resultType="BigDecimal" parameterType="WmsProductStock">
        select sum(total_amount) from wms_product_stock
        <where>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            and total_amount > 0
        </where>
    </select>

    <select id="selectProductStockByLocationAndBatch" resultMap="WmsProductStockResult">
        <include refid="selectWmsProductStockVo"/>
        where location_code = #{locationCode} and product_batch = #{productBatch} and stock_type = #{stockType}
    </select>




    <select id="selectWmsProductStockInList" parameterType="WmsProductStock" resultMap="WmsProductStockResult">
        <include refid="selectWmsProductStockVo"/>
        <where>
            <if test="warehouseId != null ">and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''">and location_code = #{locationCode}</if>
            <if test="stockType != null  and stockType != ''">and stock_type = #{stockType}</if>
            <if test="productBatch != null  and productBatch != ''">and product_batch = #{productBatch}</if>
            <if test="qualityStatus != null  and qualityStatus != ''">and quality_status = #{qualityStatus}</if>
            <if test="productId != null ">and product_id = #{productId}</if>
            <if test="planCode != null ">and plan_code = #{planCode}</if>
            <if test="planDetailCode != null ">and plan_detail_code = #{planDetailCode}</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>
            <if test="instockDate != null ">and instock_date = #{instockDate}</if>
            and totalAmount>0
        </where>
    </select>



    <select id="selectWmsProductStockJoinList" parameterType="WmsProductStock" resultMap="WmsProductStockResult">
        select wps.product_stock_id, wps.warehouse_id, wps.location_code, wps.stock_type, wps.product_batch, wps.quality_status, wps.product_id,
        wps.plan_code, wps.plan_detail_code, wps.total_amount, wps.frozen_amount, wps.occupy_amount,  wps.active_flag, wps.instock_date,
        mai.material_code,mai.material_name
        from wms_product_stock wps left join mes_base_material_info mai on wps.product_id = mai.material_id

        <where>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="stockType != null  and stockType != ''"> and stock_type = #{stockType}</if>
            <if test="productBatch != null  and productBatch != ''"> and product_batch = #{productBatch}</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="planCode != null "> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and plan_detail_code = #{planDetailCode}</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>
            <if test="instockDate != null "> and instock_date = #{instockDate}</if>
            and totalAmount>0
        </where>
    </select>

    <select id="selectWmsProductStocksBySaleorder" parameterType="WmsProductStock" resultMap="WmsProductStockResult">
        select wgb.warehouse_id,wgb.saleorder_code,wgb.product_id,mso.order_amount,wgb.total_amount,wgb.frozen_amount,wgb.occupy_amount,wbw.warehouse_name,mbmi.material_code,mbmi.material_name from mes_sale_order mso left join
        (select wps.warehouse_id,wps.saleorder_code,wps.product_id,sum(total_amount) total_amount,sum(frozen_amount) frozen_amount,sum(occupy_amount) occupy_amount from wms_product_stock wps where wps.total_amount>0 and wps.stock_type='3'
        group by wps.warehouse_id,wps.saleorder_code,wps.product_id) wgb
        on mso.saleorder_code=wgb.saleorder_code
        left join wms_base_warehouse wbw on wgb.warehouse_id=wbw.warehouse_id
        left join mes_base_material_info mbmi on mbmi.material_id=mso.material_id

        <where>
            <if test="saleorderCode != null  and saleorderCode != ''"> and mso.saleorder_code = #{saleorderCode}</if>
            <if test="warehouseId != null ">and wgb.warehouse_id = #{warehouseId}</if>
            <if test="productId != null ">and wgb.product_id = #{productId}</if>
        </where>
    </select>


</mapper>
