From 5652acfd578f66353f59f398f42c88da901e245e Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 24 四月 2026 15:55:23 +0800
Subject: [PATCH] fix: 附件上传路径link,设备巡检附件去重
---
src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java | 203 ++++++++++++++++++++++++++++++++++++++------------
1 files changed, 154 insertions(+), 49 deletions(-)
diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
index 99c4544..be5746d 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInventoryServiceImpl.java
@@ -5,7 +5,9 @@
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.common.enums.StockQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
+import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
@@ -13,20 +15,25 @@
import com.ruoyi.stock.dto.StockInRecordDto;
import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.dto.StockOutRecordDto;
+import com.ruoyi.stock.dto.StockUninventoryDto;
import com.ruoyi.stock.execl.StockInventoryExportData;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import com.ruoyi.stock.pojo.StockInventory;
import com.ruoyi.stock.service.StockInRecordService;
import com.ruoyi.stock.service.StockInventoryService;
import com.ruoyi.stock.service.StockOutRecordService;
+import com.ruoyi.stock.service.StockUninventoryService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* <p>
@@ -40,13 +47,20 @@
@AllArgsConstructor
public class StockInventoryServiceImpl extends ServiceImpl<StockInventoryMapper, StockInventory> implements StockInventoryService {
- private StockInventoryMapper stockInventoryMapper;
+ private StockInventoryMapper stockInventoryMapper;
private StockInRecordService stockInRecordService;
private StockOutRecordService stockOutRecordService;
+ private StockUninventoryService stockUninventoryService;
private SalesLedgerProductMapper salesLedgerProductMapper;
+
@Override
public IPage<StockInventoryDto> pagestockInventory(Page page, StockInventoryDto stockInventoryDto) {
return stockInventoryMapper.pagestockInventory(page, stockInventoryDto);
+ }
+
+ @Override
+ public IPage<StockInventoryDto> pageListCombinedStockInventory(Page page, StockInventoryDto stockInventoryDto) {
+ return stockInventoryMapper.pageListCombinedStockInventory(page, stockInventoryDto);
}
//鍏ュ簱璋冪敤
@@ -54,16 +68,31 @@
@Transactional(rollbackFor = Exception.class)
public Boolean addstockInventory(StockInventoryDto stockInventoryDto) {
//鏂板鍏ュ簱璁板綍鍐嶆坊鍔犲簱瀛�
+ // 鏇存柊璁㈠崟浜у搧鍏ュ簱鐘舵�侊紙浠呭綋浼犲叆浜嗕骇鍝佽ID鏃讹級
+ if (stockInventoryDto.getSalesLedgerProductId() != null) {
+ SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(stockInventoryDto.getSalesLedgerProductId());
+ if (salesLedgerProduct == null) {
+ throw new ServiceException("鍏ュ簱澶辫触,閿�鍞骇鍝佷笉瀛樺湪");
+ }
+ salesLedgerProduct.setProductStockStatus(1);
+ salesLedgerProductMapper.updateById(salesLedgerProduct);
+ }
+
StockInRecordDto stockInRecordDto = new StockInRecordDto();
stockInRecordDto.setRecordId(stockInventoryDto.getRecordId());
stockInRecordDto.setRecordType(stockInventoryDto.getRecordType());
stockInRecordDto.setStockInNum(stockInventoryDto.getQualitity());
stockInRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
stockInRecordDto.setType("0");
+ stockInRecordDto.setSalesLedgerId(stockInventoryDto.getSalesLedgerId());
+ stockInRecordDto.setSalesLedgerProductId(stockInventoryDto.getSalesLedgerProductId());
stockInRecordService.add(stockInRecordDto);
//鍐嶈繘琛屾柊澧炲簱瀛樻暟閲忓簱瀛�
//鍏堟煡璇㈠簱瀛樿〃涓殑浜у搧鏄惁瀛樺湪锛屼笉瀛樺湪鏂板锛屽瓨鍦ㄦ洿鏂�
- StockInventory oldStockInventory = stockInventoryMapper.selectOne(new QueryWrapper<StockInventory>().lambda().eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId()));
+ List<StockInventory> stockInventories = stockInventoryMapper.selectList(new QueryWrapper<StockInventory>().lambda()
+ .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId())
+ .orderByDesc(StockInventory::getId));
+ StockInventory oldStockInventory = (stockInventories == null || stockInventories.isEmpty()) ? null : stockInventories.get(0);
if (ObjectUtils.isEmpty(oldStockInventory)) {
StockInventory newStockInventory = new StockInventory();
newStockInventory.setProductModelId(stockInventoryDto.getProductModelId());
@@ -73,8 +102,8 @@
newStockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
newStockInventory.setWarnNum(stockInventoryDto.getWarnNum());
stockInventoryMapper.insert(newStockInventory);
- }else {
- stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
+ } else {
+ stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
}
return true;
}
@@ -83,80 +112,131 @@
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean subtractStockInventory(StockInventoryDto stockInventoryDto) {
- // 鏂板鍑哄簱璁板綍
+ // 鏂板鍑哄簱璁板綍
StockOutRecordDto stockOutRecordDto = new StockOutRecordDto();
stockOutRecordDto.setRecordId(stockInventoryDto.getRecordId());
stockOutRecordDto.setRecordType(stockInventoryDto.getRecordType());
stockOutRecordDto.setStockOutNum(stockInventoryDto.getQualitity());
stockOutRecordDto.setProductModelId(stockInventoryDto.getProductModelId());
stockOutRecordDto.setType("0");
+ stockOutRecordDto.setSalesLedgerId(stockInventoryDto.getSalesLedgerId());
+ stockOutRecordDto.setSalesLedgerProductId(stockInventoryDto.getSalesLedgerProductId());
stockOutRecordService.add(stockOutRecordDto);
- StockInventory oldStockInventory = stockInventoryMapper.selectOne(new QueryWrapper<StockInventory>().lambda().eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId()));
- if (stockInventoryDto.getQualitity().compareTo( oldStockInventory.getQualitity().subtract(oldStockInventory.getLockedQuantity()))>0) {
- throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
- }
+ List<StockInventory> stockInventories = stockInventoryMapper.selectList(new QueryWrapper<StockInventory>().lambda()
+ .eq(StockInventory::getProductModelId, stockInventoryDto.getProductModelId())
+ .orderByDesc(StockInventory::getId));
+ StockInventory oldStockInventory = (stockInventories == null || stockInventories.isEmpty()) ? null : stockInventories.get(0);
if (ObjectUtils.isEmpty(oldStockInventory)) {
throw new RuntimeException("浜у搧搴撳瓨涓嶅瓨鍦�");
- }else {
- stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+ }
+ BigDecimal lockedQty = oldStockInventory.getLockedQuantity();
+ if (lockedQty == null) {
+ lockedQty = BigDecimal.ZERO;
+ }
+ if (stockInventoryDto.getQualitity().compareTo(oldStockInventory.getQualitity().subtract(lockedQty)) > 0) {
+ throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
+ }
+
+ int affectRows = stockInventoryMapper.updateSubtractStockInventory(stockInventoryDto);
+ if (affectRows <= 0) {
+ throw new RuntimeException("搴撳瓨涓嶈冻鏃犳硶鍑哄簱");
}
return true;
}
@Override
+ @Transactional(rollbackFor = Exception.class)
public R importStockInventory(MultipartFile file) {
try {
// 鏌ヨ鎵�鏈夌殑浜у搧
List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectProduct();
+ Map<String, SalesLedgerProduct> productMap = new HashMap<>();
+ for (SalesLedgerProduct product : salesLedgerProducts) {
+ // 浣跨敤浜у搧鍚嶇О + 瑙勬牸鍨嬪彿 + 鍘氬害浣滀负閿�
+ String key = buildProductKey(product.getProductCategory(), product.getSpecificationModel(), product.getThickness());
+ productMap.put(key, product);
+ }
ExcelUtil<StockInventoryExportData> util = new ExcelUtil<StockInventoryExportData>(StockInventoryExportData.class);
List<StockInventoryExportData> list = util.importExcel(file.getInputStream());
// 璁板綍鏈壘鍒板尮閰嶉」鐨勬暟鎹�
List<String> unmatchedRecords = new ArrayList<>();
+ int successCount = 0;
- list.forEach(dto -> {
- boolean matched = false;
- for (SalesLedgerProduct item : salesLedgerProducts) {
- if (item.getProductCategory().equals(dto.getProductName()) &&
- item.getSpecificationModel().equals(dto.getModel())) {
+ for (StockInventoryExportData dto : list) {
+ // 鏋勫缓鏌ユ壘閿�
+ String key = buildProductKey(dto.getProductName(), dto.getModel(), dto.getThickness());
+ SalesLedgerProduct matchedProduct = productMap.get(key);
+
+ if (matchedProduct != null) {
+ // 澶勭悊鍚堟牸搴撳瓨
+ if (dto.getQualifiedQuantity() != null && dto.getQualifiedQuantity().compareTo(BigDecimal.ZERO) > 0) {
StockInventoryDto stockInventoryDto = new StockInventoryDto();
stockInventoryDto.setRecordId(0L);
- stockInventoryDto.setRecordType(StockQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode());
- stockInventoryDto.setQualitity(dto.getQualitity());
+ stockInventoryDto.setRecordType(StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode());
+ stockInventoryDto.setQualitity(dto.getQualifiedQuantity());
stockInventoryDto.setRemark(dto.getRemark());
stockInventoryDto.setWarnNum(dto.getWarnNum());
- if (ObjectUtils.isNotEmpty(dto.getLockedQuantity())&&dto.getLockedQuantity().compareTo(dto.getQualitity())>0) {
- throw new RuntimeException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃鏈瀵煎叆鐨勫簱瀛樻暟閲�");
+ // 楠岃瘉鍚堟牸鍐荤粨鏁伴噺
+ if (ObjectUtils.isNotEmpty(dto.getQualifiedLockedQuantity())) {
+ if (dto.getQualifiedLockedQuantity().compareTo(dto.getQualifiedQuantity()) > 0) {
+ throw new ServiceException("鍚堟牸鍐荤粨鏁伴噺涓嶈兘瓒呰繃鏈瀵煎叆鐨勫悎鏍煎簱瀛樻暟閲�");
+ }
+ stockInventoryDto.setLockedQuantity(dto.getQualifiedLockedQuantity());
+ } else {
+ stockInventoryDto.setLockedQuantity(BigDecimal.ZERO);
}
- stockInventoryDto.setLockedQuantity(dto.getLockedQuantity());
- stockInventoryDto.setProductModelId(item.getProductModelId());
+
+ stockInventoryDto.setProductModelId(matchedProduct.getProductModelId());
this.addstockInventory(stockInventoryDto);
- matched = true;
- break; // 鎵惧埌鍖归厤椤瑰悗璺冲嚭寰幆
+ successCount++;
}
+
+ // 澶勭悊涓嶅悎鏍煎簱瀛�
+ if (dto.getUnQualifiedQuantity() != null && dto.getUnQualifiedQuantity().compareTo(BigDecimal.ZERO) > 0) {
+ StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
+ stockUninventoryDto.setRecordId(0L);
+ stockUninventoryDto.setRecordType(StockInUnQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode());
+ stockUninventoryDto.setQualitity(dto.getUnQualifiedQuantity());
+ stockUninventoryDto.setRemark(dto.getRemark());
+
+ // 楠岃瘉涓嶅悎鏍煎喕缁撴暟閲�
+ if (ObjectUtils.isNotEmpty(dto.getUnQualifiedLockedQuantity())) {
+ if (dto.getUnQualifiedLockedQuantity().compareTo(dto.getUnQualifiedQuantity()) > 0) {
+ throw new ServiceException("涓嶅悎鏍煎喕缁撴暟閲忎笉鑳借秴杩囨湰娆″鍏ョ殑涓嶅悎鏍煎簱瀛樻暟閲�");
+ }
+ stockUninventoryDto.setLockedQuantity(dto.getUnQualifiedLockedQuantity());
+ } else {
+ stockUninventoryDto.setLockedQuantity(BigDecimal.ZERO);
+ }
+
+ stockUninventoryDto.setProductModelId(matchedProduct.getProductModelId());
+ stockUninventoryService.addStockUninventory(stockUninventoryDto);
+ successCount++;
+ }
+ } else {
+ // 璁板綍鏈尮閰嶇殑浜у搧
+ String unmatchedRecord = "浜у搧鍚嶇О锛�" + dto.getProductName() + "锛屽瀷鍙凤細" + dto.getModel() + "锛屽帤搴︼細" + normalizeThickness(dto.getThickness());
+ unmatchedRecords.add(unmatchedRecord);
}
- if (!matched) {
- // 璁板綍鏈尮閰嶇殑鏁版嵁
- String unmatchedInfo = String.format("浜у搧鍚嶇О锛�%s锛岃鏍煎瀷鍙凤細%s",
- dto.getProductName(), dto.getModel());
- unmatchedRecords.add(unmatchedInfo);
- }
- });
+ }
+
// 鏋勫缓杩斿洖淇℃伅
StringBuilder message = new StringBuilder();
if (!unmatchedRecords.isEmpty()) {
- message.append("浠ヤ笅浜у搧鏈壘鍒板尮閰嶉」锛歕n");
+ message.append("瀵煎叆鎴愬姛 " + successCount + " 鏉¤褰曪紝浠ヤ笅浜у搧鏈壘鍒板尮閰嶉」锛歕n");
for (String record : unmatchedRecords) {
message.append(record).append("\n");
}
- throw new RuntimeException(message.toString());
+ return R.ok(message.toString());
}
+
+ return R.ok("瀵煎叆鎴愬姛锛屽叡澶勭悊 " + successCount + " 鏉¤褰�");
} catch (Exception e) {
- e.printStackTrace();
+ log.error("瀵煎叆搴撳瓨澶辫触", e);
return R.fail("瀵煎叆澶辫触锛�" + e.getMessage());
}
- return R.ok("瀵煎叆鎴愬姛");
}
@@ -165,40 +245,65 @@
List<StockInventoryExportData> list = stockInventoryMapper.listStockInventoryExportData(stockInventoryDto);
ExcelUtil<StockInventoryExportData> util = new ExcelUtil<>(StockInventoryExportData.class);
- util.exportExcel(response,list, "搴撳瓨淇℃伅");
+ util.exportExcel(response, list, "搴撳瓨淇℃伅");
}
@Override
public IPage<StockInRecordDto> stockInventoryPage(StockInventoryDto stockInventoryDto, Page page) {
- return stockInventoryMapper.stockInventoryPage(stockInventoryDto,page);
+ return stockInventoryMapper.stockInventoryPage(stockInventoryDto, page);
}
@Override
public IPage<StockInventoryDto> stockInAndOutRecord(StockInventoryDto stockInventoryDto, Page page) {
- return stockInventoryMapper.stockInAndOutRecord(stockInventoryDto,page);
+ return stockInventoryMapper.stockInAndOutRecord(stockInventoryDto, page);
}
@Override
public Boolean frozenStock(StockInventoryDto stockInventoryDto) {
StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
- if (stockInventory.getQualitity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
- throw new RuntimeException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃搴撳瓨鏁伴噺");
+ if (ObjectUtils.isEmpty(stockInventory)) {
+ throw new ServiceException("搴撳瓨璁板綍涓嶅瓨鍦�");
}
- if (ObjectUtils.isEmpty(stockInventory.getLockedQuantity())) {
- stockInventory.setLockedQuantity(stockInventoryDto.getLockedQuantity());
- }else {
- stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().add(stockInventoryDto.getLockedQuantity()));
+ if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
+ throw new ServiceException("鍐荤粨鏁伴噺蹇呴』澶т簬0");
}
+ BigDecimal totalQty = ObjectUtils.isEmpty(stockInventory.getQualitity()) ? BigDecimal.ZERO : stockInventory.getQualitity();
+ BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockInventory.getLockedQuantity()) ? BigDecimal.ZERO : stockInventory.getLockedQuantity();
+ BigDecimal availableQty = totalQty.subtract(currentLockedQty);
+ if (stockInventoryDto.getLockedQuantity().compareTo(availableQty) > 0) {
+ throw new ServiceException("鍐荤粨鏁伴噺涓嶈兘瓒呰繃鍙喕缁撳簱瀛樻暟閲�");
+ }
+ stockInventory.setLockedQuantity(currentLockedQty.add(stockInventoryDto.getLockedQuantity()));
return this.updateById(stockInventory);
}
@Override
public Boolean thawStock(StockInventoryDto stockInventoryDto) {
StockInventory stockInventory = stockInventoryMapper.selectById(stockInventoryDto.getId());
- if (stockInventory.getLockedQuantity().compareTo(stockInventoryDto.getLockedQuantity())<0) {
- throw new RuntimeException("瑙e喕鏁伴噺涓嶈兘瓒呰繃鍐荤粨鏁伴噺");
+ if (ObjectUtils.isEmpty(stockInventory)) {
+ throw new ServiceException("搴撳瓨璁板綍涓嶅瓨鍦�");
}
- stockInventory.setLockedQuantity(stockInventory.getLockedQuantity().subtract(stockInventoryDto.getLockedQuantity()));
+ if (ObjectUtils.isEmpty(stockInventoryDto.getLockedQuantity()) || stockInventoryDto.getLockedQuantity().compareTo(BigDecimal.ZERO) <= 0) {
+ throw new ServiceException("瑙e喕鏁伴噺蹇呴』澶т簬0");
+ }
+ BigDecimal currentLockedQty = ObjectUtils.isEmpty(stockInventory.getLockedQuantity()) ? BigDecimal.ZERO : stockInventory.getLockedQuantity();
+ if (currentLockedQty.compareTo(stockInventoryDto.getLockedQuantity()) < 0) {
+ throw new ServiceException("瑙e喕鏁伴噺涓嶈兘瓒呰繃鍐荤粨鏁伴噺");
+ }
+ stockInventory.setLockedQuantity(currentLockedQty.subtract(stockInventoryDto.getLockedQuantity()));
return this.updateById(stockInventory);
}
-}
+
+ private String buildProductKey(String productName, String model, BigDecimal thickness) {
+ String safeProductName = productName == null ? "" : productName.trim();
+ String safeModel = model == null ? "" : model.trim();
+ return safeProductName + "|" + safeModel + "|" + normalizeThickness(thickness);
+ }
+
+ private String normalizeThickness(BigDecimal thickness) {
+ if (thickness == null) {
+ return "";
+ }
+ return thickness.stripTrailingZeros().toPlainString();
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3