From 9b67df1e8882e27b26e973c525ab4281d040d4be Mon Sep 17 00:00:00 2001 From: zhaoqing <1670883518@qq.com> Date: Thu, 7 May 2026 18:29:40 +0800 Subject: [PATCH] =?UTF-8?q?OMS-GW-=E4=BC=81=E4=B8=9A=E5=9B=BD=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ErpCountryApplicationService.java | 117 ++++++++++++++++++ .../facade/erpCountry/ErpCountryApi.java | 5 + 2 files changed, 122 insertions(+) diff --git a/mhd_oms/src/main/java/com/mhd/oms/application/service/erpCountry/ErpCountryApplicationService.java b/mhd_oms/src/main/java/com/mhd/oms/application/service/erpCountry/ErpCountryApplicationService.java index a55282432..1a6e1a377 100644 --- a/mhd_oms/src/main/java/com/mhd/oms/application/service/erpCountry/ErpCountryApplicationService.java +++ b/mhd_oms/src/main/java/com/mhd/oms/application/service/erpCountry/ErpCountryApplicationService.java @@ -1,28 +1,46 @@ package com.mhd.oms.application.service.erpCountry; +import com.alibaba.fastjson.JSONObject; +import com.mhd.common.core.web.domain.AjaxResult; import com.mhd.common.core.web.page.TableDataInfo; import com.mhd.common.security.utils.SecurityUtils; import com.mhd.oms.domain.erpCountry.entity.ErpCountry; import com.mhd.oms.domain.erpCountry.repository.mapper.ErpCountryMapper; import com.mhd.oms.domain.erpCountry.repository.po.ErpCountryPO; +import com.mhd.oms.domain.gwLog.entity.GwLog; +import com.mhd.oms.domain.gwLog.repository.mapper.GwLogMapper; import com.mhd.oms.interfaces.dto.erpCountry.ErpCountryDTO; import com.mhd.system.api.model.LoginUser; import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.io.IOException; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Service @Slf4j public class ErpCountryApplicationService{ + private static final String PUSH_API_URL = "http://10.33.0.100/ktms-erp-api/transfer/pushData"; + private static final String API_NAME = "saveCountry"; @Resource private ErpCountryMapper erpCountryMapper; + @Resource + private GwLogMapper gwLogMapper; + public TableDataInfo queryList(ErpCountryDTO erpCountryDTO) { List list = erpCountryMapper.queryList(erpCountryDTO); TableDataInfo tableDataInfo = new TableDataInfo(); @@ -64,4 +82,103 @@ public class ErpCountryApplicationService{ public int deleteByIds(List idList) { return erpCountryMapper.deleteByIds(idList); } + + public AjaxResult batchPush(List dtoList) { + if (dtoList == null || dtoList.isEmpty()) { + return AjaxResult.error("没有数据需要推送"); + } + + LoginUser loginUser = SecurityUtils.getLoginUser(); + Long pushBy = loginUser != null ? loginUser.getUserid() : null; + Long organizationId = loginUser != null ? loginUser.getUserPo().getOrganizationId() : null; + Long topOrganizationId = loginUser != null ? loginUser.getUserPo().getTopOrganizationId() : null; + String organizationName = loginUser != null ? loginUser.getUserPo().getOrganizationName() : null; + + int successCount = 0; + int failCount = 0; + StringBuilder failMsg = new StringBuilder(); + + for (ErpCountryDTO dto : dtoList) { + Map body = new HashMap<>(); + body.put("erpCountryCode", dto.getErpCountryCode()); + body.put("erpCountryName", dto.getErpCountryName()); + + GwLog gwLog = new GwLog(); + gwLog.setType("企业国别"); + gwLog.setBusinessId(String.valueOf(dto.getId())); + gwLog.setRequestBody(JSONObject.toJSONString(body)); + gwLog.setResponseBody(""); + gwLog.setStatus(1); + gwLog.setSendTime(new Date()); + gwLog.setOrganizationId(organizationId); + gwLog.setTopOrganizationId(topOrganizationId); + gwLog.setOrganizationName(organizationName); + gwLog.setCreateBy(pushBy); + gwLog.setCreateTime(new Date()); + gwLog.setDelFlag(1); + gwLogMapper.insert(gwLog); + + updatePushStatus(dto.getId(), 2, new Date(), pushBy); + + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(PUSH_API_URL); + StringEntity postingString = new StringEntity(JSONObject.toJSONString(body), "UTF-8"); + httpPost.setEntity(postingString); + httpPost.setHeader("Content-type", "application/json"); + httpPost.setHeader("apiName", API_NAME); + + try { + CloseableHttpResponse response = httpClient.execute(httpPost); + String responseString = EntityUtils.toString(response.getEntity()); + gwLog.setResponseBody(responseString); + + if (response.getStatusLine().getStatusCode() == 200) { + gwLog.setStatus(2); + updatePushStatus(dto.getId(), 3, new Date(), pushBy); + successCount++; + } else { + gwLog.setStatus(3); + gwLog.setErrorMsg(responseString); + updatePushStatus(dto.getId(), 4, new Date(), pushBy); + failCount++; + failMsg.append("erpCountryCode:").append(dto.getErpCountryCode()).append("失败;"); + } + gwLogMapper.updateById(gwLog); + response.close(); + } catch (IOException e) { + gwLog.setStatus(3); + gwLog.setErrorMsg(e.getMessage()); + gwLogMapper.updateById(gwLog); + updatePushStatus(dto.getId(), 4, new Date(), pushBy); + failCount++; + failMsg.append("erpCountryCode:").append(dto.getErpCountryCode()).append("异常:").append(e.getMessage()).append(";"); + log.error("企业国别推送异常: id={}, error={}", dto.getId(), e.getMessage()); + } finally { + try { + httpClient.close(); + } catch (Exception e) { + log.error("关闭httpClient异常: {}", e.getMessage()); + } + } + } + + String resultMsg = "推送完成: 成功" + successCount + "条, 失败" + failCount + "条"; + if (failCount > 0) { + resultMsg += "[" + failMsg.toString() + "]"; + } + return AjaxResult.success(resultMsg); + } + + private void updatePushStatus(Long id, Integer pushStatus, Date pushTime, Long pushBy) { + ErpCountry entity = new ErpCountry(); + entity.setId(id); + entity.setPushStatus(pushStatus); + if (pushTime != null) { + entity.setPushTime(pushTime); + } + if (pushBy != null) { + entity.setPushBy(String.valueOf(pushBy)); + } + erpCountryMapper.updateById(entity); + } } diff --git a/mhd_oms/src/main/java/com/mhd/oms/interfaces/facade/erpCountry/ErpCountryApi.java b/mhd_oms/src/main/java/com/mhd/oms/interfaces/facade/erpCountry/ErpCountryApi.java index 038560e02..afdd6e8c9 100644 --- a/mhd_oms/src/main/java/com/mhd/oms/interfaces/facade/erpCountry/ErpCountryApi.java +++ b/mhd_oms/src/main/java/com/mhd/oms/interfaces/facade/erpCountry/ErpCountryApi.java @@ -53,5 +53,10 @@ public class ErpCountryApi extends BaseController{ return toAjax(erpCountryApplicationService.deleteByIds(idList)); } + @ApiOperation("批量推送企业国别") + @PostMapping("/batchPush") + public AjaxResult batchPush(@RequestBody List dtoList) { + return erpCountryApplicationService.batchPush(dtoList); + } }