package com.zy.asrs.common.utils;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.generator.config.IFileCreate;
|
import com.zy.asrs.common.domain.entity.LocInPrintMatExcel;
|
import com.zy.asrs.common.wms.entity.LocInPrintMat;
|
import com.zy.asrs.common.wms.entity.Mat;
|
import com.zy.asrs.common.wms.service.LocInPrintMatService;
|
import com.zy.asrs.common.wms.service.MatService;
|
import com.zy.asrs.framework.common.Cools;
|
import com.zy.asrs.framework.common.R;
|
import com.zy.asrs.framework.common.SpringUtils;
|
import com.zy.asrs.framework.exception.CoolException;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
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 LocInPrintMatExcelListener extends AnalysisEventListener<LocInPrintMatExcel> {
|
|
private int total = 0;
|
private int totalAnfme = 0;
|
private Long userId;
|
private Long hostId;
|
List<String[]> printMatList = new ArrayList<>();
|
|
public LocInPrintMatExcelListener() {
|
}
|
|
public LocInPrintMatExcelListener(Long userId, Long hostId) {
|
this.userId = userId;
|
this.hostId = hostId;
|
}
|
|
/**
|
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
|
*/
|
private static final int BATCH_COUNT = 50;
|
|
private final List<LocInPrintMatExcel> list = new ArrayList<>();
|
|
/**
|
* 这里会一行行的返回头
|
*/
|
@Override
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
}
|
|
/**
|
* 这个每一条数据解析都会来调用
|
*/
|
@Override
|
public void invoke(LocInPrintMatExcel excel, AnalysisContext ctx) {
|
String matnr = "";
|
try{
|
matnr = excel.getMatnr();
|
if (Cools.isEmpty(matnr)){
|
throw new CoolException("品号参数为空");
|
}
|
} catch (Exception e){
|
throw new CoolException("品号参数为空");
|
}
|
MatService matService = SpringUtils.getBean(MatService.class);
|
Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, matnr));
|
if (Cools.isEmpty(mat)){
|
throw new CoolException("品号:"+matnr+"不存在,请先新增商品档案!!!");
|
}
|
String matax = "";
|
try{
|
matax = mat.getMaktx();
|
} catch (Exception e){
|
matax = "";
|
}
|
String anfme = "1";
|
try{
|
String anfme1 = excel.getAnfme();
|
try{
|
Integer anfmeInt = Integer.parseInt(anfme1);
|
if (!Cools.isEmpty(anfmeInt) && anfmeInt>1){
|
anfme = anfmeInt.toString();
|
} else {
|
anfme = "1";
|
}
|
} catch (Exception e){
|
anfme = "1";
|
}
|
} catch (Exception e){
|
anfme = "1";
|
}
|
String[] s=new String[3];
|
s[0] = matnr;
|
s[1] = matax;
|
s[2] = anfme;
|
printMatList.add(s);
|
total++;
|
totalAnfme = totalAnfme+Integer.parseInt(anfme);
|
}
|
|
/**
|
* 所有数据解析完成了调用
|
* 适合事务
|
*/
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext ctx) {
|
if (!Cools.isEmpty(printMatList) && printMatList.size() != 0) {
|
LocInPrintMatService locInPrintMatService = SpringUtils.getBean(LocInPrintMatService.class);
|
Date now = new Date();
|
for (String[] printMat:printMatList){
|
String matnr = printMat[0];
|
String matax = printMat[1];
|
String anfme = printMat[2];
|
Integer intAnfme = 1;
|
try{
|
Integer intAnfme1 = Integer.parseInt(anfme);
|
if (!Cools.isEmpty(intAnfme1) && intAnfme1 > 1){
|
intAnfme = intAnfme1;
|
}
|
} catch (Exception e){
|
}
|
for (int i = 0 ; i < intAnfme ; i++){
|
LocInPrintMat locInPrintMat = new LocInPrintMat();
|
locInPrintMat.setMatnr(matnr);
|
locInPrintMat.setMaktx(matax);
|
locInPrintMat.setCreateBy(userId);
|
locInPrintMat.setUpdateBy(userId);
|
locInPrintMat.setCreateTime(now);
|
locInPrintMat.setUpdateTime(now);
|
locInPrintMatService.save(locInPrintMat);
|
}
|
|
}
|
|
}
|
|
log.info("新增{}种待打印物料信息,共生成{}条待打印条码!", total,totalAnfme);
|
}
|
|
public int[] getTotal() {
|
return new int[]{total,totalAnfme};
|
}
|
}
|