|
|
|
@ -1,10 +1,15 @@
|
|
|
|
|
package hw.tagApi.service.service.impl;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.common.exception.ServiceException;
|
|
|
|
|
import hw.tagApi.common.utils.poi.ExcelUtil;
|
|
|
|
|
import hw.tagApi.service.domain.ImportParams;
|
|
|
|
|
import hw.tagApi.service.domain.ImportResult;
|
|
|
|
|
import hw.tagApi.service.service.IHwTagRecordService;
|
|
|
|
|
import hw.tagApi.service.service.IKDocsService;
|
|
|
|
|
import hw.tagApi.service.utils.TagExcelUtil;
|
|
|
|
|
import hw.tagApi.service.utils.httpClientUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@ -22,6 +27,8 @@ import hw.tagApi.service.domain.ApiContent;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import static hw.tagApi.service.utils.TagExcelUtil.extractImportParams;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 云文档服务实现类
|
|
|
|
@ -38,12 +45,12 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
|
|
|
|
|
// 创建线程池
|
|
|
|
|
private static final ExecutorService executorService = new ThreadPoolExecutor(
|
|
|
|
|
5, // 核心线程数
|
|
|
|
|
10, // 最大线程数
|
|
|
|
|
60L, // 空闲线程存活时间
|
|
|
|
|
TimeUnit.SECONDS, // 时间单位
|
|
|
|
|
new LinkedBlockingQueue<>(100), // 任务队列
|
|
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略:由调用线程处理
|
|
|
|
|
2, // 核心线程数
|
|
|
|
|
5, // 最大线程数
|
|
|
|
|
30L, // 空闲线程存活时间
|
|
|
|
|
TimeUnit.SECONDS,
|
|
|
|
|
new LinkedBlockingQueue<>(20), // 队列容量
|
|
|
|
|
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略(改为直接拒绝并记录日志)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -60,16 +67,17 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
executorService.execute(() -> {
|
|
|
|
|
List<ApiContent> resList = new ArrayList<>();
|
|
|
|
|
int count = 0;
|
|
|
|
|
int runSize = 10;
|
|
|
|
|
for (ApiContent apiContent : contentList) {
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
ApiContent resContent = new ApiContent();
|
|
|
|
|
resContent.setId(apiContent.getId());
|
|
|
|
|
try {
|
|
|
|
|
ApiContent result = tagRecordService.importData(apiContent);
|
|
|
|
|
ApiContent result = this.importData(apiContent);
|
|
|
|
|
// 更新响应内容
|
|
|
|
|
resContent.setFields(result.getFields());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("导入任务执行失败: {}", e.getMessage(), e);
|
|
|
|
|
//log.error("导入任务执行失败: {}", e.getMessage(), e);
|
|
|
|
|
Map<String, Object> errorFields = new HashMap<>();
|
|
|
|
|
errorFields.put(ApiConstants.IMPORT_STATUS, "导入失败");
|
|
|
|
|
errorFields.put(ApiConstants.DESCRIPTION, "导入任务执行失败: " + e.getMessage());
|
|
|
|
@ -78,11 +86,11 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
resList.add(resContent);
|
|
|
|
|
count++;
|
|
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
|
|
log.info("数据库导入完成 - ID: {}, 耗时: {}ms", apiContent.getId(), (endTime - startTime));
|
|
|
|
|
log.info("数据库导入完成 - ID: {}, 耗时: {}ms, 共: {}个文件, 第: {}个文件。", apiContent.getId(), (endTime - startTime), contentList.size(), count);
|
|
|
|
|
|
|
|
|
|
// 每处理10个数据就发送一次回调
|
|
|
|
|
if (count % 10 == 0) {
|
|
|
|
|
log.info("已处理{}条数据,发送回调", count);
|
|
|
|
|
if (count % runSize == 0) {
|
|
|
|
|
//log.info("已处理{}条数据,发送回调", count);
|
|
|
|
|
httpClientUtils.sendResultToCallback(resList, ApiConstants.CALLBACK_URL, ApiConstants.DATA_IMPORT);
|
|
|
|
|
resList.clear(); // 清空列表,准备下一批数据
|
|
|
|
|
}
|
|
|
|
@ -90,7 +98,7 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
|
|
|
|
|
// 处理剩余的数据(不足10条的部分)
|
|
|
|
|
if (!resList.isEmpty()) {
|
|
|
|
|
log.info("处理剩余{}条数据,发送回调", resList.size());
|
|
|
|
|
//log.info("处理剩余{}条数据,发送回调", resList.size());
|
|
|
|
|
httpClientUtils.sendResultToCallback(resList, ApiConstants.CALLBACK_URL, ApiConstants.DATA_IMPORT);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
@ -388,7 +396,7 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
ApiContent resContent = new ApiContent();
|
|
|
|
|
resContent.setId(apiContent.getId());
|
|
|
|
|
try {
|
|
|
|
|
ApiContent result = tagRecordService.importData(apiContent);
|
|
|
|
|
ApiContent result = this.importData(apiContent);
|
|
|
|
|
// 更新响应内容
|
|
|
|
|
resContent.setFields(result.getFields());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -623,4 +631,217 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导入数据
|
|
|
|
|
*
|
|
|
|
|
* @param apiContent 包含导入数据的ApiContent对象
|
|
|
|
|
* @return 处理后的ApiContent对象
|
|
|
|
|
*/
|
|
|
|
|
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 = tagRecordService.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 (hw.tagApi.common.utils.StringUtils.hasText(params.getModel())) {
|
|
|
|
|
tagRecord.setModelCode(params.getModel());
|
|
|
|
|
}
|
|
|
|
|
// 设置文件名
|
|
|
|
|
if (hw.tagApi.common.utils.StringUtils.hasText(params.getFileName())) {
|
|
|
|
|
tagRecord.setFileName(params.getFileName());
|
|
|
|
|
}
|
|
|
|
|
// 设置去向信息
|
|
|
|
|
if (hw.tagApi.common.utils.StringUtils.hasText(params.getDestinationInfo())) {
|
|
|
|
|
tagRecord.setDestinationInfo(params.getDestinationInfo());
|
|
|
|
|
}
|
|
|
|
|
// 设置是否在库
|
|
|
|
|
if (hw.tagApi.common.utils.StringUtils.hasText(params.getLibraryFlag())) {
|
|
|
|
|
tagRecord.setLibraryFlag(params.getLibraryFlag());
|
|
|
|
|
}
|
|
|
|
|
// 设置计划用途
|
|
|
|
|
if (hw.tagApi.common.utils.StringUtils.hasText(params.getIntendedUse())) {
|
|
|
|
|
tagRecord.setIntendedUse(params.getIntendedUse());
|
|
|
|
|
}
|
|
|
|
|
// 设置备注
|
|
|
|
|
if (hw.tagApi.common.utils.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将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 (hw.tagApi.common.utils.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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|