diff --git a/src/main/java/com/syjiaer/clinic/server/service/inventory/InventoryPurchaseService.java b/src/main/java/com/syjiaer/clinic/server/service/inventory/InventoryPurchaseService.java index 958cbd3..a8234ae 100644 --- a/src/main/java/com/syjiaer/clinic/server/service/inventory/InventoryPurchaseService.java +++ b/src/main/java/com/syjiaer/clinic/server/service/inventory/InventoryPurchaseService.java @@ -20,7 +20,6 @@ import com.syjiaer.clinic.server.mapper.inventory.*; import com.syjiaer.clinic.server.mapper.manager.ManagerUserMapper; import com.syjiaer.clinic.server.service.BaseService; import com.syjiaer.clinic.server.service.goods.GoodsService; -import lombok.val; import org.apache.poi.ss.usermodel.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -30,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional; import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.*; @@ -536,4 +536,136 @@ public class InventoryPurchaseService extends BaseService { cell.setCellValue(value.toString()); } } + + public Map fromExcel(String excelPath) { + Map map=new HashMap<>(); + List list=new ArrayList(); + //表格中头部单独的4行 + Map tagMap=new HashMap<>(); + + try { + FileInputStream fis = new FileInputStream(excelPath); + + Workbook workbook = WorkbookFactory.create(fis); + Sheet sheet = workbook.getSheetAt(0); + + //采购日期 + tagMap.put("purchase_date",getCellValueAsString(sheet.getRow(1).getCell(1))); + //货单号 + tagMap.put("shipping_code",getCellValueAsString(sheet.getRow(2).getCell(1))); + //发票号 + tagMap.put("invoice_code",getCellValueAsString(sheet.getRow(3).getCell(1))); + //备注 + tagMap.put("remark",getCellValueAsString(sheet.getRow(4).getCell(1))); + + map.put("tag",tagMap); + + int i=6; + while (true){ + Row row = sheet.getRow(i); + double id_double; + try{ + id_double= row.getCell(0).getNumericCellValue(); + }catch (Exception e) + { + break; + } + //四舍五入为int + int id = (int) Math.round(id_double); + Map rowMap=doRow(id,row); + if(rowMap!=null){ + list.add(rowMap); + } + i++; + } + map.put("list",list); + return map; + }catch (Exception e) { + logger.warning("解析文件异常:"+e.getMessage()); + e.printStackTrace(); + throw new MessageException("解析文件异常"); + } + } + private Map doRow(int id,Row row) throws ParseException { + Map map=new HashMap<>(); + Goods goods = goodsMapper.selectById(id); + if(goods==null){ + return null; + } + map.put("id",id); + map.put("goods",goods); + Map infoMap=new HashMap<>(); + map.put("info",infoMap); + double whole_number_double=0; + + try { + whole_number_double=row.getCell(7).getNumericCellValue(); + }catch (Exception e){ + return null; + } + int whole_number = (int) Math.round(whole_number_double); + infoMap.put("whole_number",whole_number); + infoMap.put("purchase_unit_price",getCellValueAsString(row.getCell(8))); + infoMap.put("production_batch_code",getCellValueAsString(row.getCell(9))); + + infoMap.put("production_date",transformDate(getCellValueAsString(row.getCell(10)))); + infoMap.put("expiry_date",transformDate(getCellValueAsString(row.getCell(11)))); + + + return map; + + } + + + public String transformDate(String inputDate) throws ParseException { + if (Objects.equals(inputDate, "")) + { + return ""; + } + + // 定义输入格式(注意 CST 时区和 Locale.US) + SimpleDateFormat inputFormat = new SimpleDateFormat( + "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US + ); + + // 解析输入日期 + Date date = inputFormat.parse(inputDate); + + // 定义输出格式 + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); + + return outputFormat.format(date); + } + + private static String getCellValueAsString(Cell cell) { + if (cell == null) { + return ""; + } + + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue().trim(); + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + // 如果是日期格式,格式化为字符串 + return cell.getDateCellValue().toString(); + } else { + // 转换为整数或保留小数 + double numericValue = cell.getNumericCellValue(); + if (numericValue == (long) numericValue) { + return String.format("%d", (long) numericValue); + } else { + return String.valueOf(numericValue); + } + } + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case FORMULA: + return cell.getCellFormula(); + case BLANK: + return ""; + default: + return ""; + } + } }