package com.zy.asrs.utils;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.core.common.SpringUtils;
|
import com.core.exception.CoolException;
|
import com.zy.asrs.entity.MatV2;
|
import com.zy.asrs.service.MatService;
|
import com.zy.asrs.service.MatV2Service;
|
import com.zy.common.entity.MatExcel;
|
import com.zy.common.entity.MatV2Excel;
|
import lombok.extern.slf4j.Slf4j;
|
|
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 MatV2ExcelListener extends AnalysisEventListener<MatV2Excel> {
|
|
private int total = 0;
|
private Long userId;
|
|
public MatV2ExcelListener() {
|
}
|
|
public MatV2ExcelListener(Long userId) {
|
this.userId = userId;
|
}
|
|
/**
|
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
|
*/
|
private static final int BATCH_COUNT = 50;
|
|
private final List<MatV2Excel> list = new ArrayList<>();
|
|
/**
|
* 这里会一行行的返回头
|
*/
|
@Override
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
}
|
|
/**
|
* 这个每一条数据解析都会来调用
|
*/
|
@Override
|
public void invoke(MatV2Excel excel, AnalysisContext ctx) {
|
MatV2Service matV2Service = SpringUtils.getBean(MatV2Service.class);
|
Date now = new Date();
|
String number = excel.getNumber();
|
String[] split = number.split("\\.");
|
String uuid;
|
String tagId;
|
if (split.length == 1) {
|
uuid = "1";
|
tagId = split[0];
|
} else {
|
StringBuilder sb = new StringBuilder();
|
StringBuilder sb2 = new StringBuilder();
|
for (int i = 0; i < split.length; i++) {
|
if (i == 0) {
|
sb.append(split[i]);
|
} else {
|
sb.append("." + split[i]);
|
}
|
}
|
tagId = sb.toString();
|
for (int i = 0; i < split.length-1; i++) {
|
if (i == 0) {
|
sb2.append(split[i]);
|
} else {
|
sb2.append("." + split[i]);
|
}
|
}
|
uuid = sb2.toString();
|
}
|
// 商品
|
MatV2 matV2 = matV2Service.selectByMatnr(excel.getMatnr());
|
if (matV2 == null) {
|
matV2 = excel;
|
matV2.setUuid(uuid);
|
matV2.setTagId(tagId);
|
if (!matV2Service.insert(matV2)) {
|
throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr());
|
}
|
total++;
|
} else {
|
matV2 = excel;
|
matV2.setUuid(uuid);
|
matV2.setTagId(tagId);
|
if (!matV2Service.update(matV2,new EntityWrapper<MatV2>().eq("matnr",matV2.getMatnr()))) {
|
throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr());
|
}
|
total++;
|
}
|
}
|
|
/**
|
* 所有数据解析完成了调用
|
* 适合事务
|
*/
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext ctx) {
|
log.info("新增{}条物料信息!", total);
|
}
|
|
public int getTotal() {
|
return total;
|
}
|
}
|