OMS-GW推送-商业合作伙伴

This commit is contained in:
zhaoqing
2026-05-06 11:20:41 +08:00
parent 9b1bae354c
commit b62fc345b0
7 changed files with 236 additions and 3 deletions
@@ -18,6 +18,7 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -208,4 +209,11 @@ public interface UserServiceFeign {
@RequestParam(value = "updateTimeFrom", required = false) String updateTimeFrom,
@RequestParam(value = "updateTimeTo", required = false) String updateTimeTo,
@RequestParam(value = "shipperType", required = false) Integer shipperType);
@ApiOperation("商业合作伙伴-更新推送")
@PostMapping("/userShipperApi/updatePushStatus")
void updatePushStatus(@RequestParam(value = "shipperId") Long shipperId,
@RequestParam(value = "pushStatus") Integer pushStatus,
@RequestParam(value = "pushTime") Date pushTime,
@RequestParam(value = "pushBy") Long pushBy);
}
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -186,6 +187,11 @@ public class RemoteUserFeignFallbackFactory implements FallbackFactory<UserServi
public AjaxResult<?> userShipperList(Integer pageNum, Integer pageSize, String userAccount, String shipperEnterpriseName, Long organizationId, String organizationName, String createTimeFrom, String createTimeTo, String updateTimeFrom, String updateTimeTo, Integer shipperType) {
return AjaxResult.error("查询失败" + throwable.getMessage());
}
@Override
public void updatePushStatus(Long shipperId, Integer pushStatus, Date pushTime, Long pushBy) {
log.error("更新失败" + throwable.getMessage());
}
};
}
}
@@ -1,23 +1,49 @@
package com.mhd.oms.application.service.businessPartner;
import cn.hutool.core.date.DateUtil;
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.gwLog.entity.GwLog;
import com.mhd.oms.domain.gwLog.repository.mapper.GwLogMapper;
import com.mhd.oms.interfaces.dto.businessPartner.BusinessPartnerDTO;
import com.mhd.system.api.UserServiceFeign;
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.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 BusinessPartnerApplicationService {
private static final String PUSH_API_URL = "http://10.33.0.100/ktms-erp-api/transfer/pushData";
private static final String API_NAME = "saveCorporation";
@Resource
private UserServiceFeign userServiceFeign;
@Resource
private GwLogMapper gwLogMapper;
public TableDataInfo queryShipperPage(BusinessPartnerDTO businessPartnerDTO) {
@@ -48,4 +74,111 @@ public class BusinessPartnerApplicationService {
}
public AjaxResult batchPush(List<BusinessPartnerDTO> businessPartnerDTOList) {
if(businessPartnerDTOList == null || businessPartnerDTOList.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 (BusinessPartnerDTO businessPartnerDTO :businessPartnerDTOList){
Map<String, String> body = new HashMap<>();
body.put("copyCode",businessPartnerDTO.getCorpCode());
body.put("copyName",businessPartnerDTO.getCorpName());
GwLog gwLog = new GwLog();
gwLog.setType("商业合作伙伴");
gwLog.setBusinessId(businessPartnerDTO.getShipperId());
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(Long.valueOf(businessPartnerDTO.getShipperId()),2,null,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(Long.valueOf(businessPartnerDTO.getShipperId()),3, new Date(), pushBy);
successCount++;
}else{
gwLog.setStatus(3);
gwLog.setErrorMsg(responseString);
updatePushStatus(Long.valueOf(businessPartnerDTO.getShipperId()),4, new Date(), pushBy);
failCount++;
failMsg.append("CorpCode:").append(businessPartnerDTO.getCorpCode()).append("失败:");
}
gwLogMapper.updateById(gwLog);
response.close();
} catch (IOException e) {
gwLog.setStatus(3);
gwLog.setErrorMsg(e.getMessage());
gwLogMapper.updateById(gwLog);
updatePushStatus(Long.valueOf(businessPartnerDTO.getShipperId()),4, new Date(), pushBy);
failCount++;
failMsg.append("CorpCode:").append(businessPartnerDTO.getCorpCode()).append("异常:").append(e.getMessage()).append(":");
log.error("GW推送异常:shipperId={} error={}",businessPartnerDTO.getShipperId(),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 shipperId, Integer pushStatus, Date pushTime, Long pushBy) {
try {
// 调用user-center的Feign接口,更新货主的推送状态
userServiceFeign.updatePushStatus(shipperId, pushStatus, pushTime, pushBy);
} catch (Exception e) {
// 如果更新失败,记录错误日志,但不阻断主流程
log.info("更新推送状态失败: shipperId={}, error={}", shipperId, e.getMessage());
}
}
}
@@ -0,0 +1,64 @@
package com.mhd.oms.domain.gwLog.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
@Data
@TableName("gw_log")
public class GwLog implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("日志id")
@TableId(type = IdType.AUTO)
private Long logId;
@ApiModelProperty("类型")
private String type;
@ApiModelProperty("业务ID")
private String businessId;
@ApiModelProperty("请求报文")
private String requestBody;
@ApiModelProperty("响应报文")
private String responseBody;
@ApiModelProperty("状态(1-初始,2-成功,3-失败)")
private Integer status;
@ApiModelProperty("错误信息")
private String errorMsg;
@ApiModelProperty("推送时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date sendTime;
@ApiModelProperty("组织ID")
private Long organizationId;
@ApiModelProperty("一级组织ID")
private Long topOrganizationId;
@ApiModelProperty("组织名称")
private String organizationName;
@ApiModelProperty("创建人")
private Long createBy;
@ApiModelProperty("创建时间")
private Date createTime;
@ApiModelProperty("删除标记:1-正常,0-删除")
private Integer delFlag;
}
@@ -0,0 +1,10 @@
package com.mhd.oms.domain.gwLog.repository.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mhd.oms.domain.gwLog.entity.GwLog;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface GwLogMapper extends BaseMapper<GwLog> {
}
@@ -48,5 +48,12 @@ public class BusinessPartnerDTO {
@ApiModelProperty(value = "每页数量")
private Integer pageSize = 10;
@ApiModelProperty(value = "合作伙伴代码")
private String corpCode;
@ApiModelProperty(value = "公司中文名称")
private String corpName;
@ApiModelProperty(value = "货主id")
private String shipperId;
}
@@ -1,15 +1,15 @@
package com.mhd.oms.interfaces.facade.businessPartner;
import com.mhd.common.core.web.domain.AjaxResult;
import com.mhd.common.core.web.page.TableDataInfo;
import com.mhd.oms.application.service.businessPartner.BusinessPartnerApplicationService;
import com.mhd.oms.interfaces.dto.businessPartner.BusinessPartnerDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@@ -26,5 +26,10 @@ public class BusinessPartnerApi {
}
@ApiOperation("商业合作伙伴-批量推送")
@PostMapping("/batchPush")
public AjaxResult batchPush(@RequestBody List<BusinessPartnerDTO> businessPartnerDTOList){
return businessPartnerApplicationService.batchPush(businessPartnerDTOList);
}
}