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

    <resultMap type="WmsRawReturnDetail" id="WmsRawReturnDetailResult">
        <result property="rawReturnDetailId"    column="raw_return_detail_id"    />
        <result property="rawReturnId"    column="raw_return_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="returnAmount"    column="return_amount"    />
        <result property="executeStatus"    column="execute_status"    />
        <result property="erpStatus"    column="erp_status"    />
        <result property="returnPerson"    column="return_person"    />
        <result property="returnTime"    column="return_time"    />
        <result property="returnWay"    column="return_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"    />
    </resultMap>

    <sql id="selectWmsRawReturnDetailVo">
        select raw_return_detail_id, raw_return_id, location_code, material_barcode, material_id, instock_batch, material_production_Date, plan_amount, return_amount, execute_status, erp_status, return_person, return_time, return_way, machine_name, quality_status, create_by, create_date, update_by, update_date, stack_amount from wms_raw_return_detail
    </sql>

    <select id="selectWmsRawReturnDetailList" parameterType="WmsRawReturnDetail" resultMap="WmsRawReturnDetailResult">
        <include refid="selectWmsRawReturnDetailVo"/>
        <where>
            <if test="rawReturnId != null "> and raw_return_id = #{rawReturnId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</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="returnAmount != null "> and return_amount = #{returnAmount}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="erpStatus != null  and erpStatus != ''"> and erp_status = #{erpStatus}</if>
            <if test="returnPerson != null  and returnPerson != ''"> and return_person = #{returnPerson}</if>
            <if test="returnTime != null "> and return_time = #{returnTime}</if>
            <if test="returnWay != null  and returnWay != ''"> and return_way = #{returnWay}</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>
            <if test="stackAmount != null "> and stack_amount = #{stackAmount}</if>
        </where>
    </select>

    <select id="selectWmsRawReturnDetailByRawReturnDetailId" parameterType="Long" resultMap="WmsRawReturnDetailResult">
        <include refid="selectWmsRawReturnDetailVo"/>
        where raw_return_detail_id = #{rawReturnDetailId}
    </select>

    <insert id="insertWmsRawReturnDetail" parameterType="WmsRawReturnDetail" useGeneratedKeys="true" keyProperty="rawReturnDetailId">
        insert into wms_raw_return_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="rawReturnId != null">raw_return_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="returnAmount != null">return_amount,</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status,</if>
            <if test="erpStatus != null">erp_status,</if>
            <if test="returnPerson != null">return_person,</if>
            <if test="returnTime != null">return_time,</if>
            <if test="returnWay != null and returnWay != ''">return_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="rawReturnId != null">#{rawReturnId},</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="returnAmount != null">#{returnAmount},</if>
            <if test="executeStatus != null and executeStatus != ''">#{executeStatus},</if>
            <if test="erpStatus != null">#{erpStatus},</if>
            <if test="returnPerson != null">#{returnPerson},</if>
            <if test="returnTime != null">#{returnTime},</if>
            <if test="returnWay != null and returnWay != ''">#{returnWay},</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="updateWmsRawReturnDetail" parameterType="WmsRawReturnDetail">
        update wms_raw_return_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="rawReturnId != null">raw_return_id = #{rawReturnId},</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="returnAmount != null">return_amount = #{returnAmount},</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status = #{executeStatus},</if>
            <if test="erpStatus != null">erp_status = #{erpStatus},</if>
            <if test="returnPerson != null">return_person = #{returnPerson},</if>
            <if test="returnTime != null">return_time = #{returnTime},</if>
            <if test="returnWay != null and returnWay != ''">return_way = #{returnWay},</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_return_detail_id = #{rawReturnDetailId}
    </update>

    <delete id="deleteWmsRawReturnDetailByRawReturnDetailId" parameterType="Long">
        delete from wms_raw_return_detail where raw_return_detail_id = #{rawReturnDetailId}
    </delete>

    <delete id="deleteWmsRawReturnDetailByRawReturnDetailIds" parameterType="String">
        delete from wms_raw_return_detail where raw_return_detail_id in
        <foreach item="rawReturnDetailId" collection="array" open="(" separator="," close=")">
            #{rawReturnDetailId}
        </foreach>
    </delete>




    <select id="selectAuditPassRawReturnDetails" parameterType="WmsRawReturnDetail" resultMap="WmsRawReturnDetailResult">
        select wrrd.raw_return_detail_id, wrrd.raw_return_id, wrrd.location_code, wrrd.material_barcode, wrrd.material_id, wrrd.instock_batch,
        wrrd.material_production_date, wrrd.plan_amount, wrrd.return_amount, wrrd.execute_status, wrrd.erp_status, wrrd.return_person,
        wrrd.return_time, wrrd.return_way, wrrd.machine_name, wrrd.quality_status, wrrd.create_by, wrrd.create_date,wrr.task_code,wrr.task_type,wrr.apply_by
        from wms_raw_return_detail wrrd
        left join wms_raw_return wrr on wrrd.raw_return_id=wrr.raw_return_id

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



</mapper>
