| | |
| | | |
| | | import com.alibaba.excel.context.AnalysisContext; |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.core.common.SpringUtils; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.asrs.entity.MatCode; |
| | | import com.zy.asrs.service.MatCodeService; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | |
| | | private static final Logger LOGGER = LoggerFactory.getLogger(MatCodeExcelListener.class); |
| | | |
| | | private int total = 0; |
| | | private Long userId; |
| | | |
| | | public MatCodeExcelListener() { |
| | | } |
| | | |
| | | public MatCodeExcelListener(Long userId) { |
| | | this.userId = userId; |
| | | } |
| | | |
| | | /** |
| | | * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
| | | */ |
| | | private static final int BATCH_COUNT = 5; |
| | | private static final int BATCH_COUNT = 50; |
| | | |
| | | private List<MatCodeExcel> list = new ArrayList<>(); |
| | | private final List<MatCode> list = new ArrayList<>(); |
| | | |
| | | /** |
| | | * 这里会一行行的返回头 |
| | | */ |
| | | @Override |
| | | public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
| | | LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap)); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public void invoke(MatCodeExcel data, AnalysisContext ctx) { |
| | | LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
| | | list.add(data); |
| | | MatCodeService matCodeService = SpringUtils.getBean(MatCodeService.class); |
| | | if (matCodeService.selectById(data.getMatNo()) == null) { |
| | | MatCode matCode = new MatCode(); |
| | | matCode.setMatNo(data.getMatNo()); |
| | | matCode.setMatName(data.getMatName()); |
| | | matCode.setBarcode(data.getBarcode()); |
| | | matCode.setStr1(data.getStr1()); |
| | | matCode.setModiTime(new Date()); |
| | | matCode.setModiUser(this.userId); |
| | | matCode.setAppeTime(new Date()); |
| | | matCode.setAppeUser(this.userId); |
| | | list.add(matCode); |
| | | total ++; |
| | | } |
| | | // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM |
| | | if (list.size() >= BATCH_COUNT) { |
| | | MatCodeService matCodeService = SpringUtils.getBean(MatCodeService.class); |
| | | if (!matCodeService.insertBatch(list)) { |
| | | throw new CoolException("导入数据异常"); |
| | | } |
| | | // 存储完成清理 list |
| | | list.clear(); |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void doAfterAllAnalysed(AnalysisContext ctx) { |
| | | LOGGER.info("所有数据解析完成!"); |
| | | LOGGER.info("新增{}条物料信息!", total); |
| | | } |
| | | |
| | | public int getTotal() { |
| | | return total; |
| | | } |
| | | } |