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

    <resultMap type="WmsRawStock" id="WmsRawStockResult">
        <result property="rawStockId"    column="raw_stock_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="stockType"    column="stock_type"    />
        <result property="materialId"    column="material_id"    />
        <result property="instockBatch"    column="instock_batch"    />
        <result property="instockDate"    column="instock_date"    />
        <result property="lastOutstockTime"    column="last_outstock_time"    />
        <result property="qualityStatus"    column="quality_status"    />
        <result property="completeFlag"    column="complete_flag"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="frozenAmount"    column="frozen_amount"    />
        <result property="occupyAmount"    column="occupy_amount"    />
        <result property="supplierId"    column="supplier_id"    />
        <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="materialGroup1"    column="material_group1"    />
        <result property="materialGroup2"    column="material_group2"    />
        <result property="materialGroup3"    column="material_group3"    />
        <result property="defAmount"    column="def_amount"    />
        <result property="activeFlag"    column="active_flag"    />
    </resultMap>

    <sql id="selectWmsRawStockVo">
        select raw_stock_id, warehouse_id, location_code, stock_type, material_id, instock_batch, instock_date, last_outstock_time, quality_status, complete_flag,total_amount, frozen_amount, occupy_amount, supplier_id, create_by, create_date, update_by, update_date, material_group1, material_group2, material_group3, def_amount, active_flag from wms_raw_stock
    </sql>

    <select id="selectWmsRawStockList" parameterType="WmsRawStock" resultMap="WmsRawStockResult">
        <include refid="selectWmsRawStockVo"/>
        <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="materialId != null "> and material_id = #{materialId}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="instockDate != null "> and instock_date = #{instockDate}</if>
            <if test="lastOutstockTime != null "> and last_outstock_time = #{lastOutstockTime}</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</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="supplierId != null "> and supplier_id = #{supplierId}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="materialGroup1 != null  and materialGroup1 != ''"> and material_group1 = #{materialGroup1}</if>
            <if test="materialGroup2 != null  and materialGroup2 != ''"> and material_group2 = #{materialGroup2}</if>
            <if test="materialGroup3 != null  and materialGroup3 != ''"> and material_group3 = #{materialGroup3}</if>
            <if test="defAmount != null "> and def_amount = #{defAmount}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
        </where>
    </select>

    <select id="selectWmsRawStockByRawStockId" parameterType="Long" resultMap="WmsRawStockResult">
        <include refid="selectWmsRawStockVo"/>
        where raw_stock_id = #{rawStockId}
    </select>

    <insert id="insertWmsRawStock" parameterType="WmsRawStock" useGeneratedKeys="true" keyProperty="rawStockId">
        insert into wms_raw_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="materialId != null">material_id,</if>
            <if test="instockBatch != null and instockBatch != ''">instock_batch,</if>
            <if test="instockDate != null">instock_date,</if>
            <if test="lastOutstockTime != null">last_outstock_time,</if>
            <if test="qualityStatus != null and qualityStatus != ''">quality_status,</if>
            <if test="completeFlag != null and completeFlag != ''">complete_flag,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="frozenAmount != null">frozen_amount,</if>
            <if test="occupyAmount != null">occupy_amount,</if>
            <if test="supplierId != null">supplier_id,</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="materialGroup1 != null">material_group1,</if>
            <if test="materialGroup2 != null">material_group2,</if>
            <if test="materialGroup3 != null">material_group3,</if>
            <if test="defAmount != null">def_amount,</if>
            <if test="activeFlag != null">active_flag,</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="materialId != null">#{materialId},</if>
            <if test="instockBatch != null and instockBatch != ''">#{instockBatch},</if>
            <if test="instockDate != null">#{instockDate},</if>
            <if test="lastOutstockTime != null">#{lastOutstockTime},</if>
            <if test="qualityStatus != null and qualityStatus != ''">#{qualityStatus},</if>
            <if test="completeFlag != null and completeFlag != ''">#{completeFlag},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="frozenAmount != null">#{frozenAmount},</if>
            <if test="occupyAmount != null">#{occupyAmount},</if>
            <if test="supplierId != null">#{supplierId},</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="materialGroup1 != null">#{materialGroup1},</if>
            <if test="materialGroup2 != null">#{materialGroup2},</if>
            <if test="materialGroup3 != null">#{materialGroup3},</if>
            <if test="defAmount != null">#{defAmount},</if>
            <if test="activeFlag != null">#{activeFlag},</if>
         </trim>
    </insert>

    <update id="updateWmsRawStock" parameterType="WmsRawStock">
        update wms_raw_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="materialId != null">material_id = #{materialId},</if>
            <if test="instockBatch != null and instockBatch != ''">instock_batch = #{instockBatch},</if>
            <if test="instockDate != null">instock_date = #{instockDate},</if>
            <if test="lastOutstockTime != null">last_outstock_time = #{lastOutstockTime},</if>
            <if test="qualityStatus != null and qualityStatus != ''">quality_status = #{qualityStatus},</if>
            <if test="completeFlag != null and completeFlag != ''">complete_flag = #{completeFlag},</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="supplierId != null">supplier_id = #{supplierId},</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="materialGroup1 != null">material_group1 = #{materialGroup1},</if>
            <if test="materialGroup2 != null">material_group2 = #{materialGroup2},</if>
            <if test="materialGroup3 != null">material_group3 = #{materialGroup3},</if>
            <if test="defAmount != null">def_amount = #{defAmount},</if>
            <if test="activeFlag != null">active_flag = #{activeFlag},</if>
        </trim>
        where raw_stock_id = #{rawStockId}
    </update>

    <delete id="deleteWmsRawStockByRawStockId" parameterType="Long">
        delete from wms_raw_stock where raw_stock_id = #{rawStockId}
    </delete>

    <delete id="deleteWmsRawStockByRawStockIds" parameterType="String">
        delete from wms_raw_stock where raw_stock_id in
        <foreach item="rawStockId" collection="array" open="(" separator="," close=")">
            #{rawStockId}
        </foreach>
    </delete>




    <select id="selectRawStockByLocationAndBatch" resultMap="WmsRawStockResult">
        <include refid="selectWmsRawStockVo"/>
        where location_code = #{locationCode} and instock_batch = #{instockBatch}
    </select>

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






    <select id="selectWmsRawStocks4Apply" parameterType="WmsRawStock" resultMap="WmsRawStockResult">
        <include refid="selectWmsRawStockVo"/>
        <where>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
        </where>
        order by instock_date asc
    </select>



    <select id="selectWmsRawStockInList" parameterType="WmsRawStock" resultMap="WmsRawStockResult">
        <include refid="selectWmsRawStockVo"/>
        <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="materialId != null "> and material_id = #{materialId}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="instockDate != null "> and instock_date = #{instockDate}</if>
            <if test="lastOutstockTime != null "> and last_outstock_time = #{lastOutstockTime}</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
            <if test="completeFlag != null  and completeFlag != ''"> and complete_flag = #{completeFlag}</if>
            <if test="frozenAmount != null "> and frozen_amount = #{frozenAmount}</if>
            <if test="occupyAmount != null "> and occupy_amount = #{occupyAmount}</if>
            <if test="supplierId != null "> and supplier_id = #{supplierId}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="materialGroup1 != null  and materialGroup1 != ''"> and material_group1 = #{materialGroup1}</if>
            <if test="materialGroup2 != null  and materialGroup2 != ''"> and material_group2 = #{materialGroup2}</if>
            <if test="materialGroup3 != null  and materialGroup3 != ''"> and material_group3 = #{materialGroup3}</if>
            <if test="defAmount != null "> and def_amount = #{defAmount}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>

             and totalAmount > 0
        </where>
    </select>



</mapper>
