sunpengfei
10 天以前 d5d33e1f100fb8db8e7bc2e1be6594a9d918f949
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using Aop.Api.Domain;
using ApiTools.Core.Utils.NongYePayUtils.Models;
using Azure;
using Azure.Core;
using Furion;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.FriendlyException;
using Microsoft.Extensions.Options;
using NetTopologySuite.Algorithm;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
 
namespace ApiTools.Core
{
    public class NongYePayUtils(
            IOptions<NongYePayOptions> options,
            IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog
        ) : ITransient
    {
        private readonly IOptions<NongYePayOptions> options = options;
        private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
 
        /// <summary>
        /// 查询账户余额
        /// </summary>
        /// <returns>余额信息</returns>
        public Task<NongYePayGetBalanceResponse> GetBalance(NongYePayGetBalanceRequest request)
        {
            return Send<NongYePayGetBalanceRequest, NongYePayGetBalanceResponse>(request);
        }
 
        /// <summary>
        /// 单笔对公转账
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<NongYePaySingleCorporateTransferResponse> SingleCorporateTransfer(NongYePaySingleCorporateTransferRequest request)
        {
            return Send<NongYePaySingleCorporateTransferRequest, NongYePaySingleCorporateTransferResponse>(request);
        }
 
        /// <summary>
        /// 查询转账状态
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<NongYePayGetTransferStatusResponse> GetTransferStatus(NongYePayGetTransferStatusRequest request)
        {
            return Send<NongYePayGetTransferStatusRequest, NongYePayGetTransferStatusResponse>(request);
        }
 
        /// <summary>
        /// 查询转账详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<NongYePayGetTransferDetailsResponse> GetTransferDetails(NongYePayGetTransferDetailsRequest request)
        {
            return SendWithFile<NongYePayGetTransferDetailsRequest, NongYePayGetTransferDetailsResponse, NongYePayGetTransferDetailsResponseItem>(request);
        }
 
        /// <summary>
        /// 获取电子回单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<NongYePayGetEreceiptsResponse> GetEreceipts(NongYePayGetEreceiptsRequest request)
        {
            return SendWithFile<NongYePayGetEreceiptsRequest, NongYePayGetEreceiptsResponse, NongYePayGetEreceiptsResponseItem>(request);
        }
 
        /// <summary>
        /// 下载电子回单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<NongYePayDownloadEreceiptResponse> DownloadEreceipt(NongYePayDownloadEreceiptRequest request)
        {
            var response = await Send<NongYePayDownloadEreceiptRequest, NongYePayDownloadEreceiptResponse>(request);
            if (response != null
                && response.RespSource == "0"
                && response.Cmp != null
                && response.Cmp.BatchFileName.IsNotNull())
            {
                response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                if (File.Exists(response.ZipFileName))
                {
                    using (var archive = ZipFile.OpenRead(response.ZipFileName))
                    {
                        foreach (var entry in archive.Entries)
                        {
                            if (entry.FullName.EndsWith(".pdf"))
                            {
                                using (var stream = entry.Open())
                                {
                                    var ms = new MemoryStream();
                                    stream.CopyTo(ms);
                                    ms.Position = 0;
                                    response.Items.Add(new NongYePayDownloadEreceiptResponseItem
                                    {
                                        FileName = entry.FullName,
                                        Stream = ms
                                    });
                                }
                            }
                        }
                    }
                }
            }
            return response;
        }
 
        public async Task<NongYePayRealTimeDownloadEreceiptResponse> RealTimeDownloadEreceipt(NongYePayRealTimeDownloadEreceiptRequest request)
        {
            var response = await Send<NongYePayRealTimeDownloadEreceiptRequest, NongYePayRealTimeDownloadEreceiptResponse>(request);
            if (response != null
                && response.RespSource == "0"
                && response.Cmp != null
                && response.Cmp.BatchFileName.IsNotNull())
            {
                response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                if (File.Exists(response.ZipFileName))
                {
                    using (var archive = ZipFile.OpenRead(response.ZipFileName))
                    {
                        foreach (var entry in archive.Entries)
                        {
                            if (entry.FullName.EndsWith(".pdf"))
                            {
                                using (var stream = entry.Open())
                                {
                                    var ms = new MemoryStream();
                                    stream.CopyTo(ms);
                                    ms.Position = 0;
                                    response.Items.Add(new NongYePayDownloadEreceiptResponseItem
                                    {
                                        FileName = entry.FullName,
                                        Stream = ms
                                    });
                                }
                            }
                        }
                    }
                }
            }
            return response;
        }
 
        /// <summary>
        /// 获取银行地区
        /// </summary>
        /// <returns></returns>
        public Task<List<SelectOption<string, string>>> GetBankAreas()
        {
            var list = new List<SelectOption<string, string>>
            {
                new SelectOption<string, string>("02", "天津市"),
                new SelectOption<string, string>("03", "上海"),
                new SelectOption<string, string>("04", "山西省"),
                new SelectOption<string, string>("05", "内蒙古"),
                new SelectOption<string, string>("06", "辽宁省"),
                new SelectOption<string, string>("07", "吉林省"),
                new SelectOption<string, string>("08", "黑龙江"),
                new SelectOption<string, string>("10", "江苏省"),
                new SelectOption<string, string>("11", "北京市"),
                new SelectOption<string, string>("12", "安徽省"),
                new SelectOption<string, string>("13", "福建省"),
                new SelectOption<string, string>("14", "江西省"),
                new SelectOption<string, string>("15", "山东省"),
                new SelectOption<string, string>("16", "河南省"),
                new SelectOption<string, string>("17", "湖北省"),
                new SelectOption<string, string>("18", "湖南省"),
                new SelectOption<string, string>("19", "浙江省"),
                new SelectOption<string, string>("20", "广西区"),
                new SelectOption<string, string>("21", "海南省"),
                new SelectOption<string, string>("22", "四川省"),
                new SelectOption<string, string>("23", "贵州省"),
                new SelectOption<string, string>("24", "云南省"),
                new SelectOption<string, string>("25", "西藏区"),
                new SelectOption<string, string>("26", "陕西省"),
                new SelectOption<string, string>("27", "甘肃省"),
                new SelectOption<string, string>("28", "青海省"),
                new SelectOption<string, string>("29", "宁夏区"),
                new SelectOption<string, string>("30", "新疆区"),
                new SelectOption<string, string>("31", "重庆市"),
                new SelectOption<string, string>("34", "大连市"),
                new SelectOption<string, string>("38", "青岛市"),
                new SelectOption<string, string>("39", "宁波市"),
                new SelectOption<string, string>("40", "厦门市"),
                new SelectOption<string, string>("41", "深圳市"),
                new SelectOption<string, string>("44", "广东省"),
                new SelectOption<string, string>("50", "河北省"),
                new SelectOption<string, string>("71", "台湾省"),
                new SelectOption<string, string>("81", "营业部"),
                new SelectOption<string, string>("97", "香港"),
                new SelectOption<string, string>("98", "澳门"),
                new SelectOption<string, string>("99", "总行"),
            };
            return Task.FromResult(list);
        }
 
        /// <summary>
        /// 获取银行
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Task<NongYePayGetBanksResponse> GetBanks(NongYePayGetBanksRequest request)
        {
            return SendWithFile<NongYePayGetBanksRequest, NongYePayGetBanksResponse, NongYePayGetBanksResponseItem>(request);
        }
 
        public string GetSeqNo()
        {
            var now = DateTime.Now;
            var random = StringUtils.GenerateRandomString(6);
            return $"{now:yyyyMMddHHmmssfff}{random}";
        }
 
        private async Task<List<T>> GetList<T>(NongYePayBaseResponse<T> response, ThreeResourceLog log)
            where T : class, new()
        {
            var list = new List<T>();
            if (response.RespSource == "0"
                && response.FileFlag == "1"
                && response.Cmp.BatchFileName.IsNotNull())
            {
                var fileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                if (File.Exists(fileName))
                {
                    var props = typeof(T).GetProperties();
                    var lines = await File.ReadAllLinesAsync(fileName, Encoding.GetEncoding("GBK"));
                    foreach (var line in lines)
                    {
                        var item = new T();
                        var columns = line.Split("|");
                        for (int i = 0; i < columns.Count() - 1; i++)
                        {
                            props[i].SetValue(item, columns[i]);
                        }
                        list.Add(item);
                    }
 
                    log.UpdatedTime = DateTimeOffset.Now;
                    var json = list.ToJson();
                    log.Response += $"\n{json}";
                    await repThreeResourceLog.UpdateNowAsync(log);
                }
            }
            return list;
        }
 
        private async Task<TResponse> SendWithFile<TRequest, TResponse, TResponseItem>(TRequest request)
            where TRequest : NongYePayBaseRequest
            where TResponse : NongYePayBaseResponse<TResponseItem>, new()
            where TResponseItem : class, new()
        {
            var response = await SendWithLog<TRequest, TResponse>(request);
            response.response.Items = await GetList<TResponseItem>(response.response, response.log);
            return response.response;
        }
 
        private async Task<TResponse> Send<TRequest, TResponse>(TRequest request)
            where TRequest : NongYePayBaseRequest
            where TResponse : NongYePayBaseResponse, new()
        {
            var response = await SendWithLog<TRequest, TResponse>(request);
            return response.response;
        }
 
        private async Task<(TResponse response, ThreeResourceLog log)> SendWithLog<TRequest, TResponse>(TRequest request)
            where TRequest : NongYePayBaseRequest
            where TResponse : NongYePayBaseResponse, new()
        {
            try
            {
                var logier = JwtUtils.GetCurrentLogier();
                var now = DateTime.Now;
                request.CorpNo = options.Value.CorpNo;
                request.OpNo = options.Value.OpNo;
                request.AuthNo = "";
                request.Sign = "";
                request.ReqDate = now.ToString("yyyyMMdd");
                request.ReqTime = now.ToString("HHmmss");
                request.ReqSeqNo = request.ReqSeqNo ?? GetSeqNo();
                request.ChannelType = "ERP";
                request.ProductID = "ICC";
 
                string requestXml = SerializeRequest(request);
                var log = new ThreeResourceLog
                {
                    CreatedTime = DateTimeOffset.Now,
                    Id = IDGen.NextID(),
                    TraceId = App.GetTraceId(),
                    Method = EnumResourceMethod.Post,
                    Domain = $"{options.Value.Ip}:{options.Value.Port}",
                    Path = request.CCTransCode,
                    CreatedUserId = logier?.Id,
                    CreatedChannelId = logier?.ChannelId,
                    Request = requestXml,
                };
                await repThreeResourceLog.InsertNowAsync(log);
                var stopwatch = Stopwatch.StartNew();
                var responseXml = await SendRequest(requestXml);
                stopwatch.Stop();
                var response = DeserializeResponse<TResponse>(responseXml);
                log.UpdatedTime = DateTimeOffset.Now;
                log.Response = responseXml;
                log.IsSuccess = response.RespSource == "0";
                log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
                await repThreeResourceLog.UpdateNowAsync(log);
                return (response, log);
            }
            catch (Exception ex)
            {
                return (new TResponse
                {
                    RespSource = "500",
                    RespInfo = ex.Message
                }, null);
            }
        }
 
        /// <summary>
        /// 泛型请求类转XML
        /// </summary>
        private string SerializeRequest<T>(T request)
        {
            var serializer = new XmlSerializer(typeof(T));
            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");
 
            var settings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = false,
                Encoding = Encoding.UTF8
            };
 
            using var stream = new MemoryStream();
            using var writer = XmlWriter.Create(stream, settings);
            serializer.Serialize(writer, request, ns);
 
            // 转换为GBK编码
            stream.Position = 0;
            using var reader = new StreamReader(stream, Encoding.UTF8);
            string utf8Xml = reader.ReadToEnd();
            return Encoding.GetEncoding("GBK").GetString(Encoding.GetEncoding("GBK").GetBytes(utf8Xml));
        }
 
        /// <summary>
        /// XML转泛型应答类
        /// </summary>
        private T DeserializeResponse<T>(string gbkXml)
        {
            var buffer = Encoding.GetEncoding("GBK").GetBytes(gbkXml);
            using var ms = new MemoryStream(buffer);
            using var sr = new StreamReader(ms, Encoding.GetEncoding("GBK"));
            var serializer = new XmlSerializer(typeof(T));
            using var xr = XmlReader.Create(sr);
            return (T)serializer.Deserialize(xr);
        }
 
        /// <summary>
        /// 发送请求并接收应答
        /// </summary>
        private async Task<string> SendRequest(string requestXml)
        {
            byte[] requestBytes = Encoding.GetEncoding("GBK").GetBytes(requestXml);
 
            // 2. 构建7字节包头(加密标志0 + 数据长度,右补空格)
            byte[] header = new byte[7];
            header[0] = 0x30; // 加密标志:0(ASCII码)
            string requestLengthStr = requestBytes.Length.ToString().PadRight(6, ' '); // 后6字节:长度右补空格
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(requestLengthStr), 0, header, 1, 6);
 
            // 3. 合并包头+数据包
            byte[] sendData = new byte[header.Length + requestBytes.Length];
            Buffer.BlockCopy(header, 0, sendData, 0, header.Length);
            Buffer.BlockCopy(requestBytes, 0, sendData, header.Length, requestBytes.Length);
 
            // 4. Socket发送(带超时)
            using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.SendTimeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;
            socket.ReceiveTimeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;
 
            try
            {
                await socket.ConnectAsync(options.Value.Ip, options.Value.Port);
                await socket.SendAsync(sendData);
 
                // 接收包头
                byte[] responseHeader = new byte[7];
                int headerLen = socket.Receive(responseHeader);
                if (headerLen != 7) throw Oops.Oh(EnumErrorCodeType.s510, "接收包头失败");
 
                // 解析数据长度
                string lengthStr = Encoding.ASCII.GetString(responseHeader, 1, 6).Trim();
                if (!int.TryParse(lengthStr, out int dataLength))
                    throw Oops.Oh(EnumErrorCodeType.s510, $"解析数据长度失败:{lengthStr}");
 
                // 接收数据包
                byte[] responseBytes = new byte[dataLength];
                int receivedLen = 0;
                while (receivedLen < dataLength)
                {
                    int len = socket.Receive(responseBytes, receivedLen, dataLength - receivedLen, SocketFlags.None);
                    receivedLen += len;
                }
 
                // 转换为GBK XML
                var responseXml = Encoding.GetEncoding("GBK").GetString(responseBytes);
                if (responseXml.IsNull())
                {
                    throw Oops.Oh(EnumErrorCodeType.s510, "未收到应答");
                }
                return responseXml;
            }
            finally
            {
                socket.Dispose();
            }
        }
    }
}