| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.entity.CoalInfo; |
| | | import com.ruoyi.basic.mapper.CoalInfoMapper; |
| | | import com.ruoyi.business.dto.ProductionMasterDto; |
| | | import com.ruoyi.business.entity.OfficialInventory; |
| | | import com.ruoyi.business.entity.Production; |
| | | import com.ruoyi.business.entity.ProductionInventory; |
| | | import com.ruoyi.business.entity.ProductionMaster; |
| | | import com.ruoyi.business.mapper.OfficialInventoryMapper; |
| | | import com.ruoyi.business.mapper.ProductionInventoryMapper; |
| | | import com.ruoyi.business.mapper.ProductionMapper; |
| | | import com.ruoyi.business.mapper.ProductionMasterMapper; |
| | | import com.ruoyi.business.entity.*; |
| | | import com.ruoyi.business.mapper.*; |
| | | import com.ruoyi.business.service.ProductionMasterService; |
| | | 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.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | private final ProductionMapper productionMapper; |
| | | |
| | | private final OfficialInventoryMapper officialInventoryMapper; |
| | | |
| | | private final CoalInfoMapper coalInfoMapper; |
| | | |
| | | private final PendingInventoryMapper pendingInventoryMapper; |
| | | |
| | | @Override |
| | | public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) { |
| | |
| | | BigDecimal totalEnergyConsumptionCost = BigDecimal.ZERO; |
| | | BigDecimal totalTotalCost = BigDecimal.ZERO; |
| | | BigDecimal totalEquipmentDepreciation = BigDecimal.ZERO; |
| | | int totalProductionQuantity = 0; |
| | | StringBuilder coalBuilder = new StringBuilder(); |
| | | BigDecimal totalProductionQuantity = BigDecimal.ZERO; |
| | | |
| | | for (Production production : productionMasterDto.getProductionList()) { |
| | | totalPurchasePrice = totalPurchasePrice.add(production.getPurchasePrice()); |
| | |
| | | totalEnergyConsumptionCost = totalEnergyConsumptionCost.add(production.getEnergyConsumptionCost()); |
| | | totalTotalCost = totalTotalCost.add(production.getTotalCost()); |
| | | totalEquipmentDepreciation = totalEquipmentDepreciation.add(production.getEquipmentDepreciation()); |
| | | totalProductionQuantity += production.getProductionQuantity(); |
| | | if (coalBuilder.length() > 0) { |
| | | coalBuilder.append(","); // 在元素之间添加逗号 |
| | | } |
| | | coalBuilder.append(production.getCoal()); |
| | | totalProductionQuantity = production.getProductionQuantity().add(totalProductionQuantity); |
| | | } |
| | | String coalStr = coalBuilder.toString(); // 直接获取拼接结果 |
| | | //煤种字段 |
| | | List<Long> coalIds = productionMasterDto.getProductionList().stream() |
| | | .map(Production::getCoalId) |
| | | .collect(Collectors.toList()); |
| | | |
| | | List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds)); |
| | | |
| | | // 2. 创建主表对象 |
| | | ProductionMaster productionMaster = new ProductionMaster(); |
| | |
| | | productionMaster.setEquipmentDepreciation(totalEquipmentDepreciation); |
| | | productionMaster.setEnergyConsumptionCost(totalEnergyConsumptionCost); |
| | | productionMaster.setLaborCost(totalLaborCost); |
| | | productionMaster.setCoal(coalStr); |
| | | productionMaster.setCoal(coalInfos.stream().map(CoalInfo::getCoal).collect(Collectors.joining(","))); |
| | | productionMaster.setCoalId(coalIds.stream().map(String::valueOf).collect(Collectors.joining(","))); |
| | | |
| | | Long masterId = productionMasterDto.getId(); |
| | | productionMaster.setId(masterId); |
| | |
| | | batchInsertProductions(masterId, productionMasterDto.getProductionList()); |
| | | batchInsertInventories(masterId, productionMasterDto.getProductionInventoryList()); |
| | | |
| | | //5. 插入到待入库 |
| | | for (Production production : productionMasterDto.getProductionList()) { |
| | | PendingInventory pendingInventory = new PendingInventory(); |
| | | pendingInventory.setCoalId(production.getCoalId()); |
| | | pendingInventory.setInventoryQuantity(production.getProductionQuantity()); |
| | | pendingInventory.setSupplierName("生产加工入库"); |
| | | pendingInventory.setTotalPriceIncludingTax(production.getTotalCost()); |
| | | pendingInventory.setPriceIncludingTax(production.getPurchasePrice()); |
| | | } |
| | | return 1; |
| | | } |
| | | |
| | | // 批量插入生产数据 |
| | | private void batchInsertProductions(Long masterId, List<Production> productions) { |
| | | List<Production> insertList = productions.stream() |
| | | .peek(p -> { |
| | | p.setId(null); |
| | | p.setProductionMasterId(masterId); |
| | | }) |
| | | if (productions.isEmpty()) { |
| | | return; |
| | | } |
| | | // 1. 收集所有需要查询的coalId |
| | | List<Long> coalIds = productions.stream() |
| | | .map(Production::getCoalId) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | |
| | | if (!insertList.isEmpty()) { |
| | | for (Production production : productions) { |
| | | // 2. 批量查询coalInfo数据 |
| | | Map<Long, CoalInfo> coalInfoMap = coalIds.isEmpty() ? |
| | | Collections.emptyMap() : |
| | | coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds)) |
| | | .stream() |
| | | .collect(Collectors.toMap(CoalInfo::getId, Function.identity())); |
| | | if (coalInfoMap.isEmpty()){ |
| | | throw new BaseException("煤种信息不存在"); |
| | | } |
| | | |
| | | // 3. 准备批量插入数据 |
| | | List<Production> batchInsertList = productions.stream() |
| | | .map(production -> { |
| | | Production p = new Production(); // 创建新对象避免副作用 |
| | | BeanUtils.copyProperties(production, p); |
| | | // 复制必要字段 |
| | | p.setProductionMasterId(masterId); |
| | | p.setCoalId(production.getCoalId()); |
| | | return p; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | if (!batchInsertList.isEmpty()) { |
| | | for (Production production : batchInsertList) { |
| | | production.setId(null); |
| | | production.setProductionMasterId(masterId); |
| | | productionMapper.insert(production); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int delByIds(Long[] ids) { |
| | | return 0; |
| | | if (ids == null || ids.length == 0) { |
| | | return 0; |
| | | } |
| | | |
| | | List<Long> idList = Arrays.asList(ids); |
| | | |
| | | // 1. 预加载所有关联数据 |
| | | List<ProductionInventory> allInventoryList = productionInventoryMapper.selectList( |
| | | new LambdaQueryWrapper<ProductionInventory>() |
| | | .in(ProductionInventory::getProductionMasterId, idList) |
| | | ); |
| | | |
| | | // 2. 按官方库存ID分组并计算库存调整量 |
| | | Map<Long, BigDecimal> inventoryAdjustMap = allInventoryList.stream() |
| | | .collect(Collectors.groupingBy( |
| | | ProductionInventory::getOfficialId, |
| | | Collectors.reducing( |
| | | BigDecimal.ZERO, |
| | | inv -> new BigDecimal(inv.getUsedQuantity()), |
| | | BigDecimal::add |
| | | ) |
| | | )); |
| | | |
| | | // 3. 批量更新官方库存 (使用SQL直接更新) |
| | | if (!inventoryAdjustMap.isEmpty()) { |
| | | inventoryAdjustMap.forEach((officialId, adjustAmount) -> |
| | | officialInventoryMapper.addInventoryQuantity(officialId, adjustAmount) |
| | | ); |
| | | } |
| | | |
| | | // 4. 批量删除关联数据 |
| | | if (!allInventoryList.isEmpty()) { |
| | | List<Long> inventoryIds = allInventoryList.stream() |
| | | .map(ProductionInventory::getId) |
| | | .collect(Collectors.toList()); |
| | | productionInventoryMapper.deleteBatchIds(inventoryIds); |
| | | } |
| | | |
| | | // 删除生产明细 |
| | | productionMapper.delete( |
| | | new LambdaQueryWrapper<Production>() |
| | | .in(Production::getProductionMasterId, idList) |
| | | ); |
| | | |
| | | // 5. 删除主记录 |
| | | return productionMasterMapper.deleteBatchIds(idList); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int deleteProductionInventory(ProductionMasterDto productionMasterDto) { |
| | | List<ProductionInventory> inventories = productionMasterDto.getProductionInventoryList(); |
| | | if (CollectionUtils.isEmpty(inventories)) { |
| | | return 0; |
| | | } |
| | | |
| | | // 预收集数据用于批量操作 |
| | | Map<Long, BigDecimal> inventoryAdjustMap = new HashMap<>(); |
| | | List<Long> productionIdsToDelete = new ArrayList<>(inventories.size()); |
| | | |
| | | for (ProductionInventory inventory : inventories) { |
| | | // 收集需要删除的生产库存ID |
| | | productionIdsToDelete.add(inventory.getId()); |
| | | |
| | | // 累计库存调整量(相同officialId的用量累加) |
| | | BigDecimal adjustment = new BigDecimal(inventory.getUsedQuantity()); |
| | | inventoryAdjustMap.merge( |
| | | inventory.getOfficialId(), |
| | | adjustment, |
| | | BigDecimal::add |
| | | ); |
| | | } |
| | | |
| | | // 批量更新官方库存 |
| | | for (Map.Entry<Long, BigDecimal> entry : inventoryAdjustMap.entrySet()) { |
| | | OfficialInventory official = officialInventoryMapper.selectById(entry.getKey()); |
| | | if (official == null) { |
| | | throw new BaseException("官方库存不存在,ID: " + entry.getKey()); |
| | | } |
| | | |
| | | // 使用线程安全的BigDecimal操作 |
| | | official.setInventoryQuantity( |
| | | Optional.ofNullable(official.getInventoryQuantity()) |
| | | .orElse(BigDecimal.ZERO) |
| | | .add(entry.getValue()) |
| | | ); |
| | | officialInventoryMapper.updateById(official); |
| | | } |
| | | |
| | | // 批量删除生产库存 |
| | | if (!productionIdsToDelete.isEmpty()) { |
| | | productionInventoryMapper.deleteBatchIds(productionIdsToDelete); |
| | | } |
| | | |
| | | return productionIdsToDelete.size(); |
| | | } |
| | | } |