| | |
| | | import com.ruoyi.business.entity.PendingInventory; |
| | | import com.ruoyi.business.mapper.OfficialInventoryMapper; |
| | | import com.ruoyi.business.mapper.PendingInventoryMapper; |
| | | import com.ruoyi.business.service.InputInventoryRecordService; |
| | | import com.ruoyi.business.service.InventorySummaryService; |
| | | import com.ruoyi.business.service.PendingInventoryService; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | private final CoalFieldMapper coalFieldMapper; |
| | | |
| | | private final InputInventoryRecordService inputInventoryRecordService; |
| | | |
| | | private final InventorySummaryService inventorySummaryService; |
| | | |
| | | @Override |
| | | public IPage<PendingInventory> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) { |
| | | public IPage<PendingInventoryDto> selectPendingInventoryList(Page page, PendingInventoryDto pendingInventoryDto) { |
| | | // 1. 构建主查询 |
| | | LambdaQueryWrapper<PendingInventory> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.orderByDesc(PendingInventory::getCreateTime); |
| | | return pendingInventoryMapper.selectPage(page, queryWrapper); |
| | | |
| | | // 2. 执行主表分页查询 |
| | | IPage<PendingInventory> pendingInventoryPage = pendingInventoryMapper.selectPage(page, queryWrapper); |
| | | |
| | | // 3. 无数据快速返回 |
| | | if (CollectionUtils.isEmpty(pendingInventoryPage.getRecords())) { |
| | | return new Page<>(page.getCurrent(), page.getSize(), pendingInventoryPage.getTotal()); |
| | | } |
| | | |
| | | // 4. 提取所有待处理库存ID |
| | | List<Long> pendingIds = pendingInventoryPage.getRecords().stream() |
| | | .map(PendingInventory::getId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 5. 批量查询关联的正式库存信息 |
| | | Map<Long, Long> pendingToOfficialMap = getOfficialInventoryMap(pendingIds); |
| | | |
| | | // 6. 使用MyBatis-Plus的convert方法转换DTO |
| | | return pendingInventoryPage.convert(record -> { |
| | | PendingInventoryDto dto = new PendingInventoryDto(); |
| | | BeanUtils.copyProperties(record, dto); |
| | | |
| | | // 从预加载的Map中获取officialId |
| | | dto.setOfficialId(pendingToOfficialMap.getOrDefault(record.getId(), null)); |
| | | return dto; |
| | | }); |
| | | } |
| | | |
| | | // 批量获取待处理库存与正式库存的映射关系 |
| | | private Map<Long, Long> getOfficialInventoryMap(List<Long> pendingIds) { |
| | | if (CollectionUtils.isEmpty(pendingIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | // 查询关联的正式库存数据 |
| | | LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.select(OfficialInventory::getId, OfficialInventory::getPendingId) |
| | | .in(OfficialInventory::getPendingId, pendingIds); |
| | | |
| | | return officialInventoryMapper.selectList(wrapper) |
| | | .stream() |
| | | .collect(Collectors.toMap( |
| | | OfficialInventory::getPendingId, |
| | | OfficialInventory::getId, |
| | | (existing, replacement) -> existing // 如果有重复,保留第一个 |
| | | )); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } else { |
| | | pendingInventoryMapper.deleteById(pendingInventoryDto.getPId()); |
| | | } |
| | | officialInventoryMapper.delete(new LambdaQueryWrapper<OfficialInventory>().eq(OfficialInventory::getPendingId, pendingInventoryDto.getPId())); |
| | | OfficialInventory officialInventory = new OfficialInventory(); |
| | | BeanUtils.copyProperties(pendingInventory, officialInventory); |
| | | officialInventory.setId(null); |
| | | officialInventory.setPendingId(pendingInventoryDto.getPId()); |
| | | officialInventory.setInventoryQuantity(quantity); |
| | | officialInventoryMapper.insert(officialInventory); |
| | | //正式库 |
| | | if (pendingInventoryDto.getOfficialId() == null) { |
| | | OfficialInventory officialInventory = new OfficialInventory(); |
| | | BeanUtils.copyProperties(pendingInventory, officialInventory); |
| | | officialInventory.setId(null); |
| | | officialInventory.setPendingId(pendingInventoryDto.getPId()); |
| | | officialInventory.setInventoryQuantity(quantity); |
| | | officialInventoryMapper.insert(officialInventory); |
| | | } else { |
| | | OfficialInventory officialInventory = officialInventoryMapper.selectById(pendingInventoryDto.getOfficialId()); |
| | | officialInventory.setInventoryQuantity(quantity.add(officialInventory.getInventoryQuantity())); |
| | | officialInventoryMapper.updateById(officialInventory); |
| | | } |
| | | } |
| | | return i; |
| | | } |