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.zy.asrs.common.domain.entity.MatExcel; import com.zy.asrs.common.wms.entity.ManMat; import com.zy.asrs.common.wms.entity.ManTag; import com.zy.asrs.common.wms.mapper.ManTagMapper; import com.zy.asrs.common.wms.service.ManMatService; import com.zy.asrs.common.wms.service.ManTagService; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.SpringUtils; import com.zy.asrs.framework.exception.CoolException; 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 { 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 list = new ArrayList<>(); /** * 这里会一行行的返回头 */ @Override public void invokeHeadMap(Map headMap, AnalysisContext context) { } /** * 这个每一条数据解析都会来调用 */ @Override public void invoke(MatExcel excel, AnalysisContext ctx) { ManTagService tagService = SpringUtils.getBean(ManTagService.class); ManTagMapper tagMapper = SpringUtils.getBean(ManTagMapper.class); ManMatService matService = SpringUtils.getBean(ManMatService.class); Date now = new Date(); Long tagId; // 分类 // 一级分类 if (!Cools.isEmpty(excel.getPriClass()) && !Cools.isEmpty(excel.getSecClass())) { ManTag priTag = tagService.getOne(new LambdaQueryWrapper().eq(ManTag::getName, excel.getPriClass()).eq(ManTag::getLevel, 2)); if (priTag == null) { ManTag top = tagService.getTop(); NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(top.getId()); priTag = new ManTag( 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("保存一级分类失败"); } } // 二级分类 ManTag secTag = tagService.getOne(new LambdaQueryWrapper().eq(ManTag::getName, excel.getPriClass()).eq(ManTag::getLevel, 3)); if (secTag == null) { NodeUtils nodeUtils = new NodeUtils(); nodeUtils.executePath(priTag.getId()); secTag = new ManTag( 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(); } // 商品 ManMat mat = matService.getOne(new LambdaQueryWrapper().eq(ManMat::getMatnr, excel.getMatnr())); if (mat == null) { mat = excel; mat.setTagId(tagId); if (!matService.save(mat)) { throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr()); } total++; } } /** * 所有数据解析完成了调用 * 适合事务 */ @Override public void doAfterAllAnalysed(AnalysisContext ctx) { log.info("新增{}条物料信息!", total); } public int getTotal() { return total; } }