package com.ruoyi.cwgl.service.impl;
|
|
import com.alibaba.fastjson2.JSON;
|
import com.ruoyi.common.utils.http.HttpUtils;
|
import com.ruoyi.cwgl.domain.PayableFeeManagement;
|
import com.ruoyi.cwgl.domain.ReceivableFeeManagement;
|
import com.ruoyi.cwgl.domain.dto.PayableAuditLog;
|
import com.ruoyi.cwgl.domain.dto.ReceivableAuditLog;
|
import com.ruoyi.cwgl.service.IPayableFeeManagementService;
|
import com.ruoyi.cwgl.service.IReceivableFeeManagementService;
|
import com.ruoyi.cwgl.service.ITmsAuditLogPushService;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
import java.util.List;
|
|
/**
|
* TMS审核日志推送服务实现类
|
*/
|
@Service
|
public class TmsAuditLogPushServiceImpl implements ITmsAuditLogPushService {
|
|
private static final Logger log = LoggerFactory.getLogger(TmsAuditLogPushServiceImpl.class);
|
|
@Value("${custom.tms.url}")
|
private String tmsBaseUrl;
|
|
@Autowired
|
private IPayableFeeManagementService payableFeeManagementService;
|
|
@Autowired
|
private IReceivableFeeManagementService receivableFeeManagementService;
|
|
@Override
|
public boolean pushPayableAuditLog(PayableAuditLog auditLog) {
|
try {
|
String url = tmsBaseUrl + "/api/third/audit/ap-bill";
|
String jsonBody = JSON.toJSONString(auditLog);
|
|
log.info("推送应付账单审核日志到TMS: {}", jsonBody);
|
|
String response = sendJsonPost(url, jsonBody);
|
log.info("TMS应付账单审核日志推送响应: {}", response);
|
|
return true;
|
} catch (Exception e) {
|
log.error("推送应付账单审核日志到TMS失败", e);
|
return false;
|
}
|
}
|
|
@Override
|
public boolean pushReceivableAuditLog(ReceivableAuditLog auditLog) {
|
try {
|
String url = tmsBaseUrl + "/api/third/audit/ar-bill";
|
String jsonBody = JSON.toJSONString(auditLog);
|
|
log.info("推送应收账单审核日志到TMS: {}", jsonBody);
|
|
String response = sendJsonPost(url, jsonBody);
|
log.info("TMS应收账单审核日志推送响应: {}", response);
|
|
return true;
|
} catch (Exception e) {
|
log.error("推送应收账单审核日志到TMS失败", e);
|
return false;
|
}
|
}
|
|
/**
|
* 发送JSON格式的POST请求
|
* @param url 请求URL
|
* @param jsonBody JSON请求体
|
* @return 响应内容
|
*/
|
private String sendJsonPost(String url, String jsonBody) {
|
java.net.HttpURLConnection connection = null;
|
try {
|
java.net.URL realUrl = new java.net.URL(url);
|
connection = (java.net.HttpURLConnection) realUrl.openConnection();
|
|
// 设置请求属性
|
connection.setRequestMethod("POST");
|
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
connection.setRequestProperty("Accept", "application/json");
|
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
|
// 设置连接参数
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
|
// 设置超时时间
|
connection.setConnectTimeout(10000); // 10秒连接超时
|
connection.setReadTimeout(30000); // 30秒读取超时
|
|
// 发送请求体
|
try (java.io.OutputStream os = connection.getOutputStream()) {
|
os.write(jsonBody.getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
os.flush();
|
}
|
|
// 获取响应
|
int responseCode = connection.getResponseCode();
|
if (responseCode == java.net.HttpURLConnection.HTTP_OK) {
|
try (java.io.BufferedReader in = new java.io.BufferedReader(
|
new java.io.InputStreamReader(connection.getInputStream(), java.nio.charset.StandardCharsets.UTF_8))) {
|
StringBuilder response = new StringBuilder();
|
String line;
|
while ((line = in.readLine()) != null) {
|
response.append(line);
|
}
|
return response.toString();
|
}
|
} else {
|
// 处理错误响应
|
try (java.io.BufferedReader in = new java.io.BufferedReader(
|
new java.io.InputStreamReader(connection.getErrorStream(), java.nio.charset.StandardCharsets.UTF_8))) {
|
StringBuilder errorResponse = new StringBuilder();
|
String line;
|
while ((line = in.readLine()) != null) {
|
errorResponse.append(line);
|
}
|
throw new RuntimeException("HTTP请求失败,状态码: " + responseCode + ", 响应: " + errorResponse.toString());
|
}
|
}
|
|
} catch (Exception e) {
|
log.error("发送JSON POST请求失败,URL: {}", url, e);
|
throw new RuntimeException("发送JSON POST请求失败: " + e.getMessage(), e);
|
} finally {
|
if (connection != null) {
|
connection.disconnect();
|
}
|
}
|
}
|
|
/**
|
* 判断应付账单是否来自TMS系统
|
* @param billNo 账单编号
|
* @return 是否来自TMS系统
|
*/
|
public boolean isPayableBillFromTms(String billNo) {
|
try {
|
PayableFeeManagement query = new PayableFeeManagement();
|
query.setRelatedBillNo(billNo);
|
List<PayableFeeManagement> fees = payableFeeManagementService.selectPayableFeeManagementList(query);
|
|
if (fees != null && !fees.isEmpty()) {
|
// 检查是否有费用来自TMS系统
|
return fees.stream()
|
.anyMatch(fee -> "TMS".equalsIgnoreCase(fee.getSourceSystem()));
|
}
|
return false;
|
} catch (Exception e) {
|
log.error("判断应付账单来源系统失败", e);
|
return false;
|
}
|
}
|
|
/**
|
* 判断应收账单是否来自TMS系统
|
* @param billNo 账单编号
|
* @return 是否来自TMS系统
|
*/
|
public boolean isReceivableBillFromTms(String billNo) {
|
try {
|
ReceivableFeeManagement query = new ReceivableFeeManagement();
|
query.setRelatedBillNo(billNo);
|
List<ReceivableFeeManagement> fees = receivableFeeManagementService.selectReceivableFeeManagementList(query);
|
|
if (fees != null && !fees.isEmpty()) {
|
// 检查是否有费用来自TMS系统
|
return fees.stream()
|
.anyMatch(fee -> "tms".equalsIgnoreCase(fee.getSourceSystem()));
|
}
|
return false;
|
} catch (Exception e) {
|
log.error("判断应收账单来源系统失败", e);
|
return false;
|
}
|
}
|
|
/**
|
* 获取应付账单的来源系统
|
* @param billNo 账单编号
|
* @return 来源系统,如果多个来源系统则返回第一个TMS系统,否则返回第一个非空系统
|
*/
|
public String getPayableBillSourceSystem(String billNo) {
|
try {
|
PayableFeeManagement query = new PayableFeeManagement();
|
query.setRelatedBillNo(billNo);
|
List<PayableFeeManagement> fees = payableFeeManagementService.selectPayableFeeManagementList(query);
|
|
if (fees != null && !fees.isEmpty()) {
|
// 优先返回TMS系统
|
return fees.stream()
|
.map(PayableFeeManagement::getSourceSystem)
|
.filter(source -> source != null && !source.trim().isEmpty())
|
.findFirst()
|
.orElse(null);
|
}
|
return null;
|
} catch (Exception e) {
|
log.error("获取应付账单来源系统失败", e);
|
return null;
|
}
|
}
|
|
/**
|
* 获取应收账单的来源系统
|
* @param billNo 账单编号
|
* @return 来源系统,如果多个来源系统则返回第一个TMS系统,否则返回第一个非空系统
|
*/
|
public String getReceivableBillSourceSystem(String billNo) {
|
try {
|
ReceivableFeeManagement query = new ReceivableFeeManagement();
|
query.setRelatedBillNo(billNo);
|
List<ReceivableFeeManagement> fees = receivableFeeManagementService.selectReceivableFeeManagementList(query);
|
|
if (fees != null && !fees.isEmpty()) {
|
// 优先返回TMS系统
|
return fees.stream()
|
.map(ReceivableFeeManagement::getSourceSystem)
|
.filter(source -> source != null && !source.trim().isEmpty())
|
.findFirst()
|
.orElse(null);
|
}
|
return null;
|
} catch (Exception e) {
|
log.error("获取应收账单来源系统失败", e);
|
return null;
|
}
|
}
|
}
|