sen
6 天以前 fd765fb823fb60fc942b09e17a5d13ad4aef2f96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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;
        }
    }
}