package com.ruoyi.business.utils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ruoyi.business.constant.InventoryRecordConstant; import com.ruoyi.business.entity.*; import com.ruoyi.business.mapper.*; import com.ruoyi.common.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.List; @Component public class InventoryUtils { @Autowired private InventorySummaryMapper inventorySummaryMapper; @Autowired private InputInventoryRecordMapper inputInventoryRecordMapper; @Autowired private OutputInventoryRecordMapper outputInventoryRecordMapper; @Autowired private OfficialInventoryMapper officialInventoryMapper; @Autowired private PendingInventoryMapper pendingInventoryMapper; /** * 更新库存 */ public void updateInventoryByIds(List ids, String type) { if (StringUtils.isEmpty(type)) { throw new RuntimeException("请指定库存类型"); } for (Long id : ids) { // 获取库存节点 InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper() .eq(InventorySummary::getInventoryId, id) .eq(InventorySummary::getInventoryType, type)); Long inputEndRecordId = inventorySummary != null ? inventorySummary.getInputEndRecordId() : 0L; Long outputEndRecordId = inventorySummary != null ? inventorySummary.getOutputEndRecordId() : 0L; List inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper() .eq(InputInventoryRecord::getInventoryId, id) .eq(InputInventoryRecord::getInventoryType, type) .gt(InputInventoryRecord::getId, inputEndRecordId)); List outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper() .eq(OutputInventoryRecord::getInventoryId, id) .eq(OutputInventoryRecord::getInventoryType, type) .gt(OutputInventoryRecord::getId, outputEndRecordId)); // 获取库存数据 BigDecimal inputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); BigDecimal outputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); // 计算这次库存数据 BigDecimal quantity = inventorySummary != null ? inventorySummary.getInventoryQuantity().add(inputQuantity.subtract(outputQuantity)) : inputQuantity.subtract(outputQuantity); if (inventorySummary != null) { if (type.equals(InventoryRecordConstant.OFFICIAL_INVENTORY)) { OfficialInventory update = new OfficialInventory(); update.setInventoryQuantity(quantity); update.setId(id); officialInventoryMapper.updateById(update); } else { PendingInventory update = new PendingInventory(); update.setInventoryQuantity(quantity); update.setId(id); pendingInventoryMapper.updateById(update); } } } } }