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

    <resultMap type="WmsMoveDetail" id="WmsMoveDetailResult">
        <result property="moveDetailId"    column="move_detail_id"    />
        <result property="moveId"    column="move_id"    />
        <result property="materialBarcode"    column="material_barcode"    />
        <result property="instockBatch"    column="instock_batch"    />
        <result property="materialId"    column="material_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="realAmount"    column="real_amount"    />
        <result property="realInstockAmount"    column="real_instock_amount"    />
        <result property="executeStatus"    column="execute_status"    />
        <result property="executePerson"    column="execute_person"    />
        <result property="executeTime"    column="execute_time"    />
        <result property="executeEndTime"    column="execute_end_time"    />
        <result property="machineName"    column="machine_name"    />
        <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="activeFlag"    column="active_flag"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <sql id="selectWmsMoveDetailVo">
        select move_detail_id, move_id, material_barcode, instock_batch, material_id, plan_amount, real_amount, execute_status, execute_person, execute_time, machine_name, create_by, create_date, update_by, update_date, active_flag from wms_move_detail wmd
    </sql>

    <select id="selectWmsMoveDetailList" parameterType="WmsMoveDetail" resultMap="WmsMoveDetailResult">
        <include refid="selectWmsMoveDetailVo"/>
        <where>
            <if test="moveId != null "> and move_id = #{moveId}</if>
            <if test="materialBarcode != null  and materialBarcode != ''"> and material_barcode = #{materialBarcode}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="planAmount != null "> and plan_amount = #{planAmount}</if>
            <if test="realAmount != null "> and real_amount = #{realAmount}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="executePerson != null  and executePerson != ''"> and execute_person = #{executePerson}</if>
            <if test="machineName != null  and machineName != ''"> and machine_name like concat('%', #{machineName}, '%')</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
            <if test="moveExecuteStatus != null  and moveExecuteStatus != ''"> and exists (select 1 from wms_move wm where wm.move_id=wmd.move_id and wm.execute_status = #{moveExecuteStatusStr}</if>
            <if test="moveExecuteStatusStr != null  and moveExecuteStatusStr != ''"> and exists (select 1 from wms_move wm where wm.move_id=wmd.move_id and wm.execute_status in (${moveExecuteStatusStr}))</if>
        </where>
    </select>

    <select id="selectWmsMoveDetailByMoveDetailId" parameterType="Long" resultMap="WmsMoveDetailResult">
        <include refid="selectWmsMoveDetailVo"/>
        where move_detail_id = #{moveDetailId}
    </select>

    <insert id="insertWmsMoveDetail" parameterType="WmsMoveDetail" useGeneratedKeys="true" keyProperty="moveDetailId">
        insert into wms_move_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="moveId != null">move_id,</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode,</if>
            <if test="instockBatch != null">instock_batch,</if>
            <if test="materialId != null">material_id,</if>
            <if test="planAmount != null">plan_amount,</if>
            <if test="realAmount != null">real_amount,</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status,</if>
            <if test="executePerson != null">execute_person,</if>
            <if test="executeTime != null">execute_time,</if>
            <if test="executeEndTime != null">execute_end_time,</if>
            <if test="machineName != null">machine_name,</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="activeFlag != null">active_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="moveId != null">#{moveId},</if>
            <if test="materialBarcode != null and materialBarcode != ''">#{materialBarcode},</if>
            <if test="instockBatch != null">#{instockBatch},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="planAmount != null">#{planAmount},</if>
            <if test="realAmount != null">#{realAmount},</if>
            <if test="executeStatus != null and executeStatus != ''">#{executeStatus},</if>
            <if test="executePerson != null">#{executePerson},</if>
            <if test="executeTime != null">#{executeTime},</if>
            <if test="executeEndTime != null">#{executeEndTime},</if>
            <if test="machineName != null">#{machineName},</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="activeFlag != null">#{activeFlag},</if>
         </trim>
    </insert>

    <update id="updateWmsMoveDetail" parameterType="WmsMoveDetail">
        update wms_move_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="moveId != null">move_id = #{moveId},</if>
            <if test="materialBarcode != null and materialBarcode != ''">material_barcode = #{materialBarcode},</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="realAmount != null">real_amount = #{realAmount},</if>
            <if test="realInstockAmount != null">real_instock_amount = #{realInstockAmount},</if>
            <if test="executeStatus != null and executeStatus != ''">execute_status = #{executeStatus},</if>
            <if test="executePerson != null">execute_person = #{executePerson},</if>
            <if test="executeTime != null">execute_time = #{executeTime},</if>
            <if test="executeEndTime != null">execute_end_time = #{executeEndTime},</if>
            <if test="machineName != null">machine_name = #{machineName},</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="activeFlag != null">active_flag = #{activeFlag},</if>
        </trim>
        where move_detail_id = #{moveDetailId}
    </update>

    <delete id="deleteWmsMoveDetailByMoveDetailId" parameterType="Long">
        delete from wms_move_detail where move_detail_id = #{moveDetailId}
    </delete>

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






    <select id="selectWmsMoveDetailByMoveIdAndBarCode" resultMap="WmsMoveDetailResult">
        <include refid="selectWmsMoveDetailVo"/>
        where move_id = #{moveId} and material_barcode = #{materialBarcode}
    </select>

    <sql id="selectWmsMoveDetailJoinVo">
        select wmd.move_detail_id, wmd.move_id, wmd.material_barcode, wmd.instock_batch, wmd.material_id, wmd.plan_amount, wmd.real_amount,wmd.real_instock_amount,
               wmd.execute_status, wmd.execute_person, wmd.execute_time, wmd.machine_name, wmd.create_by, wmd.create_date,
               mbmi.material_name
        from wms_move_detail wmd left join mes_base_material_info mbmi on wmd.material_id = mbmi.material_id
    </sql>

    <select id="selectWmsMoveDetailJoinList" parameterType="WmsMoveDetail" resultMap="WmsMoveDetailResult">
        <include refid="selectWmsMoveDetailJoinVo"/>
        <where>
            <if test="moveId != null "> and move_id = #{moveId}</if>
            <if test="materialBarcode != null  and materialBarcode != ''"> and material_barcode = #{materialBarcode}</if>
            <if test="instockBatch != null  and instockBatch != ''"> and instock_batch = #{instockBatch}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="planAmount != null "> and plan_amount = #{planAmount}</if>
            <if test="realAmount != null "> and real_amount = #{realAmount}</if>
            <if test="executeStatus != null  and executeStatus != ''"> and execute_status = #{executeStatus}</if>
            <if test="executePerson != null  and executePerson != ''"> and execute_person = #{executePerson}</if>
            <if test="machineName != null  and machineName != ''"> and machine_name like concat('%', #{machineName}, '%')</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
            <if test="moveExecuteStatus != null  and moveExecuteStatus != ''"> and exists (select 1 from wms_move wm where wm.move_id=wmd.move_id and wm.execute_status = #{moveExecuteStatusStr}</if>
            <if test="moveExecuteStatusStr != null  and moveExecuteStatusStr != ''"> and exists (select 1 from wms_move wm where wm.move_id=wmd.move_id and wm.execute_status in (${moveExecuteStatusStr}))</if>
        </where>
    </select>



</mapper>
