From 7cd9dbf45e8e29f3bb0c00ac4f14d5588029c3ba Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期六, 11 十月 2025 17:45:02 +0800
Subject: [PATCH] yys 新增车牌字段
---
main-business/src/main/java/com/ruoyi/business/service/impl/ProductionMasterServiceImpl.java | 368 +++++++++++++++++++++++++++++++++------------------
1 files changed, 237 insertions(+), 131 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 ff10b10..d4521c4 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
@@ -12,14 +12,17 @@
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.service.ProductionMasterService;
import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
import java.util.*;
-import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -46,10 +49,38 @@
private final PendingInventoryMapper pendingInventoryMapper;
+ private final ProductionSchedulingMapper productionSchedulingMapper;
+
@Override
public IPage<ProductionMasterDto> selectPMList(Page page, ProductionMasterDto productionMasterDto) {
// 1. 鏋勫缓涓昏〃鏌ヨ鏉′欢
LambdaQueryWrapper<ProductionMaster> masterQueryWrapper = new LambdaQueryWrapper<>();
+ String keyword = productionMasterDto.getSearchAll();
+ if (StringUtils.isNotBlank(keyword)) {
+ // 鏌ヨ鐓ょ鍚嶇О涓ā绯婂尮閰嶇殑coalId鍒楄〃
+ List<Long> matchedCoalIds = coalInfoMapper.selectList(
+ new LambdaQueryWrapper<CoalInfo>().like(CoalInfo::getCoal, keyword)
+ ).stream()
+ .map(CoalInfo::getId)
+ .toList();
+
+ // 缁勮鏌ヨ鏉′欢锛氱叅绉岻D鍦ㄥ尮閰嶇殑鍒楄〃涓�
+ // 濡傛灉 matchedCoalIds 涓虹┖锛岀洿鎺ヨ繑鍥� 0 鏉℃暟鎹紙鏋勯�犱竴涓笉鍙兘鎴愮珛鐨勬潯浠讹級
+ if (matchedCoalIds.isEmpty()) {
+ masterQueryWrapper.apply("1 = 0"); // 寮哄埗杩斿洖绌虹粨鏋�
+ }
+ // 濡傛灉鏈夊尮閰嶇殑 coalId锛屽垯鎸� coalId 鏌ヨ
+ else {
+ String ids = matchedCoalIds.stream()
+ .map(String::valueOf)
+ .collect(Collectors.joining(","));
+
+ masterQueryWrapper.apply(
+ "{0} = ANY(string_to_array(coal_id, ','))",
+ ids
+ );
+ }
+ }
// 2. 鎵ц涓昏〃鍒嗛〉鏌ヨ
IPage<ProductionMaster> entityPage = productionMasterMapper.selectPage(page, masterQueryWrapper);
@@ -78,7 +109,6 @@
IPage<ProductionMasterDto> dtoPage = new Page<>();
BeanUtils.copyProperties(entityPage, dtoPage, "records");
dtoPage.setRecords(dtoList);
-
return dtoPage;
}
@@ -116,141 +146,208 @@
@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;
- BigDecimal totalProductionQuantity = BigDecimal.ZERO;
+ 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().add(totalProductionQuantity);
+ // 缂栬緫鍦烘櫙锛氬洖婊氭棫搴撳瓨骞跺垹闄ゆ棫璁板綍
+ if (masterId != null) {
+ rollbackOldInventory(masterId);
+ deleteChildRecords(masterId);
}
- //鐓ょ瀛楁
- List<Long> coalIds = productionMasterDto.getProductionList().stream()
- .map(Production::getCoalId)
- .collect(Collectors.toList());
- List<CoalInfo> coalInfos = coalInfoMapper.selectList(new LambdaQueryWrapper<CoalInfo>().in(CoalInfo::getId, coalIds));
+ // 鏍¢獙浣跨敤閲忓苟鍑忓皯搴撳瓨
+ validateAndReduceInventory(dto.getProductionInventoryList());
- // 2. 鍒涘缓涓昏〃瀵硅薄
- ProductionMaster productionMaster = new ProductionMaster();
- productionMaster.setProductionQuantity(totalProductionQuantity);
- productionMaster.setTotalCost(totalTotalCost);
- productionMaster.setEquipmentDepreciation(totalEquipmentDepreciation);
- productionMaster.setEnergyConsumptionCost(totalEnergyConsumptionCost);
- productionMaster.setLaborCost(totalLaborCost);
- productionMaster.setCoal(coalInfos.stream().map(CoalInfo::getCoal).collect(Collectors.joining(",")));
- productionMaster.setCoalId(coalIds.stream().map(String::valueOf).collect(Collectors.joining(",")));
+ // 鏋勯�犱富琛ㄥ疄浣撳璞�
+ ProductionMaster master = buildProductionMaster(dto);
- Long masterId = productionMasterDto.getId();
- productionMaster.setId(masterId);
-
- // 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());
- //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());
- }
+ // 鎵归噺鎻掑叆鐢熶骇璁板綍涓庡簱瀛樿褰�
+ batchInsertProductions(masterId, dto.getProductionList());
+ batchInsertInventories(masterId, dto.getProductionInventoryList());
+
+ // 鎻掑叆寰呭叆搴撴暟鎹�
+// insertPendingInventory(dto.getProductionList());
+
return 1;
}
- // 鎵归噺鎻掑叆鐢熶骇鏁版嵁
- private void batchInsertProductions(Long masterId, List<Production> productions) {
- if (productions.isEmpty()) {
- return;
- }
- // 1. 鏀堕泦鎵�鏈夐渶瑕佹煡璇㈢殑coalId
- List<Long> coalIds = productions.stream()
- .map(Production::getCoalId)
- .filter(Objects::nonNull)
- .distinct()
- .collect(Collectors.toList());
+ /**
+ * 鍥炴粴鏃х殑搴撳瓨鏁版嵁锛堝皢搴撳瓨鏁伴噺杩樺師锛�
+ */
+ private void rollbackOldInventory(Long masterId) {
+ List<ProductionInventory> oldInventories = productionInventoryMapper.selectList(
+ new LambdaQueryWrapper<ProductionInventory>().eq(ProductionInventory::getProductionMasterId, masterId));
- // 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);
- 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());
+ }
+
+ ProductionMaster master = new ProductionMaster();
+ master.setProductionQuantity(totalQuantity);
+ master.setTotalCost(totalCost);
+ master.setLaborCost(totalLabor);
+ master.setEnergyConsumptionCost(totalEnergy);
+ master.setEquipmentDepreciation(totalDepreciation);
+ 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);
+ copy.setStatus(1);
+ 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);
+ }
+ }
+
+ /**
+ * 灏嗗姞宸ヤ骇鐢熺殑浜у搧璁板綍鍒板緟鍏ュ簱琛�
+ */
+ public void insertPendingInventory(List<Production> list) {
+ LocalDate currentDate = LocalDate.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ String formattedDate = currentDate.format(formatter);
+ final int SCALE = 2;
+ final RoundingMode ROUNDING_MODE = RoundingMode.HALF_UP;
+ final BigDecimal TAX_RATE = new BigDecimal("1.13");
+
+ for (Production p : list) {
+ PendingInventory pending = new PendingInventory();
+ pending.setCoalId(p.getCoalId());
+ pending.setInventoryQuantity(p.getProductionQuantity());
+ pending.setUnit("鍚�");
+ pending.setSupplierName(formattedDate + " - " + "鐢熶骇鍔犲伐鍏ュ簱");
+
+ // 闈炵┖澶勭悊
+ BigDecimal totalCost = p.getTotalCost() == null ? BigDecimal.ZERO : p.getTotalCost();
+ BigDecimal productionQuantity = p.getProductionQuantity() == null ? BigDecimal.ZERO : p.getProductionQuantity();
+
+ // 鍚◣鎬讳环
+ BigDecimal totalPriceIncludingTax = totalCost.multiply(productionQuantity)
+ .setScale(SCALE, ROUNDING_MODE);
+ pending.setTotalPriceIncludingTax(totalPriceIncludingTax);
+
+ // 鍚◣鍗曚环
+ pending.setPriceIncludingTax(totalCost.setScale(SCALE, ROUNDING_MODE));
+
+ // 涓嶅惈绋庡崟浠凤紙鐩存帴淇濈暀2浣嶅皬鏁帮級
+ BigDecimal priceExcludingTax = totalCost.divide(TAX_RATE, SCALE, ROUNDING_MODE);
+ pending.setPriceExcludingTax(priceExcludingTax);
+
+ // 涓嶅惈绋庢�讳环
+ BigDecimal totalPriceExcludingTax = priceExcludingTax.multiply(productionQuantity)
+ .setScale(SCALE, ROUNDING_MODE);
+ pending.setTotalPriceExcludingTax(totalPriceExcludingTax);
+
+ pending.setRegistrantId(p.getProducerId());
+ pending.setRegistrationDate(LocalDate.now());
+
+ pending.setCode(generateCode(p.getType()));
+ pending.setType(p.getType());
+ pendingInventoryMapper.insert(pending);
+ }
+ }
+
+ private String generateCode(Integer type) {
+ LocalDate now = LocalDate.now();
+ String year = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+ String s = "";
+ //鐢熸垚缂栫爜
+ switch (type){
+ case 1:
+ s = "CP";
+ break;
+ case 2:
+ s = "YL";
+ break;
+ }
+ // 鏌ヨ褰撳ぉ寰呭叆搴撴柊澧炴潯鏁�
+ Long count = pendingInventoryMapper.selectCount(new LambdaQueryWrapper<PendingInventory>()
+ .apply("date_trunc('day', create_time) = date_trunc('day', now())"));
+ return s + year + String.format("%03d", count + 1);
}
@Override
@@ -293,12 +390,21 @@
.collect(Collectors.toList());
productionInventoryMapper.deleteBatchIds(inventoryIds);
}
+ List<Production> productions = productionMapper.selectList(new LambdaQueryWrapper<Production>()
+ .in(Production::getProductionMasterId, idList));
+
+ // 鍒犻櫎鐢熶骇鎶ュ伐璁板綍
+ productionSchedulingMapper.delete(
+ new LambdaQueryWrapper<ProductionScheduling>()
+ .in(ProductionScheduling::getProductionId, productions.stream().map(Production::getId).collect(Collectors.toList()))
+ );
// 鍒犻櫎鐢熶骇鏄庣粏
productionMapper.delete(
new LambdaQueryWrapper<Production>()
.in(Production::getProductionMasterId, idList)
);
+
// 5. 鍒犻櫎涓昏褰�
return productionMasterMapper.deleteBatchIds(idList);
@@ -330,20 +436,20 @@
}
// 鎵归噺鏇存柊瀹樻柟搴撳瓨
- 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);
- }
+// 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()) {
--
Gitblit v1.9.3