Files
zszq-trs/YLErpDAL/Services/CashRecordModule/ExternalCashFlowService.cs
T
shangzhongyuan 8372a1bde4 fix(entryexit): 移除银行流水查询中的冗余参数验证
- 删除了不再需要的 clientId 参数验证逻辑
- 移除了 API 请求中多余的 account 查询参数
- 简化了银行流水数据查询接口调用
2026-03-13 16:06:33 +08:00

109 lines
4.4 KiB
C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using YLErp.Model.ExternalCashFlow;
using Qdp.Foundation.Utilities;
using YLErp.Modules.ApiModule;
using Microsoft.Extensions.Options;
namespace YLErp.Services.CashRecordModule
{
/// <summary>
/// 外部资金流水接口调用服务
/// </summary>
public interface IExternalCashFlowService
{
/// <summary>
/// 获取资金流水数据
/// </summary>
/// <param name="token">令牌</param>
/// <param name="account">账号</param>
/// <param name="beginDate">开始日期</param>
/// <param name="endDate">结束日期</param>
/// <returns>资金流水数据</returns>
Task<ExternalCashFlowResponse> GetCashFlowDataAsync(string token, string account, string beginDate, string endDate);
}
/// <summary>
/// 外部资金流水接口调用服务实现
/// </summary>
public class ExternalCashFlowService : IExternalCashFlowService
{
private static readonly IYcLogger logger = LogFactory.GetLogger<ExternalCashFlowService>();
private readonly string _baseUrl;
private readonly int _timeoutSeconds;
public ExternalCashFlowService(IOptions<ExternalCashFlowOptions> options)
{
var config = options?.Value ?? new ExternalCashFlowOptions();
_baseUrl = config.BaseUrl ?? "http://10.99.56.25:8082";
_timeoutSeconds = config.TimeoutSeconds > 0 ? config.TimeoutSeconds : 60;
}
/// <summary>
/// 获取资金流水数据
/// </summary>
/// <param name="token">令牌</param>
/// <param name="account">账号</param>
/// <param name="beginDate">开始日期</param>
/// <param name="endDate">结束日期</param>
/// <returns>资金流水数据</returns>
public async Task<ExternalCashFlowResponse> GetCashFlowDataAsync(string token, string account, string beginDate, string endDate)
{
try
{
// 构建请求 URL
var apiUrl = $"{_baseUrl}/eusp/queryStatement";
var queryString = $"?token={Uri.EscapeDataString(token)}" +
$"&beginDate={Uri.EscapeDataString(beginDate)}" +
$"&endDate={Uri.EscapeDataString(endDate)}";
var requestUrl = apiUrl + queryString;
logger.Info($"调用外部资金流水接口:{requestUrl}");
// 创建 HttpClient 并设置超时
using (var httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromSeconds(_timeoutSeconds);
// 发送 GET 请求
var response = await httpClient.GetAsync(requestUrl);
response.EnsureSuccessStatusCode();
// 读取响应内容
var responseContent = await response.Content.ReadAsStringAsync();
logger.Info($"外部资金流水接口返回:{responseContent}");
// 反序列化为响应对象
var responseObj = JsonHelper.Deserialize<ExternalCashFlowResponse>(responseContent);
if (responseObj == null)
{
logger.Error("外部资金流水接口响应为空");
throw new Exception("外部资金流水接口响应为空");
}
if (responseObj.code != "0")
{
logger.Error($"外部资金流水接口调用失败:code={responseObj.code}, message={responseObj.message}");
throw new Exception($"外部资金流水接口调用失败:{responseObj.message}");
}
return responseObj;
}
}
catch (TaskCanceledException ex)
{
logger.Error($"调用外部资金流水接口超时:{ex.Message}", ex);
throw new Exception("调用外部资金流水接口超时,请检查网络连接");
}
catch (Exception ex)
{
logger.Error($"调用外部资金流水接口失败:{ex.Message}", ex);
throw new Exception($"调用外部资金流水接口失败:{ex.Message}");
}
}
}
}