From 6dd66a44c918f1728081e9783797878243312a30 Mon Sep 17 00:00:00 2001 From: yinq Date: Wed, 30 Apr 2025 13:46:23 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=B7=BB=E5=8A=A0=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=87=8D=E8=AF=95=E6=9C=BA=E5=88=B6=E3=80=81?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tagApi/service/constant/ApiConstants.java | 11 +- .../service/impl/HwTagRecordServiceImpl.java | 4 +- .../tagApi/service/utils/httpClientUtils.java | 150 ++++++++++++++---- .../mapper/service/HwTagRecordMapper.xml | 99 +++++++----- 4 files changed, 183 insertions(+), 81 deletions(-) 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 ab0ace0..0c78984 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 @@ -19,7 +19,7 @@ public class ApiConstants { /** * 过期时间(毫秒) 6小时 */ - public static final Long PASS_TIME = 21600000L; + public static final Long PASS_TIME = 86400000L; /** * TID\EPC长度 @@ -27,9 +27,9 @@ public class ApiConstants { public static final Integer TE_LENGTH = 24; /** - * 单次查改最大数据量 + * 导出条数限制,导出为zip文件 */ - public static final Long CHECK_MAX_DATA = 10000L; + public static final Long ZIP_MAX_DATA = 100000L; /** * 回调返回请求 @@ -37,6 +37,11 @@ public class ApiConstants { public static final String CALLBACK_URL = "https://www.kdocs.cn/chatflow/api/v2/func/webhook/2w7TqqdGMqmLwdfQY3wFidcBF4K";//正式 // public static final String CALLBACK_URL = "https://www.kdocs.cn/chatflow/api/v2/func/webhook/2uZIRSpxLvtfaoCjRoLg4EvwIc5";//测试 + /** + * 企业微信机器人webhook地址 + */ + public static final String WECHAT_WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0853ff4a-a6a0-4f99-a897-63f8cad3d332"; + /** * 导入模式 */ 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 26ccae0..7fdb17c 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 @@ -219,7 +219,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService { record.setOrderCode(lot); record.setBatchNumber(orderInfo.getOrDefault("batchNo", "")); record.setOperatorId(orderInfo.getOrDefault("operatorId", "")); - Date processTime = orderInfo.containsKey("processTime") ? + Date processTime = orderInfo.containsKey("processTime") && !orderInfo.get("processTime").isEmpty() ? sdf.parse(orderInfo.get("processTime")) : nowDate; record.setProcessingTime(processTime); record.setTotalQuantity(Long.valueOf(orderInfo.getOrDefault("totalCount", "0"))); @@ -335,7 +335,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService { * 根据导入参数更新标签记录 * * @param tagRecordList 标签记录列表 - * @param params 导入参数 + * @param params 导入参数 */ private void convertExcelDataTemplateOne(List tagRecordList, ImportParams params) { for (HwTagRecord tagRecord : tagRecordList) { 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 c651002..e068407 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 @@ -14,10 +14,14 @@ import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.JsonNode; import java.io.IOException; import java.util.List; +import static hw.tagApi.service.constant.ApiConstants.WECHAT_WEBHOOK_URL; + public class httpClientUtils { private static final Logger log = LoggerFactory.getLogger(httpClientUtils.class); private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -31,6 +35,10 @@ public class httpClientUtils { .setSocketTimeout(SOCKET_TIMEOUT) .build(); + // 重试配置 + private static final int MAX_RETRIES = 2; + private static final int RETRY_INTERVAL = 1000; // 1秒 + /** * 创建并配置HttpGet请求 * @@ -87,9 +95,11 @@ public class httpClientUtils { } /** - * 发送处理结果到回调接口 + * 发送处理结果到回调接口,支持重试机制 * - * @param resList 处理结果列表 + * @param resList 处理结果列表 + * @param url 回调URL + * @param caseCode 案例代码 */ public static void sendResultToCallback(List resList, String url, String caseCode) { if (resList == null || resList.isEmpty()) { @@ -98,48 +108,122 @@ public class httpClientUtils { } HttpPost httpPost = null; + int retryCount = 0; + boolean success = false; + String errorMessage = ""; + String jsonBody = ""; + + while (!success && retryCount < MAX_RETRIES) { + try { + // 创建POST请求 + httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + + // 设置请求头 + httpPost.setHeader("Content-Type", "application/json"); + httpPost.setHeader("Accept", "application/json"); + httpPost.setHeader("Connection", "close"); + + // 创建响应对象 + ApiResponse response = new ApiResponse(); + response.setCaseCode(caseCode); + response.setResult(resList); + + // 将对象转换为JSON字符串 + jsonBody = objectMapper.writeValueAsString(response); + log.info("准备发送的数据: {}", jsonBody); + + // 设置请求体 + StringEntity entity = new StringEntity(jsonBody, "UTF-8"); + httpPost.setEntity(entity); + + // 发送请求 + try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) { + int statusCode = httpResponse.getStatusLine().getStatusCode(); + HttpEntity responseEntity = httpResponse.getEntity(); + String responseBody = responseEntity != null ? EntityUtils.toString(responseEntity, "UTF-8") : ""; + + if (statusCode == 200) { + log.info("发送处理结果到回调接口成功,响应状态码: {}", statusCode); + log.info("回调接口响应内容: {}", responseBody); + success = true; + } else { + errorMessage = String.format("回调失败 - 状态码: %d, 响应内容: %s, 请求数据: %s", + statusCode, responseBody, jsonBody); + log.error(errorMessage); + retryCount++; + if (retryCount < MAX_RETRIES) { + log.info("将在{}秒后进行第{}次重试", RETRY_INTERVAL / 1000, retryCount + 1); + Thread.sleep(RETRY_INTERVAL); + } + } + } + } catch (Exception e) { + errorMessage = String.format("回调失败 - 异常信息: %s, 请求数据: %s", e.getMessage(), jsonBody); + log.error(errorMessage); + retryCount++; + if (retryCount < MAX_RETRIES) { + log.info("catch将在{}秒后进行第{}次重试", RETRY_INTERVAL / 1000, retryCount + 1); + try { + Thread.sleep(RETRY_INTERVAL); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + } finally { + if (httpPost != null) { + httpPost.releaseConnection(); + } + } + } + + if (!success) { + String finalMessage = String.format("回调接口调用失败 - CASE代码: %s\n重试次数: %d\n错误信息: %s", + caseCode, MAX_RETRIES, errorMessage); + log.info(finalMessage); + // 发送企业微信通知 + sendWechatNotification(finalMessage); + } + } + + /** + * 发送企业微信通知 + * + * @param message 通知消息 + */ + private static void sendWechatNotification(String message) { try { - // 创建POST请求 - httpPost = new HttpPost(url); + String webhookUrl = WECHAT_WEBHOOK_URL; + HttpPost httpPost = new HttpPost(webhookUrl); httpPost.setConfig(requestConfig); - - // 设置请求头 httpPost.setHeader("Content-Type", "application/json"); - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Connection", "close"); - // 创建响应对象 - ApiResponse response = new ApiResponse(); - response.setCaseCode(caseCode); - response.setResult(resList); + // 构建消息体 + ObjectNode rootNode = objectMapper.createObjectNode(); + rootNode.put("msgtype", "text"); + ObjectNode textNode = rootNode.putObject("text"); + textNode.put("content", message); - // 将对象转换为JSON字符串 - String jsonBody = objectMapper.writeValueAsString(response); - log.info("准备发送的数据: {}", jsonBody); - - // 设置请求体 + String jsonBody = objectMapper.writeValueAsString(rootNode); StringEntity entity = new StringEntity(jsonBody, "UTF-8"); httpPost.setEntity(entity); - // 发送请求 - try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) { - int statusCode = httpResponse.getStatusLine().getStatusCode(); - HttpEntity responseEntity = httpResponse.getEntity(); - String responseBody = responseEntity != null ? EntityUtils.toString(responseEntity, "UTF-8") : ""; - - if (statusCode == 200) { - log.info("发送处理结果到回调接口成功,响应状态码: {}", statusCode); - log.info("回调接口响应内容: {}", responseBody); - } else { - log.error("发送处理结果到回调接口失败,响应状态码: {}, 响应内容: {}", statusCode, responseBody); + try (CloseableHttpResponse response = httpClient.execute(httpPost)) { + HttpEntity responseEntity = response.getEntity(); + if (responseEntity != null) { + String responseBody = EntityUtils.toString(responseEntity, "UTF-8"); + JsonNode jsonResponse = objectMapper.readTree(responseBody); + int errcode = jsonResponse.get("errcode").asInt(); + String errmsg = jsonResponse.get("errmsg").asText(); + if (errcode == 0 && "ok".equals(errmsg)) { + log.info("企业微信通知发送成功"); + } else { + log.error("企业微信通知发送失败 - errcode: {}, errmsg: {}", errcode, errmsg); + } } } } catch (Exception e) { - log.error("发送处理结果到回调接口失败: {}", e.getMessage()); - } finally { - if (httpPost != null) { - httpPost.releaseConnection(); - } + log.error("发送企业微信通知时发生错误: {}", e.getMessage()); } } diff --git a/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml b/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml index 91fc3fe..4ada4f0 100644 --- a/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml +++ b/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml @@ -17,14 +17,14 @@ - - - - - - - - + + + + + + + + @@ -87,13 +87,13 @@ and processing_time between #{params.beginProcessingTime} and #{params.endProcessingTime} and operator_id = #{operatorId} - and destination_info = #{destinationInfo} - and library_flag = #{libraryFlag} - and intended_use = #{intendedUse} - and remark like concat('%', #{remark}, '%') - and fields_1 = #{fields1} - and fields_2 = #{fields2} - and fields_3 = #{fields3} + and destination_info = #{destinationInfo} + and library_flag = #{libraryFlag} + and intended_use = #{intendedUse} + and remark like concat('%', #{remark}, '%') + and fields_1 = #{fields1} + and fields_2 = #{fields2} + and fields_3 = #{fields3} and DATE_FORMAT(processing_time, '%Y/%m/%d') >= #{startDate} @@ -245,35 +245,48 @@ t_id = #{tId}, epc = #{epc}, - password = #{password}, - password = NULL, - order_code = #{orderCode}, - order_code = NULL, - batch_number = #{batchNumber}, - batch_number = NULL, - tag_sequence = #{tagSequence}, - tag_sequence = NULL, + password = #{password}, + password = NULL, + order_code = #{orderCode}, + order_code = NULL, + batch_number = + #{batchNumber}, + + batch_number = NULL, + tag_sequence = + #{tagSequence}, + + tag_sequence = NULL, total_quantity = #{totalQuantity}, - tag_batch = #{tagBatch}, - tag_batch = NULL, - model_code = #{modelCode}, + tag_batch = #{tagBatch}, + tag_batch = NULL, + model_code = #{modelCode}, processing_time = #{processingTime}, - operator_id = #{operatorId}, - operator_id = NULL, - test_result = #{testResult}, - test_result = NULL, - test_value = #{testValue}, - test_value = NULL, - reference_value = #{referenceValue}, - reference_value = NULL, + operator_id = #{operatorId}, + + operator_id = NULL, + test_result = + #{testResult}, + + test_result = NULL, + test_value = #{testValue}, + test_value = NULL, + reference_value = + #{referenceValue}, + + reference_value = NULL, testing_time = #{testingTime}, - file_name = #{fileName}, - file_name = NULL, - destination_info = #{destinationInfo}, - destination_info = NULL, - library_flag = #{libraryFlag}, - library_flag = NULL, - + file_name = #{fileName}, + file_name = NULL, + + destination_info = #{destinationInfo}, + + destination_info = NULL, + library_flag = + #{libraryFlag}, + + library_flag = NULL, + del_flag = #{delFlag}, fields_1 = #{fields1}, fields_2 = #{fields2}, @@ -282,8 +295,8 @@ create_time = #{createTime}, update_by = #{updateBy}, update_time = #{updateTime}, - remark = #{remark}, - remark = NULL, + remark = #{remark}, + remark = NULL, where t_id = #{tId}