From 40e4ce58f634598fafd205dfe54cb90e8b63b6f2 Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期五, 20 六月 2025 17:30:15 +0800 Subject: [PATCH] 生产加工 --- main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java | 333 +++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 239 insertions(+), 94 deletions(-) diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java index 84bed74..4d3b647 100644 --- a/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java +++ b/main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java @@ -2,17 +2,14 @@ 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; @@ -21,9 +18,7 @@ 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.stream.Collectors; /** @@ -45,6 +40,10 @@ 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) { @@ -116,112 +115,258 @@ @Override @Transactional - public int addOrEditPM(ProductionMasterDto productionMasterDto) { - // 1. 鐩存帴璁$畻鑱氬悎鍊硷紝閬垮厤鍒涘缓澶氫綑鍒楄〃 - BigDecimal totalPurchasePrice = BigDecimal.ZERO; - BigDecimal totalLaborCost = BigDecimal.ZERO; - BigDecimal totalEnergyConsumptionCost = BigDecimal.ZERO; - BigDecimal totalTotalCost = BigDecimal.ZERO; - BigDecimal totalEquipmentDepreciation = BigDecimal.ZERO; - int totalProductionQuantity = 0; - StringBuilder coalBuilder = new StringBuilder(); + public int addOrEditPM(ProductionMasterDto dto) { + Long masterId = dto.getId(); - for (Production production : productionMasterDto.getProductionList()) { - totalPurchasePrice = totalPurchasePrice.add(production.getPurchasePrice()); - totalLaborCost = totalLaborCost.add(production.getLaborCost()); - 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()); + // 缂栬緫鍦烘櫙锛氬洖婊氭棫搴撳瓨骞跺垹闄ゆ棫璁板綍 + if (masterId != null) { + rollbackOldInventory(masterId); + deleteChildRecords(masterId); } - String coalStr = coalBuilder.toString(); // 鐩存帴鑾峰彇鎷兼帴缁撴灉 - // 2. 鍒涘缓涓昏〃瀵硅薄 - ProductionMaster productionMaster = new ProductionMaster(); - productionMaster.setProductionQuantity(totalProductionQuantity); - productionMaster.setTotalCost(totalTotalCost); - productionMaster.setEquipmentDepreciation(totalEquipmentDepreciation); - productionMaster.setEnergyConsumptionCost(totalEnergyConsumptionCost); - productionMaster.setLaborCost(totalLaborCost); - productionMaster.setCoal(coalStr); + // 鏍¢獙浣跨敤閲忓苟鍑忓皯搴撳瓨 + validateAndReduceInventory(dto.getProductionInventoryList()); - Long masterId = productionMasterDto.getId(); - productionMaster.setId(masterId); + // 鏋勯�犱富琛ㄥ疄浣撳璞� + ProductionMaster master = buildProductionMaster(dto); - // 3. 缁熶竴瀛愯〃澶勭悊閫昏緫 + // 鎻掑叆鎴栨洿鏂颁富琛� if (masterId == null) { - productionMasterMapper.insert(productionMaster); - masterId = productionMaster.getId(); // 鑾峰彇鏂扮敓鎴愮殑ID + productionMasterMapper.insert(master); + masterId = master.getId(); } else { - // 鍒犻櫎鍏宠仈瀛愯〃鏁版嵁 - productionMapper.delete(new LambdaQueryWrapper<Production>() - .eq(Production::getProductionMasterId, masterId)); - - productionInventoryMapper.delete(new LambdaQueryWrapper<ProductionInventory>() - .eq(ProductionInventory::getProductionMasterId, masterId)); - - productionMasterMapper.updateById(productionMaster); + master.setId(masterId); + productionMasterMapper.updateById(master); } - //搴撳瓨鏇存柊 - for (ProductionInventory productionInventory : productionMasterDto.getProductionInventoryList()) { - OfficialInventory officialInventory = officialInventoryMapper.selectById(productionInventory.getOfficialId()); - BigDecimal subtract = officialInventory.getInventoryQuantity().subtract(new BigDecimal(productionInventory.getUsedQuantity())); - if (subtract.compareTo(BigDecimal.ZERO) < 0) { - throw new BaseException("搴撳瓨涓嶈冻"); - } - officialInventory.setInventoryQuantity(subtract); - officialInventoryMapper.updateById(officialInventory); - } - - // 4. 鎵归噺鎻掑叆瀛愯〃鏁版嵁 - batchInsertProductions(masterId, productionMasterDto.getProductionList()); - batchInsertInventories(masterId, productionMasterDto.getProductionInventoryList()); + + // 鎵归噺鎻掑叆鐢熶骇璁板綍涓庡簱瀛樿褰� + batchInsertProductions(masterId, dto.getProductionList()); + batchInsertInventories(masterId, dto.getProductionInventoryList()); + + // 鎻掑叆寰呭叆搴撴暟鎹� + insertPendingInventory(dto.getProductionList()); return 1; } - // 鎵归噺鎻掑叆鐢熶骇鏁版嵁 - private void batchInsertProductions(Long masterId, List<Production> productions) { - List<Production> insertList = productions.stream() - .peek(p -> { - p.setId(null); - p.setProductionMasterId(masterId); - }) - .collect(Collectors.toList()); + /** + * 鍥炴粴鏃х殑搴撳瓨鏁版嵁锛堝皢搴撳瓨鏁伴噺杩樺師锛� + */ + private void rollbackOldInventory(Long masterId) { + List<ProductionInventory> oldInventories = productionInventoryMapper.selectList( + new LambdaQueryWrapper<ProductionInventory>().eq(ProductionInventory::getProductionMasterId, masterId)); - if (!insertList.isEmpty()) { - for (Production production : productions) { - production.setId(null); - production.setProductionMasterId(masterId); - productionMapper.insert(production); + for (ProductionInventory oldInv : oldInventories) { + OfficialInventory inv = officialInventoryMapper.selectById(oldInv.getOfficialId()); + if (inv != null) { + inv.setInventoryQuantity(inv.getInventoryQuantity().add(new BigDecimal(oldInv.getUsedQuantity()))); + officialInventoryMapper.updateById(inv); } } } - // 鎵归噺鎻掑叆搴撳瓨鏁版嵁 - private void batchInsertInventories(Long masterId, List<ProductionInventory> inventories) { - List<ProductionInventory> insertList = inventories.stream() - .peek(inv -> { - inv.setId(null); - inv.setProductionMasterId(masterId); - }) - .collect(Collectors.toList()); + /** + * 鍒犻櫎鏃х殑瀛愯〃鏁版嵁鍙婂緟鍏ュ簱鏁版嵁 + */ + private void deleteChildRecords(Long masterId) { + productionMapper.delete(new LambdaQueryWrapper<Production>().eq(Production::getProductionMasterId, masterId)); + productionInventoryMapper.delete(new LambdaQueryWrapper<ProductionInventory>().eq(ProductionInventory::getProductionMasterId, masterId)); + pendingInventoryMapper.delete(new LambdaQueryWrapper<PendingInventory>().eq(PendingInventory::getMasterId, masterId)); + } - if (!insertList.isEmpty()) { - for (ProductionInventory inventory : inventories) { - inventory.setId(null); - inventory.setProductionMasterId(masterId); - productionInventoryMapper.insert(inventory); + /** + * 鏍¢獙姣忔潯浣跨敤閲忔槸鍚﹁冻澶燂紝骞跺噺灏戞寮忓簱瀛樻暟閲� + */ + private void validateAndReduceInventory(List<ProductionInventory> inventoryList) { + for (ProductionInventory inv : inventoryList) { + OfficialInventory official = officialInventoryMapper.selectById(inv.getOfficialId()); + BigDecimal used = new BigDecimal(inv.getUsedQuantity()); + if (official.getInventoryQuantity().compareTo(used) < 0) { + throw new BaseException("搴撳瓨涓嶈冻"); } + official.setInventoryQuantity(official.getInventoryQuantity().subtract(used)); + officialInventoryMapper.updateById(official); + } + } + + /** + * 鏋勯�犱富琛ㄥ璞″苟鑱氬悎瀛楁鍊硷紙濡傛�绘垚鏈�佺叅绉嶇瓑锛� + */ + private ProductionMaster buildProductionMaster(ProductionMasterDto dto) { + BigDecimal totalPurchase = BigDecimal.ZERO; + BigDecimal totalLabor = BigDecimal.ZERO; + BigDecimal totalEnergy = BigDecimal.ZERO; + BigDecimal totalCost = BigDecimal.ZERO; + BigDecimal totalDepreciation = BigDecimal.ZERO; + BigDecimal totalQuantity = BigDecimal.ZERO; + + List<Long> coalIds = new ArrayList<>(); + + for (Production p : dto.getProductionList()) { + totalPurchase = totalPurchase.add(p.getPurchasePrice()); + totalLabor = totalLabor.add(p.getLaborCost()); + totalEnergy = totalEnergy.add(p.getEnergyConsumptionCost()); + totalCost = totalCost.add(p.getTotalCost()); + totalDepreciation = totalDepreciation.add(p.getEquipmentDepreciation()); + totalQuantity = totalQuantity.add(p.getProductionQuantity()); + coalIds.add(p.getCoalId()); + } + + List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds)); + + ProductionMaster master = new ProductionMaster(); + master.setProductionQuantity(totalQuantity); + master.setTotalCost(totalCost); + master.setLaborCost(totalLabor); + master.setEnergyConsumptionCost(totalEnergy); + master.setEquipmentDepreciation(totalDepreciation); + master.setCoal(coalInfos.stream().map(CoalInfo::getCoal).collect(Collectors.joining(","))); + master.setCoalId(coalIds.stream().map(String::valueOf).collect(Collectors.joining(","))); + + return master; + } + + /** + * 鎵归噺鎻掑叆鐢熶骇瀛愯〃鏁版嵁 + */ + private void batchInsertProductions(Long masterId, List<Production> list) { + if (list.isEmpty()) return; + + for (Production p : list) { + Production copy = new Production(); + BeanUtils.copyProperties(p, copy); + copy.setId(null); + copy.setProductionMasterId(masterId); + productionMapper.insert(copy); + } + } + + /** + * 鎵归噺鎻掑叆搴撳瓨浣跨敤瀛愯〃鏁版嵁 + */ + private void batchInsertInventories(Long masterId, List<ProductionInventory> list) { + if (list.isEmpty()) return; + + for (ProductionInventory p : list) { + p.setId(null); + p.setProductionMasterId(masterId); + productionInventoryMapper.insert(p); + } + } + + /** + * 灏嗗姞宸ヤ骇鐢熺殑浜у搧璁板綍鍒板緟鍏ュ簱琛� + */ + private void insertPendingInventory(List<Production> list) { + for (Production p : list) { + PendingInventory pending = new PendingInventory(); + pending.setCoalId(p.getCoalId()); + pending.setInventoryQuantity(p.getProductionQuantity()); + pending.setSupplierName("鐢熶骇鍔犲伐鍏ュ簱"); + pending.setTotalPriceIncludingTax(p.getTotalCost()); + pending.setPriceIncludingTax(p.getPurchasePrice()); + pending.setPriceIncludingTax(p.getPurchasePrice()); + pendingInventoryMapper.insert(pending); } } @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. 鎸夊畼鏂瑰簱瀛業D鍒嗙粍骞惰绠楀簱瀛樿皟鏁撮噺 + 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()); + } + + // 浣跨敤绾跨▼瀹夊叏鐨凚igDecimal鎿嶄綔 + official.setInventoryQuantity( + Optional.ofNullable(official.getInventoryQuantity()) + .orElse(BigDecimal.ZERO) + .add(entry.getValue()) + ); + officialInventoryMapper.updateById(official); + } + + // 鎵归噺鍒犻櫎鐢熶骇搴撳瓨 + if (!productionIdsToDelete.isEmpty()) { + productionInventoryMapper.deleteBatchIds(productionIdsToDelete); + } + + return productionIdsToDelete.size(); } } -- Gitblit v1.9.3