diff --git a/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java b/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java index d1f8882..7427b03 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java @@ -13,7 +13,8 @@ public class ApiConstants { * 本机IP端口 */ // public static final String BASE_URL = "http://127.0.0.1:9000/docs/export/"; - public static final String BASE_URL = "http://database.rfidtire.com/docs/export/"; +// public static final String BASE_URL = "http://database.rfidtire.com/docs/export/"; + public static final String BASE_URL = "http://1.13.177.47/docs/export/"; /** * 过期时间(毫秒) 6小时 @@ -57,6 +58,11 @@ public class ApiConstants { */ public static final String DATA_IMPORT = "10"; + /** + * 快速数据导入 + */ + public static final String FAST_DATA_IMPORT = "11"; + /** * 数据查询 */ diff --git a/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java b/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java index 02714ab..cae2931 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java @@ -90,6 +90,15 @@ public class KDocsApiController extends BaseController { List apiContents = ikDocsService.importDataList(content); apiResponse.setResult(apiContents); return apiResponse; + case ApiConstants.FAST_DATA_IMPORT: + // 快速数据导入 + if (content == null || content.isEmpty()) { + log.warn("快速导入数据为空"); + return apiResponse; + } + List fastApiContents = ikDocsService.fastImportDataList(content); + apiResponse.setResult(fastApiContents); + return apiResponse; case ApiConstants.DATA_QUERY: // 数据查询 if (content == null || content.isEmpty()) { diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/IHwTagRecordService.java b/tagApi-service/src/main/java/hw/tagApi/service/service/IHwTagRecordService.java index 552d489..ba70c22 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/service/IHwTagRecordService.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/IHwTagRecordService.java @@ -3,6 +3,7 @@ package hw.tagApi.service.service; import java.util.List; import java.util.Set; +import hw.tagApi.service.domain.ApiContent; import hw.tagApi.service.domain.HwTagRecord; /** @@ -106,4 +107,12 @@ public interface IHwTagRecordService * @return 已存在的EPC集合 */ public Set selectExistingEpcs(List epcs); + + /** + * 导入数据 + * + * @param apiContent 包含导入数据的ApiContent对象 + * @return 处理后的ApiContent对象 + */ + public ApiContent importData(ApiContent apiContent); } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java b/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java index 06e2501..44ef34b 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java @@ -12,13 +12,6 @@ import java.util.List; */ public interface IKDocsService { - /** - * 获取系统状态 - * - * @return 系统状态 - */ - String getSystemStatus(); - /** * 导入数据 * @@ -42,4 +35,12 @@ public interface IKDocsService { * @return 导出结果列表 */ List exportDataList(List content); + + /** + * 快速导入数据 + * + * @param content 数据内容 + * @return 处理结果 + */ + List fastImportDataList(List content); } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java index db2decf..2b9e04a 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java @@ -1,113 +1,125 @@ 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.uuid.IdGenerator; +import hw.tagApi.service.constant.ApiConstants; +import hw.tagApi.service.domain.ApiContent; +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; import org.springframework.stereotype.Service; import hw.tagApi.service.mapper.HwTagRecordMapper; 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.startsWithCheck; /** * 标签记录Service业务层处理 - * + * * @author Yinq * @date 2025-04-15 */ @Service -public class HwTagRecordServiceImpl implements IHwTagRecordService -{ +public class HwTagRecordServiceImpl implements IHwTagRecordService { + + private static final Logger log = LoggerFactory.getLogger(HwTagRecordServiceImpl.class); + @Autowired private HwTagRecordMapper hwTagRecordMapper; + private static final int BATCH_SIZE = 100; + /** * 查询标签记录 - * + * * @param rId 标签记录主键 * @return 标签记录 */ @Override - public HwTagRecord selectHwTagRecordByRId(Long rId) - { + public HwTagRecord selectHwTagRecordByRId(Long rId) { return hwTagRecordMapper.selectHwTagRecordByRId(rId); } /** * 查询标签记录列表 - * + * * @param hwTagRecord 标签记录 * @return 标签记录 */ @Override - public List selectHwTagRecordList(HwTagRecord hwTagRecord) - { + public List selectHwTagRecordList(HwTagRecord hwTagRecord) { return hwTagRecordMapper.selectHwTagRecordList(hwTagRecord); } /** * 新增标签记录 - * + * * @param hwTagRecord 标签记录 * @return 结果 */ @Override - public int insertHwTagRecord(HwTagRecord hwTagRecord) - { + public int insertHwTagRecord(HwTagRecord hwTagRecord) { hwTagRecord.setCreateTime(DateUtils.getNowDate()); return hwTagRecordMapper.insertHwTagRecord(hwTagRecord); } /** * 修改标签记录 - * + * * @param hwTagRecord 标签记录 * @return 结果 */ @Override - public int updateHwTagRecord(HwTagRecord hwTagRecord) - { + public int updateHwTagRecord(HwTagRecord hwTagRecord) { initTagRecord(hwTagRecord); return hwTagRecordMapper.updateHwTagRecord(hwTagRecord); } /** * 初始化hwTagRecord + * * @param hwTagRecord hwTagRecord */ - public void initTagRecord(HwTagRecord hwTagRecord){ + public void initTagRecord(HwTagRecord hwTagRecord) { hwTagRecord.setRId(IdGenerator.nextId()); hwTagRecord.setCreateTime(DateUtils.getNowDate()); } /** * 批量删除标签记录 - * + * * @param rIds 需要删除的标签记录主键 * @return 结果 */ @Override - public int deleteHwTagRecordByRIds(Long[] rIds) - { + public int deleteHwTagRecordByRIds(Long[] rIds) { return hwTagRecordMapper.deleteHwTagRecordByRIds(rIds); } /** * 删除标签记录信息 - * + * * @param rId 标签记录主键 * @return 结果 */ @Override - public int deleteHwTagRecordByRId(Long rId) - { + public int deleteHwTagRecordByRId(Long rId) { return hwTagRecordMapper.deleteHwTagRecordByRId(rId); } /** * 根据TID更新标签记录 - * + * * @param record 标签记录 */ @Override @@ -117,7 +129,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService /** * 批量插入标签记录 - * + * * @param batchList 标签记录列表 */ @Override @@ -134,7 +146,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService @Override public HwTagRecord selectHwTagRecordData(HwTagRecord selectTagRecord) { List tagRecordList = hwTagRecordMapper.selectHwTagRecordList(selectTagRecord); - if (!tagRecordList.isEmpty()){ + if (!tagRecordList.isEmpty()) { HwTagRecord tagRecord = tagRecordList.get(0); tagRecord.setQueryResultsNumber(tagRecordList.size()); return tagRecord; @@ -179,4 +191,195 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService } return new HashSet<>(hwTagRecordMapper.selectExistingEpcs(epcs)); } + + /** + * 导入数据 + * + * @param apiContent 包含导入数据的ApiContent对象 + * @return 处理后的ApiContent对象 + */ + @Transactional(rollbackFor = Exception.class) + public ApiContent importData(ApiContent apiContent) { + Map 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 batchList = new ArrayList<>(); + Map resFields = new HashMap<>(); + List 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 excelResult = TagExcelUtil.parseTagExcel(bis); + if (!excelResult.containsKey("tagList")) { + log.error("解析Excel文件失败,未找到标签数据"); + resFields.put(ApiConstants.IMPORT_STATUS, "导入失败"); + resFields.put(ApiConstants.DESCRIPTION, "解析Excel文件失败,未找到标签数据"); + return apiContent; + } + // 获取订单信息 + Map orderInfo = (Map) excelResult.get("orderInfo"); + // 获取标签数据列表 + List> tagList = (List>) excelResult.get("tagList"); + // 批量处理标签数据 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + // 如果不是更新模式,先批量查询所有TID + Set existingTids = new HashSet<>(); + boolean tIdFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_ADD_NO_CHECK.equals(importMode); + if (tIdFlag) { + List allTids = tagList.stream() + .map(tag -> tag.get("TID").replace("-", "")) + .collect(Collectors.toList()); + existingTids = this.selectExistingTids(allTids); + } + // 如果不是更新模式,批量查询所有EPC + Set existingEpcs = new HashSet<>(); + boolean epcCheckFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_UPDATE.equals(importMode); + if (epcCheckFlag) { + List allEpcs = tagList.stream() + .map(tag -> tag.get("EPC").replace("-", "")) + .collect(Collectors.toList()); + existingEpcs = this.selectExistingEpcs(allEpcs); + } + + for (Map 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) { + this.updateHwTagRecordByTID(record); + successCount++; + continue; + } + record.setRId(IdGenerator.nextId()); + // 添加到批处理列表 + batchList.add(record); + // 达到批量大小时执行插入 + if (batchList.size() >= BATCH_SIZE && failCount == 0) { + this.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) { + this.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; + } + + } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java index 9da099d..6114209 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java @@ -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 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 batchList = new ArrayList<>(); - Map resFields = new HashMap<>(); - List 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 excelResult = TagExcelUtil.parseTagExcel(bis); - if (!excelResult.containsKey("tagList")) { - log.error("解析Excel文件失败,未找到标签数据"); - resFields.put(ApiConstants.IMPORT_STATUS, "导入失败"); - resFields.put(ApiConstants.DESCRIPTION, "解析Excel文件失败,未找到标签数据"); - return apiContent; - } - // 获取订单信息 - Map orderInfo = (Map) excelResult.get("orderInfo"); - // 获取标签数据列表 - List> tagList = (List>) excelResult.get("tagList"); - // 批量处理标签数据 - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - // 如果不是更新模式,先批量查询所有TID - Set existingTids = new HashSet<>(); - boolean tIdFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_ADD_NO_CHECK.equals(importMode); - if (tIdFlag) { - List allTids = tagList.stream() - .map(tag -> tag.get("TID").replace("-", "")) - .collect(Collectors.toList()); - existingTids = tagRecordService.selectExistingTids(allTids); - } - // 如果不是更新模式,批量查询所有EPC - Set existingEpcs = new HashSet<>(); - boolean epcCheckFlag = ApiConstants.MODE_ADD.equals(importMode) || ApiConstants.MODE_UPDATE.equals(importMode); - if (epcCheckFlag) { - List allEpcs = tagList.stream() - .map(tag -> tag.get("EPC").replace("-", "")) - .collect(Collectors.toList()); - existingEpcs = tagRecordService.selectExistingEpcs(allEpcs); - } - - for (Map 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 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 fastImportDataList(List content) { + List 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 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; + } } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/utils/httpClientUtils.java b/tagApi-service/src/main/java/hw/tagApi/service/utils/httpClientUtils.java index 6e3bc04..c222afe 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/utils/httpClientUtils.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/utils/httpClientUtils.java @@ -92,7 +92,7 @@ public class httpClientUtils { * * @param resList 处理结果列表 */ - public static void sendResultToCallback(List resList, String url) { + public static void sendResultToCallback(List resList, String url, String caseCode) { if (resList == null || resList.isEmpty()) { log.warn("处理结果列表为空,不发送回调请求"); return; @@ -111,7 +111,7 @@ public class httpClientUtils { // 创建响应对象 ApiResponse response = new ApiResponse(); - response.setCaseCode(ApiConstants.DATA_IMPORT); + response.setCaseCode(caseCode); response.setResult(resList); // 将对象转换为JSON字符串