package com.zy.asrs.utils;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.core.common.Cools;
|
import com.core.common.SpringUtils;
|
import com.core.exception.CoolException;
|
import com.zy.asrs.entity.Pla;
|
import com.zy.asrs.entity.param.GlobleParameter;
|
import com.zy.asrs.service.PlaService;
|
import com.zy.common.entity.MatExcel;
|
import com.zy.common.entity.PlaExcel;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.BeanUtils;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* Created by vincent on 2019-11-25
|
*/
|
@Slf4j
|
public class PlaExcelListener extends AnalysisEventListener<PlaExcel> {
|
|
private int total = 0;
|
private Long userId;
|
|
public PlaExcelListener() {
|
}
|
|
public PlaExcelListener(Long userId) {
|
this.userId = userId;
|
}
|
|
/**
|
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
|
*/
|
private static final int BATCH_COUNT = 50;
|
|
private final List<MatExcel> list = new ArrayList<>();
|
|
/**
|
* 这里会一行行的返回头
|
*/
|
@Override
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
}
|
|
/**
|
* 这个每一条数据解析都会来调用
|
*/
|
@Override
|
public void invoke(PlaExcel excel, AnalysisContext ctx) {
|
PlaService plaService = SpringUtils.getBean(PlaService.class);
|
Date now = new Date();
|
|
// 商品
|
if(Cools.isEmpty(excel.getBatch()) || Cools.isEmpty(excel.getPackageNo())){
|
return;
|
}
|
Pla pla = plaService.selectByBatchAndPackageNo(excel.getBatch(),excel.getPackageNo());
|
if (pla == null) {
|
pla = excel;
|
pla.setStep(2);
|
pla.setCreateTime(new Date());
|
pla.setStatus(GlobleParameter.PLA_STATUS_0);
|
if (!plaService.insert(pla)) {
|
throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr());
|
}
|
total++;
|
}else if (pla.getStatus().equals(GlobleParameter.PLA_STATUS_0)){
|
excel.setId(pla.getId());
|
excel.setCreateTime(pla.getCreateTime());
|
excel.setModifyTime(pla.getModifyTime());
|
excel.setStep(2);
|
excel.setStatus(GlobleParameter.PLA_STATUS_0);
|
BeanUtils.copyProperties(excel,pla);
|
plaService.updateById(pla);
|
total++;
|
}
|
}
|
|
/**
|
* 所有数据解析完成了调用
|
* 适合事务
|
*/
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext ctx) {
|
log.info("新增{}条物料信息!", total);
|
}
|
|
public int getTotal() {
|
return total;
|
}
|
}
|