From 0648f44cccfc652085373740670f4ef8dd632386 Mon Sep 17 00:00:00 2001 From: aike <17730485278@139.com> Date: Thu, 24 Apr 2025 18:18:36 +0800 Subject: [PATCH] =?UTF-8?q?demand:=20=E6=B5=B7=E8=BF=90=E8=B4=A6=E5=8D=95b?= =?UTF-8?q?ug=E4=BF=AE=E6=94=B9=20committer:=20heyu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excelCellStrategy/CellStyleStrategy.java | 31 ++++- .../impl/EmisSettleBillServiceImpl.java | 116 +++++++++++++----- 2 files changed, 110 insertions(+), 37 deletions(-) diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/excelCellStrategy/CellStyleStrategy.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/excelCellStrategy/CellStyleStrategy.java index 4155bcab4..bd5bc9ac3 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/excelCellStrategy/CellStyleStrategy.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/excelCellStrategy/CellStyleStrategy.java @@ -4,7 +4,7 @@ import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; -import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; +import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy; import org.apache.poi.ss.usermodel.*; /** @@ -13,8 +13,7 @@ import org.apache.poi.ss.usermodel.*; * @Description CellStyleStrategy * @createTime 2025年04月09日 */ -public class CellStyleStrategy extends HorizontalCellStyleStrategy { - +public class CellStyleStrategy extends AbstractVerticalCellStyleStrategy { private final WriteCellStyle headWriteCellStyle; @@ -74,8 +73,34 @@ public class CellStyleStrategy extends HorizontalCellStyleStrategy { WriteCellStyle.merge(this.headWriteCellStyle, cellData.getOrCreateStyle()); } + @Override protected void setContentCellStyle(CellWriteHandlerContext context) { + // 设置行高 context.getRow().setHeight((short) (23 * 20)); + + if (stopProcessing(context)) { + return; + } + + // 获取或创建单元格样式 + WriteCellData cellData = context.getFirstCellData(); + WriteCellStyle writeCellStyle = cellData.getOrCreateStyle(); + + // 设置边框样式 + writeCellStyle.setBorderTop(BorderStyle.THIN); + writeCellStyle.setBorderRight(BorderStyle.THIN); + writeCellStyle.setBorderBottom(BorderStyle.THIN); + writeCellStyle.setBorderLeft(BorderStyle.THIN); + + // 设置对齐方式 + writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); + + // 设置字体 + WriteFont writeFont = new WriteFont(); + writeFont.setFontName("微软雅黑"); + writeFont.setFontHeightInPoints((short) 8); + writeCellStyle.setWriteFont(writeFont); } private void setRowStyle(CellWriteHandlerContext context, short height, short fontSize, short fontColor, boolean isBold, short backgroundColor) { diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisSettleBillServiceImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisSettleBillServiceImpl.java index 06b900175..146f75bad 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisSettleBillServiceImpl.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisSettleBillServiceImpl.java @@ -40,6 +40,7 @@ import com.xdadan.erp.emis.service.EmisBaseService; import com.xdadan.erp.emis.service.excelCellStrategy.CellStyleStrategy; import com.xdadan.erp.emis.service.excelCellStrategy.CustomCellWriteHandler; import com.xdadan.erp.emis.utils.WaybillHelper; +import jodd.util.StringUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -833,7 +834,6 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS return 1; } - @Override public void exportSeaWayBill(HttpServletResponse response, EmisSettleBill emisSettleBill) { try { @@ -845,20 +845,27 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS EmisSettleBill bill = emisBaseService.getSettleBillBySettleBillNo(emisSettleBill.getSettleBillNo()); // 子账单 - List list = emisBaseService.selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo()); + List list = emisBaseService + .selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo()); // 运单号 List billCodeList = list.stream() .map(EmisSettleSubBill::getBillCode) .collect(Collectors.toList()); - if (org.springframework.util.CollectionUtils.isEmpty(list) || org.springframework.util.CollectionUtils.isEmpty(billCodeList)) { + if (CollectionUtils.isEmpty(list) + || CollectionUtils.isEmpty(billCodeList)) { return; } + // 结算对象 + String settlement = Optional.ofNullable(list.get(0)).map(e -> StringUtil.isNotBlank(e.getCustName()) ? e.getCustName() : e.getCustomerName()).orElse(""); + // 根据运单号查询运单详情 - List emisWaybills = emisSettleSubBillFeeitemMapper.selectEmisWaybillListByBillCodeList(billCodeList); - Map emisWaybillMap = emisWaybills.stream().collect(Collectors.toMap(EmisWaybillAndFeeitem::getBillCode, i -> i)); - if (MapUtil.isEmpty(emisWaybillMap) || org.springframework.util.CollectionUtils.isEmpty(emisWaybills)) { + List emisWaybills = emisSettleSubBillFeeitemMapper + .selectEmisWaybillListByBillCodeList(billCodeList); + Map emisWaybillMap = emisWaybills.stream() + .collect(Collectors.toMap(EmisWaybillAndFeeitem::getBillCode, i -> i)); + if (MapUtil.isEmpty(emisWaybillMap) || CollectionUtils.isEmpty(emisWaybills)) { return; } log.info("select emisWaybills is success, emisWaybills,{}", emisWaybills); @@ -866,7 +873,24 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS // 运单费用条目 List emisWaybillFeeitemList = new ArrayList<>(); emisWaybills.forEach(emisWaybill -> { - if (Objects.nonNull(emisWaybill) && CollectionUtil.isNotEmpty(emisWaybill.getSettleSubBillFeeitemList())) { + // 一口价运单特殊处理 + EmisWaybill waybill = null; + String billCode = emisWaybill.getBillCode(); + if(StringUtil.isNotBlank(billCode)) { + waybill = emisWaybillMapper.selectEmisWaybillByBillCode(billCode); + } + if (waybill != null && "3".equals(waybill.getCalcFeeType()) && CollectionUtil.isNotEmpty(emisWaybill.getSettleSubBillFeeitemList())) { + EmisSettleSubBillFeeitem emisSettleSubBillFeeitem = emisWaybill.getSettleSubBillFeeitemList().get(0); + emisSettleSubBillFeeitem.setFeeName("运输费"); + emisSettleSubBillFeeitem.setCurrency("CNY"); + List bills = list.stream().filter(i -> billCode.equals(i.getBillCode())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(bills) && (bills.get(0) != null && (bills.get(0).getBillFee() != null))) { + emisSettleSubBillFeeitem.setBillFee(bills.get(0).getBillFee()); + } + } + + if (Objects.nonNull(emisWaybill) + && CollectionUtil.isNotEmpty(emisWaybill.getSettleSubBillFeeitemList())) { emisWaybillFeeitemList.addAll(emisWaybill.getSettleSubBillFeeitemList()); } }); @@ -878,10 +902,10 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS String year = DateUtils.parseDateToStr("yyyy", list.get(0).getBillDate()); String month = DateUtils.parseDateToStr("M", list.get(0).getBillDate()); -// String filePath = "D:\\log\\stduent_01.xlsx"; + // String filePath = "D:\\log\\stduent_01.xlsx"; ServletOutputStream outputStream = response.getOutputStream(); - //内容的策略 + // 内容的策略 WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); @@ -893,21 +917,26 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLSX).build(); List> contentHeader = this.buildHeader(); - HorizontalCellStyleStrategy horizontalCellStyleStrategy = - new HorizontalCellStyleStrategy(null, contentWriteCellStyle); + HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(null, + contentWriteCellStyle); // 构建大表头 WriteSheet writeSheet1 = EasyExcel.writerSheet("sheet").needHead(Boolean.FALSE) .registerWriteHandler(horizontalCellStyleStrategy) .registerWriteHandler(new CellStyleStrategy(new WriteCellStyle())).build(); - List> header1 = contentHeader.stream().map(item -> Collections.singletonList("探路供应链(上海)")).collect(Collectors.toList()); + List> header1 = contentHeader.stream().map(item -> Collections.singletonList("探路供应链(上海)")) + .collect(Collectors.toList()); WriteTable t1 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).head(header1).build(); excelWriter.write((Collection) null, writeSheet1, t1); - List> header2 = contentHeader.stream().map(item -> Collections.singletonList(year + "年" + month + "月海运费确认书")).collect(Collectors.toList()); + List> header2 = contentHeader.stream() + .map(item -> Collections.singletonList(year + "年" + month + "月海运费确认书")) + .collect(Collectors.toList()); WriteTable t2 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).head(header2).build(); excelWriter.write((Collection) null, writeSheet1, t2); - List> header3 = contentHeader.stream().map(item -> Collections.singletonList("结算对象:探路供应链公司")).collect(Collectors.toList()); + List> header3 = contentHeader.stream() + .map(item -> Collections.singletonList("结算对象:" + settlement)) + .collect(Collectors.toList()); header3.set(9, Collections.singletonList("出账日期:" + date)); header3.set(10, Collections.singletonList("出账日期:" + date)); header3.set(11, Collections.singletonList("出账日期:" + date)); @@ -926,12 +955,14 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS List settleSubBillFeeitemList = emisWaybill.getSettleSubBillFeeitemList(); calculateSubtotal(settleSubBillFeeitemList, subtotalMap); settleSubBillFeeitemList.forEach(item -> { - result.add(Lists.newArrayList(emisWaybill.getLadingBillCode(), emisWaybill.getBillCode(), emisWaybill.getCompanyCode(), emisWaybill.getDeparturePort(), - emisWaybill.getDestinationPort(), emisWaybill.getBoxQuantity(), String.valueOf(emisWaybill.getParcelQty()), + result.add(Lists.newArrayList(emisWaybill.getLadingBillCode(), emisWaybill.getBillCode(), + emisWaybill.getCompanyCode(), emisWaybill.getDeparturePort(), + emisWaybill.getDestinationPort(), emisWaybill.getBoxQuantity(), + String.valueOf(emisWaybill.getParcelQty()), emisWaybill.getBillWeight().stripTrailingZeros().toPlainString(), emisWaybill.getTotalVolume().stripTrailingZeros().toPlainString(), - item.getFeeName(), item.getCurrency(), item.getBillFee().stripTrailingZeros().toPlainString() - )); + item.getFeeName(), item.getCurrency(), + item.getBillFee().stripTrailingZeros().toPlainString())); }); // 小表头 WriteSheet sheet = EasyExcel.writerSheet("sheet") @@ -941,29 +972,43 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS excelWriter.write((Collection) null, sheet, writeTable0); // 行数据 - WriteTable writeTable1 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(contentHeader).build(); + WriteTable writeTable1 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(contentHeader) + .build(); excelWriter.write(result, writeSheet1, writeTable1); log.info("Building the first row is success"); // 小计 - List> nameHeader2 = contentHeader.stream().map(item -> Collections.singletonList("小计: USD金额:" + subtotalMap.get("usdSubtotal") + " CNY金额:" + subtotalMap.get("cnySubtotal"))).collect(Collectors.toList()); - WriteTable writeTable2 = EasyExcel.writerTable(num + 2).needHead(Boolean.TRUE).head(nameHeader2).build(); - excelWriter.write((Collection) null, writeSheet1, writeTable2); + if (MapUtil.isNotEmpty(subtotalMap)) { + List> nameHeader2 = contentHeader.stream() + .map(item -> Collections.singletonList("小计: USD金额:" + subtotalMap.get("usdSubtotal") + + " CNY金额:" + subtotalMap.get("cnySubtotal"))) + .collect(Collectors.toList()); + WriteTable writeTable2 = EasyExcel.writerTable(num + 2).needHead(Boolean.TRUE).head(nameHeader2) + .build(); + excelWriter.write((Collection) null, writeSheet1, writeTable2); + } num = num + 2; } // 总计 - List> nameHeader3 = contentHeader.stream().map(item -> Collections.singletonList("总计: USD金额:" + textMap.get("usdTotal") + " CNY金额:" + textMap.get("cnyTotal"))).collect(Collectors.toList()); - WriteTable writeTable3 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(nameHeader3).build(); - excelWriter.write((Collection) null, writeSheet1, writeTable3); + if (MapUtil.isNotEmpty(textMap)) { + List> nameHeader3 = contentHeader.stream() + .map(item -> Collections.singletonList( + "总计: USD金额:" + textMap.get("usdTotal") + " CNY金额:" + textMap.get("cnyTotal"))) + .collect(Collectors.toList()); + WriteTable writeTable3 = EasyExcel.writerTable(num + 1).needHead(Boolean.TRUE).head(nameHeader3) + .build(); + excelWriter.write((Collection) null, writeSheet1, writeTable3); + } log.info("exportSeaWayBill is success"); // 设置响应头信息 String fileName = bill.getSettleBillName() + "_海运账单.xlsx"; - response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); + response.setHeader("Content-Disposition", + "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); response.setContentType("application/octet-stream"); - //关流 + // 关流 excelWriter.finish(); IOUtils.closeQuietly(outputStream); } catch (Exception e) { @@ -988,10 +1033,10 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS return header; } - - - private static void calculateTotal(List emisWaybillFeeitemList, Map textMap) { + private static void calculateTotal(List emisWaybillFeeitemList, + Map textMap) { Map> emisWaybillFeeitemMap = emisWaybillFeeitemList.stream() + .filter(i -> StringUtil.isNotBlank(i.getCurrency())) .collect(Collectors.groupingBy(EmisSettleSubBillFeeitem::getCurrency)); if (MapUtil.isNotEmpty(emisWaybillFeeitemMap)) { @@ -1012,8 +1057,10 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS } } - private static void calculateSubtotal(List emisWaybillFeeitemList, Map textMap) { + private static void calculateSubtotal(List emisWaybillFeeitemList, + Map textMap) { Map> emisWaybillFeeitemMap = emisWaybillFeeitemList.stream() + .filter(i -> StringUtil.isNotBlank(i.getCurrency())) .collect(Collectors.groupingBy(EmisSettleSubBillFeeitem::getCurrency)); if (MapUtil.isNotEmpty(emisWaybillFeeitemMap)) { @@ -1034,7 +1081,8 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS } } - private static void countA(String key, Map textMap, String usdSubtotal, String count, String cnySubtotal) { + private static void countA(String key, Map textMap, String usdSubtotal, String count, + String cnySubtotal) { if ("USD".equals(key)) { textMap.put(usdSubtotal, count); } else { @@ -1042,7 +1090,8 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS } } - private static void countB(String key, Map textMap, String usdSubtotal, String count, String cnySubtotal) { + private static void countB(String key, Map textMap, String usdSubtotal, String count, + String cnySubtotal) { if ("USD".equals(key)) { textMap.put(usdSubtotal, count); textMap.put(cnySubtotal, "0"); @@ -1052,7 +1101,6 @@ public class EmisSettleBillServiceImpl extends EmisBaseService implements IEmisS } } - /** * 修改结算总账单 *