<?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.ruoyi.inventory.mapper.TbInventoryDetailMapper">
    <resultMap type="TbInventoryDetail" id="TbInventoryDetailResult">
        <result property="id"    column="id"    />
        <result property="orderNumber"    column="order_number"    />
        <result property="inventoryMetadata"    column="inventory_metadata"    />
        <result property="inventoryStatus"    column="inventory_status"    />
        <result property="inventoryResult"    column="inventory_result"    />
        <result property="inventoryQuantity"    column="inventory_quantity"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectTbInventoryDetailVo">
        select id, order_number, inventory_metadata, inventory_status, inventory_result, inventory_quantity, remark, create_time, create_by from tb_inventory_detail
    </sql>

    <select id="selectTbInventoryDetailList" parameterType="TbInventoryDetail" resultMap="TbInventoryDetailResult">
        <include refid="selectTbInventoryDetailVo"/>
        <where>  
            <if test="orderNumber != null  and orderNumber != ''"> and order_number = #{orderNumber}</if>
            <if test="inventoryStatus != null "> and inventory_status = #{inventoryStatus}</if>
        </where>
    </select>
    
    <select id="selectTbInventoryDetailById" parameterType="Long" resultMap="TbInventoryDetailResult">
        <include refid="selectTbInventoryDetailVo"/>
        where id = #{id}
    </select>

    <select id="selectTbInventoryDetailByOrderNumber" parameterType="String" resultMap="TbInventoryDetailResult">
        <include refid="selectTbInventoryDetailVo"/>
        where order_number = #{orderNumber}
    </select>

    <select id="countUncounted" parameterType="String" resultType="Integer">
        SELECT COUNT(0) FROM tb_inventory_detail WHERE order_number = #{orderNumber} and inventory_status = 0;
    </select>

    <select id="countInventoryShortages" parameterType="String" resultType="Integer">
        SELECT COUNT(0) FROM tb_inventory_detail WHERE order_number = #{orderNumber} and inventory_result = -1 and inventory_status &lt;&gt; -1;
    </select>

    <select id="countInventoryProfit" parameterType="String" resultType="Integer">
        SELECT COUNT(0) FROM tb_inventory_detail WHERE order_number = #{orderNumber} and inventory_result = 1 and inventory_status &lt;&gt; -1;
    </select>
        
    <insert id="insertTbInventoryDetail" parameterType="TbInventoryDetail" useGeneratedKeys="true" keyProperty="id">
        insert into tb_inventory_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">order_number,</if>
            <if test="inventoryMetadata != null and inventoryMetadata != ''">inventory_metadata,</if>
            <if test="inventoryStatus != null">inventory_status,</if>
            <if test="inventoryResult != null">inventory_result,</if>
            <if test="inventoryQuantity != null">inventory_quantity,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">#{orderNumber},</if>
            <if test="inventoryMetadata != null and inventoryMetadata != ''">#{inventoryMetadata},</if>
            <if test="inventoryStatus != null">#{inventoryStatus},</if>
            <if test="inventoryResult != null">#{inventoryResult},</if>
            <if test="inventoryQuantity != null">#{inventoryQuantity},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTbInventoryDetail" parameterType="TbInventoryDetail">
        update tb_inventory_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNumber != null and orderNumber != ''">order_number = #{orderNumber},</if>
            <if test="inventoryMetadata != null and inventoryMetadata != ''">inventory_metadata = #{inventoryMetadata},</if>
            <if test="inventoryStatus != null">inventory_status = #{inventoryStatus},</if>
            <if test="inventoryResult != null">inventory_result = #{inventoryResult},</if>
            <if test="inventoryQuantity != null">inventory_quantity = #{inventoryQuantity},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTbInventoryDetailById" parameterType="Long">
        delete from tb_inventory_detail where id = #{id}
    </delete>

    <delete id="deleteTbInventoryDetailByOrderNumber" parameterType="String">
        delete from tb_inventory_detail where order_number = #{orderNumber}
    </delete>

    <delete id="deleteTbInventoryDetailByOrderNumbers" parameterType="String">
        delete from tb_inventory_detail where order_number in
        <foreach item="orderNumber" collection="orderNumbers" open="(" separator="," close=")">
            #{orderNumber}
        </foreach>
    </delete>

    <delete id="deleteTbInventoryDetailByIds" parameterType="String">
        delete from tb_inventory_detail where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>