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

    <resultMap type="WmsRawInstock" id="WmsRawInstockResult">
        <result property="rawInstockId"    column="raw_instock_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="poNo"    column="po_no"    />
        <result property="operationType"    column="operation_type"    />
        <result property="instockType"    column="instock_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"    />
    </resultMap>

    <resultMap id="WmsRawInstockWmsRawInstockDetailResult" type="WmsRawInstock" extends="WmsRawInstockResult">
        <collection property="wmsRawInstockDetailList" notNullColumn="sub_raw_instock_detail_id" javaType="java.util.List" resultMap="WmsRawInstockDetailResult" />
    </resultMap>

    <resultMap type="WmsRawInstockDetail" id="WmsRawInstockDetailResult">
        <result property="rawInstockDetailId"    column="sub_raw_instock_detail_id"    />
        <result property="rawInstockId"    column="sub_raw_instock_id"    />
        <result property="locationCode"    column="sub_location_code"    />
        <result property="materialBarcode"    column="sub_material_barcode"    />
        <result property="materialId"    column="sub_material_id"    />
        <result property="poNo"    column="po_no"    />
        <result property="instockBatch"    column="sub_instock_batch"    />
        <result property="materialProductionDate"    column="sub_material_production_Date"    />
        <result property="planAmount"    column="sub_plan_amount"    />
        <result property="instockAmount"    column="sub_instock_amount"    />
        <result property="executeStatus"    column="sub_execute_status"    />
        <result property="erpStatus"    column="sub_erp_status"    />
        <result property="instockPerson"    column="sub_instock_person"    />
        <result property="instockTime"    column="sub_instock_time"    />
        <result property="instockWay"    column="sub_instock_way"    />
        <result property="machineName"    column="sub_machine_name"    />
        <result property="qualityStatus"    column="sub_quality_status"    />
        <result property="stackAmount"    column="sub_stack_amount"    />
        <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="activeFlag"    column="sub_active_flag"    />
        <result property="poLine"    column="sub_po_line"    />
    </resultMap>

    <sql id="selectWmsRawInstockVo">
        select raw_instock_id, task_code, warehouse_id, location_code, po_no, operation_type, instock_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_instock
    </sql>

    <select id="selectWmsRawInstockList" parameterType="WmsRawInstock" resultMap="WmsRawInstockResult">
        <include refid="selectWmsRawInstockVo"/>
        <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="poNo != null  and poNo != ''"> and po_no = #{poNo}</if>
            <if test="operationType != null  and operationType != ''"> and operation_type = #{operationType}</if>
            <if test="instockType != null  and instockType != ''"> and instock_type = #{instockType}</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="selectWmsRawInstockByRawInstockId" parameterType="Long" resultMap="WmsRawInstockWmsRawInstockDetailResult">
        select a.raw_instock_id, a.task_code, a.warehouse_id, a.location_code, a.po_no, a.operation_type, a.instock_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.update_by, a.update_date, a.begin_time, a.end_time,
 b.raw_instock_detail_id as sub_raw_instock_detail_id, b.raw_instock_id as sub_raw_instock_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.instock_amount as sub_instock_amount, b.execute_status as sub_execute_status, b.erp_status as sub_erp_status, b.instock_person as sub_instock_person, b.instock_time as sub_instock_time, b.instock_way as sub_instock_way, b.machine_name as sub_machine_name, b.quality_status as sub_quality_status, b.stack_amount as sub_stack_amount, b.create_by as sub_create_by, b.create_date as sub_create_date, b.update_by as sub_update_by, b.update_date as sub_update_date, b.active_flag as sub_active_flag, b.po_line as sub_po_line
        from wms_raw_instock a
        left join wms_raw_instock_detail b on b.raw_instock_id = a.raw_instock_id
        where a.raw_instock_id = #{rawInstockId}
    </select>

    <insert id="insertWmsRawInstock" parameterType="WmsRawInstock" useGeneratedKeys="true" keyProperty="rawInstockId">
        insert into wms_raw_instock
        <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="poNo != null">po_no,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="instockType != null and instockType != ''">instock_type,</if>
            <if test="applyReason != null">apply_reason,</if>
            <if test="auditReason != null">audit_reason,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="executeStatus != null">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">#{taskCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="poNo != null">#{poNo},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="instockType != null and instockType != ''">#{instockType},</if>
            <if test="applyReason != null">#{applyReason},</if>
            <if test="auditReason != null">#{auditReason},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="executeStatus != null">#{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="updateWmsRawInstock" parameterType="WmsRawInstock">
        update wms_raw_instock
        <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="poNo != null">po_no = #{poNo},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="instockType != null and instockType != ''">instock_type = #{instockType},</if>
            <if test="applyReason != null">apply_reason = #{applyReason},</if>
            <if test="auditReason != null">audit_reason = #{auditReason},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="executeStatus != null">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_instock_id = #{rawInstockId}
    </update>

    <delete id="deleteWmsRawInstockByRawInstockId" parameterType="Long">
        delete from wms_raw_instock where raw_instock_id = #{rawInstockId}
    </delete>

    <delete id="deleteWmsRawInstockByRawInstockIds" parameterType="String">
        delete from wms_raw_instock where raw_instock_id in
        <foreach item="rawInstockId" collection="array" open="(" separator="," close=")">
            #{rawInstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsRawInstockDetailByRawInstockIds" parameterType="String">
        delete from wms_raw_instock_detail where raw_instock_id in
        <foreach item="rawInstockId" collection="array" open="(" separator="," close=")">
            #{rawInstockId}
        </foreach>
    </delete>

    <delete id="deleteWmsRawInstockDetailByRawInstockId" parameterType="Long">
        delete from wms_raw_instock_detail where raw_instock_id = #{rawInstockId}
    </delete>

    <insert id="batchWmsRawInstockDetail">
        insert into wms_raw_instock_detail( raw_instock_detail_id, raw_instock_id, location_code, material_barcode, material_id, po_no, instock_batch, material_production_Date, plan_amount,instock_amount, execute_status, erp_status, instock_person, instock_time, instock_way, machine_name, quality_status, stack_amount, create_by, create_date, update_by, update_date, active_flag, po_line) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.rawInstockDetailId}, #{item.rawInstockId}, #{item.locationCode}, #{item.materialBarcode}, #{item.materialId}, #{item.poNo}, #{item.instockBatch}, #{item.materialProductionDate},  #{item.planAmount},#{item.instockAmount}, #{item.executeStatus}, #{item.erpStatus}, #{item.instockPerson}, #{item.instockTime}, #{item.instockWay}, #{item.machineName}, #{item.qualityStatus}, #{item.stackAmount}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}, #{item.activeFlag}, #{item.poLine})
        </foreach>
    </insert>
</mapper>
