From 028c03fd7379bf95e8ad37f04fec16b28269baea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E5=93=B2=E5=A5=87?= <13840175730@139.com> Date: Mon, 28 Oct 2024 17:24:02 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=B0=83=E6=95=B4=E8=AE=A2=E8=B4=A7=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E5=AF=BC=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/dsic/gj_erp/ExcelMergedCellRead.java | 43 ++++++ .../com/dsic/gj_erp/bean/jhgk/DmDhqdp.java | 30 ++-- .../service/jhgk/impl/DmDhqdpServiceImpl.java | 141 +++++++++++++++--- src/main/resources/application.properties | 4 +- 4 files changed, 187 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/dsic/gj_erp/ExcelMergedCellRead.java diff --git a/src/main/java/com/dsic/gj_erp/ExcelMergedCellRead.java b/src/main/java/com/dsic/gj_erp/ExcelMergedCellRead.java new file mode 100644 index 0000000..d161898 --- /dev/null +++ b/src/main/java/com/dsic/gj_erp/ExcelMergedCellRead.java @@ -0,0 +1,43 @@ +package com.dsic.gj_erp; + +import cn.hutool.core.util.StrUtil; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; + +public class ExcelMergedCellRead { + public static void main(String[] args) throws IOException { + String fileName="E:\\订货清单.xls"; + FileInputStream fileInputStream = new FileInputStream(fileName); + Workbook workbook = WorkbookFactory.create(fileInputStream); + Sheet sheet = workbook.getSheetAt(0); + + // 获取合并单元格的信息 + List mergedRegions = sheet.getMergedRegions(); + + for (int i = 0; i < mergedRegions.size(); i++) { + CellRangeAddress cellRangeAddress = mergedRegions.get(i); + // 获取合并单元格的起始行号、结束行号、起始列号、结束列号 + int firstRow = cellRangeAddress.getFirstRow(); + int lastRow = cellRangeAddress.getLastRow(); + int firstCol = cellRangeAddress.getFirstColumn(); + int lastCol = cellRangeAddress.getLastColumn(); + + // 获取合并单元格的值 + Row row = sheet.getRow(firstRow); + Cell cell = row.getCell(firstCol); + try { + String mergedCellValue = cell.getStringCellValue(); + + System.out.println(StrUtil.format("合并单元格的值: {},位于第{}个", mergedCellValue,i)); + }catch (Exception ignored){} + + } + + fileInputStream.close(); + } + +} diff --git a/src/main/java/com/dsic/gj_erp/bean/jhgk/DmDhqdp.java b/src/main/java/com/dsic/gj_erp/bean/jhgk/DmDhqdp.java index 11c2c1d..8cb6630 100644 --- a/src/main/java/com/dsic/gj_erp/bean/jhgk/DmDhqdp.java +++ b/src/main/java/com/dsic/gj_erp/bean/jhgk/DmDhqdp.java @@ -10,6 +10,7 @@ import lombok.Data; import lombok.EqualsAndHashCode; import java.io.Serializable; +import java.math.BigDecimal; /** *

@@ -42,40 +43,40 @@ public class DmDhqdp implements Serializable { private String wpxh; @ApiModelProperty(value = "厚度mm") - private Double thickness; + private BigDecimal thickness; @ApiModelProperty(value = "宽度mm") - private Double width; + private BigDecimal width; @ApiModelProperty(value = "长度mm") - private Double length; + private BigDecimal length; @ApiModelProperty(value = "重量吨/张") - private Double weight; + private BigDecimal weight; @ApiModelProperty(value = "设计数量张") - private Double sjsl; + private BigDecimal sjsl; @ApiModelProperty(value = "裕量张") - private Double yl; + private BigDecimal yl; @ApiModelProperty(value = "总数量张") - private Double zsl; + private BigDecimal zsl; @ApiModelProperty(value = "总重量吨") - private Double zzl; + private BigDecimal zzl; @ApiModelProperty(value = "利库数量张") - private Double lksl; + private BigDecimal lksl; @ApiModelProperty(value = "利库重量吨") - private Double lkzl; + private BigDecimal lkzl; @ApiModelProperty(value = "实订数量张") - private Double sdzs; + private BigDecimal sdzs; @ApiModelProperty(value = "实订重量吨") - private Double sdzl; + private BigDecimal sdzl; @ApiModelProperty(value = "备注") private String bz; @@ -88,5 +89,10 @@ public class DmDhqdp implements Serializable { @TableId(value = "id", type = IdType.ASSIGN_UUID) private String id; + private String plzlsl;//批量组立数量 + private String kdzlsl;//跨道组立数量 + private String ptybsl;//平铁用板数量 + @TableField(exist = false) + private String xh;//序号列用于去除合计行 } diff --git a/src/main/java/com/dsic/gj_erp/service/jhgk/impl/DmDhqdpServiceImpl.java b/src/main/java/com/dsic/gj_erp/service/jhgk/impl/DmDhqdpServiceImpl.java index c7dd34f..720adbe 100644 --- a/src/main/java/com/dsic/gj_erp/service/jhgk/impl/DmDhqdpServiceImpl.java +++ b/src/main/java/com/dsic/gj_erp/service/jhgk/impl/DmDhqdpServiceImpl.java @@ -2,6 +2,8 @@ package com.dsic.gj_erp.service.jhgk.impl; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ObjUtil; +import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import com.alibaba.fastjson.JSONObject; @@ -12,18 +14,23 @@ import com.dsic.gj_erp.mapper.jhgk.DmDhqdpMapper; import com.dsic.gj_erp.service.jhgk.DmDhqdpService; import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** *

- * 服务实现类 + * 服务实现类 *

* * @author xn @@ -36,26 +43,124 @@ public class DmDhqdpServiceImpl extends ServiceImpl impl @Override public List getList(JSONObject json) { - QueryWrapper queryWrapper=new QueryWrapper<>(); - if(StringUtils.isNotEmpty(json.getString("dcCh"))){ - queryWrapper.eq("DC_CH",json.getString("dcCh")); + QueryWrapper queryWrapper = new QueryWrapper<>(); + if (StringUtils.isNotEmpty(json.getString("dcCh"))) { + queryWrapper.eq("DC_CH", json.getString("dcCh")); } - if(StringUtils.isNotEmpty(json.getString("Zt"))){ - queryWrapper.eq("ZT",json.getString("Zt")); + if (StringUtils.isNotEmpty(json.getString("Zt"))) { + queryWrapper.eq("ZT", json.getString("Zt")); } - List dmDhqdps=dmDhqdpMapper.selectList(queryWrapper); - return dmDhqdps; + queryWrapper.orderByAsc("wpxh"); + return dmDhqdpMapper.selectList(queryWrapper); } @Override + @Transactional public List save(List dmDhqdps) { saveBatch(dmDhqdps); return dmDhqdps; } @Override + @Transactional public List upload(MultipartFile file, HttpServletRequest req) throws IOException { - ExcelReader reader= ExcelUtil.getReader(file.getInputStream()); + Workbook workbook = WorkbookFactory.create(file.getInputStream()); + Sheet sheet = workbook.getSheetAt(0); + List mergedRegions = sheet.getMergedRegions(); + if (mergedRegions.size() > 74) { + String ddh = null; + String dcch = null; + String qc = null; + CellRangeAddress ddhAddr = mergedRegions.get(68); + if (ddhAddr != null) { + ddh = this.getMergedData(sheet, ddhAddr); + } + CellRangeAddress chAddr = mergedRegions.get(74); + if (chAddr != null) { + dcch = this.getMergedData(sheet, chAddr); + } + + CellRangeAddress qcAddr = mergedRegions.get(75); + if (qcAddr != null) { + qc = this.getMergedData(sheet, qcAddr); + qc=StrUtil.subBetween(qc, "(", ")"); + } + + if (StrUtil.isAllNotEmpty(dcch, ddh)) { + List list = new ArrayList<>(); + List dmDhqdps = this.readExcel(file, dcch, ddh, qc, 1, req); + List dmDhqdps1 = this.readExcel(file, dcch, ddh, qc, 2, req); + if (ObjUtil.isNotEmpty(dmDhqdps)) { + list.addAll(dmDhqdps); + } + if (ObjUtil.isNotEmpty(dmDhqdps1)) { + list.addAll(dmDhqdps1); + } + this.saveBatch(list.stream().filter(item->StrUtil.isNotEmpty(item.getDcCh())).collect(Collectors.toList())); + return list; + } + } + + return null; + } + + private String getMergedData(Sheet sheet, CellRangeAddress addr) { + int firstRow = addr.getFirstRow(); + int firstCol = addr.getFirstColumn(); + Row row = sheet.getRow(firstRow); + Cell cell = row.getCell(firstCol); + try { + return cell.getStringCellValue(); + } catch (Exception ignored) { + } + return null; + } + + @Transactional + public List readExcel(MultipartFile file, String dcch, String ddh, String qc, int sheet, HttpServletRequest req) throws IOException { + ExcelReader reader = ExcelUtil.getReader(file.getInputStream()); + Map alias = Maps.newLinkedHashMap(); + alias.put("序号\nNo.", "xh"); + alias.put("材质\nMATERIAL", "wpxh"); + alias.put("厚度\r\nTHICK.\nmm", "thickness"); + alias.put("宽度\r\nWIDTH\nmm", "width"); + alias.put("长度\r\nLENGTH\nmm", "length"); + alias.put("重量\r\n吨/张\nWEIGHT\nTON/PIECE\r", "weight"); + alias.put("设计数量\n张\nDESIGN\nPIECE", "sjsl"); + alias.put("裕量\n张\nMARGIN\nPIECE", "yl"); + alias.put("总数量\n张\nTOTAL\nPIECE", "zsl"); + alias.put("总重量\n吨\nTOTAL\nTON ", "zzl"); + alias.put("利库数量\n张\nSTORE\nPIECE", "lksl"); + alias.put("利库重量\n吨\nSTORE\nTON", "lkzl"); + alias.put("实订数量\n张\nORDER\nPIECE", "sdzs"); + alias.put("实订重量\n吨\nORDER\nTON", "sdzl"); + alias.put("备注\nNOTE", "bz"); + alias.put("批量组立数量", "plzlsl"); + alias.put("跨道组立数量", "kdzlsl"); + alias.put("平铁用板数量", "ptybsl"); + + reader.setHeaderAlias(alias); + reader.setSheet(sheet); + List dmDhqdps = reader.read(1, 2, DmDhqdp.class); + String day = DateUtil.format(DateUtil.date(), "yyyy/MM/dd"); + String user = (String) req.getAttribute("yhms"); + this.remove(new QueryWrapper().eq("Dc_ch", dcch) + .eq("qdh", ddh) + .eq("qc", qc)); + dmDhqdps.stream().filter(item->StrUtil.isNotEmpty(item.getXh())&&!item.getXh().contains("合计")) + .forEach(l -> { + l.setDcCh(dcch); + l.setQdh(ddh); + l.setQc(qc); + l.setDrr(user); + l.setDrrq(day); + }); + return dmDhqdps; + } + + @Transactional + public List upload1(MultipartFile file, HttpServletRequest req) throws IOException { + ExcelReader reader = ExcelUtil.getReader(file.getInputStream()); Map alias = Maps.newLinkedHashMap(); alias.put("船号", "dcCh"); alias.put("订货清单号", "qdh"); @@ -111,15 +216,15 @@ public class DmDhqdpServiceImpl extends ServiceImpl impl "TON", "sdzl"); alias.put("备注\n" + "NOTE", "bz"); - // alias.put("状态", "zt"); + // alias.put("状态", "zt"); reader.setHeaderAlias(alias); - List dmDhqdps= reader.readAll(DmDhqdp.class); + List dmDhqdps = reader.readAll(DmDhqdp.class); String day = DateUtil.format(DateUtil.date(), "yyyy/MM/dd"); String user = (String) req.getAttribute("yhms"); - dmDhqdps.forEach(l->{ - this.remove(new QueryWrapper().eq("Dc_ch",l.getDcCh()) - .eq("qdh",l.getQdh()) - .eq("qc",l.getQc())); + dmDhqdps.forEach(l -> { + this.remove(new QueryWrapper().eq("Dc_ch", l.getDcCh()) + .eq("qdh", l.getQdh()) + .eq("qc", l.getQc())); l.setDrr(user); l.setDrrq(day); }); @@ -127,6 +232,7 @@ public class DmDhqdpServiceImpl extends ServiceImpl impl } @Override + @Transactional public void sh(List dmDhqdps) { dmDhqdps.forEach(dmZrcjh -> { dmZrcjh.setZt("2"); @@ -135,10 +241,11 @@ public class DmDhqdpServiceImpl extends ServiceImpl impl } @Override + @Transactional public void tj(List dmXbjhs) { dmXbjhs.forEach(dmZrcjh -> { - String zt=dmZrcjh.getZt(); - String setZt=zt.equals("1")?"0":"1"; + String zt = dmZrcjh.getZt(); + String setZt = zt.equals("1") ? "0" : "1"; dmZrcjh.setZt(setZt); }); updateBatchById(dmXbjhs); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index db0265a..8ad0969 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -22,8 +22,8 @@ spring.servlet.multipart.enabled = true server.port=8081 spring.datasource.master.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver -spring.datasource.master.url=jdbc:sqlserver://192.127.0.3:1433;DatabaseName=GJ_ERP_java -spring.datasource.master.jdbc-url=jdbc:sqlserver://192.127.0.3:1433;DatabaseName=GJ_ERP_java +spring.datasource.master.url=jdbc:sqlserver://192.127.0.3:1433;DatabaseName=GJ_ERP +spring.datasource.master.jdbc-url=jdbc:sqlserver://192.127.0.3:1433;DatabaseName=GJ_ERP spring.datasource.master.username=sa spring.datasource.master.password=gjerp spring.datasource.master.initialize=true