case i.inventory_type when 1 then '普通' when 2 then '退库' else '未知' end as inventory_type_name,
i.warehouses_code,
w.warehouses_name as warehouse_name,
i.location_id,
sl.location_name,
i.owner_id,
o.owner_name,
sum(i.quantity) as total_quantity,
sum(i.locked_quantity) as total_locked_quantity,
sum(i.quantity - ifnull(i.locked_quantity, 0)) as total_available_quantity,
sum(ifnull(i.total_weight, 0)) as total_weight,
sum(ifnull(i.total_volume, 0)) as total_volume,
case max(i.inventory_status) when 0 then '已出库' when 1 then '正常' else '未知' end as inventory_status_name
from inventory i
left join materials m on i.material_id = m.material_code
left join warehouses w on i.warehouses_code = w.warehouses_code
left join storage_locations sl on i.location_id = sl.location_code
left join owners o on i.owner_id = o.owner_code
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>