package zy.cloud.wms.common.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 lombok.extern.slf4j.Slf4j; import zy.cloud.wms.common.entity.MatExcel; import zy.cloud.wms.manager.entity.Mat; import zy.cloud.wms.manager.entity.Tag; import zy.cloud.wms.manager.mapper.TagMapper; import zy.cloud.wms.manager.service.MatService; import zy.cloud.wms.manager.service.TagService; import zy.cloud.wms.manager.utils.NodeUtils; 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 { private int total = 0; private Long userId; private Long hostId; public MatExcelListener() { } public MatExcelListener(Long userId, Long hostId) { this.userId = userId; this.hostId = hostId; } /** * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 50; private final List list = new ArrayList<>(); /** * 这里会一行行的返回头 */ @Override public void invokeHeadMap(Map 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( hostId, null, // 编号 excel.getPriClass(), // 名称 top.getId(), // 父级 top.getName(), // 父级名称 nodeUtils.path.toString(), // 关联路径 nodeUtils.pathName.toString(), // 关联路径名 0, // 类型 null, // 负责人 null, // 图片 null, // 简要描述 null, // 数量 2, // 等级 null, // 排序 1, // 状态 now, // 添加时间 userId, // 添加人员 now, // 修改时间 userId, // 修改人员 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( hostId, null, // 编号 excel.getSecClass(), // 名称 priTag.getId(), // 父级 priTag.getName(), // 父级名称 nodeUtils.path.toString(), // 关联路径 nodeUtils.pathName.toString(), // 关联路径名 0, // 类型 null, // 负责人 null, // 图片 null, // 简要描述 null, // 数量 3, // 等级 null, // 排序 1, // 状态 now, // 添加时间 userId, // 添加人员 now, // 修改时间 userId, // 修改人员 null // 备注 ); if (tagMapper.insert(secTag) == 0) { throw new CoolException("保存二级分类失败"); } } tagId = secTag.getId(); } else { tagId = tagService.getTop().getId(); } // 商品 Mat mat = matService.selectByMatnr(hostId, 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; } }