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.NodeExcel; import zy.cloud.wms.manager.entity.Node; import zy.cloud.wms.manager.mapper.NodeMapper; import zy.cloud.wms.manager.service.NodeService; 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 NodeExcelListener extends AnalysisEventListener { private int total = 0; private Long userId; private Long hostId; public NodeExcelListener() { } public NodeExcelListener(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(NodeExcel excel, AnalysisContext ctx) { NodeService nodeService = SpringUtils.getBean(NodeService.class); NodeMapper nodeMapper = SpringUtils.getBean(NodeMapper.class); Date now = new Date(); Node top = nodeService.getTop(); // 仓库 if (!Cools.isEmpty(excel.getWarehouse())) { Node wareHouse = nodeService.selectByUuid(excel.getWarehouse(), hostId, 1, top.getId()); if (wareHouse == null) { wareHouse = new Node(); wareHouse.setHostId(this.hostId); wareHouse.setUuid(excel.getWarehouse()); wareHouse.setName(excel.getWarehouse()); wareHouse.setType(1); wareHouse.setParentId(top.getId()); wareHouse.setParentName(top.getName()); wareHouse.setLevel(top.getLevel() + 1); NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(wareHouse); wareHouse.setPath(nodeUtils.path.toString()); wareHouse.setNamePath(nodeUtils.pathName.toString()); wareHouse.setStatus(1); wareHouse.setCreateTime(now); wareHouse.setUpdateTime(now); if (nodeMapper.insert(wareHouse) == 0) { throw new CoolException("保存仓库数据失败"); } total ++; } // 库区 if (!Cools.isEmpty(excel.getArea())) { Node area = nodeService.selectByUuid(excel.getArea(), hostId, 2, wareHouse.getId()); if (area == null) { area = new Node(); area.setHostId(this.hostId); area.setUuid(excel.getArea()); area.setName(excel.getArea()); area.setType(2); area.setParentId(wareHouse.getId()); area.setParentName(wareHouse.getName()); area.setLevel(wareHouse.getLevel() + 1); NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(area); area.setPath(nodeUtils.path.toString()); area.setNamePath(nodeUtils.pathName.toString()); area.setStatus(1); area.setCreateTime(now); area.setUpdateTime(now); if (nodeMapper.insert(area) == 0) { throw new CoolException("保存库区数据失败"); } total ++; } // 货位 if (!Cools.isEmpty(excel.getAllo())) { Node allo = nodeService.selectByUuid(excel.getAllo(), hostId, 3); if (allo == null) { allo = new Node(); allo.setHostId(this.hostId); allo.setUuid(excel.getAllo()); allo.setName(excel.getAllo()); allo.setType(3); allo.setParentId(area.getId()); allo.setParentName(area.getName()); allo.setLevel(area.getLevel() + 1); NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(allo); allo.setPath(nodeUtils.path.toString()); allo.setNamePath(nodeUtils.pathName.toString()); allo.setStatus(1); allo.setCreateTime(now); allo.setUpdateTime(now); if (nodeMapper.insert(allo) == 0) { throw new CoolException("保存货位数据失败"); } total ++; } } } } } /** * 所有数据解析完成了调用 * 适合事务 */ @Override public void doAfterAllAnalysed(AnalysisContext ctx) { log.info("新增{}条物料信息!", total); } public int getTotal() { return total; } }