|
|
|
@ -1,35 +1,26 @@
|
|
|
|
|
package hw.tagApi.service.service.impl;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.common.exception.ServiceException;
|
|
|
|
|
import hw.tagApi.common.utils.uuid.IdGenerator;
|
|
|
|
|
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.rmi.server.ServerCloneException;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import hw.tagApi.service.constant.ApiConstants;
|
|
|
|
|
import hw.tagApi.service.domain.HwTagRecord;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import hw.tagApi.service.domain.ApiResponse;
|
|
|
|
|
import hw.tagApi.service.domain.ApiContent;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import static hw.tagApi.service.utils.TagExcelUtil.startsWithCheck;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 云文档服务实现类
|
|
|
|
@ -44,9 +35,6 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private IHwTagRecordService tagRecordService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final int BATCH_SIZE = 100;
|
|
|
|
|
|
|
|
|
|
// 创建线程池
|
|
|
|
|
private static final ExecutorService executorService = new ThreadPoolExecutor(
|
|
|
|
|
5, // 核心线程数
|
|
|
|
@ -75,7 +63,7 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
ApiContent resContent = new ApiContent();
|
|
|
|
|
resContent.setId(apiContent.getId());
|
|
|
|
|
try {
|
|
|
|
|
ApiContent result = this.importData(apiContent);
|
|
|
|
|
ApiContent result = tagRecordService.importData(apiContent);
|
|
|
|
|
// 更新响应内容
|
|
|
|
|
resContent.setFields(result.getFields());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -91,7 +79,7 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送处理结果到回调接口
|
|
|
|
|
httpClientUtils.sendResultToCallback(resList, ApiConstants.IN_CALLBACK_URL);
|
|
|
|
|
httpClientUtils.sendResultToCallback(resList, ApiConstants.IN_CALLBACK_URL, ApiConstants.DATA_IMPORT);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return resApiContentList;
|
|
|
|
@ -118,213 +106,6 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
return resApiContentList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导入数据
|
|
|
|
|
*
|
|
|
|
|
* @param apiContent 包含导入数据的ApiContent对象
|
|
|
|
|
* @return 处理后的ApiContent对象
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public ApiContent importData(ApiContent apiContent) {
|
|
|
|
|
Map<String, Object> fields = apiContent.getFields();
|
|
|
|
|
String importMode = String.valueOf(fields.get("导入模式"));
|
|
|
|
|
String model = String.valueOf(fields.getOrDefault("型号", ""));
|
|
|
|
|
String fileUrl = String.valueOf(fields.get("文件链接"));
|
|
|
|
|
String fileName = String.valueOf(fields.getOrDefault("文件名", ""));
|
|
|
|
|
String TIDCheck = String.valueOf(fields.getOrDefault("TID校验", ""));
|
|
|
|
|
String EPCCheck = String.valueOf(fields.getOrDefault("EPC校验", ""));
|
|
|
|
|
String EPCNumberCheck = String.valueOf(fields.getOrDefault("EPC位数校验", "0"));
|
|
|
|
|
int EPCNumberCheckLength = Integer.parseInt(EPCNumberCheck);
|
|
|
|
|
int tidCharCount = TIDCheck.length();
|
|
|
|
|
int epcCharCount = EPCCheck.length();
|
|
|
|
|
List<HwTagRecord> batchList = new ArrayList<>();
|
|
|
|
|
Map<String, Object> resFields = new HashMap<>();
|
|
|
|
|
List<String> errorMessages = new ArrayList<>();
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
int failCount = 0;
|
|
|
|
|
apiContent.setFields(resFields);
|
|
|
|
|
try {
|
|
|
|
|
log.info("开始处理文件,URL: {}", fileUrl);
|
|
|
|
|
// 获取文件字节数组
|
|
|
|
|
byte[] fileData = httpClientUtils.getByteArrayFromUrl(fileUrl);
|
|
|
|
|
if (fileData == null) {
|
|
|
|
|
log.error("获取文件失败");
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "系统错误");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, "获取文件失败");
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析Excel数据
|
|
|
|
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(fileData)) {
|
|
|
|
|
Map<String, Object> excelResult = TagExcelUtil.parseTagExcel(bis);
|
|
|
|
|
if (!excelResult.containsKey("tagList")) {
|
|
|
|
|
log.error("解析Excel文件失败,未找到标签数据");
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "导入失败");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, "解析Excel文件失败,未找到标签数据");
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
// 获取订单信息
|
|
|
|
|
Map<String, String> orderInfo = (Map<String, String>) excelResult.get("orderInfo");
|
|
|
|
|
// 获取标签数据列表
|
|
|
|
|
List<Map<String, String>> tagList = (List<Map<String, String>>) excelResult.get("tagList");
|
|
|
|
|
// 批量处理标签数据
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
// 如果不是更新模式,先批量查询所有TID
|
|
|
|
|
Set<String> existingTids = new HashSet<>();
|
|
|
|
|
boolean tIdFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_ADD_NO_CHECK.equals(importMode);
|
|
|
|
|
if (tIdFlag) {
|
|
|
|
|
List<String> allTids = tagList.stream()
|
|
|
|
|
.map(tag -> tag.get("TID").replace("-", ""))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
existingTids = tagRecordService.selectExistingTids(allTids);
|
|
|
|
|
}
|
|
|
|
|
// 如果不是更新模式,批量查询所有EPC
|
|
|
|
|
Set<String> existingEpcs = new HashSet<>();
|
|
|
|
|
boolean epcCheckFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_UPDATE.equals(importMode);
|
|
|
|
|
if (epcCheckFlag) {
|
|
|
|
|
List<String> allEpcs = tagList.stream()
|
|
|
|
|
.map(tag -> tag.get("EPC").replace("-", ""))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
existingEpcs = tagRecordService.selectExistingEpcs(allEpcs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Map<String, String> tagData : tagList) {
|
|
|
|
|
try {
|
|
|
|
|
String tid = tagData.get("TID").replace("-", "");
|
|
|
|
|
String epc = tagData.get("EPC").replace("-", "");
|
|
|
|
|
if (tid.length() != ApiConstants.TE_LENGTH || (EPCNumberCheckLength > 0 && epc.length() != EPCNumberCheckLength)) {
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
if (tid.length() != ApiConstants.TE_LENGTH) {
|
|
|
|
|
String errorMsg = String.format("TID长度校验失败!预期长度:24,实际长度:%d,TID值:%s", tid.length(), tid);
|
|
|
|
|
builder.append(errorMsg);
|
|
|
|
|
log.error("异常 - {}", errorMsg);
|
|
|
|
|
}
|
|
|
|
|
if (EPCNumberCheckLength > 0 && epc.length() != EPCNumberCheckLength) {
|
|
|
|
|
String errorMsg = String.format("EPC长度校验失败!预期长度:24,实际长度:%d,EPC值:%s", epc.length(), epc);
|
|
|
|
|
builder.append(errorMsg);
|
|
|
|
|
log.error("EPC长度校验异常 - {}", errorMsg);
|
|
|
|
|
}
|
|
|
|
|
errorMessages.add(builder.toString());
|
|
|
|
|
failCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (tidCharCount > 0 && !startsWithCheck(tid, TIDCheck)) {
|
|
|
|
|
String errorMsg = String.format("TID校验失败!TID校验:%s,实际TID:%s", TIDCheck, tid);
|
|
|
|
|
log.error("TID校验异常 - {}", errorMsg);
|
|
|
|
|
errorMessages.add(errorMsg);
|
|
|
|
|
failCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (epcCharCount > 0 && !startsWithCheck(epc, EPCCheck)) {
|
|
|
|
|
String errorMsg = String.format("EPC校验失败!EPC校验:%s,实际EPC:%s", EPCCheck, epc);
|
|
|
|
|
log.error("EPC校验异常 - {}", errorMsg);
|
|
|
|
|
errorMessages.add(errorMsg);
|
|
|
|
|
failCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
HwTagRecord record = new HwTagRecord();
|
|
|
|
|
record.setOrderCode(orderInfo.get("orderNo"));
|
|
|
|
|
record.setBatchNumber(orderInfo.get("batchNo"));
|
|
|
|
|
record.setOperatorId(orderInfo.get("operatorId"));
|
|
|
|
|
record.setProcessingTime(sdf.parse(orderInfo.get("processTime")));
|
|
|
|
|
record.setTotalQuantity(Long.valueOf(orderInfo.get("totalCount")));
|
|
|
|
|
// 设置标签数据
|
|
|
|
|
record.setModelCode(model);
|
|
|
|
|
record.setTagSequence(tagData.get("序号"));
|
|
|
|
|
record.setTId(tid);
|
|
|
|
|
record.setEpc(epc);
|
|
|
|
|
record.setPassword(tagData.get("密码"));
|
|
|
|
|
record.setTestResult(tagData.get("测试结果"));
|
|
|
|
|
record.setTestValue(tagData.get("测试值"));
|
|
|
|
|
record.setReferenceValue(tagData.get("参考值"));
|
|
|
|
|
record.setTestingTime(sdf.parse(tagData.get("测试时间")));
|
|
|
|
|
record.setFileName(fileName);
|
|
|
|
|
|
|
|
|
|
//校验tId唯一
|
|
|
|
|
if (tIdFlag && existingTids.contains(tid)) {
|
|
|
|
|
String errorMsg = String.format("TID已存在:%s", tid);
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
errorMessages.add(errorMsg);
|
|
|
|
|
failCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//校验EPC唯一
|
|
|
|
|
if (epcCheckFlag && existingEpcs.contains(epc)) {
|
|
|
|
|
String errorMsg = String.format("EPC已存在:%s", epc);
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
errorMessages.add(errorMsg);
|
|
|
|
|
failCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// 更新方法
|
|
|
|
|
if ((ApiConstants.MODE_UPDATE.equals(importMode) || ApiConstants.MODE_UPDATE_NO_CHECK.equals(importMode)) && failCount == 0) {
|
|
|
|
|
tagRecordService.updateHwTagRecordByTID(record);
|
|
|
|
|
successCount++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
record.setRId(IdGenerator.nextId());
|
|
|
|
|
// 添加到批处理列表
|
|
|
|
|
batchList.add(record);
|
|
|
|
|
// 达到批量大小时执行插入
|
|
|
|
|
if (batchList.size() >= BATCH_SIZE && failCount == 0) {
|
|
|
|
|
tagRecordService.batchInsertHwTagRecord(batchList);
|
|
|
|
|
log.info("批量插入{}条数据成功", batchList.size());
|
|
|
|
|
successCount += batchList.size();
|
|
|
|
|
batchList.clear();
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String errorMsg = String.format("处理数据失败:%s", e.getMessage());
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
errorMessages.add(errorMsg);
|
|
|
|
|
failCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理剩余的数据
|
|
|
|
|
if (!batchList.isEmpty() && failCount == 0) {
|
|
|
|
|
tagRecordService.batchInsertHwTagRecord(batchList);
|
|
|
|
|
log.info("批量插入剩余{}条数据成功", batchList.size());
|
|
|
|
|
successCount += batchList.size();
|
|
|
|
|
batchList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置导入结果
|
|
|
|
|
if (failCount > 0) {
|
|
|
|
|
throw new ServiceException(String.format(importMode + "失败:%d条。失败原因:%s",
|
|
|
|
|
failCount, String.join("; ", errorMessages)));
|
|
|
|
|
} else {
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "导入成功");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, String.format("成功导入%d条数据", successCount));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("处理文件失败: {}", e.getMessage(), e);
|
|
|
|
|
resFields.put(ApiConstants.IMPORT_STATUS, "导入失败");
|
|
|
|
|
resFields.put(ApiConstants.DESCRIPTION, e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getSystemStatus() {
|
|
|
|
|
try {
|
|
|
|
|
ApiResponse response = new ApiResponse();
|
|
|
|
|
response.setCaseCode("10");
|
|
|
|
|
// 添加导入模式信息
|
|
|
|
|
ApiContent modeContent = new ApiContent();
|
|
|
|
|
Map<String, String> modeFields = new HashMap<>();
|
|
|
|
|
modeFields.put(ApiConstants.DESCRIPTION, "");
|
|
|
|
|
// 使用ObjectMapper将对象转换为JSON字符串
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
return mapper.writeValueAsString(response);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("生成系统状态响应失败", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询数据
|
|
|
|
|
*
|
|
|
|
@ -517,7 +298,7 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
log.info("数据库导出完成 - ID: {}, 记录数: {}, 耗时: {}分{}秒", condition.getId(), records.size(), minutes, seconds);
|
|
|
|
|
responseFields.put(ApiConstants.EXPORT_STATUS, "导出成功");
|
|
|
|
|
responseFields.put(ApiConstants.DESCRIPTION, "耗时: " + minutes + "分" + seconds + "秒");
|
|
|
|
|
responseFields.put("文件路径", filePath);
|
|
|
|
|
responseFields.put("文件链接", filePath);
|
|
|
|
|
responseFields.put("链接有效期", sdf.format(futureDate));
|
|
|
|
|
log.info("导出文件生成成功 - ID: {}, 路径: {}", condition.getId(), filePath);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -544,13 +325,42 @@ public class KDocsServiceImpl implements IKDocsService {
|
|
|
|
|
resApiContentList.add(response);
|
|
|
|
|
}
|
|
|
|
|
// 发送处理结果到回调接口
|
|
|
|
|
httpClientUtils.sendResultToCallback(resApiContentList, ApiConstants.OUT_CALLBACK_URL);
|
|
|
|
|
httpClientUtils.sendResultToCallback(resApiContentList, ApiConstants.OUT_CALLBACK_URL, ApiConstants.DATA_EXPORT);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 快速导入数据
|
|
|
|
|
*
|
|
|
|
|
* @param content 数据内容
|
|
|
|
|
* @return 处理结果
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public List<ApiContent> fastImportDataList(List<ApiContent> content) {
|
|
|
|
|
List<ApiContent> resList = new ArrayList<>();
|
|
|
|
|
for (ApiContent apiContent : content) {
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
|
ApiContent resContent = new ApiContent();
|
|
|
|
|
resContent.setId(apiContent.getId());
|
|
|
|
|
try {
|
|
|
|
|
ApiContent result = tagRecordService.importData(apiContent);
|
|
|
|
|
// 更新响应内容
|
|
|
|
|
resContent.setFields(result.getFields());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("快速导入任务执行失败: {}", e.getMessage(), e);
|
|
|
|
|
Map<String, Object> errorFields = new HashMap<>();
|
|
|
|
|
errorFields.put(ApiConstants.IMPORT_STATUS, "导入失败");
|
|
|
|
|
errorFields.put(ApiConstants.DESCRIPTION, "快速导入任务执行失败: " + e.getMessage());
|
|
|
|
|
resContent.setFields(errorFields);
|
|
|
|
|
}
|
|
|
|
|
resList.add(resContent);
|
|
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
|
|
log.info("数据库快速导入完成 - ID: {}, 耗时: {}ms", apiContent.getId(), (endTime - startTime));
|
|
|
|
|
}
|
|
|
|
|
return resList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|