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 { /// /// 外部资金流水接口调用服务 /// public interface IExternalCashFlowService { /// /// 获取资金流水数据 /// /// 令牌 /// 账号 /// 开始日期 /// 结束日期 /// 资金流水数据 Task GetCashFlowDataAsync(string token, string account, string beginDate, string endDate); } /// /// 外部资金流水接口调用服务实现 /// public class ExternalCashFlowService : IExternalCashFlowService { private static readonly IYcLogger logger = LogFactory.GetLogger(); private readonly string _baseUrl; private readonly int _timeoutSeconds; public ExternalCashFlowService(IOptions options) { var config = options?.Value ?? new ExternalCashFlowOptions(); _baseUrl = config.BaseUrl ?? "http://10.99.56.25:8082"; _timeoutSeconds = config.TimeoutSeconds > 0 ? config.TimeoutSeconds : 60; } /// /// 获取资金流水数据 /// /// 令牌 /// 账号 /// 开始日期 /// 结束日期 /// 资金流水数据 public async Task 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(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}"); } } } }