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

    <resultMap type="WmsMove" id="WmsMoveResult">
        <result property="moveId"    column="move_id"    />
        <result property="taskCode"    column="task_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="oriLocationCode"    column="ori_location_code"    />
        <result property="targetLocationCode"    column="target_location_code"    />
        <result property="instockBatch"    column="instock_batch"    />
        <result property="materialId"    column="material_id"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="realOutstockAmount"    column="real_outstock_amount"    />
        <result property="realInstockAmount"    column="real_instock_amount"    />
        <result property="operationType"    column="operation_type"    />
        <result property="moveWay"    column="move_way"    />
        <result property="moveType"    column="move_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"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <resultMap id="WmsMoveWmsMoveDetailResult" type="WmsMove" extends="WmsMoveResult">
        <collection property="wmsMoveDetailList" notNullColumn="sub_move_detail_id" javaType="java.util.List" resultMap="WmsMoveDetailResult" />
    </resultMap>

    <resultMap type="WmsMoveDetail" id="WmsMoveDetailResult">
        <result property="moveDetailId"    column="sub_move_detail_id"    />
        <result property="moveId"    column="sub_move_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="executeEndTime"    column="sub_execute_end_time"    />
        <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="selectWmsMoveVo">
        select move_id, task_code, warehouse_id, ori_location_code, target_location_code, instock_batch, material_id, plan_amount, real_outstock_amount, real_instock_amount, operation_type, move_way, move_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_move
    </sql>

    <select id="selectWmsMoveList" parameterType="WmsMove" resultMap="WmsMoveResult">
        <include refid="selectWmsMoveVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="oriLocationCode != null  and oriLocationCode != ''"> and ori_location_code = #{oriLocationCode}</if>
            <if test="targetLocationCode != null  and targetLocationCode != ''"> and target_location_code = #{targetLocationCode}</if>
            <if test="operationType != null  and operationType != ''"> and operation_type = #{operationType}</if>
            <if test="moveWay != null  and moveWay != ''"> and move_way = #{moveWay}</if>
            <if test="moveType != null  and moveType != ''"> and move_type = #{moveType}</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="selectWmsMoveByMoveId" parameterType="Long" resultMap="WmsMoveWmsMoveDetailResult">
        select a.move_id, a.task_code, a.warehouse_id, a.ori_location_code, a.target_location_code, a.instock_batch, a.material_id, a.plan_amount, a.real_outstock_amount, a.real_instock_amount, a.operation_type, a.move_way, a.move_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.move_detail_id as sub_move_detail_id, b.move_id as sub_move_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.move_detail_type as sub_move_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_move a
                 left join wms_move_detail b on b.move_id = a.move_id
        where a.move_id = #{moveId}
    </select>

    <insert id="insertWmsMove" parameterType="WmsMove" useGeneratedKeys="true" keyProperty="moveId">
        insert into wms_move
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">task_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">ori_location_code,</if>
            <if test="targetLocationCode != null and targetLocationCode != ''">target_location_code,</if>
            <if test="instockBatch != null">instock_batch,</if>
            <if test="materialId != null">material_id,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="realOutstockAmount != null">real_outstock_amount,</if>
            <if test="realInstockAmount != null">real_instock_amount,</if>
            <if test="operationType != null and operationType != ''">operation_type,</if>
            <if test="moveWay != null and moveWay != ''">move_way,</if>
            <if test="moveType != null and moveType != ''">move_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="warehouseId != null">#{warehouseId},</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">#{oriLocationCode},</if>
            <if test="targetLocationCode != null and targetLocationCode != ''">#{targetLocationCode},</if>
            <if test="instockBatch != null">#{instockBatch},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="realOutstockAmount != null">#{realOutstockAmount},</if>
            <if test="realInstockAmount != null">#{realInstockAmount},</if>
            <if test="operationType != null and operationType != ''">#{operationType},</if>
            <if test="moveWay != null and moveWay != ''">#{moveWay},</if>
            <if test="moveType != null and moveType != ''">#{moveType},</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="updateWmsMove" parameterType="WmsMove">
        update wms_move
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="oriLocationCode != null and oriLocationCode != ''">ori_location_code = #{oriLocationCode},</if>
            <if test="targetLocationCode != null and targetLocationCode != ''">target_location_code = #{targetLocationCode},</if>
            <if test="instockBatch != null">instock_batch = #{instockBatch},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="planAmount != null">plan_amount = #{planAmount},</if>
            <if test="realOutstockAmount != null">real_outstock_amount = #{realOutstockAmount},</if>
            <if test="realInstockAmount != null">real_instock_amount = #{realInstockAmount},</if>
            <if test="operationType != null and operationType != ''">operation_type = #{operationType},</if>
            <if test="moveWay != null and moveWay != ''">move_way = #{moveWay},</if>
            <if test="moveType != null and moveType != ''">move_type = #{moveType},</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 move_id = #{moveId}
    </update>

    <delete id="deleteWmsMoveByMoveId" parameterType="Long">
        delete from wms_move where move_id = #{moveId}
    </delete>

    <delete id="deleteWmsMoveByMoveIds" parameterType="String">
        delete from wms_move where move_id in
        <foreach item="moveId" collection="array" open="(" separator="," close=")">
            #{moveId}
        </foreach>
    </delete>

    <delete id="deleteWmsMoveDetailByMoveIds" parameterType="String">
        delete from wms_move_detail where move_id in
        <foreach item="moveId" collection="array" open="(" separator="," close=")">
            #{moveId}
        </foreach>
    </delete>

    <delete id="deleteWmsMoveDetailByMoveId" parameterType="Long">
        delete from wms_move_detail where move_id = #{moveId}
    </delete>

    <insert id="batchWmsMoveDetail">
        insert into wms_move_detail( move_detail_id, move_id, location_code, material_barcode, instock_batch, material_id, plan_amount, real_amount, execute_status, execute_person, execute_time, execute_end_time, machine_name, create_by, create_date, update_by, update_date, active_flag) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.moveDetailId}, #{item.moveId}, #{item.locationCode}, #{item.materialBarcode}, #{item.instockBatch}, #{item.materialId}, #{item.planAmount}, #{item.realAmount}, #{item.executeStatus}, #{item.executePerson}, #{item.executeTime}, #{item.executeEndTime}, #{item.machineName}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate}, #{item.activeFlag})
        </foreach>
    </insert>









    <sql id="selectWmsMoveJoinVo">
        select wm.move_id, wm.task_code, wm.warehouse_id, wm.ori_location_code, wm.target_location_code,
                wm.move_type,wm.instock_batch,wm.plan_amount,wm.real_outstock_amount,wm.real_instock_amount,wm.execute_status,
               mbmi.material_name,wbw.warehouse_name
        from wms_move wm left join mes_base_material_info mbmi on wm.material_id = mbmi.material_id
        left join wms_base_warehouse wbw on wm.warehouse_id = wbw.warehouse_id
    </sql>

    <select id="selectWmsMoveJoinList" parameterType="WmsMove" resultMap="WmsMoveResult">
        <include refid="selectWmsMoveJoinVo"/>
        <where>
            <if test="taskCode != null  and taskCode != ''"> and task_code = #{taskCode}</if>
            <if test="executeStatusStr != null  and executeStatusStr != ''"> and execute_status in (${executeStatusStr})</if>
        </where>
    </select>



    <select id="selectWmsMoveGroupJoinList" parameterType="WmsMove" resultMap="WmsMoveResult">
        select wm.task_code,wm.warehouse_id,wm.ori_location_code,wm.target_location_code,
               wbw.warehouse_name
               from wms_move wm left join wms_base_warehouse wbw on wm.warehouse_id=wbw.warehouse_id
        <where>
           <if test="executeStatusStr != null  and executeStatusStr != ''"> and wm.execute_status in (${executeStatusStr})</if>
            <if test="moveType != null  and moveType != ''"> and wm.move_type = #{moveType}</if>
        </where>
        group by wm.task_code,wm.warehouse_id,wm.ori_location_code,wm.target_location_code
    </select>



    <insert id="batchWmsMove">
        insert into wms_move(move_id, task_code, warehouse_id,ori_location_code,target_location_code, instock_batch, material_id, plan_amount,operation_type,move_way,move_type,audit_status, execute_status, apply_by, apply_date) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.moveId}, #{item.taskCode}, #{item.warehouseId}, #{item.oriLocationCode}, #{item.targetLocationCode}, #{item.instockBatch}, #{item.materialId}, #{item.planAmount}, #{item.operationType}, #{item.moveWay}, #{item.moveType}, #{item.auditStatus}, #{item.executeStatus}, #{item.applyBy}, #{item.applyDate})
        </foreach>
    </insert>

</mapper>
