sunpengfei
8 天以前 a22af1da254b90c79fd3e1433ed98f51c0a39a65
ApiTools.Core/Utils/NongYePayUtils/NongYePayUtils.cs
@@ -4,9 +4,11 @@
using Azure.Core;
using Furion;
using Furion.DatabaseAccessor;
using Furion.DataEncryption.Extensions;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.FriendlyException;
using Furion.HttpRemote;
using Microsoft.Extensions.Options;
using NetTopologySuite.Algorithm;
using Org.BouncyCastle.Asn1.Ocsp;
@@ -25,10 +27,12 @@
namespace ApiTools.Core
{
    public class NongYePayUtils(
            IHttpRemoteService httpRemoteService,
            IOptions<NongYePayOptions> options,
            IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog
        ) : ITransient
    {
        private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
        private readonly IOptions<NongYePayOptions> options = options;
        private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
@@ -88,74 +92,17 @@
        /// <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;
            return await SendWithZip<NongYePayDownloadEreceiptRequest, NongYePayDownloadEreceiptResponse>(request);
        }
        /// <summary>
        /// 实时下载电子回单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        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;
            return await SendWithZip<NongYePayRealTimeDownloadEreceiptRequest, NongYePayRealTimeDownloadEreceiptResponse>(request);
        }
        /// <summary>
@@ -228,6 +175,69 @@
            return $"{now:yyyyMMddHHmmssfff}{random}";
        }
        private async Task<List<NongYePayResponseZipFile>> GetZipFiles(NongYePayZipResponse response)
        {
            var list = new List<NongYePayResponseZipFile>();
            if (response != null
                && response.RespSource == "0"
                && response.Cmp != null
                && response.Cmp.BatchFileName.IsNotNull())
            {
                var memoryStream = new MemoryStream();
                if (options.Value.RemoteFileUrl.IsNotNull())
                {
                    response.ZipFileName = response.Cmp.BatchFileName;
                    var url = $"{options.Value.RemoteFileUrl}/api/file/download";
                    var command = new DownloadFileCommand
                    {
                        Scene = "NongYePay",
                        Path = response.Cmp.BatchFileName
                    };
                    var timestamp = DateTime.Now.ToTimeStamp(true);
                    var sign = $"POST|/api/file/download|{command.ToJson()}|{options.Value.RemoteFilePrivateKey}|{timestamp}".ToMD5Encrypt();
                    using var stream = await httpRemoteService.PostAsStreamAsync(url,
                        builder => builder
                            .SetJsonContent(command)
                            .WithHeader("x-timestamp", timestamp, replace: true)
                            .WithHeader("x-sign", sign, replace: true));
                    stream.CopyTo(memoryStream);
                    memoryStream.Position = 0;
                }
                else if (options.Value.FilePath.IsNotNull())
                {
                    response.ZipFileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                    if (File.Exists(response.ZipFileName))
                    {
                        using var stream = File.OpenRead(response.ZipFileName);
                        stream.CopyTo(memoryStream);
                        memoryStream.Position = 0;
                    }
                }
                using (var archive = new ZipArchive(memoryStream))
                {
                    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;
                                list.Add(new NongYePayResponseZipFile
                                {
                                    FileName = entry.FullName,
                                    Stream = ms
                                });
                            }
                        }
                    }
                }
            }
            return list;
        }
        private async Task<List<T>> GetList<T>(NongYePayBaseResponse<T> response, ThreeResourceLog log)
            where T : class, new()
        {
@@ -236,11 +246,37 @@
                && response.FileFlag == "1"
                && response.Cmp.BatchFileName.IsNotNull())
            {
                var fileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                if (File.Exists(fileName))
                var lines = new List<string>();
                if (options.Value.RemoteFileUrl.IsNotNull())
                {
                    var url = $"{options.Value.RemoteFileUrl}/api/file/download";
                    var command = new DownloadFileCommand
                    {
                        Scene = "NongYePay",
                        Path = response.Cmp.BatchFileName
                    };
                    var timestamp = DateTime.Now.ToTimeStamp(true);
                    var sign = $"POST|/api/file/download|{command.ToJson()}|{options.Value.RemoteFilePrivateKey}|{timestamp}".ToMD5Encrypt();
                    var buffer = await httpRemoteService.PostAsByteArrayAsync(url,
                       builder => builder
                           .SetJsonContent(command)
                           .WithHeader("x-timestamp", timestamp, replace: true)
                           .WithHeader("x-sign", sign, replace: true));
                    var text = Encoding.GetEncoding("GBK").GetString(buffer);
                    lines = text.Split("\n").Where(it => it.IsNotNull()).ToList();
                }
                else if (options.Value.FilePath.IsNotNull())
                {
                    var fileName = $"{options.Value.FilePath}{response.Cmp.BatchFileName}";
                    if (File.Exists(fileName))
                    {
                        var array = await File.ReadAllLinesAsync(fileName, Encoding.GetEncoding("GBK"));
                        lines = array.ToList();
                    }
                }
                if (lines.IsNotNull())
                {
                    var props = typeof(T).GetProperties();
                    var lines = await File.ReadAllLinesAsync(fileName, Encoding.GetEncoding("GBK"));
                    foreach (var line in lines)
                    {
                        var item = new T();
@@ -251,12 +287,12 @@
                        }
                        list.Add(item);
                    }
                    log.UpdatedTime = DateTimeOffset.Now;
                    var json = list.ToJson();
                    log.Response += $"\n{json}";
                    await repThreeResourceLog.UpdateNowAsync(log);
                }
                log.UpdatedTime = DateTimeOffset.Now;
                var json = list.ToJson();
                log.Response += $"\n{json}";
                await repThreeResourceLog.UpdateNowAsync(log);
            }
            return list;
        }
@@ -267,7 +303,16 @@
            where TResponseItem : class, new()
        {
            var response = await SendWithLog<TRequest, TResponse>(request);
            response.response.Items = await GetList<TResponseItem>(response.response, response.log);
            response.response.Items = await GetList(response.response, response.log);
            return response.response;
        }
        private async Task<TResponse> SendWithZip<TRequest, TResponse>(TRequest request)
            where TRequest : NongYePayBaseRequest
            where TResponse : NongYePayZipResponse, new()
        {
            var response = await SendWithLog<TRequest, TResponse>(request);
            response.response.Items = await GetZipFiles(response.response);
            return response.response;
        }