public class ImportCustomerExcelListener extends AnalysisEventListener<CpCustomer> {
private static final Logger LOGGER = LoggerFactory.getLogger(ImportCustomerExcelListener.class);
private static final int BATCH_COUNT = 5;
List<CpCustomer> list = new ArrayList<>();
private static List<ImportCustomerExcelModel> errList = new ArrayList<>();
public static List<ImportCustomerExcelModel> failList(){
List<ImportCustomerExcelModel> listerror = new ArrayList<>();
listerror.addAll(errList);
errList.clear();
return listerror;
}
Map<Integer, String> headMap = new HashMap<>();
private CpCustomerService cpCustomerService;
public ImportCustomerExcelListener() {
}
public ImportCustomerExcelListener(CpCustomerService cpCustomerService) {
this.cpCustomerService = cpCustomerService;
}
@Override
public void invoke(CpCustomer data, AnalysisContext context) {
updateCustomerName(data);
}
public void updateCustomerName(CpCustomer data) {
if (!Validator.isMobile(data.getPhoneNum())){
failDataOperation(data, "手机号格式错误");
throw new ExcelAnalysisStopException(data.getCustomerName()+"的手机号格式错误,错误手机号:"+data.getPhoneNum());
}
CpCustomer cpCustomer = cpCustomerService.getOne(Wrappers.<CpCustomer>query().eq("phone_num", data.getPhoneNum()));
Optional.ofNullable(cpCustomer).ifPresent(customer -> {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
String newName = "";
String oldName = "";
if (StrUtil.isNotBlank(data.getCustomerName())) {
newName = data.getCustomerName().replace(" ", "");
}
if (StrUtil.isNotBlank(cpCustomer.getCustomerName())) {
oldName = cpCustomer.getCustomerName().replace(" ", "");
}
if (!newName.equals(oldName)) {
data.setId(cpCustomer.getId());
list.add(data);
}
LOGGER.info("需要修改的数据:{}", list);
});
if (list.size() >= BATCH_COUNT) {
updateData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
if (CollUtil.isNotEmpty(list)) {
updateData();
LOGGER.info("所有数据解析完成!");
}else {
LOGGER.info("没有需要修改的数据");
}
}
private void saveData() {
LOGGER.info("{}条数据,开始存储数据库!", list.size());
cpCustomerService.saveBatch(list);
LOGGER.info("存储数据库成功!");
}
@Override
public void onException(Exception exception, AnalysisContext context) {
LOGGER.error("解析失败:{}", exception.getMessage());
int row = 0;
int col = 0;
String title = "";
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
col = excelDataConvertException.getColumnIndex();
row = excelDataConvertException.getRowIndex();
LOGGER.error("第{}行,第{}列解析异常,数据为:{}", row, col , excelDataConvertException.getCellData());
title = this.headMap.get(col);
LOGGER.info("出错标题列名称:{}", title);
}
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
this.headMap = headMap;
}
public void updateData() {
LOGGER.info("{}条数据,开始修改数据库信息!", list.size());
try {
cpCustomerService.updateBatchById(list);
}catch (Exception e){
LOGGER.error("以下数据修改过程中出现错误,请重新导入:{}", list);
list.forEach(data -> failDataOperation(data, "修改过程中出现错误,请重新导入!"));
throw new ExcelAnalysisStopException("以下数据修改过程中出现错误,请重新导入:"+list);
}
LOGGER.info("修改数据库成功!");
}
private void failDataOperation(CpCustomer data, String message) {
ImportCustomerExcelModel model = new ImportCustomerExcelModel();
BeanUtil.copyProperties(data, model);
model.setMessage(message);
errList.add(model);
}
}