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.Mat;
|
import com.zy.asrs.entity.Tag;
|
import com.zy.asrs.mapper.TagMapper;
|
import com.zy.asrs.service.MatService;
|
import com.zy.asrs.service.TagService;
|
import com.zy.common.entity.MatExcel;
|
import com.zy.common.utils.NodeUtils;
|
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 MatExcelListener extends AnalysisEventListener<MatExcel> {
|
|
private int total = 0;
|
private Long userId;
|
|
public MatExcelListener() {
|
}
|
|
public MatExcelListener(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(MatExcel excel, AnalysisContext ctx) {
|
TagService tagService = SpringUtils.getBean(TagService.class);
|
TagMapper tagMapper = SpringUtils.getBean(TagMapper.class);
|
MatService matService = SpringUtils.getBean(MatService.class);
|
Date now = new Date();
|
Long tagId;
|
// 分类
|
// 一级分类
|
if (!Cools.isEmpty(excel.getPriClass()) && !Cools.isEmpty(excel.getSecClass())) {
|
Tag priTag = tagService.selectByName(excel.getPriClass(), 2);
|
if (priTag == null) {
|
Tag top = tagService.getTop();
|
NodeUtils nodeUtils = new NodeUtils();
|
nodeUtils.executePath(top.getId());
|
priTag = new Tag(
|
null, // 编号
|
excel.getPriClass(), // 名称
|
top.getId(), // 父级
|
top.getName(), // 父级名称
|
nodeUtils.path.toString(), // 关联路径
|
nodeUtils.pathName.toString(), // 关联路径名
|
0, // 类型
|
null, // 负责人
|
null, // 图片
|
null, // 简要描述
|
null, // 数量
|
2, // 等级
|
null, // 排序
|
1, // 状态
|
now, // 添加时间
|
null, // 添加人员
|
now, // 修改时间
|
null, // 修改人员
|
null // 备注
|
);
|
if (tagMapper.insert(priTag) == 0) {
|
throw new CoolException("保存一级分类失败");
|
}
|
}
|
// 二级分类
|
Tag secTag = tagService.selectByName(excel.getSecClass(), 3);
|
if (secTag == null) {
|
NodeUtils nodeUtils = new NodeUtils();
|
nodeUtils.executePath(priTag.getId());
|
secTag = new Tag(
|
null, // 编号
|
excel.getSecClass(), // 名称
|
priTag.getId(), // 父级
|
priTag.getName(), // 父级名称
|
nodeUtils.path.toString(), // 关联路径
|
nodeUtils.pathName.toString(), // 关联路径名
|
0, // 类型
|
null, // 负责人
|
null, // 图片
|
null, // 简要描述
|
null, // 数量
|
3, // 等级
|
null, // 排序
|
1, // 状态
|
now, // 添加时间
|
null, // 添加人员
|
now, // 修改时间
|
null, // 修改人员
|
null // 备注
|
);
|
if (tagMapper.insert(secTag) == 0) {
|
throw new CoolException("保存二级分类失败");
|
}
|
}
|
tagId = secTag.getId();
|
} else {
|
tagId = tagService.getTop().getId();
|
}
|
// 商品
|
Mat mat = matService.selectByMatnr(excel.getMatnr());
|
if (mat == null) {
|
mat = excel;
|
mat.setTagId(tagId);
|
if (!matService.insert(mat)) {
|
throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr());
|
}
|
total++;
|
}
|
}
|
|
/**
|
* 所有数据解析完成了调用
|
* 适合事务
|
*/
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext ctx) {
|
log.info("新增{}条物料信息!", total);
|
}
|
|
public int getTotal() {
|
return total;
|
}
|
}
|