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

    <resultMap type="WmsRawOutstock" id="WmsRawOutstockResult">
        <result property="rawOutstockId"    column="raw_outstock_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="orderId"    column="order_id"    />
        <result property="planCode"    column="plan_code"    />
        <result property="planDetailCode"    column="plan_detail_code"    />
        <result property="stationId"    column="station_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialBatch"    column="material_batch"    />
        <result property="palletInfoCode"    column="pallet_info_code"    />
        <result property="outstockAmount"    column="outstock_amount"    />
        <result property="realOutstockAmount"    column="real_outstock_amount"    />
        <result property="endStationCode"    column="end_station_code"    />
        <result property="operationType"    column="operation_type"    />
        <result property="taskType"    column="task_type"    />
        <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"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="instockBatch"    column="instock_batch"    />
        <result property="rawOutstockDetailId"    column="raw_outstock_detail_id"    />
    </resultMap>

    <resultMap id="WmsRawOutstockWmsRawOutstockDetailResult" type="WmsRawOutstock" extends="WmsRawOutstockResult">
        <collection property="wmsRawOutstockDetailList" notNullColumn="sub_raw_outstock_detail_id" javaType="java.util.List" resultMap="WmsRawOutstockDetailResult" />
    </resultMap>

    <resultMap type="WmsRawOutstockDetail" id="WmsRawOutstockDetailResult">
        <result property="rawOutstockDetailId"    column="sub_raw_outstock_detail_id"    />
        <result property="rawOutstockId"    column="sub_raw_outstock_id"    />
        <result property="locationCode"    column="sub_location_code"    />
        <result property="materialBarcode"    column="sub_material_barcode"    />
        <result property="materialId"    column="sub_material_id"    />
        <result property="instockBatch"    column="sub_instock_batch"    />
        <result property="materialProductionDate"    column="sub_material_production_date"    />
        <result property="planAmount"    column="sub_plan_amount"    />
        <result property="outstockAmount"    column="sub_outstock_amount"    />
        <result property="executeStatus"    column="sub_execute_status"    />
        <result property="erpStatus"    column="sub_erp_status"    />
        <result property="outstockPerson"    column="sub_outstock_person"    />
        <result property="outstockTime"    column="sub_outstock_time"    />
        <result property="outstockWay"    column="sub_outstock_way"    />
        <result property="machineName"    column="sub_machine_name"    />
        <result property="qualityStatus"    column="sub_quality_status"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createDate"    column="sub_create_date"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateDate"    column="sub_update_date"    />
        <result property="stackAmount"    column="sub_stack_amount"    />
    </resultMap>

    <sql id="selectWmsRawOutstockVo">
        select raw_outstock_id, task_code, warehouse_id, location_code, order_id, plan_code, plan_detail_code, station_id, material_id, material_batch, pallet_info_code, outstock_amount, real_outstock_amount, end_station_code, operation_type, task_type, 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_raw_outstock
    </sql>

    <select id="selectWmsRawOutstockList" parameterType="WmsRawOutstock" resultMap="WmsRawOutstockResult">
        <include refid="selectWmsRawOutstockVo"/>
        <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="orderId != null "> and order_id = #{orderId}</if>
            <if test="planCode != null "> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and plan_detail_code = #{planDetailCode}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="operationType != null  and operationType != ''"> and operation_type = #{operationType}</if>
            <if test="taskType != null  and taskType != ''"> and task_type = #{taskType}</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="executeStatusStr != null  and executeStatusStr != ''"> and execute_status in (${executeStatusStr})</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="selectWmsRawOutstockByRawOutstockId" parameterType="Long" resultMap="WmsRawOutstockResult">
        <include refid="selectWmsRawOutstockVo"/>
        where raw_outstock_id = #{rawOutstockId}
    </select>

    <insert id="insertWmsRawOutstock" parameterType="WmsRawOutstock" useGeneratedKeys="true" keyProperty="rawOutstockId">
        insert into wms_raw_outstock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskCode != null and taskCode != ''">task_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="locationCode != null">location_code,</if>
            <if test="orderId != null">order_id,</if>
            <if test="planCode != null">plan_code,</if>
            <if test="planDetailCode != null">plan_detail_code,</if>
            <if test="stationId != null">station_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialBatch != null">material_batch,</if>
            <if test="palletInfoCode != null">pallet_info_code,</if>
            <if test="outstockAmount != null">outstock_amount,</if>
            <if test="realOutstockAmount != null">real_outstock_amount,</if>
            <if test="endStationCode != null">end_station_code,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="taskType != null and taskType != ''">task_type,</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 and taskCode != ''">#{taskCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="planDetailCode != null">#{planDetailCode},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialBatch != null">#{materialBatch},</if>
            <if test="palletInfoCode != null">#{palletInfoCode},</if>
            <if test="outstockAmount != null">#{outstockAmount},</if>
            <if test="realOutstockAmount != null">#{realOutstockAmount},</if>
            <if test="endStationCode != null">#{endStationCode},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="taskType != null and taskType != ''">#{taskType},</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="updateWmsRawOutstock" parameterType="WmsRawOutstock">
        update wms_raw_outstock
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskCode != null and taskCode != ''">task_code = #{taskCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="locationCode != null">location_code = #{locationCode},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="planCode != null">plan_code = #{planCode},</if>
            <if test="planDetailCode != null">plan_detail_code = #{planDetailCode},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialBatch != null">material_batch = #{materialBatch},</if>
            <if test="palletInfoCode != null">pallet_info_code = #{palletInfoCode},</if>
            <if test="outstockAmount != null">outstock_amount = #{outstockAmount},</if>
            <if test="realOutstockAmount != null">real_outstock_amount = #{realOutstockAmount},</if>
            <if test="endStationCode != null">end_station_code = #{endStationCode},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="taskType != null and taskType != ''">task_type = #{taskType},</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 raw_outstock_id = #{rawOutstockId}
    </update>

    <delete id="deleteWmsRawOutstockByRawOutstockId" parameterType="Long">
        delete from wms_raw_outstock where raw_outstock_id = #{rawOutstockId}
    </delete>

    <delete id="deleteWmsRawOutstockByRawOutstockIds" parameterType="String">
        delete from wms_raw_outstock where raw_outstock_id in
        <foreach item="rawOutstockId" collection="array" open="(" separator="," close=")">
            #{rawOutstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsRawOutstockDetailByRawOutstockIds" parameterType="String">
        delete from wms_raw_outstock_detail where raw_outstock_id in
        <foreach item="rawOutstockId" collection="array" open="(" separator="," close=")">
            #{rawOutstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsRawOutstockDetailByRawOutstockId" parameterType="Long">
        delete from wms_raw_outstock_detail where raw_outstock_id = #{rawOutstockId}
    </delete>

    <insert id="batchWmsRawOutstockDetail">
        insert into wms_raw_outstock_detail( raw_outstock_detail_id, raw_outstock_id, location_code, material_barcode, material_id, instock_batch, material_production_date, plan_amount, outstock_amount, execute_status, erp_status, outstock_person, outstock_time, outstock_way, machine_name, quality_status, create_by, create_date, update_by, update_date, stack_amount) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.rawOutstockDetailId}, #{item.rawOutstockId}, #{item.locationCode}, #{item.materialBarcode}, #{item.materialId}, #{item.instockBatch}, #{item.materialProductionDate}, #{item.planAmount}, #{item.outstockAmount}, #{item.executeStatus}, #{item.erpStatus}, #{item.outstockPerson}, #{item.outstockTime}, #{item.outstockWay}, #{item.machineName}, #{item.qualityStatus}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}, #{item.stackAmount})
        </foreach>
    </insert>



    <select id="selectWmsRawOutstockJoinList" parameterType="WmsRawOutstock" resultMap="WmsRawOutstockResult">
        select wro.raw_outstock_id, wro.task_code, wro.warehouse_id, wro.location_code, wro.order_id, wro.plan_code, wro.plan_detail_code, wro.station_id,wro.material_batch,
        wro.material_id, wro.operation_type, wro.task_type, wro.apply_reason, wro.audit_reason, wro.audit_status, wro.execute_status, wro.apply_by, wro.apply_date,
        wro.audit_by, wro.audit_date, wro.update_by, wro.update_date, wro.begin_time, wro.end_time,wbw.warehouse_name
        from wms_raw_outstock wro left join wms_base_warehouse wbw on wro.warehouse_id = wbw.warehouse_id
        <where>
            <if test="taskCode != null  and taskCode != ''"> and wro.task_code like concat('%', #{taskCode}, '%')</if>
            <if test="warehouseId != null "> and wro.warehouse_id = #{warehouseId}</if>
            <if test="planCode != null "> and wro.plan_code like concat('%', #{planCode}, '%')</if>
            <if test="planDetailCode != null "> and wro.plan_detail_code like concat('%', #{planDetailCode}, '%')</if>
            <if test="applyReason != null  and applyReason != ''"> and wro.apply_reason like concat('%', #{applyReason}, '%')</if>
            <if test="auditStatus != null  and auditStatus != ''"> and wro.audit_status = #{auditStatus}</if>
            <if test="taskType != null  and taskType != ''"> and wro.task_type = #{taskType}</if>
            <if test="applyBy != null  and applyBy != ''"> and wro.apply_by like concat('%', #{applyBy}, '%')</if>
        </where>
    </select>

    <select id="selectWmsRawOutstockJoinList4Audit" parameterType="WmsRawOutstock" resultMap="WmsRawOutstockResult">
        select wro.task_code, wro.warehouse_id,  wro.apply_reason, wro.apply_by, wro.apply_date,wro.plan_code,wro.plan_detail_code,wro.audit_status,wro.task_type,
         wbw.warehouse_name
        from wms_raw_outstock wro left join wms_base_warehouse wbw on wro.warehouse_id = wbw.warehouse_id
        <where>
            <if test="taskCode != null  and taskCode != ''"> and wro.task_code like concat('%', #{taskCode}, '%')</if>
            <if test="warehouseId != null "> and wro.warehouse_id = #{warehouseId}</if>
            <if test="planCode != null "> and wro.plan_code like concat('%', #{planCode}, '%')</if>
            <if test="planDetailCode != null "> and wro.plan_detail_code like concat('%', #{planDetailCode}, '%')</if>
            <if test="applyReason != null  and applyReason != ''"> and wro.apply_reason like concat('%', #{applyReason}, '%')</if>
            <if test="auditStatus != null  and auditStatus != ''"> and wro.audit_status = #{auditStatus}</if>
            <if test="taskType != null  and taskType != ''"> and wro.task_type = #{taskType}</if>
            <if test="applyBy != null  and applyBy != ''"> and wro.apply_by like concat('%', #{applyBy}, '%')</if>
        </where>
        group by wro.task_code,wro.warehouse_id,  wro.apply_reason, wro.apply_by, wro.apply_date,wro.plan_code,wro.plan_detail_code,wro.audit_status,wro.task_type
    </select>

    <!--as转换是为了兼容PDA转换-->
    <select id="selectWmsRawOutstockJoinMaterialList" parameterType="WmsRawOutstock" resultMap="WmsRawOutstockResult">
        select wro.raw_outstock_id as raw_outstock_detail_id, wro.task_code,wro.location_code,wro.material_batch as instock_batch,wro.outstock_amount as plan_amount,wro.real_outstock_amount as outstock_amount,
               wro.apply_by,wro.apply_date,wro.apply_reason,wro.audit_by,wro.audit_date,wro.audit_reason,wro.plan_code,wro.plan_detail_code,wro.audit_status,wro.task_type,
               wro.execute_status,wro.erp_status,wro.erp_amount,
               mbmi.material_code,mbmi.material_name
        from wms_raw_outstock wro left join mes_base_material_info mbmi on wro.material_id = mbmi.material_id
        <where>
            <if test="taskCode != null  and taskCode != ''"> and wro.task_code = #{taskCode}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and wro.audit_status = #{auditStatus}</if>
            <if test="applyBy != null  and applyBy != ''"> and wro.apply_by like concat('%', #{applyBy}, '%')</if>
        </where>
    </select>

    <insert id="batchWmsRawOutstock">
        insert into wms_raw_outstock(raw_outstock_id, task_code,warehouse_id,location_code,order_id,plan_code,plan_detail_code,station_id,material_id,material_batch,pallet_info_code,outstock_amount,end_station_code,operation_type,task_type,audit_status,execute_status,apply_by,apply_date) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.rawOutstockId}, #{item.taskCode}, #{item.warehouseId}, #{item.locationCode}, #{item.orderId}, #{item.planCode}, #{item.planDetailCode}, #{item.stationId}, #{item.materialId}, #{item.materialBatch}, #{item.palletInfoCode}, #{item.outstockAmount}, #{item.endStationCode}, #{item.operationType}, #{item.taskType}, #{item.auditStatus}, #{item.executeStatus}, #{item.applyBy}, #{item.applyDate})
        </foreach>
    </insert>

    <update id="auditWmsRawOutstockByTaskCode" parameterType="WmsRawOutstock">
        update wms_raw_outstock
        <trim prefix="SET" suffixOverrides=",">
            <if test="auditReason != null">audit_reason = #{auditReason},</if>
            <if test="auditStatus != null and auditStatus != ''">audit_status = #{auditStatus},</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>
        </trim>
        where task_code = #{taskCode}
    </update>


    <select id="selectWmsRawOutstockJoinMaterialById" parameterType="WmsRawOutstock" resultMap="WmsRawOutstockResult">
        select wro.raw_outstock_id as raw_outstock_detail_id, wro.task_code,wro.location_code,wro.material_batch,wro.outstock_amount,wro.real_outstock_amount,
        wro.apply_by,wro.apply_date,wro.apply_reason,wro.audit_by,wro.audit_date,wro.audit_reason,wro.plan_code,wro.plan_detail_code,wro.audit_status,wro.task_type,
        wro.execute_status,wro.erp_status,wro.erp_amount,
        mbmi.material_code,mbmi.material_name
        from wms_raw_outstock wro left join mes_base_material_info mbmi on wro.material_id = mbmi.material_id
        where wro.raw_outstock_id = #{rawOutstockId}
    </select>


</mapper>
