移动破调整 重构excel解析:1.物料匹配调整 2.导出excel 调整 3.下载模版替换

This commit is contained in:
jing's 2024-03-21 23:27:26 +08:00
parent 3637302695
commit 2c2c4c2cf9
5 changed files with 394 additions and 221 deletions

View File

@ -76,8 +76,8 @@ public class OptionalBomConstant {
public enum ExcelErrorTagEnum implements ValueEnum<Integer> {
NO(0, "无错误"),
REPEAT(1, "重复的标配"),
MATERIA_NO_UNKOWN(2, "无物料号"),
ALL(3, "同时满足(重复标配、无物料号)");
MATERIA_NO_UNKOWN(2, "主数据匹配物料号"),
ALL(3, "同时满足(重复标配、主数据匹配物料号)");
private final Integer value;
private final String description;

View File

@ -28,6 +28,8 @@ import org.apache.poi.xssf.usermodel.XSSFColor;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**'
* 移动破导出设备
@ -49,7 +51,7 @@ public class ExportDeviceHelper {
.needHead(false)//是否需要head
.build();
buildHeader(2, voObj.getDeviceInfo(), sheet, writer, tableNoCounting);
buildHeader(4, voObj.getDeviceInfo(), sheet, writer, tableNoCounting);
buildList(voObj.getSingleList(), sheet, writer, tableNoCounting);
writer.finish();
@ -177,15 +179,21 @@ public class ExportDeviceHelper {
case 0:
columnWidth = 50;
break;
case 1:
columnWidth = 70;
columnWidth = 20;
break;
case 2:
columnWidth = 20;
columnWidth = 70;
break;
case 3:
columnWidth = 40;
columnWidth = 20;
break;
case 4:
columnWidth = 20;
break;
default:
@ -200,13 +208,10 @@ public class ExportDeviceHelper {
// 自定义表头样式处理
if (context.getHead() != null && context.getHead().booleanValue()) {
if(cellIndex==0 || cellIndex ==1) {
if(cellIndex==0 || cellIndex ==1 || cellIndex ==2) {
WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
CellStyle cellStyle = initCellStyle(workbook);
XSSFCellStyle xssfCellStyle = (XSSFCellStyle) workbook.createCellStyle();
xssfCellStyle.cloneStyleFrom(cellStyle);
xssfCellStyle.getFont().setBold(true);
@ -224,10 +229,11 @@ public class ExportDeviceHelper {
}
if(cellIndex ==1){
//合并选项头单元格 0~2
if(cellIndex == 2){
WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
Sheet sheet = writeSheetHolder.getSheet();
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, rowIndex, 0, 1);
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowIndex, rowIndex, 0, 2);
sheet.addMergedRegion(cellRangeAddress);
}
@ -270,11 +276,15 @@ public class ExportDeviceHelper {
head2.add("");
List<String> head3 = new ArrayList<String>();
head3.add("");
List<String> head4 = new ArrayList<String>();
head4.add("");
list.add(head0);
list.add(head1);
list.add(head2);
list.add(head3);
list.add(head4);
return list;
}
@ -318,6 +328,7 @@ public class ExportDeviceHelper {
cellList.add(StrUtil.format("{} {}", header.getDeviceNo(), header.getDeviceName()));
List<String> cellList2 = new ArrayList<>();
cellList2.add("选配内容");
cellList2.add(""); //空格合并单元格
cellList2.add("");
cellList2.add("标配\\可选");
cellList2.add("备注");
@ -327,7 +338,15 @@ public class ExportDeviceHelper {
//写入表格
writer.write(rowList, sheet, table);
}
boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
/**
* 构建列表部分
*/
@ -348,11 +367,6 @@ public class ExportDeviceHelper {
optionParentDrawingNoName = item1.getOptionDrawingNo();
List<OptionalEbomImportVO> list2 = item1.getChild();
// List<String> cellList = new ArrayList<>();
// cellList.add(opParentName);
for (OptionalEbomImportVO item2 : list2) {
{
@ -363,15 +377,19 @@ public class ExportDeviceHelper {
ImportExcelField excelField = new ImportExcelField();
excelField.setCellFirst(StrUtil.format("{} {}",optionChildDrawingNo,opChildName));
excelField.setCellSecond(StrUtil.format("{}", item3.getMaterialNo()));
if(StrUtil.isEmpty(item3.getDrawingNo())) {
excelField.setCellSecond(StrUtil.format("{}", item3.getMaterialName()));
}else {
excelField.setCellSecond(StrUtil.format ("{} {}", item3.getDrawingNo(), item3.getMaterialName()));
excelField.setCellThird(StrUtil.format("{}", item3.getMaterialName()));
}else if(isContainChinese(item3.getDrawingNo())){
excelField.setCellThird(StrUtil.format("{}", item3.getDrawingNo()));
}else{
excelField.setCellThird(StrUtil.format ("{} {}", item3.getDrawingNo(), item3.getMaterialName()));
}
excelField.setCellThird(StrUtil.format("{}",item3.getChooseStatus() == 1 ? "标配" : "可选")) ;
excelField.setCellFourth(item3.getRemak());
excelField.setCellFourth(StrUtil.format("{}",item3.getChooseStatus() == 1 ? "标配" : "可选")); ;
excelField.setCellFifth(item3.getRemak());
items.add(excelField);
}

View File

@ -9,19 +9,22 @@ import lombok.Data;
public class ImportExcelField{
@ExcelIgnore
private Integer lineNo;
//选项
@ExcelProperty(value="",index = 0)
private String cellFirst;
//物料
@ExcelProperty(value="",index = 1)
private String cellSecond;
//选配内容
@ExcelProperty(value="",index = 2)
private String cellThird;
//标配/可选
@ExcelProperty(value="",index = 3)
private String cellFourth;
//备注
@ExcelProperty(value="",index = 4)
private String cellFifth;

View File

@ -22,6 +22,7 @@ import com.nflg.product.bomnew.pojo.entity.OptionalEbomImportChildEntity;
import com.nflg.product.bomnew.pojo.entity.OptionalEbomImportEntity;
import com.nflg.product.bomnew.pojo.entity.OptionalEbomMainEntity;
import com.nflg.product.bomnew.pojo.vo.BaseMaterialVO;
import com.nflg.product.bomnew.pojo.vo.BomNewMbomMiddleVO;
import com.nflg.product.bomnew.service.*;
import com.nflg.product.bomnew.service.domain.OptionalEBom.ConvertToMainHistory;
import lombok.extern.slf4j.Slf4j;
@ -51,70 +52,56 @@ public class OptionalExcelService {
@Resource
private OptionalEbomImportService optionalEbomImportService;
private void parsecColumn3(List<ImportExcelField> v1, OptionalEbomImportDTO optionalEbomImportLevel2Dto) {
AtomicInteger partTypeCount = new AtomicInteger();
boolean tag=false;
//解析物料
for (ImportExcelField item:
v1) {
/*
解析物料
*/
private void parseColumn3(List<ImportExcelField> v1, OptionalEbomImportDTO optionalEbomImportLevel2Dto) {
AtomicInteger partTypeCount = new AtomicInteger();
boolean tag = false;
int lineNo =v1.get(0).getLineNo();
//解析物料
for (ImportExcelField item :
v1) {
if (StrUtil.isEmpty(item.getCellSecond())) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行, {} {}出现空物料数据", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
}
if (StrUtil.isEmpty(item.getCellThird())) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行, {} {}无选项数据", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
}
if (StrUtil.isEmpty(item.getCellFourth())) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,物料信息{}未填写标配/可选", item.getLineNo(), item.getCellSecond()));
}
//OCE-NFS780DP-037B-OPT加装回料输送机可选
OptionalEbomImportChildDTO levelDto3 = new OptionalEbomImportChildDTO();
OptionalEbomImportChildDTO levelDto3 = new OptionalEbomImportChildDTO();
if (CollectionUtil.isEmpty(optionalEbomImportLevel2Dto.getChild())) {
optionalEbomImportLevel2Dto.setChild(new ArrayList<OptionalEbomImportChildDTO>());
}
String materialDesc = item.getCellSecond().trim();
//替换统一格式
// materialDesc = materialDesc.replace(":", "");
int optionChildPos=materialDesc.indexOf(" ");
String materialName = item.getCellThird().trim();
if(optionChildPos<0){
throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,格式错误,选项使用空格分隔图号和名称 {}",item.getLineNo(), item.getCellSecond()));
}
levelDto3.setDrawingNo(StrUtil.sub(materialDesc, 0, optionChildPos));
String materialName = StrUtil.subSuf(materialDesc, optionChildPos + 1);
// if (StrUtil.isEmpty(levelDto3.getDrawingNo())) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后不包含物料图号", item.getLineNo(), item.getCellSecond()));
// }
if (StrUtil.isEmpty(materialName)) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后不包含物料名", item.getLineNo(), item.getCellSecond()));
}
levelDto3.setMaterialName(materialName);
levelDto3.setLineNo(item.getLineNo());
levelDto3.setPartType(OptionalBomConstant.PartTypeEnum.PART_TYPE_RADIO.getValue());
//标配 可选
levelDto3.setMaterialNo(item.getCellSecond());
levelDto3.setTag(materialName.contains(""));
levelDto3.setMaterialDesc(materialDesc);
//多个无
// levelDto3.setMaterialDesc(materialName);
// levelDto3.setMaterialNo(materialName);
// levelDto3.setMaterialName(materialName);
//多个无
if (tag && levelDto3.getTag()) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个无", item.getLineNo(),optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个无", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
}
tag = levelDto3.getTag();
// if (levelDto3.getTag()) {
// levelDto3.setMaterialDesc(materialName);
// }
if (item.getCellThird().contains("标配")) {
if (item.getCellFourth().contains("标配")) {
levelDto3.setChooseStatus(OptionalBomConstant.ChooseStatusEnum.CHOOSE_STATUS_YES.getValue());
partTypeCount.getAndIncrement();
//包含多个标配
@ -122,36 +109,129 @@ public class OptionalExcelService {
levelDto3.setError(OptionalBomConstant.ExcelErrorTagEnum.REPEAT.getValue());
optionalEbomImportLevel2Dto.setError(true);
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个标配", item.getLineNo(),optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个标配", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
}
} else {
levelDto3.setChooseStatus(OptionalBomConstant.ChooseStatusEnum.CHOOSE_STATUS_NO.getValue());
}
levelDto3.setRemak(item.getCellFourth());
levelDto3.setRemak(item.getCellFifth());
optionalEbomImportLevel2Dto.getChild().add(levelDto3);
// levelDto3.setMaterialName(materialName.replace("(标配)", "").replace("(可选)", ""));
// levelDto3.setMaterialDesc(levelDto3.getMaterialDesc());
}
}
private void parsecColumn2(Map<String, List<ImportExcelField>> listMap, OptionalEbomImportDTO optionalEbomImportDTO) {
// private void parseColumn3(List<ImportExcelField> v1, OptionalEbomImportDTO optionalEbomImportLevel2Dto) {
// AtomicInteger partTypeCount = new AtomicInteger();
// boolean tag = false;
// //解析物料
// for (ImportExcelField item :
// v1) {
//
// if (StrUtil.isEmpty(item.getCellSecond())) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行, {} {}出现空物料数据", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
// }
//
// if (StrUtil.isEmpty(item.getCellThird())) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,物料信息{}未填写标配/可选", item.getLineNo(), item.getCellSecond()));
// }
//
//
// //OCE-NFS780DP-037B-OPT加装回料输送机可选
// OptionalEbomImportChildDTO levelDto3 = new OptionalEbomImportChildDTO();
//
// if (CollectionUtil.isEmpty(optionalEbomImportLevel2Dto.getChild())) {
// optionalEbomImportLevel2Dto.setChild(new ArrayList<OptionalEbomImportChildDTO>());
// }
//
//
// String materialDesc = item.getCellSecond().trim();
// //替换统一格式
//
// int optionChildPos = materialDesc.indexOf(" ");
//
// if (optionChildPos < 0) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,选项使用空格分隔图号和名称 {}", item.getLineNo(), item.getCellSecond()));
// }
//
// levelDto3.setDrawingNo(StrUtil.sub(materialDesc, 0, optionChildPos));
// String materialName = StrUtil.subSuf(materialDesc, optionChildPos + 1);
//
//// if (StrUtil.isEmpty(levelDto3.getDrawingNo())) {
//// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后不包含物料图号", item.getLineNo(), item.getCellSecond()));
//// }
//
// if (StrUtil.isEmpty(materialName)) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后不包含物料名", item.getLineNo(), item.getCellSecond()));
// }
//
// levelDto3.setMaterialName(materialName);
// levelDto3.setLineNo(item.getLineNo());
// levelDto3.setPartType(OptionalBomConstant.PartTypeEnum.PART_TYPE_RADIO.getValue());
// //标配 可选
// levelDto3.setTag(materialName.contains(""));
// levelDto3.setMaterialDesc(materialDesc);
// //多个无
// if (tag && levelDto3.getTag()) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个无", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
//
// }
// tag = levelDto3.getTag();
//
// if (item.getCellThird().contains("标配")) {
// levelDto3.setChooseStatus(OptionalBomConstant.ChooseStatusEnum.CHOOSE_STATUS_YES.getValue());
// partTypeCount.getAndIncrement();
// //包含多个标配
// if (partTypeCount.get() > 1) {
// levelDto3.setError(OptionalBomConstant.ExcelErrorTagEnum.REPEAT.getValue());
// optionalEbomImportLevel2Dto.setError(true);
//
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,{} {} 包含多个标配", item.getLineNo(), optionalEbomImportLevel2Dto.getOptionName(), optionalEbomImportLevel2Dto.getOptionDrawingNo()));
// }
// } else {
// levelDto3.setChooseStatus(OptionalBomConstant.ChooseStatusEnum.CHOOSE_STATUS_NO.getValue());
// }
//
// levelDto3.setRemak(item.getCellFourth());
// optionalEbomImportLevel2Dto.getChild().add(levelDto3);
//
//
// }
// }
/**
* 解析子项
*/
private void parseOptionColumn2(Map<String, List<ImportExcelField>> listMap, OptionalEbomImportDTO optionalEbomImportDTO) {
List<OptionalEbomImportChildDTO> optionList=new ArrayList();
//子项
listMap.forEach((k, v) -> {
if(CollectionUtil.isEmpty(v)){
throw new NflgBusinessException(STATE.Error,StrUtil.format("{} {} 下无物料数据",optionalEbomImportDTO.getOptionDrawingNo(),optionalEbomImportDTO.getOptionName()));
if (CollectionUtil.isEmpty(v)) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("{} {} 下无物料数据", optionalEbomImportDTO.getOptionDrawingNo(), optionalEbomImportDTO.getOptionName()));
}
int optionChildPos=k.indexOf(" ");
k= k.replace("\t", " ");
int optionChildPos = k.indexOf(" ");
if(optionChildPos<0){
throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,格式错误,选项使用空格分隔图号和名称",v.get(0).getLineNo()));
if (optionChildPos < 0) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,选项使用空格分隔图号和名称", v.get(0).getLineNo()));
}
OptionalEbomImportDTO optionalEbomImportL2 = new OptionalEbomImportDTO();
optionalEbomImportL2.setOptionDrawingNo(StrUtil.sub(k,0,optionChildPos));
optionalEbomImportL2.setOptionName(StrUtil.subSuf(k,optionChildPos+1));
optionalEbomImportL2.setOptionDrawingNo(StrUtil.sub(k, 0, optionChildPos));
optionalEbomImportL2.setOptionName(StrUtil.subSuf(k, optionChildPos + 1));
if (StrUtil.isEmpty(optionalEbomImportL2.getOptionDrawingNo())) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后没有图号", v.get(0).getLineNo()));
@ -161,184 +241,263 @@ public class OptionalExcelService {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,解析后没有选项名", v.get(0).getLineNo()));
}
if (CollectionUtil.isEmpty(optionalEbomImportDTO.getChild())) {
optionalEbomImportDTO.setChild(new ArrayList<OptionalEbomImportDTO>());
}
optionalEbomImportDTO.getChild().add(optionalEbomImportL2);
parsecColumn3(v, optionalEbomImportL2);
parseColumn3(v, optionalEbomImportL2);
optionList.addAll(optionalEbomImportL2.getChild());
});
//检查选项下是否包含重复物料
Map<String,Long> frequencyMap = optionList.stream().collect(Collectors.groupingBy(OptionalEbomImportChildDTO::getMaterialNo, Collectors.counting()));
List<String> duplicates = frequencyMap.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if(CollectionUtil.isNotEmpty(duplicates)){
String result = duplicates.stream().collect(Collectors.joining(","));
throw new NflgBusinessException(STATE.Error, StrUtil.format(" {} {} 选项下有相同物料号{}", optionalEbomImportDTO.getOptionDrawingNo(),optionalEbomImportDTO.getOptionName(),result));
}
//选项下同步主数据
SpringUtil.getBean(MaterialMainService.class).intiMaterialInfo(optionList,OptionalEbomImportChildDTO::getMaterialNo);
for (OptionalEbomImportChildDTO childDTO : optionList) {
//!childDTO.getTag() &&
if (StringUtils.isEmpty(childDTO.getDrawingNo())) {
childDTO.setError(childDTO.getError() | OptionalBomConstant.ExcelErrorTagEnum.MATERIA_NO_UNKOWN.getValue());
optionalEbomImportDTO.setError(true);
}
}
}
private OptionalEbomImportExcelDTO parsecColumn1(List<ImportExcelField> list) {
//
// private OptionalEbomImportExcelDTO parsecColumn1(List<ImportExcelField> list) {
// OptionalEbomImportExcelDTO optionalEbomInportExcelDTO = new OptionalEbomImportExcelDTO();
// //第一行设备信息
// ImportExcelField device = list.get(0);
// optionalEbomInportExcelDTO.setDeviceInfo(parseDevcieInfo(device));
//
//
// String name = "";
// //相同类
// List<ImportExcelField> classList = new ArrayList<>();
// //跳过 第一行标题 选配内容 标配\可选
// int row = 2;
// for (; row < list.size(); row++) {
// ImportExcelField rowField = list.get(row);
//
// //第一选项未合并单元格 单元格拆分后 第1 2 3列相等
//// if (!rowField.getCellFirst().equals(rowField.getCellSecond()) || !rowField.getCellFirst().equals(rowField.getCellThird()) ){
//// throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,请保持相同格式,{} 未合并单元格 ",rowField.getLineNo(),rowField.getCellFirst()));
//// }
//
// //相同列为根选项黄色背景
// if ((row == list.size() - 1) || (rowField.getCellFirst().equals(rowField.getCellSecond())
// && StrUtil.isEmpty(rowField.getCellThird()))) {
//
//
// if ((row == list.size() - 1)) {
// classList.add(rowField);
// }
//
// name = rowField.getCellFirst();
// if (CollectionUtil.isNotEmpty(classList)) {
// String finalName = name;
// //黄色根部选项解析
// int optionParentPos = finalName.indexOf(" ");
//
// OptionalEbomImportDTO optionalEbomImportL1 = new OptionalEbomImportDTO();
// optionalEbomImportL1.setOptionDrawingNo(StrUtil.sub(finalName, 0, optionParentPos));
// optionalEbomImportL1.setOptionName(StrUtil.subSuf(finalName, optionParentPos + 1));
//
// if (CollectionUtil.isEmpty(optionalEbomInportExcelDTO.getOptionInfo())) {
// optionalEbomInportExcelDTO.setOptionInfo(new ArrayList<OptionalEbomImportDTO>());
// }
// optionalEbomInportExcelDTO.getOptionInfo().add(optionalEbomImportL1);
//
// Map<String, List<ImportExcelField>> group1 = classList.parallelStream()
// .collect(Collectors.groupingBy(ImportExcelField::getCellFirst, LinkedHashMap::new, Collectors.toList()));
//
// if (group1 == null || CollectionUtil.isEmpty(group1)) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("转换{}选项下数据异常", name));
// }
//
// parsecColumn2(group1, optionalEbomImportL1);
//
// classList.clear();
//
// }
// if ((row != list.size() - 1)) {
// name = rowField.getCellFirst();
// if (StrUtil.isEmpty(name)) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,选项数据不能为空", rowField.getLineNo()));
// }
//
// if (!name.contains(" ")) {
// throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,选项使用空格分隔名称和图号", rowField.getLineNo()));
// }
//
// }
//
// } else {
// classList.add(rowField);
// }
// }
// //最后一项处理
// return optionalEbomInportExcelDTO;
//
// }
OptionalEbomImportExcelDTO parseOptionColumn(List<ImportExcelField> list) {
OptionalEbomImportExcelDTO optionalEbomInportExcelDTO = new OptionalEbomImportExcelDTO();
//第一行设备信息
ImportExcelField device = list.get(0);
optionalEbomInportExcelDTO.setDeviceInfo(parseDevcieInfo(device));
if(StrUtil.isEmpty(device.getCellFirst())){
throw new NflgBusinessException(STATE.Error,"机型数据不能为空");
}
String deviceInfo = device.getCellFirst();
int deviceSplitPos=deviceInfo.indexOf(" ");
//设备信息
if(deviceSplitPos<0 ){
throw new NflgBusinessException(STATE.Error,"第1行机型设备型号格式异常,使用空格分隔设备编号和名称");
}
optionalEbomInportExcelDTO.setDeviceInfo(new OptionalEbomMainDTO());
optionalEbomInportExcelDTO.getDeviceInfo().setDeviceNo(StrUtil.sub(deviceInfo,0 ,deviceSplitPos ));
optionalEbomInportExcelDTO.getDeviceInfo().setDeviceName(StrUtil.subSuf(deviceInfo, deviceSplitPos+1 ));
if(StrUtil.isEmpty( optionalEbomInportExcelDTO.getDeviceInfo().getDeviceName())){
throw new NflgBusinessException(STATE.Error,"第1行格式错误解析后不包含设备名称");
}
optionalEbomInportExcelDTO.getDeviceInfo().setDeviceName( optionalEbomInportExcelDTO.getDeviceInfo().getDeviceName() .replace("\n",""));
String name = "";
String optionName = "";
//相同类
List<ImportExcelField> classList = new ArrayList<>();
//跳过 第一行标题 选配内容 标配\可选
int row = 2;
for (; row < list.size(); row++) {
ImportExcelField rowField = list.get(row);
optionName = rowField.getCellFirst();
optionName= optionName.replace("\t", " ");
//第一选项未合并单元格
if (StrUtil.isEmpty(rowField.getCellThird()) && !rowField.getCellFirst().equals(rowField.getCellSecond())){
throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,请保持相同格式,{} 未合并单元格 ",rowField.getLineNo(),rowField.getCellFirst()));
//黄色根部选项解析
int optionParentPos = optionName.indexOf(" ");
if (optionParentPos<0) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("第{}行,格式错误,选项{}使用空格分隔图号和名称", rowField.getLineNo(), optionName));
}
OptionalEbomImportDTO optionalEbomImportL1 = new OptionalEbomImportDTO();
optionalEbomImportL1.setOptionDrawingNo(StrUtil.sub(optionName, 0, optionParentPos).trim());
optionalEbomImportL1.setOptionName(StrUtil.subSuf(optionName, optionParentPos + 1));
if (CollectionUtil.isEmpty(optionalEbomInportExcelDTO.getOptionInfo())) {
optionalEbomInportExcelDTO.setOptionInfo(new ArrayList<OptionalEbomImportDTO>());
}
optionalEbomInportExcelDTO.getOptionInfo().add(optionalEbomImportL1);
while (++row<list.size()){
ImportExcelField rowField1 = list.get(row);
if(StrUtil.contains(rowField1.getCellFirst(),optionalEbomImportL1.getOptionDrawingNo())) {
classList.add(rowField1);
}else{
--row;
break;
}
}
//相同列为根
if ((row==list.size()-1)||(rowField.getCellFirst().equals(rowField.getCellSecond())
&& StrUtil.isEmpty(rowField.getCellThird() ) )) {
if((row==list.size()-1)){
classList.add(rowField);
}
if (CollectionUtil.isNotEmpty(classList)) {
String finalName = name;
//黄色根部选项解析
int optionParentPos=finalName.indexOf(" ");
OptionalEbomImportDTO optionalEbomImportL1 = new OptionalEbomImportDTO();
optionalEbomImportL1.setOptionDrawingNo(StrUtil.sub(finalName,0,optionParentPos));
optionalEbomImportL1.setOptionName(StrUtil.subSuf(finalName,optionParentPos+1));
if (CollectionUtil.isEmpty(optionalEbomInportExcelDTO.getOptionInfo())) {
optionalEbomInportExcelDTO.setOptionInfo(new ArrayList<OptionalEbomImportDTO>());
}
optionalEbomInportExcelDTO.getOptionInfo().add(optionalEbomImportL1);
Map<String, List<ImportExcelField> > group1 = classList.parallelStream()
.collect(Collectors.groupingBy(ImportExcelField::getCellFirst,LinkedHashMap::new,Collectors.toList()));
if(group1==null || CollectionUtil.isEmpty(group1)){
throw new NflgBusinessException(STATE.Error,StrUtil.format("转换{}选项下数据异常",name));
}
parsecColumn2(group1, optionalEbomImportL1);
classList.clear();
}
if((row!=list.size()-1)) {
name = rowField.getCellFirst();
if(StrUtil.isEmpty(name)){
throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,选项数据不能为空",rowField.getLineNo()));
}
if(!name.contains(" ")){
throw new NflgBusinessException(STATE.Error,StrUtil.format("第{}行,格式错误,选项使用空格分隔名称和图号",rowField.getLineNo()));
}
}
} else {
classList.add(rowField);
Map<String, List<ImportExcelField>> group1 = classList.parallelStream()
.collect(Collectors.groupingBy(ImportExcelField::getCellFirst, LinkedHashMap::new, Collectors.toList()));
if (group1 == null || CollectionUtil.isEmpty(group1)) {
throw new NflgBusinessException(STATE.Error, StrUtil.format("转换{}选项下数据异常", optionName));
}
parseOptionColumn2(group1, optionalEbomImportL1);
classList.clear();
}
//最后一项处理
return optionalEbomInportExcelDTO;
}
/**
* 解析设备
*
* @param device
* @return
*/
OptionalEbomMainDTO parseDevcieInfo(ImportExcelField device) {
if (StrUtil.isEmpty(device.getCellFirst())) {
throw new NflgBusinessException(STATE.Error, "机型数据不能为空");
}
String deviceInfo = device.getCellFirst();
int deviceSplitPos = deviceInfo.indexOf(" ");
//设备信息
if (deviceSplitPos < 0) {
throw new NflgBusinessException(STATE.Error, "第1行机型设备型号格式异常,使用空格分隔设备编号和名称");
}
OptionalEbomMainDTO deviceDto = new OptionalEbomMainDTO();
deviceDto.setDeviceNo(StrUtil.sub(deviceInfo, 0, deviceSplitPos));
deviceDto.setDeviceName(StrUtil.subSuf(deviceInfo, deviceSplitPos + 1));
if (StrUtil.isEmpty(deviceDto.getDeviceNo())) {
throw new NflgBusinessException(STATE.Error, "第1行格式错误解析后不包含设备编码");
}
if (StrUtil.isEmpty(deviceDto.getDeviceName())) {
throw new NflgBusinessException(STATE.Error, "第1行格式错误解析后不包含设备名称");
}
deviceDto.setDeviceName(deviceDto.getDeviceName().replace("\n", ""));
return deviceDto;
}
public Boolean importExcel(InputStream inputStream) throws NflgBusinessException {
if (inputStream == null) {
new NflgBusinessException(STATE.Error, "文件流内容空");
throw new NflgBusinessException(STATE.Error, "文件流内容空");
}
//解析excel
ImportExcelHelper<ImportExcelField> helper = new ImportExcelHelper<>();
List<ImportExcelField> list = helper.getList(inputStream, ImportExcelField.class, 0, 0);
OptionalEbomImportExcelDTO optionalEbomInportExcelDTO = null;
if (CollectionUtil.isNotEmpty(list)) {
optionalEbomInportExcelDTO = parsecColumn1(list);
optionalEbomInportExcelDTO = parseOptionColumn(list);
}
if (optionalEbomInportExcelDTO != null) {
optionalEbomInportExcelDTO.getOptionInfo().forEach(item -> {
List<OptionalEbomImportDTO> listParent = item.getChild();
for (OptionalEbomImportDTO item1 :
listParent) {
List<OptionalEbomImportChildDTO> listChild = item1.getChild();
//
SpringUtil.getBean(MaterialMainService.class).initMaterialForDrawdingNoImmul(listChild, OptionalEbomImportChildDTO::getDrawingNo, OptionalEbomImportChildDTO::setMaterialNo, OptionalEbomImportChildDTO::setMaterialDesc);
for (OptionalEbomImportChildDTO childDTO : listChild) {
if (!childDTO.getTag() && StringUtils.isEmpty(childDTO.getMaterialNo())) {
//in 批量没匹配到 再模糊匹配一次
List<BaseMaterialVO> likeList= SpringUtil.getBean(MaterialMainService.class)
.getMaterialLikeByDrawingNo(StrUtil.format("{} {}",childDTO.getDrawingNo(),childDTO.getMaterialName()));
if(CollectionUtil.isNotEmpty(likeList) ) {
if (likeList.size() == 1) {
BaseMaterialVO materialVO = likeList.get(0);
childDTO.setMaterialNo(materialVO.getMaterialNo());
childDTO.setMaterialDesc(materialVO.getMaterialDesc());
continue;
} else if (likeList.size() > 1) {
throw new NflgBusinessException(STATE.BusinessError, StrUtil.format("第{}行,{} 图号匹配到多条物料数据", childDTO.getLineNo(), childDTO.getDrawingNo()));
}
}
// childDTO.setMaterialNo(childDTO.getDrawingNo());
childDTO.setError(childDTO.getError() | OptionalBomConstant.ExcelErrorTagEnum.MATERIA_NO_UNKOWN.getValue());
item1.setError(true);
}
}
}
});
if(optionalEbomInportExcelDTO==null){
throw new NflgBusinessException(STATE.Error, "解析数据失败");
}
// if (optionalEbomInportExcelDTO != null) {
// for( OptionalEbomImportDTO item:optionalEbomInportExcelDTO.getOptionInfo()) {
// List<OptionalEbomImportDTO> listParent = item.getChild();
// for (OptionalEbomImportDTO item1 :
// listParent) {
// List<OptionalEbomImportChildDTO> listChild = item1.getChild();
// //
// // SpringUtil.getBean(MaterialMainService.class).initMaterialForDrawdingNoImmul(listChild, OptionalEbomImportChildDTO::getDrawingNo, OptionalEbomImportChildDTO::setMaterialNo, OptionalEbomImportChildDTO::setMaterialDesc);
//
// SpringUtil.getBean(MaterialMainService.class).intiMaterialInfo(listChild,OptionalEbomImportChildDTO::getMaterialNo);
// for (OptionalEbomImportChildDTO childDTO : listChild) {
// //!childDTO.getTag() &&
// if (StringUtils.isEmpty(childDTO.getDrawingNo())) {
// childDTO.setError(childDTO.getError() | OptionalBomConstant.ExcelErrorTagEnum.MATERIA_NO_UNKOWN.getValue());
// item1.setError(true);
// }
// }
// }
// }
// }
//检查是否满足条件 除了 物料号不能为空
String hintMsg = isValiData(optionalEbomInportExcelDTO.getOptionInfo());
if (StrUtil.isNotEmpty(hintMsg)) {
throw new NflgBusinessException(STATE.Error, hintMsg);
}
System.out.println(JSON.toJSONString(optionalEbomInportExcelDTO));
// return true;
System.out.println(JSON.toJSONString(optionalEbomInportExcelDTO));
// return true;
//保存db
return batchExcelToRepertory(optionalEbomInportExcelDTO);
return batchExcelToRepertory(optionalEbomInportExcelDTO);
}
@ -348,8 +507,6 @@ public class OptionalExcelService {
excelList.forEach(item -> {
List<OptionalEbomImportDTO> listParent = item.getChild();
for (OptionalEbomImportDTO item1 : listParent) {
if (item1.isError()) {
List<OptionalEbomImportChildDTO> listChild = item1.getChild();
for (OptionalEbomImportChildDTO item2 : listChild) {
if (item2.getError() == OptionalBomConstant.ExcelErrorTagEnum.REPEAT.getValue()) {
@ -360,9 +517,6 @@ public class OptionalExcelService {
title.append(String.format("第%d行 %s ", item2.getLineNo(), OptionalBomConstant.ExcelErrorTagEnum.ALL.getDescription()));
}
}
}
}
});
@ -371,7 +525,7 @@ public class OptionalExcelService {
}
// @Transactional(rollbackFor = Exception.class)
// @Transactional(rollbackFor = Exception.class)
public Boolean batchExcelToRepertory(OptionalEbomImportExcelDTO ebomImportExcelDTO) {
OptionalEbomMainEntity optionalEbomMainEntity = Convert.convert(new TypeReference<OptionalEbomMainEntity>() {
@ -396,10 +550,10 @@ public class OptionalExcelService {
optionalEbomMainService.save(optionalEbomMainEntity);
}else{
optionalEbomMainEntity=queryDevice;
} else {
optionalEbomMainEntity = queryDevice;
}
}
if (optionalEbomMainEntity == null || optionalEbomMainEntity.getRowId() == null) {
throw new NflgBusinessException(STATE.Error, "保存设备数据失败");
@ -409,7 +563,7 @@ public class OptionalExcelService {
List<OptionalEbomImportDTO> parentOptionList = ebomImportExcelDTO.getOptionInfo();
ConvertToMainHistory convertToMainHistory=new ConvertToMainHistory();
ConvertToMainHistory convertToMainHistory = new ConvertToMainHistory();
convertToMainHistory.convertToMainHistory(optionalEbomMainEntity.getRowId());
//保存选项
@ -425,7 +579,7 @@ public class OptionalExcelService {
parentOptionEntity = Convert.convert(new TypeReference<OptionalEbomImportEntity>() {
}, parentOption);
parentOptionEntity.setRootRowId(optionalEbomMainEntity.getRowId());
parentOptionEntity.setCreatedTime(DateUtil.now());
parentOptionEntity.setCreatedTime(DateUtil.now());
parentOptionEntity.setCreatedBy(SessionUtil.getUserCode());
optionalEbomImportService.save(parentOptionEntity);
}
@ -437,8 +591,6 @@ public class OptionalExcelService {
List<OptionalEbomImportDTO> childOptionList = parentOption.getChild();
for (OptionalEbomImportDTO childOption : childOptionList) {
QueryWrapper<OptionalEbomImportEntity> queryWrapper2 = new QueryWrapper<>();
@ -453,7 +605,7 @@ public class OptionalExcelService {
childOptionEntity.setRootRowId(optionalEbomMainEntity.getRowId());
childOptionEntity.setParentRowId(parentOptionEntity.getRowId());
childOptionEntity.setCreatedTime(DateUtil.now());
childOptionEntity.setCreatedTime(DateUtil.now());
optionalEbomImportService.save(childOptionEntity);
}
// else{
@ -494,8 +646,8 @@ public class OptionalExcelService {
}
//首次添加设备不用操作保存历史记录
if(queryDevice!=null){
SpringUtil.getBean(OptionalEbomMainHistoryService.class).convertToHistory(convertToMainHistory,true);
if (queryDevice != null) {
SpringUtil.getBean(OptionalEbomMainHistoryService.class).convertToHistory(convertToMainHistory, true);
}
return true;