@Autowired
protected Validator validator;
@Override
public String importData(List
//TODO:修改T为实际数据对象
if (CollectionUtils.isEmpty(list)){
throw new ServiceException("导入数据不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for (GatewayData data : list) {
try {
//检查要添加的数据是否已经在库中存在
GatewayData mac = gatewayDataMapper.selectGatewayDataByMac(data.getMac());
if (Objects.isNull(mac)){
BeanValidators.validateWithException(validator,data);
data.setCreateBy(operName);
gatewayDataMapper.insertGatewayData(data);
successNum++;
successMsg.append("<br/>" + successNum + "、mac: " + data.getMac() + " 导入成功");
}else if (updateSupport){
BeanValidators.validateWithException(validator,data);
data.setCreateBy(operName);
gatewayDataMapper.updateGatewayData(data);
successNum++;
successMsg.append("<br/>" + successNum + "、mac: " + data.getMac() + " 更新成功");
}else {
failureNum++;
failureMsg.append("<br/>" + successNum + "、mac: " + data.getMac() + " 已存在");
}
} catch (Exception e) {
failureNum++;
String msg = "<br/>" + failureNum + "、账号 " + data.getMac() + " 导入失败:";
failureMsg.append(msg + e.getMessage());
logger.error(msg,e);
}
}
if (failureNum > 0 ){
failureMsg.insert(0,"很抱歉,导入失败,共 "+failureNum+" 条数据格式不正确,错误如下:");
throw new ServiceException(failureMsg.toString());
}else {
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
//TODO:此处可在导入完成后,做相应处理
return successMsg.toString();
}
标签:getMac,successMsg,框架,若依,successNum,导入,failureNum,data From: https://www.cnblogs.com/jundong2177/p/18225104