提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
写了一段比较弯弯绕绕的代码。让gpt帮我做了一下修正:
1.原始代码
for (SalesInvoiceLine invoiceLine : salesInvoiceLines) {
// 遍历每一条明细表是否存在类似(*美容护肤品*折扣行数15(7.231%))的商品名称,如果有,进行加权评价法的处理
if (invoiceLine.getItemName().contains("折扣行数")) {
hasDiscount = true;
break;
}
}
if (hasDiscount) {
BigDecimal sumCount = new BigDecimal(0);
SalesInvoiceLine discountLineInvoiceLine = null;
for (SalesInvoiceLine salesInvoiceLine : salesInvoiceLines) {
if (!salesInvoiceLine.getItemName().contains("折扣行数")) {
// 不是整体折扣行的就数量累加
sumCount = sumCount.add(new BigDecimal(salesInvoiceLine.getQty()));
} else {
// 找到整体折扣行
discountLineInvoiceLine = salesInvoiceLine;
}
}
BigDecimal amountWithTaxDecimal = null;
BigDecimal taxAmountDecimal = null;
BigDecimal amountWithoutTaxDecimal = null;
amountWithTaxDecimal = new BigDecimal(discountLineInvoiceLine.getAmountWithTax()).divide(sumCount, 2, RoundingMode.HALF_UP);
taxAmountDecimal = new BigDecimal(discountLineInvoiceLine.getTaxAmount()).divide(sumCount, 2, RoundingMode.HALF_UP);
amountWithoutTaxDecimal = new BigDecimal(discountLineInvoiceLine.getAmountWithoutTax()).divide(sumCount, 2, RoundingMode.HALF_UP);
for (SalesInvoiceLine invoiceLine : salesInvoiceLines) {
// 从头来一遍,只遍历被折扣抵消的行数
if (!invoiceLine.getItemName().contains("折扣行数")) {
BigDecimal qtyDecimal = new BigDecimal(invoiceLine.getQty());
// 金额抵扣
BigDecimal discountAmountWithoutTax = new BigDecimal(invoiceLine.getAmountWithoutTax()).add(amountWithoutTaxDecimal.multiply(qtyDecimal));
invoiceLine.setAmountWithoutTax(discountAmountWithoutTax.toString());
// 税额抵扣
BigDecimal discountTaxAmount = new BigDecimal(invoiceLine.getTaxAmount()).add(taxAmountDecimal.multiply(qtyDecimal));
invoiceLine.setTaxAmount(discountTaxAmount.toString());
//价税合计抵扣
BigDecimal discountAmountWithTax = new BigDecimal(invoiceLine.getAmountWithTax()).add(amountWithTaxDecimal.multiply(qtyDecimal));
invoiceLine.setAmountWithTax(discountAmountWithTax.toString());
}
}
// 最后将整体折扣行删除,不进行入库操作
salesInvoiceLines.removeIf(invoiceLine -> invoiceLine.getItemName().contains("折扣行数"));
}
2.改良代码
代码如下(示例):
Optional<SalesInvoiceLine> discountLineOptional = salesInvoiceLines.stream()
.filter(invoiceLine -> invoiceLine.getItemName().contains("折扣行数"))
.findFirst();
if (discountLineOptional.isPresent()) {
SalesInvoiceLine discountLine = discountLineOptional.get();
BigDecimal sumCount = salesInvoiceLines.stream()
.filter(invoiceLine -> !invoiceLine.getItemName().contains("折扣行数"))
.map(invoiceLine -> new BigDecimal(invoiceLine.getQty()))
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal amountWithTaxDecimal = new BigDecimal(discountLine.getAmountWithTax()).divide(sumCount, 2, RoundingMode.HALF_UP);
BigDecimal taxAmountDecimal = new BigDecimal(discountLine.getTaxAmount()).divide(sumCount, 2, RoundingMode.HALF_UP);
BigDecimal amountWithoutTaxDecimal = new BigDecimal(discountLine.getAmountWithoutTax()).divide(sumCount, 2, RoundingMode.HALF_UP);
salesInvoiceLines.parallelStream()
.filter(invoiceLine -> !invoiceLine.getItemName().contains("折扣行数"))
.forEach(invoiceLine -> {
BigDecimal qtyDecimal = new BigDecimal(invoiceLine.getQty());
BigDecimal discountAmountWithoutTax = new BigDecimal(invoiceLine.getAmountWithoutTax()).add(amountWithoutTaxDecimal.multiply(qtyDecimal));
invoiceLine.setAmountWithoutTax(discountAmountWithoutTax.toString());
BigDecimal discountTaxAmount = new BigDecimal(invoiceLine.getTaxAmount()).add(taxAmountDecimal.multiply(qtyDecimal));
invoiceLine.setTaxAmount(discountTaxAmount.toString());
BigDecimal discountAmountWithTax = new BigDecimal(invoiceLine.getAmountWithTax()).add(amountWithTaxDecimal.multiply(qtyDecimal));
invoiceLine.setAmountWithTax(discountAmountWithTax.toString());
});
salesInvoiceLines.removeIf(invoiceLine -> invoiceLine.getItemName().contains("折扣行数"));
}
该处使用的lambda表达式
总结
提示:使用steam流的方式以及lambda表达式会让代码变得更加简洁美观: