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

    <resultMap type="WmsRawReturn" id="WmsRawReturnResult">
        <result property="rawReturnId"    column="raw_return_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="planCode"    column="plan_code"    />
        <result property="planDetailCode"    column="plan_detail_code"    />
        <result property="productId"    column="product_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="returnAmount"    column="return_amount"    />
        <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="warehouseName"    column="warehouse_name"    />
    </resultMap>

    <resultMap id="WmsRawReturnWmsRawReturnDetailResult" type="WmsRawReturn" extends="WmsRawReturnResult">
        <collection property="wmsRawReturnDetailList" notNullColumn="sub_raw_return_detail_id" javaType="java.util.List" resultMap="WmsRawReturnDetailResult" />
    </resultMap>

    <resultMap type="WmsRawReturnDetail" id="WmsRawReturnDetailResult">
        <result property="rawReturnDetailId"    column="sub_raw_return_detail_id"    />
        <result property="rawReturnId"    column="sub_raw_return_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="returnAmount"    column="sub_return_amount"    />
        <result property="executeStatus"    column="sub_execute_status"    />
        <result property="erpStatus"    column="sub_erp_status"    />
        <result property="returnPerson"    column="sub_return_person"    />
        <result property="returnTime"    column="sub_return_time"    />
        <result property="returnWay"    column="sub_return_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="selectWmsRawReturnVo">
        select raw_return_id, task_code, warehouse_id, location_code, plan_code, plan_detail_code, product_id, 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 from wms_raw_return
    </sql>

    <select id="selectWmsRawReturnList" parameterType="WmsRawReturn" resultMap="WmsRawReturnResult">
        <include refid="selectWmsRawReturnVo"/>
        <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="planCode != null "> and plan_code = #{planCode}</if>
            <if test="planDetailCode != null "> and plan_detail_code = #{planDetailCode}</if>
            <if test="productId != null "> and product_id = #{productId}</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="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="selectWmsRawReturnByRawReturnId" parameterType="Long" resultMap="WmsRawReturnWmsRawReturnDetailResult">
        select a.raw_return_id, a.task_code, a.warehouse_id, a.location_code, a.plan_code, a.plan_detail_code, a.product_id, a.operation_type, a.task_type, 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.plan_amount,a.return_amount, a.begin_time, a.end_time,a.material_id,a.product_id,
 b.raw_return_detail_id as sub_raw_return_detail_id, b.raw_return_id as sub_raw_return_id, b.location_code as sub_location_code, b.material_barcode as sub_material_barcode, b.material_id as sub_material_id, b.instock_batch as sub_instock_batch, b.material_production_Date as sub_material_production_Date, b.plan_amount as sub_plan_amount, b.return_amount as sub_return_amount,
 b.execute_status as sub_execute_status, b.erp_status as sub_erp_status, b.return_person as sub_return_person, b.return_time as sub_return_time, b.return_way as sub_return_way, b.machine_name as sub_machine_name, b.quality_status as sub_quality_status, b.create_by as sub_create_by, b.stack_amount as sub_stack_amount

        from wms_raw_return a
        left join wms_raw_return_detail b on b.raw_return_id = a.raw_return_id
        where a.raw_return_id = #{rawReturnId}
    </select>

    <insert id="insertWmsRawReturn" parameterType="WmsRawReturn" useGeneratedKeys="true" keyProperty="rawReturnId">
        insert into wms_raw_return
        <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="planCode != null">plan_code,</if>
            <if test="planDetailCode != null">plan_detail_code,</if>
            <if test="productId != null">product_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="operationType != null">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>
         </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="planCode != null">#{planCode},</if>
            <if test="planDetailCode != null">#{planDetailCode},</if>
            <if test="productId != null">#{productId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="operationType != null">#{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>
         </trim>
    </insert>

    <update id="updateWmsRawReturn" parameterType="WmsRawReturn">
        update wms_raw_return
        <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="planCode != null">plan_code = #{planCode},</if>
            <if test="planDetailCode != null">plan_detail_code = #{planDetailCode},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="returnAmount != null">return_amount = #{returnAmount},</if>
            <if test="operationType != null">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>
        </trim>
        where raw_return_id = #{rawReturnId}
    </update>

    <delete id="deleteWmsRawReturnByRawReturnId" parameterType="Long">
        delete from wms_raw_return where raw_return_id = #{rawReturnId}
    </delete>

    <delete id="deleteWmsRawReturnByRawReturnIds" parameterType="String">
        delete from wms_raw_return where raw_return_id in
        <foreach item="rawReturnId" collection="array" open="(" separator="," close=")">
            #{rawReturnId}
        </foreach>
    </delete>

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

    <delete id="deleteWmsRawReturnDetailByRawReturnId" parameterType="Long">
        delete from wms_raw_return_detail where raw_return_id = #{rawReturnId}
    </delete>

    <insert id="batchWmsRawReturnDetail">
        insert into wms_raw_return_detail( 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) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.rawReturnDetailId}, #{item.rawReturnId}, #{item.locationCode}, #{item.materialBarcode}, #{item.materialId}, #{item.instockBatch}, #{item.materialProductionDate}, #{item.planAmount}, #{item.returnAmount}, #{item.executeStatus}, #{item.erpStatus}, #{item.returnPerson}, #{item.returnTime}, #{item.returnWay}, #{item.machineName}, #{item.qualityStatus}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}, #{item.stackAmount})
        </foreach>
    </insert>






    <select id="selectWmsRawReturnJoinList" parameterType="WmsRawReturn" resultMap="WmsRawReturnResult">
        select wrr.raw_return_id, wrr.task_code, wrr.warehouse_id, wrr.location_code, wrr.plan_code, wrr.plan_detail_code,
        wrr.material_id,wrr.plan_amount,wrr.return_amount,wrr.product_id,
        wrr.operation_type, wrr.task_type, wrr.apply_reason, wrr.audit_reason, wrr.audit_status,
        wrr.execute_status, wrr.apply_by, wrr.apply_date, wrr.audit_by, wrr.audit_date,wrr.begin_time, wrr.end_time,
        wbw.warehouse_name
        from wms_raw_return wrr left join wms_base_warehouse wbw on wrr.warehouse_id = wbw.warehouse_id

        <where>
            <if test="auditStatus != null  and auditStatus != ''"> and wrr.audit_status = #{auditStatus}</if>
        </where>
    </select>


    <select id="selectOnlyWmsRawReturnByRawReturnId" parameterType="Long" resultMap="WmsRawReturnResult">
        select wrr.raw_return_id, wrr.task_code, wrr.warehouse_id, wrr.location_code, wrr.plan_code, wrr.plan_detail_code,
               wrr.material_id,wrr.plan_amount,wrr.return_amount,wrr.product_id,
               wrr.operation_type, wrr.task_type, wrr.apply_reason, wrr.audit_reason, wrr.audit_status,
               wrr.execute_status, wrr.apply_by, wrr.apply_date, wrr.audit_by, wrr.audit_date,wrr.begin_time, wrr.end_time,
               wbw.warehouse_name
        from wms_raw_return wrr left join wms_base_warehouse wbw on wrr.warehouse_id = wbw.warehouse_id
        where wrr.raw_return_id = #{rawReturnId}
    </select>


</mapper>
