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

    <resultMap type="OutboundOrderLog" id="OutboundOrderLogResult">
        <result property="id"                column="id" />
        <result property="orderId"           column="order_id" />
        <result property="inventoryId"       column="inventory_id" />
        <result property="materialId"        column="material_id" />
        <result property="warehouseId"       column="warehouse_id" />
        <result property="batchCode"         column="batch_code" />
        <result property="actualQuantity"    column="actual_quantity" />
        <result property="itemStatus"        column="item_status" />
        <result property="isUsed"            column="is_used" />
    </resultMap>

    <!-- 补充inventory_id到查询字段中 -->
    <sql id="selectOutboundOrderLogVo">
        select id, order_id, inventory_id, material_id, warehouse_id, batch_code, actual_quantity, item_status, is_used
        from outbound_order_log
    </sql>

    <select id="selectOutboundOrderLogList" parameterType="OutboundOrderLog" resultMap="OutboundOrderLogResult">
        <include refid="selectOutboundOrderLogVo"/>
        where is_used=1
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="inventoryId != null  and inventoryId != ''"> and inventory_id = #{inventoryId}</if>
            <if test="materialId != null  and materialId != ''"> and material_id = #{materialId}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and warehouse_id = #{warehouseId}</if>
            <if test="batchCode != null  and batchCode != ''"> and batch_code = #{batchCode}</if>
            <if test="actualQuantity != null "> and actual_quantity = #{actualQuantity}</if>
            <if test="itemStatus != null "> and item_status = #{itemStatus}</if>
    </select>

    <update id="deleteLog" parameterType="OutboundOrderLog">
        update outbound_order_log set is_used=0
        <where>
            <if test="materialId != null  and materialId != ''"> and material_id = #{materialId}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and warehouse_id = #{warehouseId}</if>
            <if test="batchCode != null  and batchCode != ''"> and batch_code = #{batchCode}</if>
            <if test="itemStatus != null "> and item_status = #{itemStatus}</if>
            <if test="inventoryId != null  and inventoryId != ''"> and inventory_id = #{inventoryId}</if>
        </where>
    </update>

    <select id="selectLockedQuantityByInventory" parameterType="OutboundOrderLog" resultType="java.lang.Long">
        select ifnull(sum(actual_quantity), 0)
        from outbound_order_log
        where item_status=1 and is_used=1
            <if test="inventoryId != null  and inventoryId != ''"> and inventory_id = #{inventoryId}</if>
    </select>

    <select id="selectOutboundOrderLogById" parameterType="String" resultMap="OutboundOrderLogResult">
        <include refid="selectOutboundOrderLogVo"/>
        where id = #{id} and is_used=1
    </select>

    <!-- 插入语句补充inventory_id -->
    <insert id="insertOutboundOrderLog" parameterType="OutboundOrderLog">
        insert into outbound_order_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="inventoryId != null">inventory_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="batchCode != null">batch_code,</if>
            <if test="actualQuantity != null">actual_quantity,</if>
            <if test="itemStatus != null">item_status,</if>
            <if test="isUsed != null">is_used,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="inventoryId != null">#{inventoryId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="batchCode != null">#{batchCode},</if>
            <if test="actualQuantity != null">#{actualQuantity},</if>
            <if test="itemStatus != null">#{itemStatus},</if>
            <if test="isUsed != null">#{isUsed},</if>
        </trim>
    </insert>

    <!-- 更新语句补充inventory_id -->
    <update id="updateOutboundOrderLog" parameterType="OutboundOrderLog">
        update outbound_order_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="inventoryId != null">inventory_id = #{inventoryId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="actualQuantity != null">actual_quantity = #{actualQuantity},</if>
            <if test="itemStatus != null">item_status = #{itemStatus},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteOutboundOrderLogById" parameterType="String">
        update outbound_order_log set is_used=0 where id = #{id}
    </update>

    <update id="deleteOutboundOrderLogByOrdersId" parameterType="String">
        update outbound_order_log set is_used=0 where order_id = #{id}
    </update>

    <update id="deleteOutboundOrderLogByOrdersIds" parameterType="String">
        update outbound_order_log set is_used=0 where order_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <!-- 批量插入补充inventory_id -->
    <insert id="batchOutboundOrderLog">
        <!-- 增加非空判断，避免空列表导致SQL语法错误 -->
        <if test="list != null and list.size() > 0">
            insert into outbound_order_log(
            id,
            order_id,
            inventory_id,  <!-- 新增 -->
            material_id,
            warehouse_id,
            batch_code,        <!-- 对应实体类batchCode -->
            actual_quantity,
            item_status,
            is_used
            ) values
            <foreach item="item" index="index" collection="list" separator=",">
                (
                #{item.id},
                #{item.orderId},
                #{item.inventoryId},
                #{item.materialId},
                #{item.warehouseId},
                #{item.batchCode},
                #{item.actualQuantity},
                #{item.itemStatus},
                #{item.isUsed}
                )
            </foreach>
        </if>
    </insert>
</mapper>