Selaa lähdekoodia

解析修正、匹配不到原因赋other、源文件延期商品标红

zhouxingyu 4 päivää sitten
vanhempi
commit
82fbf469f3

+ 84 - 21
lg-code/src/main/java/org/jeecg/modules/ProdPlan/service/impl/ProdPlanServiceImpl.java

@@ -1,10 +1,12 @@
 package org.jeecg.modules.ProdPlan.service.impl;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.Units;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.XSSFPicture;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.jeecg.modules.Contact.entity.Contact;
@@ -33,9 +35,7 @@ import org.springframework.stereotype.Service;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
-import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.io.IOException;
+import java.io.*;
 import java.time.*;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.IsoFields;
@@ -88,25 +88,28 @@ public class ProdPlanServiceImpl extends ServiceImpl<ProdPlanMapper, ProdPlan> i
                 throw new IllegalArgumentException("不支持的文件格式,请使用.xlsx或.xls文件");
             }
             //第一张表
-            Sheet sheet = workbook.getSheetAt(0);
-            //第二行为表头
-            Row headerRow = sheet.getRow(1);
-            if (headerRow == null) {
-                throw new RuntimeException("未找到表头行");
-            }
-            //获取表头对应的列索引
-            Map<String, Integer> columnIndices = parseHeadRow(headerRow);
-            //获取表头中的日期列
-            Map<String, Integer> dateColumnIndices = parseDateColumn(columnIndices);
-            //从第三行开始读取数据
-            for (int i = 2; i <= sheet.getLastRowNum(); i++) {
-                Row row = sheet.getRow(i);
-                if(row==null) break;
-                if (row.getCell(columnIndices.get("Line"))==null) break;
-
-                parseDataRow(row, columnIndices, dateColumnIndices, delayProducts);
+            for(int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum ++) {
+                Sheet sheet = workbook.getSheetAt(sheetNum);
+                //第二行为表头
+                Row headerRow = sheet.getRow(1);
+                if (headerRow == null) {
+                    throw new RuntimeException("未找到表头行");
+                }
+                //获取表头对应的列索引
+                Map<String, Integer> columnIndices = parseHeadRow(headerRow);
+                //获取表头中的日期列
+                Map<String, Integer> dateColumnIndices = parseDateColumn(columnIndices);
+                //从第三行开始读取数据
+                for (int i = 2; i <= sheet.getLastRowNum(); i++) {
+                    Row row = sheet.getRow(i);
+                    if(row==null) break;
+                    if (row.getCell(columnIndices.get("Line"))==null) break;
+
+                    parseDataRow(row, columnIndices, dateColumnIndices, delayProducts);
+                }
             }
 
+
             List<RegionRule> regionRuleList = regionRuleService.list();
             Map<String, RegionRule> regionRules = regionRuleList.stream().collect(Collectors.toMap(RegionRule::getSuffix, i -> i));
             List<DestRule> destRuleList = destRuleService.list();
@@ -136,7 +139,6 @@ public class ProdPlanServiceImpl extends ServiceImpl<ProdPlanMapper, ProdPlan> i
                 LocalDate dueDate = getDueDate(delayProduct.getBucket(), schecule);
                 if(dueDate==null) continue;
                 delayProduct.setDueDate(dueDate);
-
                 // 计算计划生产日期超出要求完成日期天数
                 if (delayProduct.getPst() != null && delayProduct.getDueDate() != null) {
                     int days = delayProduct.getDueDate().until(delayProduct.getPst()).getDays();
@@ -155,10 +157,23 @@ public class ProdPlanServiceImpl extends ServiceImpl<ProdPlanMapper, ProdPlan> i
                         delayProduct.setRemark(newRemark);
                     }
                 });
+
+                //没匹配到,且商品为延期商品,置部门及原因为OTHER
+                if(delayProduct.getRemark() == null && delayProduct.getOverdueDays() != null && delayProduct.getOverdueDays() > 0) {
+                    delayProduct.setRemark("OTHER");
+                    delayProduct.setComment1("OTHER");
+                    delayProduct.setComment2("OTHER");
+                }
             }
 
             List<DelayProduct> overDueList = delayProducts.stream().filter(i -> i.getRemark()!=null && i.getOverdueDays()!=null && i.getOverdueDays() > 0).collect(Collectors.toList());
 
+            try {
+                remarkDelayProduct(overDueList, filePath);
+            }catch (Exception e) {
+                log.error("源文件标注出错", e);
+            }
+
             return overDueList;
         } catch (Exception e) {
             e.printStackTrace();
@@ -166,6 +181,54 @@ public class ProdPlanServiceImpl extends ServiceImpl<ProdPlanMapper, ProdPlan> i
         }
     }
 
+    private void remarkDelayProduct(List<DelayProduct> overDueList,  String filePath) throws Exception{
+        Map<String, String> delayProduct = overDueList.stream().collect(Collectors.toMap(DelayProduct::getDemandId, DelayProduct::getDemandId));
+        try (FileInputStream fis = new FileInputStream(filePath)) {
+            Workbook workbook;
+            // 根据文件扩展名选择合适的工作簿类型
+            if (filePath.endsWith(".xlsx")) {
+                workbook = new XSSFWorkbook(fis);
+            } else if (filePath.endsWith(".xls")) {
+                workbook = new HSSFWorkbook(fis);
+            } else {
+                throw new IllegalArgumentException("不支持的文件格式,请使用.xlsx或.xls文件");
+            }
+
+            for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
+                Sheet sheet = workbook.getSheetAt(sheetNum);
+                //第二行为表头
+                Row headerRow = sheet.getRow(1);
+                if (headerRow == null) {
+                    throw new RuntimeException("未找到表头行");
+                }
+                //获取表头对应的列索引
+                Map<String, Integer> columnIndices = parseHeadRow(headerRow);
+
+                CellStyle cellStyle =  workbook.createCellStyle();
+                cellStyle.setFillForegroundColor(IndexedColors.RED1.getIndex());
+                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+                //从第三行开始读取数据
+                for (int i = 2; i <= sheet.getLastRowNum(); i++) {
+                    Row row = sheet.getRow(i);
+                    if (row == null) break;
+                    if (row.getCell(columnIndices.get("Line")) == null) break;
+                    String demandId = getCellValueAsString(row.getCell(columnIndices.get("Demand ID")));
+                    if (delayProduct.containsKey(demandId)) {
+                        for(int cellNum = 0; cellNum < row.getLastCellNum(); cellNum ++) {
+                            Cell cell = row.getCell(cellNum);
+                            cell.setCellStyle(cellStyle);
+                        }
+                    }
+                }
+            }
+            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
+                workbook.write(outputStream);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     /**
      * 获取表头对应的列索引
      * @param headerRow