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

    <resultMap type="WmsProductOutstock" id="WmsProductOutstockResult">
        <result property="productOutstockId"    column="product_outstock_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="productType"    column="product_type"    />
        <result property="operationType"    column="operation_type"    />
        <result property="outstockType"    column="outstock_type"    />
        <result property="planCode"    column="plan_code"    />
        <result property="planDetailCode"    column="plan_detail_code"    />
        <result property="saleOrderId"    column="sale_order_id"    />
        <result property="saleorderCode"    column="saleorder_code"    />
        <result property="projectNo"    column="project_no"    />
        <result property="productId"    column="product_id"    />
        <result property="applyQty"    column="apply_qty"    />
        <result property="outstockQty"    column="outstock_qty"    />
        <result property="applyReason"    column="apply_reason"    />
        <result property="auditReason"    column="audit_reason"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="executeStatus"    column="execute_status"    />
        <result property="applyBy"    column="apply_by"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="auditBy"    column="audit_by"    />
        <result property="auditDate"    column="audit_date"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateDate"    column="update_date"    />
        <result property="beginTime"    column="begin_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="erpStatus"    column="erp_status"    />
        <result property="erpAmount"    column="erp_amount"    />

        <result property="warehouseName"    column="warehouse_name"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <resultMap id="WmsProductOutstockWmsProductOutstockDetailResult" type="WmsProductOutstock" extends="WmsProductOutstockResult">
        <collection property="wmsProductOutstockDetailList" notNullColumn="sub_product_outstock_detail_id" javaType="java.util.List" resultMap="WmsProductOutstockDetailResult" />
    </resultMap>

    <resultMap type="WmsProductOutstockDetail" id="WmsProductOutstockDetailResult">
        <result property="productOutstockDetailId"    column="sub_product_outstock_detail_id"    />
        <result property="productOutstockId"    column="sub_product_outstock_id"    />
        <result property="warehouseId"    column="sub_warehouse_id"    />
        <result property="locationCode"    column="sub_location_code"    />
        <result property="productId"    column="sub_product_id"    />
        <result property="outstockAmount"    column="sub_outstock_amount"    />
        <result property="executeStatus"    column="sub_execute_status"    />
        <result property="erpStatus"    column="sub_erp_status"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateDate"    column="sub_update_date"    />
        <result property="beginTime"    column="sub_begin_time"    />
        <result property="endTime"    column="sub_end_time"    />
    </resultMap>

    <sql id="selectWmsProductOutstockVo">
        select product_outstock_id, task_code, warehouse_id, location_code, product_type, operation_type, outstock_type, plan_code, plan_detail_code, sale_order_id, saleorder_code, project_no, product_id, product_batch, pallet_info_code, end_station_code, apply_qty, outstock_qty, apply_reason, audit_reason, audit_status, execute_status, apply_by, apply_date, audit_by, audit_date, update_by, update_date, begin_time, end_time, erp_status, erp_amount from wms_product_outstock
    </sql>

    <select id="selectWmsProductOutstockList" parameterType="WmsProductOutstock" resultMap="WmsProductOutstockResult">
        <include refid="selectWmsProductOutstockVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="productType != null  and productType != ''"> and product_type = #{productType}</if>
            <if test="operationType != null  and operationType != ''"> and operation_type = #{operationType}</if>
            <if test="outstockType != null  and outstockType != ''"> and outstock_type = #{outstockType}</if>
            <if test="planCode != null "> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and plan_detail_code = #{planDetailCode}</if>
            <if test="applyReason != null  and applyReason != ''"> and apply_reason = #{applyReason}</if>
            <if test="auditReason != null  and auditReason != ''"> and audit_reason = #{auditReason}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and audit_status = #{auditStatus}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="applyBy != null  and applyBy != ''"> and apply_by = #{applyBy}</if>
            <if test="applyDate != null "> and apply_date = #{applyDate}</if>
            <if test="auditBy != null  and auditBy != ''"> and audit_by = #{auditBy}</if>
            <if test="auditDate != null "> and audit_date = #{auditDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="beginTime != null "> and begin_time = #{beginTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
    </select>

    <select id="selectWmsProductOutstockByProductOutstockId" parameterType="Long" resultMap="WmsProductOutstockWmsProductOutstockDetailResult">
        select a.product_outstock_id, a.task_code, a.warehouse_id, a.location_code, a.product_type, a.operation_type, a.outstock_type, a.plan_code, a.plan_detail_code, a.sale_order_id, a.saleorder_code, a.project_no, a.product_id, a.product_batch, a.pallet_info_code, a.end_station_code, a.apply_qty, a.outstock_qty, a.apply_reason, a.audit_reason, a.audit_status, a.execute_status, a.apply_by, a.apply_date, a.audit_by, a.audit_date, a.update_by, a.update_date, a.begin_time, a.end_time, a.erp_status, a.erp_amount,
               b.product_outstock_detail_id as sub_product_outstock_detail_id, b.product_outstock_id as sub_product_outstock_id, b.warehouse_id as sub_warehouse_id, b.location_code as sub_location_code, b.product_barcode as sub_product_barcode, b.product_batch as sub_product_batch, b.product_id as sub_product_id, b.plan_amount as sub_plan_amount, b.outstock_amount as sub_outstock_amount, b.execute_status as sub_execute_status, b.erp_status as sub_erp_status, b.erp_amount as sub_erp_amount, b.update_by as sub_update_by, b.update_date as sub_update_date, b.begin_time as sub_begin_time, b.end_time as sub_end_time
        from wms_product_outstock a
                 left join wms_product_outstock_detail b on b.product_outstock_id = a.product_outstock_id
        where a.product_outstock_id = #{productOutstockId}
    </select>


    <insert id="insertWmsProductOutstock" parameterType="WmsProductOutstock" useGeneratedKeys="true" keyProperty="productOutstockId">
        insert into wms_product_outstock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">task_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="locationCode != null">location_code,</if>
            <if test="productType != null and productType != ''">product_type,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="outstockType != null and outstockType != ''">outstock_type,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="planDetailCode != null">plan_detail_code,</if>
            <if test="saleOrderId != null">sale_order_id,</if>
            <if test="saleorderCode != null">saleorder_code,</if>
            <if test="projectNo != null">project_no,</if>
            <if test="productId != null">product_id,</if>
            <if test="productBatch != null">product_batch,</if>
            <if test="palletInfoCode != null">pallet_info_code,</if>
            <if test="endStationCode != null">end_station_code,</if>
            <if test="applyQty != null">apply_qty,</if>
            <if test="outstockQty != null">outstock_qty,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="auditReason != null">audit_reason,</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status,</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status,</if>
            <if test="applyBy != null">apply_by,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="auditBy != null">audit_by,</if>
            <if test="auditDate != null">audit_date,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateDate != null">update_date,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="erpStatus != null">erp_status,</if>
            <if test="erpAmount != null">erp_amount,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">#{taskCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="productType != null and productType != ''">#{productType},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="outstockType != null and outstockType != ''">#{outstockType},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="planDetailCode != null">#{planDetailCode},</if>
            <if test="saleOrderId != null">#{saleOrderId},</if>
            <if test="saleorderCode != null">#{saleorderCode},</if>
            <if test="projectNo != null">#{projectNo},</if>
            <if test="productId != null">#{productId},</if>
            <if test="productBatch != null">#{productBatch},</if>
            <if test="palletInfoCode != null">#{palletInfoCode},</if>
            <if test="endStationCode != null">#{endStationCode},</if>
            <if test="applyQty != null">#{applyQty},</if>
            <if test="outstockQty != null">#{outstockQty},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="auditReason != null">#{auditReason},</if>
            <if test="auditStatus != null and auditStatus != ''">#{auditStatus},</if>
            <if test="executeStatus != null and executeStatus != ''">#{executeStatus},</if>
            <if test="applyBy != null">#{applyBy},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="auditBy != null">#{auditBy},</if>
            <if test="auditDate != null">#{auditDate},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateDate != null">#{updateDate},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="erpStatus != null">#{erpStatus},</if>
            <if test="erpAmount != null">#{erpAmount},</if>
        </trim>
    </insert>

    <update id="updateWmsProductOutstock" parameterType="WmsProductOutstock">
        update wms_product_outstock
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="locationCode != null">location_code = #{locationCode},</if>
            <if test="productType != null and productType != ''">product_type = #{productType},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="outstockType != null and outstockType != ''">outstock_type = #{outstockType},</if>
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="planDetailCode != null">plan_detail_code = #{planDetailCode},</if>
            <if test="saleOrderId != null">sale_order_id = #{saleOrderId},</if>
            <if test="saleorderCode != null">saleorder_code = #{saleorderCode},</if>
            <if test="projectNo != null">project_no = #{projectNo},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="productBatch != null">product_batch = #{productBatch},</if>
            <if test="palletInfoCode != null">pallet_info_code = #{palletInfoCode},</if>
            <if test="endStationCode != null">end_station_code = #{endStationCode},</if>
            <if test="applyQty != null">apply_qty = #{applyQty},</if>
            <if test="outstockQty != null">outstock_qty = #{outstockQty},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="auditReason != null">audit_reason = #{auditReason},</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status = #{auditStatus},</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status = #{executeStatus},</if>
            <if test="applyBy != null">apply_by = #{applyBy},</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="auditBy != null">audit_by = #{auditBy},</if>
            <if test="auditDate != null">audit_date = #{auditDate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateDate != null">update_date = #{updateDate},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="erpStatus != null">erp_status = #{erpStatus},</if>
            <if test="erpAmount != null">erp_amount = #{erpAmount},</if>
        </trim>
        where product_outstock_id = #{productOutstockId}
    </update>

    <delete id="deleteWmsProductOutstockByProductOutstockId" parameterType="Long">
        delete from wms_product_outstock where product_outstock_id = #{productOutstockId}
    </delete>

    <delete id="deleteWmsProductOutstockByProductOutstockIds" parameterType="String">
        delete from wms_product_outstock where product_outstock_id in
        <foreach item="productOutstockId" collection="array" open="(" separator="," close=")">
            #{productOutstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsProductOutstockDetailByProductOutstockIds" parameterType="String">
        delete from wms_product_outstock_detail where product_outstock_id in
        <foreach item="productOutstockId" collection="array" open="(" separator="," close=")">
            #{productOutstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsProductOutstockDetailByProductOutstockId" parameterType="Long">
        delete from wms_product_outstock_detail where product_outstock_id = #{productOutstockId}
    </delete>

    <insert id="batchWmsProductOutstockDetail">
        insert into wms_product_outstock_detail( product_outstock_detail_id, product_outstock_id, warehouse_id, location_code, sales_order, project_no, product_id, outstock_amount, execute_status, erp_status, update_by, update_date, begin_time, end_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.productOutstockDetailId}, #{item.productOutstockId}, #{item.warehouseId}, #{item.locationCode}, #{item.salesOrder}, #{item.projectNo}, #{item.productId}, #{item.outstockAmount}, #{item.executeStatus}, #{item.erpStatus}, #{item.updateBy}, #{item.updateDate}, #{item.beginTime}, #{item.endTime})
        </foreach>
    </insert>






    <select id="selectWmsProductOutstockJoinList" parameterType="WmsProductOutstock" resultMap="WmsProductOutstockResult">
        select wpo.product_outstock_id, wpo.task_code, wpo.warehouse_id, wpo.location_code, wpo.product_type,wpo.operation_type, wpo.outstock_type,
        wpo.plan_code, wpo.plan_detail_code,wpo.saleorder_code, wpo.product_id,wpo.apply_qty,wpo.outstock_qty,wpo.apply_reason, wpo.execute_status,wpo.apply_by, wpo.apply_date,
        wbw.warehouse_name,mbmi.material_code,mbmi.material_name
        from wms_product_outstock wpo left join wms_base_warehouse wbw on wpo.warehouse_id =wbw.warehouse_id
        left join mes_base_material_info mbmi on wpo.product_id = mbmi.material_id
        <where>
            <if test="taskCode != null  and taskCode != ''"> and wpo.task_code = #{taskCode}</if>
            <if test="warehouseId != null "> and wpo.warehouse_id = #{warehouseId}</if>
            <if test="locationCode != null  and locationCode != ''"> and wpo.location_code = #{locationCode}</if>
            <if test="productType != null  and productType != ''"> and wpo.product_type = #{productType}</if>
            <if test="operationType != null  and operationType != ''"> and wpo.operation_type = #{operationType}</if>
            <if test="outstockType != null  and outstockType != ''"> and wpo.outstock_type = #{outstockType}</if>
            <if test="planCode != null "> and wpo.plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and wpo.plan_detail_code = #{planDetailCode}</if>
            <if test="applyReason != null  and applyReason != ''"> and wpo.apply_reason = #{applyReason}</if>
            <if test="auditReason != null  and auditReason != ''"> and wpo.audit_reason = #{auditReason}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and wpo.audit_status = #{auditStatus}</if>
            <if test="executeStatus != null  and executeStatus != '' and executeStatus != 'notFinish'"> and wpo.execute_status = #{executeStatus}</if>
            <if test="executeStatus != null  and executeStatus != '' and executeStatus == 'notFinish'"> and (wpo.execute_status = '0' or wpo.execute_status = '1')</if>
            <if test="applyBy != null  and applyBy != ''"> and wpo.apply_by = #{applyBy}</if>
            <if test="applyDate != null "> and wpo.apply_date = #{applyDate}</if>
            <if test="auditBy != null  and auditBy != ''"> and wpo.audit_by = #{auditBy}</if>
            <if test="auditDate != null "> and wpo.audit_date = #{auditDate}</if>
            <if test="updateDate != null "> and wpo.update_date = #{updateDate}</if>
            <if test="beginTime != null "> and wpo.begin_time = #{beginTime}</if>
            <if test="endTime != null "> and wpo.end_time = #{endTime}</if>
        </where>
    </select>


    <select id="selectWmsProductOutstockJoinByOutstockId" parameterType="WmsProductOutstock" resultMap="WmsProductOutstockResult">
        select wpo.product_outstock_id, wpo.task_code, wpo.warehouse_id, wpo.location_code, wpo.product_type,wpo.operation_type, wpo.outstock_type,
        wpo.plan_code, wpo.plan_detail_code,wpo.saleorder_code, wpo.product_id,wpo.apply_qty,wpo.outstock_qty,wpo.apply_reason, wpo.execute_status,wpo.apply_by, wpo.apply_date,
        wbw.warehouse_name,mbmi.material_code,mbmi.material_name
        from wms_product_outstock wpo left join wms_base_warehouse wbw on wpo.warehouse_id =wbw.warehouse_id
        left join mes_base_material_info mbmi on wpo.product_id = mbmi.material_id
        where wpo.product_outstock_id = #{productOutstockId}
    </select>



    <select id="selectOnlyWmsProductOutstockByOutstockId" parameterType="Long" resultMap="WmsProductOutstockResult">
        select a.product_outstock_id, a.task_code, a.warehouse_id, a.location_code, a.product_type, a.operation_type, a.outstock_type, a.plan_code, a.plan_detail_code, a.product_id,a.apply_qty,a.outstock_qty,a.sale_order_id, a.execute_status, a.apply_by, a.apply_date, a.begin_time, a.end_time
        from wms_product_outstock a
        where a.product_outstock_id = #{productOutstockId}
    </select>

</mapper>
