package com.vincent.rsf.server.manager.service.impl; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.vincent.rsf.framework.common.R; import com.vincent.rsf.framework.exception.CoolException; import com.vincent.rsf.server.common.utils.ExcelUtil; import com.vincent.rsf.server.manager.entity.MatnrGroup; import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate; import com.vincent.rsf.server.manager.mapper.MatnrMapper; import com.vincent.rsf.server.manager.entity.Matnr; import com.vincent.rsf.server.manager.service.MatnrGroupService; import com.vincent.rsf.server.manager.service.MatnrService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.util.ArrayList; import java.util.List; import java.util.Objects; @Service("matnrService") public class MatnrServiceImpl extends ServiceImpl implements MatnrService { @Autowired private MatnrGroupService matnrGroupService; /** * @return * @throws * @author Ryan * @description 物料数据导入接处理实现 * @time 2025/3/3 13:08 */ @Override @Transactional(rollbackFor = Exception.class) public R importExcels(MultipartFile file) throws Exception { //读取上传文件内容 ExcelImportResult result = ExcelImportUtil.importExcelMore(file.getInputStream(), MatnrsTemplate.class, ExcelUtil.getDefaultImportParams()); if (result.getList().isEmpty()) { throw new CoolException("物料导入失败!!"); } List matnrs = new ArrayList<>(); result.getList().forEach(template -> { Matnr matnr = new Matnr(); matnr.setBarcode(template.getBarcode()) .setCode(template.getErpCode()) .setDescrible(template.getDescrible()) .setColor(template.getColor()) .setFlagCheck(Short.parseShort(template.getFlagCheck())) .setWeight(Double.parseDouble(template.getWeight())) .setValidWarn(Integer.parseInt(template.getValidWarn())) .setValid(Integer.parseInt(template.getValid())) .setUnit(template.getUnit()) .setStockUnit(template.getPurUnit()) .setSpec(template.getSpec()) .setStagn(Integer.parseInt(template.getStagn())) .setModel(template.getModel()) .setGroupCode(template.getGroupCode()) .setPurUnit(template.getPurUnit()) .setStockLevel(Short.parseShort(template.getStockLevel())) .setSafeQty(Double.parseDouble(template.getSafeQty())) .setMinQty(Double.parseDouble(template.getMinQty())); if (Objects.isNull(template.getGroupCode()) && Objects.isNull(template.getGroupName())) { MatnrGroup matnrGroups = matnrGroupService.getOne(new LambdaQueryWrapper() .eq(!Objects.isNull(template.getGroupCode()), MatnrGroup::getCode, template.getGroupCode()) .eq(!Objects.isNull(template.getGroupName()),MatnrGroup::getName, template.getName())); matnr.setGroupId(matnrGroups.getId()); } matnrs.add(matnr); }); if (!this.saveBatch(matnrs)) { throw new CoolException("导入失败!!"); } return R.ok("导入成功!!"); } }