✨ 汇总账单导出保留收货时间

This commit is contained in:
caojia
2026-11-20 11:59:15 +08:00
parent 7af0feb188
commit 213e0b24bb
2 changed files with 48 additions and 10 deletions
@@ -26,6 +26,8 @@ import com.bms.receivable.domain.ArExportTemplate;
import com.bms.receivable.mapper.ArBillDetailFeeMapper;
import com.bms.receivable.mapper.ArBillDetailMapper;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -37,6 +39,36 @@ class ArBillWorkbookWriterTest {
@TempDir
Path temporary;
@Test
void summaryDetailsKeepReceiptDateTimeAsExcelDateForNormalAndHighlightedRows() throws Exception {
ArBillDetail normal = detail(1, "P1", "1.00");
normal.setReceiveTime(Timestamp.valueOf("2026-10-01 21:32:51"));
ArBillDetail failed = detail(2, "P1", "1.00");
failed.setReceiveTime(Timestamp.valueOf("2026-10-02 00:01:02"));
failed.setBillingFailStatus(2);
ArBillDetail missing = detail(3, "P1", "1.00");
missing.setReceiveTime(null);
Path target = temporary.resolve("summary-receipt-times.xlsx");
summaryWriter(List.of(normal, failed, missing)).write(bill(3, "3.00", "CNY"), summaryTemplate(), target);
try (XSSFWorkbook workbook = new XSSFWorkbook(target.toFile())) {
XSSFSheet sheet = workbook.getSheetAt(1);
var normalCell = sheet.getRow(4).getCell(1);
var failedCell = sheet.getRow(5).getCell(1);
assertEquals(CellType.NUMERIC, normalCell.getCellType());
assertEquals(CellType.NUMERIC, failedCell.getCellType());
assertTrue(DateUtil.isCellDateFormatted(normalCell));
assertTrue(DateUtil.isCellDateFormatted(failedCell));
assertEquals("yyyy/mm/dd", normalCell.getCellStyle().getDataFormatString());
assertEquals("2026/10/01", new DataFormatter().formatCellValue(normalCell));
assertEquals(LocalDateTime.of(2026, 10, 1, 21, 32, 51), normalCell.getLocalDateTimeCellValue());
assertEquals("2026/10/02", new DataFormatter().formatCellValue(failedCell));
assertEquals(LocalDateTime.of(2026, 10, 2, 0, 1, 2), failedCell.getLocalDateTimeCellValue());
assertEquals(IndexedColors.YELLOW.getIndex(), failedCell.getCellStyle().getFillForegroundColor());
assertEquals("", sheet.getRow(6).getCell(1).getStringCellValue());
}
}
@Test
void summaryAndDetailsReconcileSnapshotAmountsAndKeepFreightFormulaAndBlankFields() throws Exception {
ArBill bill = bill(3, "13.20", "CNY");
@@ -231,9 +263,9 @@ class ArBillWorkbookWriterTest {
assertEquals("20261001-20261030", summary.getRow(3).getCell(3).getStringCellValue());
assertEquals("20261001-20261030", summary.getRow(4).getCell(3).getStringCellValue());
assertEquals("20261001-20261030", freight.getRow(2).getCell(18).getStringCellValue());
assertEquals("2026-10-20", freight.getRow(4).getCell(1).getStringCellValue());
assertEquals("2026-10-01", freight.getRow(5).getCell(1).getStringCellValue());
assertEquals("2026-10-30", freight.getRow(6).getCell(1).getStringCellValue());
assertEquals(LocalDateTime.of(2026, 10, 20, 0, 0), freight.getRow(4).getCell(1).getLocalDateTimeCellValue());
assertEquals(LocalDateTime.of(2026, 10, 1, 0, 0), freight.getRow(5).getCell(1).getLocalDateTimeCellValue());
assertEquals(LocalDateTime.of(2026, 10, 30, 0, 0), freight.getRow(6).getCell(1).getLocalDateTimeCellValue());
}
verify(writer.detailMapper, times(1)).selectReceiveTimeRangeByBillId(7L);
assertEquals(Date.valueOf("2026-09-29"), bill.getBillPeriodStart());
@@ -9,7 +9,6 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -27,6 +26,7 @@ import com.bms.receivable.domain.ArBillDetailFee;
import com.bms.receivable.domain.ArExportTemplate;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
@@ -159,10 +159,11 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
XSSFCell cell = detail.getRow(4).getCell(column, XSSFRow.MissingCellPolicy.CREATE_NULL_AS_BLANK);
boolean identifier = column == 0 || column == 2 || column == 3 || column == 6;
boolean money = column == 10 || column == 13 || column == 14 || column == 15 || column == 16;
if (!identifier && !money) continue;
boolean receiveDate = column == 1;
if (!identifier && !money && !receiveDate) continue;
XSSFCellStyle style = workbook.createCellStyle();
style.cloneStyleFrom(cell.getCellStyle());
String format = identifier ? "@" : "0.00";
String format = receiveDate ? "yyyy/mm/dd" : identifier ? "@" : "0.00";
style.setDataFormat(workbook.createDataFormat().getFormat(format));
cell.setCellStyle(style);
}
@@ -194,7 +195,7 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
failed[column] = Short.toUnsignedInt(style.getIndex());
}
return new DetailStyles(normal, failed, workbook.getSheetAt(1).getRow(4).getHeightInPoints());
return new DetailStyles(normal, failed, workbook.getSheetAt(1).getRow(4).getHeightInPoints(), workbook.isDate1904());
}
/** 产品行之间不画网格线;最后一行沿用模板底边,组成完整外框。 */
@@ -260,11 +261,16 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
}
private String detailRow(int rowNumber, ArBill bill, ArBillDetail detail,
DetailAmounts amounts, DetailStyles styles) {
DetailAmounts amounts, DetailStyles styles) throws IOException {
String failure = exportBillingFailDescription(detail.getBillingFailStatus());
Double receiveTime = detail.getReceiveTime() == null ? null
: DateUtil.getExcelDate(detail.getReceiveTime(), styles.use1904());
if (receiveTime != null && !DateUtil.isValidExcelDate(receiveTime)) {
throw new IOException("账单明细收货时间超出 Excel 日期范围,请检查账单");
}
Object[] values = {
bill.getCustomerCode(),
detail.getReceiveTime() == null ? "" : new SimpleDateFormat("yyyy-MM-dd").format(detail.getReceiveTime()),
receiveTime,
detail.getTransferNo(), detail.getTrackingNo(),
StringUtils.isEmpty(detail.getProductName()) ? detail.getProductCode() : detail.getProductName(),
StringUtils.isEmpty(detail.getCountryName()) ? detail.getCountryCode() : detail.getCountryName(),
@@ -412,7 +418,7 @@ public class SummaryArBillWorkbookWriter extends ArBillWorkbookSupport {
private record SheetXml(String prefix, Map<Integer, String> rows, String suffix) { }
private record DetailResult(int count, int lastRow, BigDecimal amount) { }
private record SummaryStyles(int[] normal, int[] last) { }
private record DetailStyles(int[] normal, int[] failed, float rowHeight) { }
private record DetailStyles(int[] normal, int[] failed, float rowHeight, boolean use1904) { }
private record DetailAmounts(BigDecimal freight, BigDecimal processing, BigDecimal remote,
BigDecimal surcharge, BigDecimal romania, BigDecimal total) { }