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

    <resultMap type="WmsRawOutstockDetail" id="WmsRawOutstockDetailResult">
        <result property="rawOutstockDetailId"    column="raw_outstock_detail_id"    />
        <result property="rawOutstockId"    column="raw_outstock_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="materialBarcode"    column="material_barcode"    />
        <result property="materialId"    column="material_id"    />
        <result property="instockBatch"    column="instock_batch"    />
        <result property="materialProductionDate"    column="material_production_date"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="outstockAmount"    column="outstock_amount"    />
        <result property="executeStatus"    column="execute_status"    />
        <result property="erpStatus"    column="erp_status"    />
        <result property="outstockPerson"    column="outstock_person"    />
        <result property="outstockTime"    column="outstock_time"    />
        <result property="outstockWay"    column="outstock_way"    />
        <result property="machineName"    column="machine_name"    />
        <result property="qualityStatus"    column="quality_status"    />
        <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="stackAmount"    column="stack_amount"    />
        <result property="applyBy"    column="apply_by"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="taskCode"    column="task_code"    />
        <result property="taskType"    column="task_type"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <sql id="selectWmsRawOutstockDetailVo">
        select 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 from wms_raw_outstock_detail wrod
    </sql>

    <select id="selectWmsRawOutstockDetailList" parameterType="WmsRawOutstockDetail" resultMap="WmsRawOutstockDetailResult">
        <include refid="selectWmsRawOutstockDetailVo"/>
        <where>
            <if test="rawOutstockId != null "> and raw_outstock_id = #{rawOutstockId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and exists (select 1 from wms_raw_outstock wro where wro.raw_outstock_id=wrod.raw_outstock_id and wro.audit_status=#{auditStatus})</if>
            <if test="materialBarcode != null  and materialBarcode != ''"> and material_barcode = #{materialBarcode}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="materialProductionDate != null "> and material_production_Date = #{materialProductionDate}</if>
            <if test="planAmount != null "> and plan_amount = #{planAmount}</if>

            <if test="executeStatusStr != null  and executeStatusStr != ''"> and execute_status in (${executeStatusStr})</if>
            <if test="erpStatus != null  and erpStatus != ''"> and erp_status = #{erpStatus}</if>
            <if test="outstockPerson != null  and outstockPerson != ''"> and outstock_person = #{outstockPerson}</if>
            <if test="outstockTime != null "> and outstock_time = #{outstockTime}</if>
            <if test="outstockWay != null  and outstockWay != ''"> and outstock_way = #{outstockWay}</if>
            <if test="machineName != null  and machineName != ''"> and machine_name like concat('%', #{machineName}, '%')</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
        </where>
    </select>

    <select id="selectWmsRawOutstockDetailByRawOutstockDetailId" parameterType="Long" resultMap="WmsRawOutstockDetailResult">
        <include refid="selectWmsRawOutstockDetailVo"/>
        where raw_outstock_detail_id = #{rawOutstockDetailId}
    </select>

    <insert id="insertWmsRawOutstockDetail" parameterType="WmsRawOutstockDetail" useGeneratedKeys="true" keyProperty="rawOutstockDetailId">
        insert into wms_raw_outstock_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rawOutstockId != null">raw_outstock_id,</if>
            <if test="locationCode != null and locationCode != ''">location_code,</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode,</if>
            <if test="materialId != null">material_id,</if>
            <if test="instockBatch != null">instock_batch,</if>
            <if test="materialProductionDate != null">material_production_Date,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="outstockAmount != null">outstock_amount,</if>
            <if test="executeStatus != null">execute_status,</if>
            <if test="erpStatus != null">erp_status,</if>
            <if test="outstockPerson != null">outstock_person,</if>
            <if test="outstockTime != null">outstock_time,</if>
            <if test="outstockWay != null">outstock_way,</if>
            <if test="machineName != null">machine_name,</if>
            <if test="qualityStatus != null">quality_status,</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="stackAmount != null">stack_amount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="rawOutstockId != null">#{rawOutstockId},</if>
            <if test="locationCode != null and locationCode != ''">#{locationCode},</if>
            <if test="materialBarcode != null and materialBarcode != ''">#{materialBarcode},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="instockBatch != null">#{instockBatch},</if>
            <if test="materialProductionDate != null">#{materialProductionDate},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="outstockAmount != null">#{outstockAmount},</if>
            <if test="executeStatus != null">#{executeStatus},</if>
            <if test="erpStatus != null">#{erpStatus},</if>
            <if test="outstockPerson != null">#{outstockPerson},</if>
            <if test="outstockTime != null">#{outstockTime},</if>
            <if test="outstockWay != null">#{outstockWay},</if>
            <if test="machineName != null">#{machineName},</if>
            <if test="qualityStatus != null">#{qualityStatus},</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="stackAmount != null">#{stackAmount},</if>
         </trim>
    </insert>

    <update id="updateWmsRawOutstockDetail" parameterType="WmsRawOutstockDetail">
        update wms_raw_outstock_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="rawOutstockId != null">raw_outstock_id = #{rawOutstockId},</if>
            <if test="locationCode != null and locationCode != ''">location_code = #{locationCode},</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode = #{materialBarcode},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="instockBatch != null">instock_batch = #{instockBatch},</if>
            <if test="materialProductionDate != null">material_production_Date = #{materialProductionDate},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="outstockAmount != null">outstock_amount = #{outstockAmount},</if>
            <if test="executeStatus != null">execute_status = #{executeStatus},</if>
            <if test="erpStatus != null">erp_status = #{erpStatus},</if>
            <if test="outstockPerson != null">outstock_person = #{outstockPerson},</if>
            <if test="outstockTime != null">outstock_time = #{outstockTime},</if>
            <if test="outstockWay != null">outstock_way = #{outstockWay},</if>
            <if test="machineName != null">machine_name = #{machineName},</if>
            <if test="qualityStatus != null">quality_status = #{qualityStatus},</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="stackAmount != null">stack_amount = #{stackAmount},</if>
        </trim>
        where raw_outstock_detail_id = #{rawOutstockDetailId}
    </update>

    <delete id="deleteWmsRawOutstockDetailByRawOutstockDetailId" parameterType="Long">
        delete from wms_raw_outstock_detail where raw_outstock_detail_id = #{rawOutstockDetailId}
    </delete>

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


    <select id="selectWmsManualRawOutstockDetail"  resultMap="WmsRawOutstockDetailResult">
        <include refid="selectWmsRawOutstockDetailVo"/>
        where raw_outstock_id = #{rawOutstockId} and location_code = #{locationCode} and material_barcode = #{materialBarcode}
        and outstock_way='1' limit 1
    </select>






    <select id="selectAuditPassRawOutstockDetails" parameterType="WmsRawOutstockDetail" resultMap="WmsRawOutstockDetailResult">
        select wrod.raw_outstock_detail_id, wrod.raw_outstock_id, wrod.location_code, wrod.material_id,
        wrod.instock_batch, wrod.material_production_date, wrod.plan_amount, wrod.outstock_amount,
        wrod.execute_status, wrod.erp_status, wrod.outstock_person, wrod.outstock_time, wrod.outstock_way,
        wrod.machine_name, wrod.quality_status,wro.task_code,wro.task_type,wro.apply_by,wro.apply_date,
        mbmi.material_code,mbmi.material_name
        from wms_raw_outstock_detail wrod
            left join wms_raw_outstock wro on wrod.raw_outstock_id=wro.raw_outstock_id
            left join mes_base_material_info mbmi on wrod.material_id=mbmi.material_id

        <where>
            wro.audit_status = '1' and wrod.execute_status!='2'
            <if test="applyBy != null  and applyBy != ''"> and wro.apply_by like concat('%', #{applyBy}, '%')</if>
        </where>
    </select>

    <select id="selectCountOfWmsRawOutstockDetail" parameterType="WmsRawOutstockDetail">
        select count(1) from wms_raw_outstock_detail
        <where>
            <if test="rawOutstockId != null "> and raw_outstock_id = #{rawOutstockId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and exists (select 1 from wms_raw_outstock wro where wro.raw_outstock_id=wrod.raw_outstock_id and wro.audit_status=#{auditStatus})</if>
            <if test="materialBarcode != null  and materialBarcode != ''"> and material_barcode = #{materialBarcode}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="materialProductionDate != null "> and material_production_Date = #{materialProductionDate}</if>
            <if test="planAmount != null "> and plan_amount = #{planAmount}</if>

            <if test="executeStatusStr != null  and executeStatusStr != ''"> and execute_status in (${executeStatusStr})</if>
            <if test="erpStatus != null  and erpStatus != ''"> and erp_status = #{erpStatus}</if>
            <if test="outstockPerson != null  and outstockPerson != ''"> and outstock_person = #{outstockPerson}</if>
            <if test="outstockTime != null "> and outstock_time = #{outstockTime}</if>
            <if test="outstockWay != null  and outstockWay != ''"> and outstock_way = #{outstockWay}</if>
            <if test="machineName != null  and machineName != ''"> and machine_name like concat('%', #{machineName}, '%')</if>
            <if test="qualityStatus != null  and qualityStatus != ''"> and quality_status = #{qualityStatus}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
        </where>
    </select>

</mapper>
