OMS-GW-企业国别

This commit is contained in:
zhaoqing
2026-05-07 18:29:40 +08:00
parent 9139485e6d
commit 9b67df1e88
2 changed files with 122 additions and 0 deletions
@@ -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<ErpCountryPO> list = erpCountryMapper.queryList(erpCountryDTO);
TableDataInfo tableDataInfo = new TableDataInfo();
@@ -64,4 +82,103 @@ public class ErpCountryApplicationService{
public int deleteByIds(List<Long> idList) {
return erpCountryMapper.deleteByIds(idList);
}
public AjaxResult batchPush(List<ErpCountryDTO> 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<String, String> 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);
}
}
@@ -53,5 +53,10 @@ public class ErpCountryApi extends BaseController{
return toAjax(erpCountryApplicationService.deleteByIds(idList));
}
@ApiOperation("批量推送企业国别")
@PostMapping("/batchPush")
public AjaxResult batchPush(@RequestBody List<ErpCountryDTO> dtoList) {
return erpCountryApplicationService.batchPush(dtoList);
}
}