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

    <resultMap type="WmsInventoryCheckDetail" id="WmsInventoryCheckDetailResult">
        <result property="inventoryCheckDetailId"    column="inventory_check_detail_id"    />
        <result property="inventoryCheckId"    column="inventory_check_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="locationCode"    column="location_code"    />
        <result property="materialBatch"    column="material_batch"    />
        <result property="stockType"    column="stock_type"    />
        <result property="stockId"    column="stock_id"    />
        <result property="stockAmount"    column="stock_amount"    />
        <result property="realAmount"    column="real_amount"    />
        <result property="checkStatus"    column="check_status"    />
        <result property="inventoryTime"    column="inventory_time"    />
        <result property="erpStatus"    column="erp_status"    />
        <result property="erpAmount"    column="erp_amount"    />
        <result property="remark"    column="remark"    />
        <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="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
    </resultMap>

    <sql id="selectWmsInventoryCheckDetailVo">
        select inventory_check_detail_id, inventory_check_id, material_id, location_code, material_batch, stock_type, stock_id, stock_amount, real_amount, check_status, inventory_time, erp_status, erp_amount, remark, create_by, create_date, update_by, update_date from wms_inventory_check_detail
    </sql>

    <select id="selectWmsInventoryCheckDetailList" parameterType="WmsInventoryCheckDetail" resultMap="WmsInventoryCheckDetailResult">
        <include refid="selectWmsInventoryCheckDetailVo"/>
        <where>
            <if test="inventoryCheckId != null "> and inventory_check_id = #{inventoryCheckId}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="locationCode != null  and locationCode != ''"> and location_code = #{locationCode}</if>
            <if test="materialBatch != null  and materialBatch != ''"> and material_batch = #{materialBatch}</if>
            <if test="stockType != null  and stockType != ''"> and stock_type = #{stockType}</if>
            <if test="stockAmount != null "> and stock_amount = #{stockAmount}</if>
            <if test="realAmount != null "> and real_amount = #{realAmount}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and check_status = #{checkStatus}</if>
            <if test="inventoryTime != null "> and inventory_time = #{inventoryTime}</if>
            <if test="erpStatus != null  and erpStatus != ''"> and erp_status = #{erpStatus}</if>
            <if test="erpAmount != null "> and erp_amount = #{erpAmount}</if>
            <if test="checkStatusStr != null  and checkStatusStr!= ''"> and check_status in (${checkStatusStr})</if>
        </where>
    </select>

    <select id="selectWmsInventoryCheckDetailByInventoryCheckDetailId" parameterType="Long" resultMap="WmsInventoryCheckDetailResult">
        <include refid="selectWmsInventoryCheckDetailVo"/>
        where inventory_check_detail_id = #{inventoryCheckDetailId}
    </select>

    <insert id="insertWmsInventoryCheckDetail" parameterType="WmsInventoryCheckDetail" useGeneratedKeys="true" keyProperty="inventoryCheckDetailId">
        insert into wms_inventory_check_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inventoryCheckId != null">inventory_check_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="locationCode != null and locationCode != ''">location_code,</if>
            <if test="materialBatch != null and materialBatch != ''">material_batch,</if>
            <if test="stockType != null">stock_type,</if>
            <if test="stockId != null">stock_id,</if>
            <if test="stockAmount != null">stock_amount,</if>
            <if test="realAmount != null">real_amount,</if>
            <if test="checkStatus != null and checkStatus != ''">check_status,</if>
            <if test="inventoryTime != null">inventory_time,</if>
            <if test="erpStatus != null">erp_status,</if>
            <if test="erpAmount != null">erp_amount,</if>
            <if test="remark != null">remark,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inventoryCheckId != null">#{inventoryCheckId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="locationCode != null and locationCode != ''">#{locationCode},</if>
            <if test="materialBatch != null and materialBatch != ''">#{materialBatch},</if>
            <if test="stockType != null">#{stockType},</if>
            <if test="stockId != null">#{stockId},</if>
            <if test="stockAmount != null">#{stockAmount},</if>
            <if test="realAmount != null">#{realAmount},</if>
            <if test="checkStatus != null and checkStatus != ''">#{checkStatus},</if>
            <if test="inventoryTime != null">#{inventoryTime},</if>
            <if test="erpStatus != null">#{erpStatus},</if>
            <if test="erpAmount != null">#{erpAmount},</if>
            <if test="remark != null">#{remark},</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>
        </trim>
    </insert>

    <update id="updateWmsInventoryCheckDetail" parameterType="WmsInventoryCheckDetail">
        update wms_inventory_check_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="inventoryCheckId != null">inventory_check_id = #{inventoryCheckId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="locationCode != null and locationCode != ''">location_code = #{locationCode},</if>
            <if test="materialBatch != null and materialBatch != ''">material_batch = #{materialBatch},</if>
            <if test="stockType != null">stock_type = #{stockType},</if>
            <if test="stockId != null">stock_id = #{stockId},</if>
            <if test="stockAmount != null">stock_amount = #{stockAmount},</if>
            <if test="realAmount != null">real_amount = #{realAmount},</if>
            <if test="checkStatus != null and checkStatus != ''">check_status = #{checkStatus},</if>
            <if test="inventoryTime != null">inventory_time = #{inventoryTime},</if>
            <if test="erpStatus != null">erp_status = #{erpStatus},</if>
            <if test="erpAmount != null">erp_amount = #{erpAmount},</if>
            <if test="remark != null">remark = #{remark},</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>
        </trim>
        where inventory_check_detail_id = #{inventoryCheckDetailId}
    </update>

    <delete id="deleteWmsInventoryCheckDetailByInventoryCheckDetailId" parameterType="Long">
        delete from wms_inventory_check_detail where inventory_check_detail_id = #{inventoryCheckDetailId}
    </delete>

    <delete id="deleteWmsInventoryCheckDetailByInventoryCheckDetailIds" parameterType="String">
        delete from wms_inventory_check_detail where inventory_check_detail_id in
        <foreach item="inventoryCheckDetailId" collection="array" open="(" separator="," close=")">
            #{inventoryCheckDetailId}
        </foreach>
    </delete>





    <select id="selectCountOfCheckDetailByCheckIdAndLocation">
        select count(1) from wms_inventory_check_detail
        where inventory_check_id = #{inventoryCheckId} and location_code = #{locationCode}
    </select>




    <sql id="selectWmsInventoryCheckDetailJoinVo">
        select wicd.inventory_check_detail_id, wicd.inventory_check_id, wicd.material_id, wicd.material_batch,
               wicd.stock_type, wicd.stock_id, wicd.stock_amount, wicd.real_amount, wicd.check_status, wicd.inventory_time,
               mbmi.material_code,mbmi.material_name
        from wms_inventory_check_detail wicd left join mes_base_material_info mbmi on wicd.material_id = mbmi.material_id
    </sql>

    <select id="selectWmsInventoryCheckDetailJoinList" parameterType="WmsInventoryCheckDetail" resultMap="WmsInventoryCheckDetailResult">
        <include refid="selectWmsInventoryCheckDetailJoinVo"/>
        <where>
            <if test="inventoryCheckId != null "> and wicd.inventory_check_id = #{inventoryCheckId}</if>
            <if test="materialId != null "> and wicd.material_id = #{materialId}</if>
            <if test="locationCode != null  and locationCode != ''"> and wicd.location_code = #{locationCode}</if>
            <if test="materialBatch != null  and materialBatch != ''"> and wicd.material_batch = #{materialBatch}</if>
            <if test="stockType != null  and stockType != ''"> and wicd.stock_type = #{stockType}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and wicd.check_status = #{checkStatus}</if>
            <if test="checkStatusStr != null  and checkStatusStr!= ''"> and wicd.check_status in (${checkStatusStr})</if>
        </where>
    </select>

</mapper>
