demand: TMS系统 - 客户账单管理 - 业务账单确认- 导出excel电子表-解决空指针数据导致导出异常的问题 tms-运输管理-货物组批次添加校验未接单的不允许组批次

This commit is contained in:
aike 2026-02-06 14:29:10 +08:00
parent 1a80335c71
commit 9aa4009e01
2 changed files with 176 additions and 114 deletions

View File

@ -714,63 +714,104 @@ public class EmisSettleBillController extends BaseController
{ {
try { try {
EmisSettleBill bill=emisBaseService.getSettleBillBySettleBillNo(emisSettleBill.getSettleBillNo()); // 参数校验
if (emisSettleBill == null || StringUtils.isEmpty(emisSettleBill.getSettleBillNo())) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
EmisSettleBill bill = emisBaseService.getSettleBillBySettleBillNo(emisSettleBill.getSettleBillNo());
// 账单不存在校验
if (bill == null) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 获取子账单数据 // 获取子账单数据
List<EmisSettleSubBill> list = emisBaseService.selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo()); List<EmisSettleSubBill> list = emisBaseService.selectSubBillListBySettleBillNo(emisSettleBill.getSettleBillNo());
// 子账单列表为空校验
if (CollectionUtils.isEmpty(list)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 转换子账单为 MAP // 转换子账单为 MAP
Map<String, Object> textMap = MapUtils.newHashMap(); Map<String, Object> textMap = MapUtils.newHashMap();
textMap.put("title", bill.getSettleBillName()); textMap.put("title", bill.getSettleBillName() != null ? bill.getSettleBillName() : "");
textMap.put("total", bill.getRecMoney().stripTrailingZeros().toPlainString()); // 处理可能为null的金额
BigDecimal recMoney = bill.getRecMoney();
textMap.put("total", recMoney != null ? recMoney.stripTrailingZeros().toPlainString() : "0");
String tplName="tpl/bill_tpl_simple.xlsx"; String tplName = "tpl/bill_tpl_simple.xlsx";
// 是否使用复复杂模版,如果有清关费用和其他费用都需要使用复杂模版 // 是否使用复复杂模版,如果有清关费用和其他费用都需要使用复杂模版
boolean isNeedComplexTpl=false; boolean isNeedComplexTpl = false;
List<Map<String, Object>> listMap = new ArrayList<>();
for (EmisSettleSubBill billItem : list) {
// 跳过waybillDetail为null的项
if (billItem == null || billItem.getWaybillDetail() == null) {
continue;
}
List<Map<String, Object>> listMap=new ArrayList<>();
for(EmisSettleSubBill billItem:list){
Map<String, Object> voMap = new HashMap<>(); Map<String, Object> voMap = new HashMap<>();
voMap.put("sendDate", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,billItem.getWaybillDetail().getSendDate())) ; // 安全处理日期
voMap.put("billCode",billItem.getBillCode()); Date sendDate = billItem.getWaybillDetail().getSendDate();
voMap.put("fjr",billItem.getWaybillDetail().getSendName()+"--"+billItem.getWaybillDetail().getReceiveName()); voMap.put("sendDate", sendDate != null ? DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, sendDate) : "");
voMap.put("kjlx",billItem.getWaybillDetail().getSendSiteName()+"--"+billItem.getWaybillDetail().getDispatchUnderlingSiteName());
voMap.put("billCode", billItem.getBillCode() != null ? billItem.getBillCode() : "");
String company=billItem.getWaybillDetail().getCustName(); // 安全处理字符串拼接
if( String sendName = billItem.getWaybillDetail().getSendName() != null ? billItem.getWaybillDetail().getSendName() : "";
"1".equals(billItem.getWaybillDetail().getPaymentType()) || String receiveName = billItem.getWaybillDetail().getReceiveName() != null ? billItem.getWaybillDetail().getReceiveName() : "";
"3".equals(billItem.getWaybillDetail().getPaymentType()) || voMap.put("fjr", sendName + "--" + receiveName);
"5".equals(billItem.getWaybillDetail().getPaymentType())
){ String sendSiteName = billItem.getWaybillDetail().getSendSiteName() != null ? billItem.getWaybillDetail().getSendSiteName() : "";
company=billItem.getWaybillDetail().getSendCompany(); String dispatchUnderlingSiteName = billItem.getWaybillDetail().getDispatchUnderlingSiteName() != null ? billItem.getWaybillDetail().getDispatchUnderlingSiteName() : "";
voMap.put("kjlx", sendSiteName + "--" + dispatchUnderlingSiteName);
String company = billItem.getWaybillDetail().getCustName() != null ? billItem.getWaybillDetail().getCustName() : "";
String paymentType = billItem.getWaybillDetail().getPaymentType();
if ("1".equals(paymentType) || "3".equals(paymentType) || "5".equals(paymentType)) {
String sendCompany = billItem.getWaybillDetail().getSendCompany();
company = sendCompany != null ? sendCompany : "";
} }
voMap.put("company",company); voMap.put("company", company);
voMap.put("paymentType", DictUtils.getDictLabel("emis_payment_type",billItem.getWaybillDetail().getPaymentType())); voMap.put("paymentType", DictUtils.getDictLabel("emis_payment_type", paymentType));
voMap.put("productTypeName", billItem.getWaybillDetail().getProductTypeName());
voMap.put("parcelQty", billItem.getWaybillDetail().getParcelQty().toString()); String productTypeName = billItem.getWaybillDetail().getProductTypeName();
voMap.put("billWeight", billItem.getWaybillDetail().getBillWeight().stripTrailingZeros().toPlainString()); voMap.put("productTypeName", productTypeName != null ? productTypeName : "");
voMap.put("settlementWeight", billItem.getWaybillDetail().getSettlementWeight().stripTrailingZeros().toPlainString());
voMap.put("totalVolume", billItem.getWaybillDetail().getTotalVolume().stripTrailingZeros().toPlainString());
voMap.put("freight", billItem.getWaybillDetail().getFreight().stripTrailingZeros().toPlainString());
String feeRemark=""; // 安全处理Integer转String
if(!StringUtils.isEmpty(billItem.getWaybillDetail().getFeeRemark())){ Integer parcelQty = billItem.getWaybillDetail().getParcelQty();
feeRemark=billItem.getWaybillDetail().getFeeRemark(); voMap.put("parcelQty", parcelQty != null ? parcelQty.toString() : "0");
// 安全处理BigDecimal
BigDecimal billWeight = billItem.getWaybillDetail().getBillWeight();
voMap.put("billWeight", billWeight != null ? billWeight.stripTrailingZeros().toPlainString() : "0");
BigDecimal settlementWeight = billItem.getWaybillDetail().getSettlementWeight();
voMap.put("settlementWeight", settlementWeight != null ? settlementWeight.stripTrailingZeros().toPlainString() : "0");
BigDecimal totalVolume = billItem.getWaybillDetail().getTotalVolume();
voMap.put("totalVolume", totalVolume != null ? totalVolume.stripTrailingZeros().toPlainString() : "0");
BigDecimal freight = billItem.getWaybillDetail().getFreight();
voMap.put("freight", freight != null ? freight.stripTrailingZeros().toPlainString() : "0");
String feeRemark = "";
String feeRemarkValue = billItem.getWaybillDetail().getFeeRemark();
if (!StringUtils.isEmpty(feeRemarkValue)) {
feeRemark = feeRemarkValue;
} }
String waybillRemark=""; String waybillRemark = "";
if(!StringUtils.isEmpty(billItem.getWaybillDetail().getRemark())){ String remarkValue = billItem.getWaybillDetail().getRemark();
waybillRemark=billItem.getWaybillDetail().getRemark(); if (!StringUtils.isEmpty(remarkValue)) {
waybillRemark = remarkValue;
} }
// if(!StringUtils.isEmpty(billItem.getWaybillDetail().getFeeRemark())){
// feeRemark=feeRemark+" "+billItem.getWaybillDetail().getFeeRemark();
// }
BigDecimal otherFee = BigDecimal.ZERO; BigDecimal otherFee = BigDecimal.ZERO;
BigDecimal transFee = BigDecimal.ZERO; BigDecimal transFee = BigDecimal.ZERO;
BigDecimal cebFee = BigDecimal.ZERO; BigDecimal cebFee = BigDecimal.ZERO;
@ -780,150 +821,167 @@ public class EmisSettleBillController extends BaseController
String declareFeeStr = ""; String declareFeeStr = "";
String otherFeeStr = ""; String otherFeeStr = "";
String feeItemRemark=""; String feeItemRemark = "";
//计算分项数据 // 计算分项数据
List<EmisSettleSubBillFeeitem> feeItemList=billItem.getFeeitemList(); List<EmisSettleSubBillFeeitem> feeItemList = billItem.getFeeitemList();
if( !CollectionUtils.isEmpty(feeItemList)){ if (!CollectionUtils.isEmpty(feeItemList)) {
for (EmisSettleSubBillFeeitem feeitem : feeItemList) { for (EmisSettleSubBillFeeitem feeitem : feeItemList) {
if (feeitem == null) {
// 所有币种全部统一换算成RMB continue;
BigDecimal rmbBillFee=feeitem.getBillFee();
// 币种
String currencyStr="";
if(StringUtils.isEmpty(feeitem.getCurrency())){
feeitem.setCurrency("CNY");
} }
if(!"CNY".equals(feeitem.getCurrency())){ // 所有币种全部统一换算成RMB
currencyStr=""+feeitem.getCurrency()+""; BigDecimal billFee = feeitem.getBillFee();
// 非人民币需要换算成人民币 if (billFee == null) {
EmisExchangeRate emisExchangeRate=emisExchangeRateService.getValidExchangeRate(currencyStr,"CNY",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,billItem.getWaybillDetail().getSendDate())); billFee = BigDecimal.ZERO;
double rate=0.0D; }
if(emisExchangeRate!=null){ BigDecimal rmbBillFee = billFee;
rate=Double.valueOf(emisExchangeRate.getRate());
rmbBillFee=rmbBillFee.multiply(BigDecimal.valueOf(rate)); // 币种
} String currencyStr = "";
String currency = feeitem.getCurrency();
if (StringUtils.isEmpty(currency)) {
feeitem.setCurrency("CNY");
currency = "CNY";
}
if (!"CNY".equals(currency)) {
currencyStr = currency;
// 非人民币需要换算成人民币
if (sendDate != null) {
String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, sendDate);
EmisExchangeRate emisExchangeRate = emisExchangeRateService.getValidExchangeRate(currencyStr, "CNY", dateStr);
if (emisExchangeRate != null && emisExchangeRate.getRate() != null) {
try {
double rate = Double.parseDouble(emisExchangeRate.getRate());
rmbBillFee = rmbBillFee.multiply(BigDecimal.valueOf(rate));
} catch (NumberFormatException e) {
// 汇率转换失败,使用原值
logger.warn("汇率转换失败,币种: {}, 汇率: {}", currencyStr, emisExchangeRate.getRate());
}
}
}
} }
// 费用细分 // 费用细分
if ("1001".equals(feeitem.getFeeCode())) { String feeCode = feeitem.getFeeCode();
if ("1001".equals(feeCode)) {
transFee = transFee.add(rmbBillFee); transFee = transFee.add(rmbBillFee);
// transFeeStr= feeitem.getBillFee().stripTrailingZeros().toPlainString()+currencyStr;
} }
// 报关费 // 报关费
else if ("2002".equals(feeitem.getFeeCode())) { else if ("2002".equals(feeCode)) {
cebFee = cebFee.add(rmbBillFee); cebFee = cebFee.add(rmbBillFee);
// cebFeeStr= feeitem.getBillFee().stripTrailingZeros().toPlainString()+currencyStr;
} }
// 清关费 // 清关费
else if ("2003".equals(feeitem.getFeeCode())) { else if ("2003".equals(feeCode)) {
declareFee = declareFee.add(rmbBillFee); declareFee = declareFee.add(rmbBillFee);
// declareFeeStr= feeitem.getBillFee().stripTrailingZeros().toPlainString()+currencyStr;
//
} else { } else {
otherFee = otherFee.add(rmbBillFee); otherFee = otherFee.add(rmbBillFee);
// otherFeeStr= otherFeeStr+""+feeitem.getFeeName()+":"+feeitem.getBillFee().stripTrailingZeros().toPlainString()+currencyStr+",";
//
} }
if (cebFee.compareTo(BigDecimal.ZERO) > 0
if( || declareFee.compareTo(BigDecimal.ZERO) > 0
cebFee.compareTo(BigDecimal.ZERO)>0 || otherFee.compareTo(BigDecimal.ZERO) > 0) {
|| declareFee.compareTo(BigDecimal.ZERO)>0 isNeedComplexTpl = true;
|| otherFee.compareTo(BigDecimal.ZERO)>0
){
isNeedComplexTpl=true;
} }
if(!StringUtils.isEmpty(feeitem.getRemark())){ String feeItemRemarkValue = feeitem.getRemark();
if(!feeitem.getRemark().contains("w")){ if (!StringUtils.isEmpty(feeItemRemarkValue)) {
feeItemRemark=feeItemRemark+"|"+feeitem.getFeeName()+":"+feeitem.getBillFee().stripTrailingZeros().toPlainString()+currencyStr+"(费用说明:"+feeitem.getRemark()+")"; if (!feeItemRemarkValue.contains("w")) {
String feeName = feeitem.getFeeName() != null ? feeitem.getFeeName() : "";
String billFeeStr = billFee.stripTrailingZeros().toPlainString();
feeItemRemark = feeItemRemark + "|" + feeName + ":" + billFeeStr + currencyStr + "(费用说明:" + feeItemRemarkValue + ")";
} }
} }
} }
transFeeStr= transFee.stripTrailingZeros().toPlainString(); transFeeStr = transFee.stripTrailingZeros().toPlainString();
cebFeeStr= cebFee.stripTrailingZeros().toPlainString(); cebFeeStr = cebFee.stripTrailingZeros().toPlainString();
declareFeeStr= declareFee.stripTrailingZeros().toPlainString(); declareFeeStr = declareFee.stripTrailingZeros().toPlainString();
otherFeeStr= otherFee.stripTrailingZeros().toPlainString(); otherFeeStr = otherFee.stripTrailingZeros().toPlainString();
}else{ } else {
transFeeStr=billItem.getWaybillDetail().getFreight().stripTrailingZeros().toPlainString(); // 如果没有费用项列表,使用运单的运费
if (freight != null) {
transFeeStr = freight.stripTrailingZeros().toPlainString();
} else {
transFeeStr = "0";
}
} }
if(StringUtils.isEmpty(transFeeStr)){ if (StringUtils.isEmpty(transFeeStr)) {
transFeeStr="0"; transFeeStr = "0";
} }
if(StringUtils.isEmpty(cebFeeStr)){ if (StringUtils.isEmpty(cebFeeStr)) {
cebFeeStr="0"; cebFeeStr = "0";
} }
if(StringUtils.isEmpty(declareFeeStr)){ if (StringUtils.isEmpty(declareFeeStr)) {
declareFeeStr="0"; declareFeeStr = "0";
} }
if(StringUtils.isEmpty(otherFeeStr)){ if (StringUtils.isEmpty(otherFeeStr)) {
otherFeeStr="0"; otherFeeStr = "0";
} }
voMap.put("transFee",transFeeStr); voMap.put("transFee", transFeeStr);
voMap.put("cebFee",cebFeeStr); voMap.put("cebFee", cebFeeStr);
voMap.put("declareFee",declareFeeStr); voMap.put("declareFee", declareFeeStr);
otherFeeStr=StringUtils.removeEnd(otherFeeStr,","); otherFeeStr = StringUtils.removeEnd(otherFeeStr, ",");
voMap.put("otherFee",otherFeeStr); voMap.put("otherFee", otherFeeStr);
if(!StringUtils.isEmpty(feeItemRemark)){ if (!StringUtils.isEmpty(feeItemRemark)) {
feeItemRemark="费用项:"+StringUtils.removeEnd(feeItemRemark,"|"); feeItemRemark = "费用项:" + StringUtils.removeEnd(feeItemRemark, "|");
} }
// feeRemark=feeRemark+","+feeItemRemark;
feeRemark=StringUtils.removeEnd(feeRemark,",")+""; feeRemark = StringUtils.removeEnd(feeRemark, ",") + "";
voMap.put("feeRemark", feeRemark+" "+waybillRemark); voMap.put("feeRemark", feeRemark + " " + waybillRemark);
listMap.add(voMap); listMap.add(voMap);
} }
if (isNeedComplexTpl) {
if(isNeedComplexTpl) {
tplName = "tpl/bill_tpl.xlsx"; tplName = "tpl/bill_tpl.xlsx";
}else{ } else {
tplName = "tpl/bill_tpl_simple.xlsx"; tplName = "tpl/bill_tpl_simple.xlsx";
} }
ServletOutputStream outputStream = response.getOutputStream(); ServletOutputStream outputStream = response.getOutputStream();
//账单主数据填充 // 账单主数据填充
InputStream inputStream = ResourceUtil.getResourceObj(tplName).getStream(); InputStream inputStream = ResourceUtil.getResourceObj(tplName).getStream();
// 设置创建行的方式 // 设置创建行的方式
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
// FillConfig fillConfig2 = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).excelType(ExcelTypeEnum.XLSX).build(); ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).excelType(ExcelTypeEnum.XLSX).build();
// 写入到sheet // 写入到sheet
WriteSheet sheet0 = EasyExcel.writerSheet(0).build(); WriteSheet sheet0 = EasyExcel.writerSheet(0).build();
excelWriter.fill(textMap, sheet0); excelWriter.fill(textMap, sheet0);
// 填充 // 填充
logger.error("====>listMap===>{}",JSON.toJSONString(listMap)); logger.error("====>listMap===>{}", JSON.toJSONString(listMap));
excelWriter.fill(listMap, fillConfig, sheet0); excelWriter.fill(listMap, fillConfig, sheet0);
// 设置响应头信息 // 设置响应头信息
String fileName = bill.getSettleBillName()+".xlsx"; String billName = bill.getSettleBillName();
String fileName = (billName != null ? billName : "账单") + ".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"); response.setContentType("application/octet-stream");
//关流 // 关流
excelWriter.finish(); excelWriter.finish();
IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(outputStream);
} catch (IOException e) { } catch (IOException e) {
logger.error("导出账单失败", e);
e.printStackTrace(); e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (Exception e) {
logger.error("导出账单异常", e);
e.printStackTrace();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} }
} }

View File

@ -172,6 +172,10 @@ public class EmisTmsSiteBatchDtlServiceImpl implements IEmisTmsSiteBatchDtlServi
return null; return null;
} }
if(!"1".equals(emisWaybill.getOrderStatus())){
throw new EmisBizError(EmisBizErrorType.FAIL, "未接单");
}
// 创建返回结果对象 // 创建返回结果对象
EmisTmsSiteBatchDtl result = new EmisTmsSiteBatchDtl(); EmisTmsSiteBatchDtl result = new EmisTmsSiteBatchDtl();
result.setWaybillDetail(emisWaybill); result.setWaybillDetail(emisWaybill);