这篇文章将为大家详细讲解有关大数据中如何实现导入,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
@PostConstruct
public void init(){
format = new SimpleDateFormat(CollectChargeEnum.DATEFORMAT.getName());
headers = new String[]{"停车场名称", "车牌号", "支付方式", "账号类型", "交易号", "支付金额", "支付时间", "平帐状态"};
exportFields = new String[]{"stoppingPlaceName", "vehicleNo", "payTypeName", "chargeType", "transactionNo", "payCharge", "payTime", "flatAccountStatusName"};
setMethodName = new String[]{"setStoppingPlaceId", "setVehicleNo", "setPayType", "setChargeType", "setTransactionNo", "setPayCharge", "setPayTime", "setFlatAccountStatus"};
intFileIndex = Arrays.asList(new Integer[]{2,7});
}
@Override
public boolean importExcel(MultipartFile importFile) throws BizException{
List<FlatAccoutEntity> listFlatAccountInfo = new ArrayList<FlatAccoutEntity>(100);
// 解析Excel importFile文件, 0 sheet页
List<Map<String,String>> readExcel = ExcelUtil.readExcel(importFile, 0);
// 获取当期时间
// 获取用户信息
net.sf.json.JSONObject userInfo = CurrentUserInfoUtil.getCurrentUserInfo(redisTemplate);
// 获取用户名
String userName = userInfo.get("userName").toString();
// 获取小区id
String stoppingPlaceId = currloginInfoService.getCurrLoginStoppingPlaceId();
// 获取停车场name
StoppingPlaceInfomationEntity stoppingPlaceInfomation = stoppingPlaceInfomationService.getById(stoppingPlaceId);
String stoppingPlaceName = StringUtils.EMPTY;
if(stoppingPlaceInfomation!=null) {
stoppingPlaceName = stoppingPlaceInfomation.getStoppingPlaceName();
}
// 循环获取数据
for (Map<String, String> map : readExcel) {
FlatAccoutEntity accoutEntity = new FlatAccoutEntity();
// 判断是否是该停车场
if(StringUtils.isNotEmpty(map.get("停车场名称")) && stoppingPlaceName.equals(map.get("停车场名称"))) {
}else {
throw new BizException("10003","该数据不是当前停车场");
}
// 设置停车场
accoutEntity.setStoppingPlaceId(stoppingPlaceId);
// excel中第1列是停车场名称,不需要取
for(int i=1;i<headers.length;i++) {
String valueStr = map.get(headers[i]);
// 判断该字段是否为数字
if(intFileIndex.contains(i)){
int valueInt = -1;
// 将valueStr转换为int
// 若是支付方式
if(i==2){
valueInt = EnumUtils.getPayTypeValue(valueStr);
}
// 若是平帐状态
if(i==7){
valueInt = EnumUtils.getFlatStatusValue(valueStr);
}
// 调用该字段的set方法
MethodUtils.callSetMethod(accoutEntity,setMethodName[i], Integer.class,valueInt);
}else if(i==6){
// 转换为日期格式
try {
final Date valueDate = format.parse(valueStr);
MethodUtils.callSetMethod(accoutEntity,setMethodName[i],Date.class,valueDate);
} catch (ParseException e) {
log.error(e);
continue;
}
}else {
// 其余都是str类型 不需要转换类型
MethodUtils.callSetMethod(accoutEntity,setMethodName[i],String.class,valueStr);
}
}
listFlatAccountInfo.add(accoutEntity);
}
// 保存数据至数据库
boolean saveResult = this.saveBatch(listFlatAccountInfo);
return saveResult;
}
关于“大数据中如何实现导入”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/3631797/blog/4494631