Commit 8f3c397b7fd9be67b3e64a5876a1963ef236b91d

Authored by 房远帅
2 parents edd2bef4 97969950

Merge branch 'master_chujiang' into master_warehouse

@@ -26,6 +26,29 @@ import java.util.stream.Collectors; @@ -26,6 +26,29 @@ import java.util.stream.Collectors;
26 @Slf4j 26 @Slf4j
27 public class CustomerShortImportListener extends ExcelImportListener<CustomerShortModel> { 27 public class CustomerShortImportListener extends ExcelImportListener<CustomerShortModel> {
28 28
  29 + /**
  30 + * 失败行数(校验失败 + 保存失败)
  31 + */
  32 + private int failCount = 0;
  33 +
  34 + /**
  35 + * 出错行不中断整体导入:基类 invoke 先将该行加入待保存列表再校验,
  36 + * 此处把出错行从待保存列表移除,记录错误提示后继续下一行
  37 + */
  38 + @Override
  39 + public void onException(Exception exception, AnalysisContext context) {
  40 + if (!datas.isEmpty()) {
  41 + datas.remove(datas.size() - 1);
  42 + }
  43 + failCount++;
  44 + String msg = exception.getMessage();
  45 + if ((msg == null || !msg.startsWith("第")) && context != null
  46 + && context.readRowHolder() != null) {
  47 + msg = "第" + (context.readRowHolder().getRowIndex() + 1) + "行," + msg;
  48 + }
  49 + addTipMsg(msg);
  50 + }
  51 +
29 @Override 52 @Override
30 protected void doInvoke(CustomerShortModel data, AnalysisContext context) { 53 protected void doInvoke(CustomerShortModel data, AnalysisContext context) {
31 // 客户名称 54 // 客户名称
@@ -102,17 +125,21 @@ public class CustomerShortImportListener extends ExcelImportListener<CustomerSho @@ -102,17 +125,21 @@ public class CustomerShortImportListener extends ExcelImportListener<CustomerSho
102 @Override 125 @Override
103 protected void afterAllAnalysed(AnalysisContext context) { 126 protected void afterAllAnalysed(AnalysisContext context) {
104 CustomerShortService customerShortService = ApplicationUtil.getBean(CustomerShortService.class); 127 CustomerShortService customerShortService = ApplicationUtil.getBean(CustomerShortService.class);
105 - for (int i = 0; i < datas.size(); i++) {  
106 - CustomerShortModel data = datas.get(i); 128 + int successCount = 0;
  129 + for (CustomerShortModel data : datas) {
107 CustomerShort customerShort = JsonUtil.parseObject(JsonUtil.toJsonString(data), CustomerShort.class); 130 CustomerShort customerShort = JsonUtil.parseObject(JsonUtil.toJsonString(data), CustomerShort.class);
108 try { 131 try {
109 customerShortService.save(customerShort); 132 customerShortService.save(customerShort);
  133 + successCount++;
110 } catch (Exception e) { 134 } catch (Exception e) {
111 - throw new DefaultClientException(  
112 - "第" + (i + 1) + "行新增失败,失败原因:" + e.getMessage()); 135 + // 保存失败的行记录错误继续,不中断整体导入
  136 + failCount++;
  137 + addTipMsg("客户[" + data.getCustomerName() + "]新增失败,失败原因:" + e.getMessage());
113 } 138 }
114 - this.setSuccessProcess(i); 139 + this.setSuccessProcess(successCount + failCount);
115 } 140 }
  141 + // 导入结果汇总
  142 + addTipMsg("导入完成:成功" + successCount + "条,失败" + failCount + "条");
116 } 143 }
117 144
118 @Override 145 @Override