select tab.*, case when total_quantity < min_stock_level then '2' when total_quantity > max_stock_level then '1' else '3' end alterType from (
<includerefid="selectInventoryCount"/>
<where>
and i.quantity > 0 and i.inventory_status = 1 and i.is_used = 1
<iftest="inventoryType != null "> and i.inventory_type = #{inventoryType}</if>
<iftest="materialId != null and materialId != ''"> and (i.material_id like concat('%', #{materialId}, '%') or m.material_code like concat('%', #{materialId}, '%'))</if>
<iftest="batchId != null and batchId != ''"> and i.batch_id = #{batchId}</if>
<iftest="warehousesCode != null and warehousesCode != ''"> and i.warehouses_code = #{warehousesCode}</if>
<iftest="materialId != null and materialId != ''"> and m.material_code like concat('%', #{materialId}, '%')</if>
<iftest="batchId != null and batchId != ''"> and i.batch_id like concat('%', #{batchId}, '%')</if>
<iftest="warehousesId != null and warehousesId != ''"> and w.warehouses_id = #{warehousesId}</if>
<iftest="locationId != null and locationId != ''"> and i.location_id = #{locationId}</if>
<iftest="ownerId != null and ownerId != ''"> and i.owner_id = #{ownerId}</if>
<iftest="inventoryStatus != null "> and i.inventory_status = #{inventoryStatus}</if>
<iftest="isUsed != null "> and i.is_used = #{isUsed}</if>
<!-- 预警类型过滤(需要根据业务逻辑判断,这里暂时不实现) -->
</where>
group by i.material_id, i.warehouses_code, i.location_id, i.owner_id, i.inventory_type, i.inventory_status
order by i.material_id, i.warehouses_code, i.location_id
group by i.material_id, m.material_name, m.sap_no,
m.ts_code,
m.hazard_id,
m.specification,
m.material_unit,
m.unit_weight,
m.package_weight,
m.total_weight,
m.volume,
m.shelf_life_days,
m.storage_temperature,
m.special_requirements ) tab
<where>
<iftest="alertType == '0'.toString()"> and total_quantity < min_stock_level or total_quantity > max_stock_level</if>
<iftest="alertType == '1'.toString()"> and total_quantity > max_stock_level</if>
<iftest="alertType == '2'.toString()"> and total_quantity < min_stock_level</if>