package com.panzhihua.common.listen;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.alibaba.fastjson.JSONObject;
|
import com.panzhihua.common.exceptions.ServiceException;
|
import com.panzhihua.common.model.vos.R;
|
import com.panzhihua.common.model.vos.community.ComMngCarExcelVO;
|
import com.panzhihua.common.service.community.CommunityService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
* @description: 车辆导入监听
|
* @author: Null
|
* @date: 2021/3/11 13:26
|
*/
|
@Slf4j
|
public class ComMngCarExcelListen extends AnalysisEventListener<ComMngCarExcelVO> {
|
|
private static final int BATCH_COUNT = 1000;
|
private CommunityService communityService;
|
private Long communityId;
|
private List<ComMngCarExcelVO> list = new ArrayList<>();
|
|
public ComMngCarExcelListen(CommunityService communityService, Long communityId) {
|
this.communityService = communityService;
|
this.communityId = communityId;
|
}
|
|
@Override
|
public void invoke(ComMngCarExcelVO comMngCarExcelVO, AnalysisContext analysisContext) {
|
list.add(comMngCarExcelVO);
|
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
if (list.size() >= BATCH_COUNT) {
|
log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
|
R r = this.communityService.listSaveMngCarExcelVO(list, this.communityId);
|
if (!R.isOk(r)) {
|
throw new ServiceException(r.getMsg());
|
}
|
list.clear(); // 清空list
|
}
|
|
}
|
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
|
R r = this.communityService.listSaveMngCarExcelVO(list, this.communityId);// 确保最后遗留的数据保存在数据库中
|
if (!R.isOk(r)) {
|
throw new ServiceException(r.getMsg());
|
}
|
}
|
}
|