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

    <resultMap type="WmsTransfer" id="WmsTransferResult">
        <result property="transferId"    column="transfer_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="oriWarehouseId"    column="ori_warehouse_id"    />
        <result property="oriLocationCode"    column="ori_location_code"    />
        <result property="targetWarehouseId"    column="target_warehouse_id"    />
        <result property="targetLocationCode"    column="target_location_code"    />
        <result property="materialId"    column="material_id"    />
        <result property="productBatch"    column="product_batch"    />
        <result property="planCode"    column="plan_code"    />
        <result property="applyQty"    column="apply_qty"    />
        <result property="outstockQty"    column="outstock_qty"    />
        <result property="instockQty"    column="instock_qty"    />
        <result property="operationType"    column="operation_type"    />
        <result property="transferType"    column="transfer_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="oriWarehouseName"    column="ori_warehouse_name"    />
        <result property="targetWarehouseName"    column="target_warehouse_name"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <resultMap id="WmsTransferWmsTransferDetailResult" type="WmsTransfer" extends="WmsTransferResult">
        <collection property="wmsTransferDetailList" notNullColumn="sub_transfer_detail_id" javaType="java.util.List" resultMap="WmsTransferDetailResult" />
    </resultMap>

    <resultMap type="WmsTransferDetail" id="WmsTransferDetailResult">
        <result property="transferDetailId"    column="sub_transfer_detail_id"    />
        <result property="transferId"    column="sub_transfer_id"    />
        <result property="locationCode"    column="sub_location_code"    />
        <result property="materialBarcode"    column="sub_material_barcode"    />
        <result property="instockBatch"    column="sub_instock_batch"    />
        <result property="materialId"    column="sub_material_id"    />
        <result property="planAmount"    column="sub_plan_amount"    />
        <result property="realAmount"    column="sub_real_amount"    />
        <result property="executeStatus"    column="sub_execute_status"    />
        <result property="executePerson"    column="sub_execute_person"    />
        <result property="executeTime"    column="sub_execute_time"    />
        <result property="transferDetailType"    column="sub_transfer_detail_type"    />
        <result property="machineName"    column="sub_machine_name"    />
        <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"    />
    </resultMap>

    <sql id="selectWmsTransferVo">
        select transfer_id, task_code, ori_warehouse_id, ori_location_code, target_warehouse_id, target_location_code, material_id, product_batch, plan_code, apply_qty, outstock_qty, instock_qty, operation_type, transfer_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_transfer
    </sql>

    <select id="selectWmsTransferList" parameterType="WmsTransfer" resultMap="WmsTransferResult">
        <include refid="selectWmsTransferVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="oriWarehouseId != null "> and ori_warehouse_id = #{oriWarehouseId}</if>
            <if test="oriLocationCode != null  and oriLocationCode != ''"> and ori_location_code = #{oriLocationCode}</if>
            <if test="targetWarehouseId != null "> and target_warehouse_id = #{targetWarehouseId}</if>
            <if test="targetLocationCode != null  and targetLocationCode != ''"> and target_location_code = #{targetLocationCode}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="productBatch != null  and productBatch != ''"> and product_batch = #{productBatch}</if>
            <if test="planCode != null  and planCode != ''"> and plan_code = #{planCode}</if>
            <if test="applyQty != null "> and apply_qty = #{applyQty}</if>
            <if test="outstockQty != null "> and outstock_qty = #{outstockQty}</if>
            <if test="instockQty != null "> and instock_qty = #{instockQty}</if>
            <if test="operationType != null  and operationType != ''"> and operation_type = #{operationType}</if>
            <if test="transferType != null  and transferType != ''"> and transfer_type = #{transferType}</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 executeStatus != 'notFinish'"> and execute_status = #{executeStatus}</if>
            <if test="executeStatus != null  and executeStatus != '' and executeStatus == 'notFinish'"> and (execute_status = '0' or execute_status = '1')</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="selectWmsTransferByTransferId" parameterType="Long" resultMap="WmsTransferWmsTransferDetailResult">
        select a.transfer_id, a.task_code, a.ori_warehouse_id, a.ori_location_code, a.target_warehouse_id, a.target_location_code, a.material_id, a.product_batch, a.plan_code, a.apply_qty, a.outstock_qty, a.instock_qty, a.operation_type, a.transfer_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.transfer_detail_id as sub_transfer_detail_id, b.transfer_id as sub_transfer_id, b.location_code as sub_location_code, b.material_barcode as sub_material_barcode, b.instock_batch as sub_instock_batch, b.material_id as sub_material_id, b.plan_amount as sub_plan_amount, b.real_amount as sub_real_amount, b.execute_status as sub_execute_status, b.execute_person as sub_execute_person, b.execute_time as sub_execute_time, b.transfer_detail_type as sub_transfer_detail_type, b.machine_name as sub_machine_name, 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
        from wms_transfer a
        left join wms_transfer_detail b on b.transfer_id = a.transfer_id
        where a.transfer_id = #{transferId}
    </select>

    <insert id="insertWmsTransfer" parameterType="WmsTransfer" useGeneratedKeys="true" keyProperty="transferId">
        insert into wms_transfer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">task_code,</if>
            <if test="oriWarehouseId != null">ori_warehouse_id,</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">ori_location_code,</if>
            <if test="targetWarehouseId != null">target_warehouse_id,</if>
            <if test="targetLocationCode != null">target_location_code,</if>
            <if test="materialId != null">material_id,</if>
            <if test="productBatch != null and productBatch != ''">product_batch,</if>
            <if test="planCode != null and planCode != ''">plan_code,</if>
            <if test="applyQty != null">apply_qty,</if>
            <if test="outstockQty != null">outstock_qty,</if>
            <if test="instockQty != null">instock_qty,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="transferType != null and transferType != ''">transfer_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">#{taskCode},</if>
            <if test="oriWarehouseId != null">#{oriWarehouseId},</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">#{oriLocationCode},</if>
            <if test="targetWarehouseId != null">#{targetWarehouseId},</if>
            <if test="targetLocationCode != null">#{targetLocationCode},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="productBatch != null and productBatch != ''">#{productBatch},</if>
            <if test="planCode != null and planCode != ''">#{planCode},</if>
            <if test="applyQty != null">#{applyQty},</if>
            <if test="outstockQty != null">#{outstockQty},</if>
            <if test="instockQty != null">#{instockQty},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="transferType != null and transferType != ''">#{transferType},</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="updateWmsTransfer" parameterType="WmsTransfer">
        update wms_transfer
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="oriWarehouseId != null">ori_warehouse_id = #{oriWarehouseId},</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">ori_location_code = #{oriLocationCode},</if>
            <if test="targetWarehouseId != null">target_warehouse_id = #{targetWarehouseId},</if>
            <if test="targetLocationCode != null">target_location_code = #{targetLocationCode},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="productBatch != null and productBatch != ''">product_batch = #{productBatch},</if>
            <if test="planCode != null and planCode != ''">plan_code = #{planCode},</if>
            <if test="applyQty != null">apply_qty = #{applyQty},</if>
            <if test="outstockQty != null">outstock_qty = #{outstockQty},</if>
            <if test="instockQty != null">instock_qty = #{instockQty},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="transferType != null and transferType != ''">transfer_type = #{transferType},</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 transfer_id = #{transferId}
    </update>

    <delete id="deleteWmsTransferByTransferId" parameterType="Long">
        delete from wms_transfer where transfer_id = #{transferId}
    </delete>

    <delete id="deleteWmsTransferByTransferIds" parameterType="String">
        delete from wms_transfer where transfer_id in
        <foreach item="transferId" collection="array" open="(" separator="," close=")">
            #{transferId}
        </foreach>
    </delete>

    <delete id="deleteWmsTransferDetailByTransferIds" parameterType="String">
        delete from wms_transfer_detail where transfer_id in
        <foreach item="transferId" collection="array" open="(" separator="," close=")">
            #{transferId}
        </foreach>
    </delete>

    <delete id="deleteWmsTransferDetailByTransferId" parameterType="Long">
        delete from wms_transfer_detail where transfer_id = #{transferId}
    </delete>

    <insert id="batchWmsTransferDetail">
        insert into wms_transfer_detail( transfer_detail_id, transfer_id, location_code, material_barcode, instock_batch, material_id, plan_amount, real_amount, execute_status, execute_person, execute_time, transfer_detail_type, machine_name, create_by, create_date, update_by, update_date, active_flag) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.transferDetailId}, #{item.transferId}, #{item.locationCode}, #{item.materialBarcode}, #{item.instockBatch}, #{item.materialId}, #{item.planAmount}, #{item.realAmount}, #{item.executeStatus}, #{item.executePerson}, #{item.executeTime}, #{item.transferDetailType}, #{item.machineName}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}, #{item.activeFlag})
        </foreach>
    </insert>












    <select id="selectWmsTransferJoinList" parameterType="WmsTransfer" resultMap="WmsTransferResult">
        select wt.transfer_id, wt.task_code, wt.ori_warehouse_id, wt.ori_location_code, wt.target_warehouse_id, wt.target_location_code, wt.material_id, wt.product_batch,
        wt.plan_code, wt.apply_qty, wt.outstock_qty, wt.instock_qty, wt.operation_type, wt.transfer_type,
        wt.audit_status, wt.execute_status, wt.apply_by, wt.apply_date,
        wbw.warehouse_name ori_warehouse_name,wbw2.warehouse_name target_warehouse_name,mbmi.material_code,mbmi.material_name
        from wms_transfer wt
               left join wms_base_warehouse wbw on wbw.warehouse_id = wt.ori_warehouse_id
               left join wms_base_warehouse wbw2 on wbw2.warehouse_id = wt.target_warehouse_id
               left join mes_base_material_info mbmi on mbmi.material_id=wt.material_id
        <where>
            <if test="taskCode != null  and taskCode != ''"> and wt.task_code = #{taskCode}</if>
            <if test="oriWarehouseId != null "> and wt.ori_warehouse_id = #{oriWarehouseId}</if>
            <if test="oriLocationCode != null  and oriLocationCode != ''"> and wt.ori_location_code = #{oriLocationCode}</if>
            <if test="targetWarehouseId != null "> and wt.target_warehouse_id = #{targetWarehouseId}</if>
            <if test="targetLocationCode != null  and targetLocationCode != ''"> and wt.target_location_code = #{targetLocationCode}</if>
            <if test="materialId != null "> and wt.material_id = #{materialId}</if>
            <if test="productBatch != null  and productBatch != ''"> and wt.product_batch = #{productBatch}</if>
            <if test="planCode != null  and planCode != ''"> and wt.plan_code = #{planCode}</if>
            <if test="applyQty != null "> and wt.apply_qty = #{applyQty}</if>
            <if test="outstockQty != null "> and wt.outstock_qty = #{outstockQty}</if>
            <if test="instockQty != null "> and wt.instock_qty = #{instockQty}</if>
            <if test="operationType != null  and operationType != ''"> and wt.operation_type = #{operationType}</if>
            <if test="transferType != null  and transferType != ''"> and wt.transfer_type = #{transferType}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and wt.audit_status = #{auditStatus}</if>
            <if test="executeStatus != null  and executeStatus != '' and executeStatus != 'notFinish'"> and wt.execute_status = #{executeStatus}</if>
            <if test="executeStatus != null  and executeStatus != '' and executeStatus = 'notFinish'"> and (wt.execute_status = '0' or wt.execute_status = '1')</if>
            <if test="applyBy != null  and applyBy != ''"> and wt.apply_by = #{applyBy}</if>
            <if test="applyDate != null "> and wt.apply_date = #{applyDate}</if>
            <if test="auditBy != null  and auditBy != ''"> and wt.audit_by = #{auditBy}</if>
        </where>
    </select>

</mapper>
