package com.ruoyi.business.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.business.dto.OfficialInventoryDto; import com.ruoyi.business.dto.PendingInventoryDto; import com.ruoyi.business.entity.*; import com.ruoyi.business.mapper.*; import com.ruoyi.business.service.InventorySummaryService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.List; import static com.ruoyi.business.constant.InventoryRecordConstant.OFFICIAL_INVENTORY; import static com.ruoyi.business.constant.InventoryRecordConstant.PENDING_INVENTORY; /** *

* 库存汇总表 服务实现类 *

* * @author chenhj * @since 2025-06-14 */ @Service @RequiredArgsConstructor public class InventorySummaryServiceImpl extends ServiceImpl implements InventorySummaryService { private final InventorySummaryMapper inventorySummaryMapper; private final InputInventoryRecordMapper inputInventoryRecordMapper; private final OutputInventoryRecordMapper outputInventoryRecordMapper; private final PendingInventoryMapper pendingInventoryMapper; private final OfficialInventoryMapper officialInventoryMapper; @Override public int updateInventory(PendingInventoryDto pendingInventoryDto, OfficialInventoryDto officialInventoryDto) { if ((pendingInventoryDto != null && officialInventoryDto != null) || (pendingInventoryDto == null && officialInventoryDto == null)) { throw new RuntimeException("库存记录异常"); } String inventoryType = pendingInventoryDto != null ? PENDING_INVENTORY : OFFICIAL_INVENTORY; Long inventoryId = pendingInventoryDto != null ? pendingInventoryDto.getId() : officialInventoryDto.getId(); Long inputEndRecordId = 0L; Long outputEndRecordId = 0L; InventorySummary inventorySummary = inventorySummaryMapper.selectOne(new LambdaQueryWrapper() .eq(InventorySummary::getInventoryId, inventoryId) .eq(InventorySummary::getInventoryType, inventoryType)); if (inventorySummary != null) { inputEndRecordId = inventorySummary.getInputEndRecordId(); outputEndRecordId = inventorySummary.getOutputEndRecordId(); } // 查询节点以后所有入库记录 List inputInventoryRecords = inputInventoryRecordMapper.selectList(new LambdaQueryWrapper() .eq(InputInventoryRecord::getInventoryId, inventoryId) .eq(InputInventoryRecord::getInventoryType, inventoryType) .gt(InputInventoryRecord::getId, inputEndRecordId)); // 查询节点以后所有出库记录 List outputInventoryRecords = outputInventoryRecordMapper.selectList(new LambdaQueryWrapper() .eq(OutputInventoryRecord::getInventoryId, inventoryId) .eq(OutputInventoryRecord::getInventoryType, inventoryType) .gt(OutputInventoryRecord::getId, outputEndRecordId)); // 如果入库数量大于出库数量,则库存数量为入库数量减去出库数量,反之则为0 BigDecimal quantity = BigDecimal.ZERO; BigDecimal totalInputQuantity = inputInventoryRecords.stream().map(InputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); BigDecimal totalOutputQuantity = outputInventoryRecords.stream().map(OutputInventoryRecord::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add); if (totalInputQuantity.compareTo(totalOutputQuantity) > 0) { quantity = totalInputQuantity.subtract(totalOutputQuantity); } if (inventoryType.equals(PENDING_INVENTORY)) { // 变更待入库值 PendingInventory pendingInventory = pendingInventoryMapper.getPendingInventoryForUpdateById(pendingInventoryDto.getId()); if (pendingInventory == null) { throw new RuntimeException("库存记录不存在"); } pendingInventory.setInventoryQuantity(quantity); return pendingInventoryMapper.updateById(pendingInventory); } else { OfficialInventory officialInventory = officialInventoryMapper.getOfficialInventoryForUpdateById(officialInventoryDto.getId()); if (officialInventory == null) { throw new RuntimeException("库存记录不存在"); } officialInventory.setInventoryQuantity(quantity); return officialInventoryMapper.updateById(officialInventory); } } }