<?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="cn.iocoder.yudao.module.system.dal.mysql.biz.MaterialInBalanceMapper">

    <!--
      入库批次选择列表查询
      已从 material_in_balance 表切换为直接使用 material_inbound_order_item 承担批次余额职责
    -->
    <select id="selectInBalanceList"
            resultType="cn.iocoder.yudao.module.system.controller.admin.biz.vo.materialio.MaterialInBalanceRespVO">
        SELECT
            ioi.id,
            ioi.inbound_order_id    AS inOrderId,
            io.order_no             AS inOrderNo,
            ioi.store_id            AS storeId,
            sm.store_name           AS storeName,
            ioi.material_id         AS materialId,
            mi.material_number      AS materialNumber,
            mi.material_name        AS materialName,
            mi.material_unit        AS materialUnit,
            ioi.in_qty              AS inQty,
            ioi.available_qty       AS availableQty,
            ioi.in_unit_price       AS inUnitPrice,
            ioi.sale_unit_price     AS saleUnitPrice
        FROM material_inbound_order_item ioi
                 INNER JOIN material_inbound_order io ON io.id = ioi.inbound_order_id
                 LEFT JOIN store_manage sm ON sm.id = ioi.store_id
                 LEFT JOIN material_info mi ON mi.id = ioi.material_id
        WHERE 1 = 1
        <if test="reqVO.tenantId != null and reqVO.tenantId != ''">
            AND io.tenant_id = #{reqVO.tenantId}
        </if>
        <if test="reqVO.onlyAvailable != null and reqVO.onlyAvailable">
            AND ioi.available_qty &gt; 0
        </if>
        <if test="reqVO.keyword != null and reqVO.keyword != ''">
            AND (
                    mi.material_number LIKE CONCAT('%', #{reqVO.keyword}, '%')
                    OR mi.material_name LIKE CONCAT('%', #{reqVO.keyword}, '%')
                )
        </if>
        ORDER BY ioi.update_time DESC
    </select>

</mapper>
