From 0891b6708cc09c2672a19661a8555aae0e742df1 Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@tuqin.cn>
Date: 星期四, 24 九月 2026 16:54:27 +0800
Subject: [PATCH] feat: 新增多模块导入导出功能

---
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java |   86 +++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
index 5207cc9..b136cbf 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -14,6 +14,10 @@
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.stock.dto.StockInRecordDto;
+import com.ruoyi.stock.dto.StockInRecordImportDto;
+import com.ruoyi.basic.mapper.ProductModelMapper;
+import com.ruoyi.basic.pojo.ProductModel;
+import com.ruoyi.framework.web.domain.AjaxResult;
 import com.ruoyi.stock.dto.StockInventoryDto;
 import com.ruoyi.stock.dto.StockUninventoryDto;
 import com.ruoyi.stock.execl.StockInRecordExportData;
@@ -28,11 +32,13 @@
 import com.ruoyi.stock.service.StockInRecordService;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.AllArgsConstructor;
+import org.springframework.web.multipart.MultipartFile;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.List;
 
 @Service
@@ -43,6 +49,7 @@
     private StockInventoryMapper stockInventoryMapper;
     private StockUninventoryMapper stockUninventoryMapper;
     private ProductionOrderPickMapper productionOrderPickMapper;
+    private ProductModelMapper productModelMapper;
 
     @Override
     public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -52,12 +59,13 @@
     // 鏂板鍏ュ簱
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int add(StockInRecordDto stockInRecordDto) {
+    public StockInRecord add(StockInRecordDto stockInRecordDto) {
         String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches");
         stockInRecordDto.setInboundBatches(no);
         StockInRecord stockInRecord = new StockInRecord();
         BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
-        return stockInRecordMapper.insert(stockInRecord);
+        stockInRecordMapper.insert(stockInRecord);
+        return stockInRecord;
     }
 
     @Override
@@ -263,9 +271,9 @@
             if (stockInRecord == null) {
                 throw new BaseException("鍏ュ簱璁板綍涓嶅瓨鍦�,鏃犳硶瀹℃壒!!!");
             }
-            if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) {
+            /*if (stockInRecord.getApprovalStatus() != null && !ReviewStatusEnum.PENDING_REVIEW.getCode().equals(stockInRecord.getApprovalStatus())) {
                 throw new BaseException("鍙湁寰呭鎵圭姸鎬佺殑璁板綍鎵嶈兘瀹℃壒,鍏ュ簱鎵规:" + stockInRecord.getInboundBatches());
-            }
+            }*/
             stockInRecord.setApprovalStatus(approvalStatus);
             stockInRecordMapper.updateById(stockInRecord);
 
@@ -368,4 +376,74 @@
 
         return ids.size();
     }
+
+    @Override
+    public AjaxResult importData(MultipartFile file, String type) {
+        List<StockInRecordImportDto> rows;
+        try {
+            ExcelUtil<StockInRecordImportDto> util = new ExcelUtil<>(StockInRecordImportDto.class);
+            rows = util.importExcel(file.getInputStream());
+        } catch (Exception e) {
+            return AjaxResult.error("瑙f瀽Excel澶辫触锛�" + e.getMessage());
+        }
+        if (CollectionUtils.isEmpty(rows)) {
+            return AjaxResult.error("瀵煎叆鏂囦欢鏃犳暟鎹�");
+        }
+        String stockType = StringUtils.isEmpty(type) ? "0" : type.trim();
+        String recordType = "1".equals(stockType)
+                ? StockInQualifiedRecordTypeEnum.CUSTOMIZATION_UNSTOCK_IN.getCode()
+                : StockInQualifiedRecordTypeEnum.CUSTOMIZATION_STOCK_IN.getCode();
+
+        int success = 0;
+        List<String> errors = new ArrayList<>();
+        for (int i = 0; i < rows.size(); i++) {
+            StockInRecordImportDto row = rows.get(i);
+            int rowNo = i + 2; // 棣栬涓鸿〃澶�
+            try {
+                String productName = trim(row.getProductName());
+                String model = trim(row.getModel());
+                if (StringUtils.isEmpty(productName)) {
+                    errors.add("绗�" + rowNo + "琛岋細浜у搧澶х被涓虹┖");
+                    continue;
+                }
+                if (StringUtils.isEmpty(model)) {
+                    errors.add("绗�" + rowNo + "琛岋細瑙勬牸鍨嬪彿涓虹┖");
+                    continue;
+                }
+                if (row.getStockInNum() == null) {
+                    errors.add("绗�" + rowNo + "琛岋細鍏ュ簱鏁伴噺涓虹┖");
+                    continue;
+                }
+                ProductModel productModel = productModelMapper.selectByProductNameAndModel(productName, model);
+                if (productModel == null) {
+                    errors.add("绗�" + rowNo + "琛岋細鏈壘鍒颁骇鍝乕" + productName + " / " + model + "]");
+                    continue;
+                }
+                StockInRecordDto dto = new StockInRecordDto();
+                dto.setProductModelId(productModel.getId());
+                dto.setBatchNo(trim(row.getBatchNo()));
+                dto.setStockInNum(row.getStockInNum());
+                dto.setRemark(row.getRemark());
+                dto.setType(stockType);
+                dto.setRecordType(recordType);
+                dto.setApprovalStatus(ReviewStatusEnum.PENDING_REVIEW.getCode());
+                add(dto);
+                success++;
+            } catch (Exception e) {
+                errors.add("绗�" + rowNo + "琛岋細" + e.getMessage());
+            }
+        }
+
+        AjaxResult result = AjaxResult.success();
+        result.put("totalCount", rows.size());
+        result.put("successCount", success);
+        result.put("failCount", rows.size() - success);
+        result.put("errors", errors);
+        result.put("msg", "瀵煎叆瀹屾垚锛氭垚鍔� " + success + " 鏉★紝澶辫触 " + (rows.size() - success) + " 鏉�");
+        return result;
+    }
+
+    private String trim(String value) {
+        return value == null ? null : value.trim();
+    }
 }

--
Gitblit v1.9.3