|
|
|
@ -1,21 +1,13 @@
|
|
|
|
|
package hw.tagApi.service.service.impl;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.common.exception.ServiceException;
|
|
|
|
|
import hw.tagApi.common.utils.DateUtils;
|
|
|
|
|
import hw.tagApi.common.utils.StringUtils;
|
|
|
|
|
import hw.tagApi.common.utils.poi.ExcelUtil;
|
|
|
|
|
import hw.tagApi.common.utils.uuid.IdGenerator;
|
|
|
|
|
import hw.tagApi.service.constant.ApiConstants;
|
|
|
|
|
import hw.tagApi.service.domain.ApiContent;
|
|
|
|
|
import hw.tagApi.service.domain.ImportParams;
|
|
|
|
|
import hw.tagApi.service.domain.ImportResult;
|
|
|
|
|
import hw.tagApi.service.utils.TagExcelUtil;
|
|
|
|
|
import hw.tagApi.service.utils.httpClientUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
@ -25,7 +17,6 @@ import hw.tagApi.service.domain.HwTagRecord;
|
|
|
|
|
import hw.tagApi.service.service.IHwTagRecordService;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import static hw.tagApi.service.utils.TagExcelUtil.extractImportParams;
|
|
|
|
|
import static hw.tagApi.service.utils.TagExcelUtil.startsWithCheck;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -42,7 +33,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private HwTagRecordMapper hwTagRecordMapper;
|
|
|
|
|
|
|
|
|
|
private static final int BATCH_SIZE = 100;
|
|
|
|
|
private static final int BATCH_SIZE = 500;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询标签记录
|
|
|
|
@ -197,226 +188,36 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
|
|
|
|
|
return hwTagRecordMapper.selectExistingEpcs(epcs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将Excel数据转换为HwTagRecord列表
|
|
|
|
|
*
|
|
|
|
|
* @param orderInfo 订单信息
|
|
|
|
|
* @param tagList 标签列表
|
|
|
|
|
* @param params 导入参数
|
|
|
|
|
* @return HwTagRecord列表
|
|
|
|
|
*/
|
|
|
|
|
private List<HwTagRecord> convertExcelDataToTagRecords(Map<String, String> orderInfo, List<Map<String, String>> tagList, ImportParams params) {
|
|
|
|
|
List<HwTagRecord> records = new ArrayList<>();
|
|
|
|
|
Date nowDate = new Date();
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
for (Map<String, String> tagData : tagList) {
|
|
|
|
|
HwTagRecord record = new HwTagRecord();
|
|
|
|
|
|
|
|
|
|
// 设置订单信息
|
|
|
|
|
String lot = orderInfo.getOrDefault("orderNo", "").replaceAll("Lot-?", "");
|
|
|
|
|
// 新模板订单号:Lot20250207-001
|
|
|
|
|
if (lot.contains("-")) {
|
|
|
|
|
String[] parts = lot.split("-");
|
|
|
|
|
String orderCode = parts[0];
|
|
|
|
|
String batchNumber = parts[1];
|
|
|
|
|
record.setOrderCode(orderCode);
|
|
|
|
|
record.setBatchNumber(batchNumber);
|
|
|
|
|
record.setTagBatch(orderInfo.getOrDefault("batchNo", ""));
|
|
|
|
|
} else {
|
|
|
|
|
record.setOrderCode(lot);
|
|
|
|
|
record.setBatchNumber(orderInfo.getOrDefault("batchNo", ""));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.hasText(params.getTagBatch())) {
|
|
|
|
|
record.setTagBatch(params.getTagBatch());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
record.setOperatorId(orderInfo.getOrDefault("operatorId", ""));
|
|
|
|
|
Date processTime = orderInfo.containsKey("processTime") && !orderInfo.get("processTime").isEmpty() ?
|
|
|
|
|
sdf.parse(orderInfo.get("processTime")) : nowDate;
|
|
|
|
|
Date testingTime = tagData.containsKey("测试时间") ? sdf.parse(tagData.get("测试时间")) : nowDate;
|
|
|
|
|
if (tagData.containsKey("测试时间")) {
|
|
|
|
|
record.setProcessingTime(testingTime);
|
|
|
|
|
} else {
|
|
|
|
|
record.setProcessingTime(processTime);
|
|
|
|
|
}
|
|
|
|
|
record.setTotalQuantity(Long.valueOf(orderInfo.getOrDefault("totalCount", "0")));
|
|
|
|
|
|
|
|
|
|
// 设置标签数据
|
|
|
|
|
record.setModelCode(params.getModel());
|
|
|
|
|
record.setTagSequence(tagData.get("序号"));
|
|
|
|
|
record.setTId(tagData.get("TID").replace("-", ""));
|
|
|
|
|
record.setEpc(tagData.get("EPC").replace("-", ""));
|
|
|
|
|
record.setPassword(tagData.get("密码"));
|
|
|
|
|
record.setTestResult(tagData.get("测试结果"));
|
|
|
|
|
record.setTestValue(tagData.get("测试值"));
|
|
|
|
|
record.setReferenceValue(tagData.get("参考值"));
|
|
|
|
|
record.setTestingTime(testingTime);
|
|
|
|
|
record.setFileName(params.getFileName());
|
|
|
|
|
record.setDestinationInfo(params.getDestinationInfo());
|
|
|
|
|
record.setLibraryFlag(params.getLibraryFlag());
|
|
|
|
|
record.setIntendedUse(params.getIntendedUse());
|
|
|
|
|
record.setRemark(params.getRemark());
|
|
|
|
|
|
|
|
|
|
records.add(record);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("转换Excel数据失败: {}", e.getMessage(), e);
|
|
|
|
|
throw new ServiceException("转换Excel数据失败: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return records;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导入数据
|
|
|
|
|
*
|
|
|
|
|
* @param apiContent 包含导入数据的ApiContent对象
|
|
|
|
|
* @return 处理后的ApiContent对象
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public ApiContent importData(ApiContent apiContent) {
|
|
|
|
|
Map<String, Object> fields = apiContent.getFields();
|
|
|
|
|
Map<String, Object> resFields = new HashMap<>();
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
|
|
|
|
apiContent.setFields(resFields);
|
|
|
|
|
try {
|
|
|
|
|
// 获取导入参数
|
|
|
|
|
ImportParams params = extractImportParams(fields);
|
|
|
|
|
log.info("开始处理文件,URL: {}", params.getFileUrl());
|
|
|
|
|
|
|
|
|
|
// 获取文件数据
|
|
|
|
|
byte[] fileData = httpClientUtils.getByteArrayFromUrl(params.getFileUrl());
|
|
|
|
|
if (fileData == null) {
|
|
|
|
|
handleImportError(resFields, "获取文件失败");
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析Excel数据
|
|
|
|
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(fileData)) {
|
|
|
|
|
Map<String, Object> excelResult = TagExcelUtil.parseTagExcel(bis);
|
|
|
|
|
if (!excelResult.containsKey("tagList")) {
|
|
|
|
|
handleImportError(resFields, "解析Excel文件失败,未找到标签数据");
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
String templateType = String.valueOf(excelResult.get("templateType"));
|
|
|
|
|
|
|
|
|
|
List<HwTagRecord> tagRecordList = new ArrayList<>();
|
|
|
|
|
if (templateType.equals(ApiConstants.IMPORT_TEMPLATE_ONE)) {
|
|
|
|
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
|
|
|
|
|
ExcelUtil<HwTagRecord> utils = new ExcelUtil<>(HwTagRecord.class);
|
|
|
|
|
tagRecordList = utils.importExcel(inputStream);
|
|
|
|
|
convertExcelDataTemplateOne(tagRecordList, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (templateType.equals(ApiConstants.IMPORT_TEMPLATE_TWO) || templateType.equals(ApiConstants.IMPORT_TEMPLATE_THREE)
|
|
|
|
|
|| templateType.equals(ApiConstants.IMPORT_TEMPLATE_FOUR)) {
|
|
|
|
|
// 获取订单信息和标签数据
|
|
|
|
|
Map<String, String> orderInfo = (Map<String, String>) excelResult.get("orderInfo");
|
|
|
|
|
List<Map<String, String>> tagList = (List<Map<String, String>>) excelResult.get("tagList");
|
|
|
|
|
// 转换Excel数据为HwTagRecord列表
|
|
|
|
|
tagRecordList = convertExcelDataToTagRecords(orderInfo, tagList, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (templateType.equals(ApiConstants.IMPORT_TEMPLATE_FIVE)) {
|
|
|
|
|
// 获取订单信息和标签数据
|
|
|
|
|
Map<String, String> orderInfo = new HashMap<>();
|
|
|
|
|
String fileName = params.getFileName().replace(".xlx", "").replace(".xlsx", "");
|
|
|
|
|
String lotAndNo = fileName.replaceAll("Lot-?", "");
|
|
|
|
|
String[] parts = lotAndNo.split("-");
|
|
|
|
|
String orderCode = parts[0];
|
|
|
|
|
String batchNumber = parts[1];
|
|
|
|
|
orderInfo.put("orderNo", orderCode);
|
|
|
|
|
orderInfo.put("batchNo", batchNumber);
|
|
|
|
|
List<Map<String, String>> tagList = (List<Map<String, String>>) excelResult.get("tagList");
|
|
|
|
|
orderInfo.put("totalCount", String.valueOf(tagList.size()));
|
|
|
|
|
// 转换Excel数据为HwTagRecord列表
|
|
|
|
|
tagRecordList = convertExcelDataToTagRecords(orderInfo, tagList, params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查TID是否存在
|
|
|
|
|
if (tagRecordList.stream().anyMatch(tag -> !StringUtils.hasText(tag.getTId()))) {
|
|
|
|
|
throw new IllegalArgumentException("解析excel错误:存在缺少TID的数据!");
|
|
|
|
|
}
|
|
|
|
|
// 处理标签数据
|
|
|
|
|
ImportResult result = processTagList(tagRecordList, params);
|
|
|
|
|
successCount = result.getSuccessCount();
|
|
|
|
|
failCount = result.getFailCount();
|
|
|
|
|
List<String> errorMessages = new ArrayList<>(result.getErrorMessages());
|
|
|
|
|
|
|
|
|
|
// 设置导入结果
|
|
|
|
|
if (failCount > 0) {
|
|
|
|
|
throw new ServiceException(String.format(params.getImportMode() + "失败:%d条。失败原因:%s",
|
|
|
|
|
failCount, String.join("; ", errorMessages)));
|
|
|
|
|
} else {
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "导入成功");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, String.format(params.getImportMode() + "成功导入%d条数据", successCount));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理文件失败: {}", e.getMessage(), e);
|
|
|
|
|
handleImportError(resFields, e.getMessage());
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理模板一的Excel数据
|
|
|
|
|
* 根据导入参数更新标签记录
|
|
|
|
|
*
|
|
|
|
|
* @param tagRecordList 标签记录列表
|
|
|
|
|
* @param params 导入参数
|
|
|
|
|
*/
|
|
|
|
|
private void convertExcelDataTemplateOne(List<HwTagRecord> tagRecordList, ImportParams params) {
|
|
|
|
|
for (HwTagRecord tagRecord : tagRecordList) {
|
|
|
|
|
// 设置型号
|
|
|
|
|
if (StringUtils.hasText(params.getModel())) {
|
|
|
|
|
tagRecord.setModelCode(params.getModel());
|
|
|
|
|
}
|
|
|
|
|
// 设置文件名
|
|
|
|
|
if (StringUtils.hasText(params.getFileName())) {
|
|
|
|
|
tagRecord.setFileName(params.getFileName());
|
|
|
|
|
}
|
|
|
|
|
// 设置去向信息
|
|
|
|
|
if (StringUtils.hasText(params.getDestinationInfo())) {
|
|
|
|
|
tagRecord.setDestinationInfo(params.getDestinationInfo());
|
|
|
|
|
}
|
|
|
|
|
// 设置是否在库
|
|
|
|
|
if (StringUtils.hasText(params.getLibraryFlag())) {
|
|
|
|
|
tagRecord.setLibraryFlag(params.getLibraryFlag());
|
|
|
|
|
}
|
|
|
|
|
// 设置计划用途
|
|
|
|
|
if (StringUtils.hasText(params.getIntendedUse())) {
|
|
|
|
|
tagRecord.setIntendedUse(params.getIntendedUse());
|
|
|
|
|
}
|
|
|
|
|
// 设置备注
|
|
|
|
|
if (StringUtils.hasText(params.getRemark())) {
|
|
|
|
|
tagRecord.setRemark(params.getRemark());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理导入错误
|
|
|
|
|
*/
|
|
|
|
|
private void handleImportError(Map<String, Object> resFields, String errorMessage) {
|
|
|
|
|
log.error("导入错误: {}", errorMessage);
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "导入失败");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理标签列表
|
|
|
|
|
*/
|
|
|
|
|
private ImportResult processTagList(List<HwTagRecord> records, ImportParams params) {
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public ImportResult processTagList(List<HwTagRecord> records, ImportParams params) {
|
|
|
|
|
ImportResult result = new ImportResult();
|
|
|
|
|
List<HwTagRecord> batchList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// 校验当前批次中TID是否重复
|
|
|
|
|
Set<String> tidSet = new HashSet<>();
|
|
|
|
|
for (HwTagRecord record : records) {
|
|
|
|
|
if (!tidSet.add(record.getTId())) {
|
|
|
|
|
String errorMsg = String.format("当前批次中TID重复:%s", record.getTId());
|
|
|
|
|
result.getErrorMessages().add(errorMsg);
|
|
|
|
|
result.incrementFailCount();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验当前批次中EPC是否重复
|
|
|
|
|
Set<String> epcSet = new HashSet<>();
|
|
|
|
|
for (HwTagRecord record : records) {
|
|
|
|
|
if (!epcSet.add(record.getEpc())) {
|
|
|
|
|
String errorMsg = String.format("当前批次中EPC重复:%s", record.getEpc());
|
|
|
|
|
result.getErrorMessages().add(errorMsg);
|
|
|
|
|
result.incrementFailCount();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取已存在的TID和EPC
|
|
|
|
|
Set<String> existingTids = getExistingTids(records, params);
|
|
|
|
|
List<HwTagRecord> existingEpcs = getExistingEpcs(records, params);
|
|
|
|
@ -592,7 +393,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
|
|
|
|
|
*/
|
|
|
|
|
private void processBatchInsert(List<HwTagRecord> batchList, ImportResult result) {
|
|
|
|
|
this.batchInsertHwTagRecord(batchList);
|
|
|
|
|
log.info("批量插入{}条数据成功", batchList.size());
|
|
|
|
|
//log.info("批量插入{}条数据成功", batchList.size());
|
|
|
|
|
result.addSuccessCount(batchList.size());
|
|
|
|
|
batchList.clear();
|
|
|
|
|
}
|
|
|
|
@ -602,7 +403,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService {
|
|
|
|
|
*/
|
|
|
|
|
private void handleTagProcessingError(HwTagRecord record, Exception e, ImportResult result) {
|
|
|
|
|
String errorMsg = String.format("处理数据失败 - TID: %s, 错误: %s", record.getTId(), e.getMessage());
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
//log.error(errorMsg);
|
|
|
|
|
result.getErrorMessages().add(errorMsg);
|
|
|
|
|
result.incrementFailCount();
|
|
|
|
|
}
|
|
|
|
|