<?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.WmsInventoryCheckMapper">
    
    <resultMap type="WmsInventoryCheck" id="WmsInventoryCheckResult">
        <result property="inventoryCheckId"    column="inventory_check_id"    />
        <result property="inventoryCheckCode"    column="inventory_check_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="checkStatus"    column="check_status"    />
        <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"    />
    </resultMap>

    <resultMap id="WmsInventoryCheckWmsInventoryCheckDetailResult" type="WmsInventoryCheck" extends="WmsInventoryCheckResult">
        <collection property="wmsInventoryCheckDetailList" notNullColumn="sub_inventory_check_detail_id" javaType="java.util.List" resultMap="WmsInventoryCheckDetailResult" />
    </resultMap>

    <resultMap type="WmsInventoryCheckDetail" id="WmsInventoryCheckDetailResult">
        <result property="inventoryCheckDetailId"    column="sub_inventory_check_detail_id"    />
        <result property="inventoryCheckId"    column="sub_inventory_check_id"    />
        <result property="materialId"    column="sub_material_id"    />
        <result property="locationCode"    column="sub_location_code"    />
        <result property="stockAmount"    column="sub_stock_amount"    />
        <result property="realAmount"    column="sub_real_amount"    />
        <result property="checkStatus"    column="sub_check_status"    />
        <result property="erpStatus"    column="sub_erp_status"    />
        <result property="remark"    column="sub_remark"    />
        <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"    />
    </resultMap>

    <sql id="selectWmsInventoryCheckVo">
        select inventory_check_id, inventory_check_code, warehouse_id, check_status, remark, create_by, create_date, update_by, update_date from wms_inventory_check
    </sql>

    <select id="selectWmsInventoryCheckList" parameterType="WmsInventoryCheck" resultMap="WmsInventoryCheckResult">
        <include refid="selectWmsInventoryCheckVo"/>
        <where>  
            <if test="inventoryCheckCode != null  and inventoryCheckCode != ''"> and inventory_check_code = #{inventoryCheckCode}</if>
            <if test="warehouseId != null "> and warehouse_id = #{warehouseId}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and check_status = #{checkStatus}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
        </where>
    </select>
    
    <select id="selectWmsInventoryCheckByInventoryCheckId" parameterType="Long" resultMap="WmsInventoryCheckWmsInventoryCheckDetailResult">
        select a.inventory_check_id, a.inventory_check_code, a.warehouse_id, a.check_status, a.remark, a.create_by, a.create_date, a.update_by, a.update_date,
 b.inventory_check_detail_id as sub_inventory_check_detail_id, b.inventory_check_id as sub_inventory_check_id, b.material_id as sub_material_id, b.location_code as sub_location_code, b.stock_amount as sub_stock_amount, b.real_amount as sub_real_amount, b.check_status as sub_check_status, b.erp_status as sub_erp_status, b.remark as sub_remark, 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
        from wms_inventory_check a
        left join wms_inventory_check_detail b on b.inventory_check_id = a.inventory_check_id
        where a.inventory_check_id = #{inventoryCheckId}
    </select>
        
    <insert id="insertWmsInventoryCheck" parameterType="WmsInventoryCheck" useGeneratedKeys="true" keyProperty="inventoryCheckId">
        insert into wms_inventory_check
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inventoryCheckCode != null and inventoryCheckCode != ''">inventory_check_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="checkStatus != null and checkStatus != ''">check_status,</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="inventoryCheckCode != null and inventoryCheckCode != ''">#{inventoryCheckCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="checkStatus != null and checkStatus != ''">#{checkStatus},</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="updateWmsInventoryCheck" parameterType="WmsInventoryCheck">
        update wms_inventory_check
        <trim prefix="SET" suffixOverrides=",">
            <if test="inventoryCheckCode != null and inventoryCheckCode != ''">inventory_check_code = #{inventoryCheckCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="checkStatus != null and checkStatus != ''">check_status = #{checkStatus},</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_id = #{inventoryCheckId}
    </update>

    <delete id="deleteWmsInventoryCheckByInventoryCheckId" parameterType="Long">
        delete from wms_inventory_check where inventory_check_id = #{inventoryCheckId}
    </delete>

    <delete id="deleteWmsInventoryCheckByInventoryCheckIds" parameterType="String">
        delete from wms_inventory_check where inventory_check_id in 
        <foreach item="inventoryCheckId" collection="array" open="(" separator="," close=")">
            #{inventoryCheckId}
        </foreach>
    </delete>
    
    <delete id="deleteWmsInventoryCheckDetailByInventoryCheckIds" parameterType="String">
        delete from wms_inventory_check_detail where inventory_check_id in 
        <foreach item="inventoryCheckId" collection="array" open="(" separator="," close=")">
            #{inventoryCheckId}
        </foreach>
    </delete>

    <delete id="deleteWmsInventoryCheckDetailByInventoryCheckId" parameterType="Long">
        delete from wms_inventory_check_detail where inventory_check_id = #{inventoryCheckId}
    </delete>

    <insert id="batchWmsInventoryCheckDetail">
        insert into wms_inventory_check_detail( inventory_check_detail_id, inventory_check_id, material_id, location_code, stock_amount, real_amount, check_status, erp_status, remark, create_by, create_date, update_by, update_date) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.inventoryCheckDetailId}, #{item.inventoryCheckId}, #{item.materialId}, #{item.locationCode}, #{item.stockAmount}, #{item.realAmount}, #{item.checkStatus}, #{item.erpStatus}, #{item.remark}, #{item.createBy}, #{item.createDate}, #{item.updateBy}, #{item.updateDate})
        </foreach>
    </insert>
</mapper>