PusherProviderService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using DataSharing.RawData;
  2. using DataSharing.SendTask;
  3. using DataSharing.Share.Dtos.Province;
  4. using DataSharing.Share.Dtos.Province.XieTong.Receive;
  5. using MapsterMapper;
  6. using MediatR;
  7. using Newtonsoft.Json;
  8. using System.Net;
  9. using XF.Domain.Dependency;
  10. using XF.Domain.Repository;
  11. namespace DataSharing.Province
  12. {
  13. public class PusherProviderService : IPusherProviderService, IScopeDependency
  14. {
  15. private readonly IMapper _mapper;
  16. private readonly IMediator _mediator;
  17. private readonly IChannelConfigurationManager _channelConfigurationManager;
  18. private readonly PusherProvider _pusherProvider;
  19. private readonly IHttpClientFactory _httpClientFactory;
  20. private readonly XieTongClient _xieTongClient;
  21. private readonly IRepository<DsOrder> _dsOrderRepository;
  22. private readonly IRepository<DsSendTask> _dsSendTaskRepository;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="mapper"></param>
  27. /// <param name="mediator"></param>
  28. /// <param name="channelConfigurationManager"></param>
  29. /// <param name="pusherProvider"></param>
  30. /// <param name="httpClientFactory"></param>
  31. /// <param name="xieTongClient"></param>
  32. /// <param name="dsOrderRepository"></param>
  33. /// <param name="dsSendTaskRepository"></param>
  34. public PusherProviderService(IMapper mapper, IMediator mediator,
  35. IChannelConfigurationManager channelConfigurationManager,
  36. PusherProvider pusherProvider,
  37. IHttpClientFactory httpClientFactory,
  38. XieTongClient xieTongClient,
  39. IRepository<DsOrder> dsOrderRepository,
  40. IRepository<DsSendTask> dsSendTaskRepository)
  41. {
  42. _mapper = mapper;
  43. _mediator = mediator;
  44. _channelConfigurationManager = channelConfigurationManager;
  45. _pusherProvider = pusherProvider;
  46. _httpClientFactory = httpClientFactory;
  47. _xieTongClient = xieTongClient;
  48. _dsOrderRepository = dsOrderRepository;
  49. _dsSendTaskRepository = dsSendTaskRepository;
  50. }
  51. /// <summary>
  52. ///附件上传处理
  53. /// </summary>
  54. /// <param name="dto"></param>
  55. /// <param name="cancellationToken"></param>
  56. /// <returns></returns>
  57. public async Task GetCaseMaterialInfoPusher(DsSendTask dto, CancellationToken cancellationToken)
  58. {
  59. if (dto is null)
  60. return;
  61. bool isRight = true;
  62. //读取每一条数据中的附件参数
  63. if (dto.FileJson != null && dto.FileJson.Count > 0)
  64. {
  65. foreach (var itemFile in dto.FileJson)
  66. {
  67. var result = await SendFileData(dto.Request, itemFile.FileId, itemFile.FileId, cancellationToken);
  68. if (result != null && result.ReturnInfo != null)
  69. {
  70. //结果处理
  71. if (result.ReturnInfo.Code != "1")
  72. isRight = false;
  73. }
  74. }
  75. }
  76. if (isRight)
  77. {
  78. dto.IsSuccess = true;
  79. await _dsSendTaskRepository.UpdateAsync(dto, cancellationToken);
  80. }
  81. }
  82. /// <summary>
  83. /// 附件读取以及附件上传
  84. /// </summary>
  85. /// <param name="dto"></param>
  86. /// <param name="fileName"></param>
  87. /// <param name="fileId"></param>
  88. /// <returns></returns>
  89. private async Task<ProvinceResponse> SendFileData(string request, string fileName, string fileId, CancellationToken cancellationToken)
  90. {
  91. //获取配置信息
  92. var configProvince = _channelConfigurationManager.GetConfigurationProvince();
  93. var businessFile = _channelConfigurationManager.GetConfigurationBusinessFile();
  94. byte[] fileContentBytes = null;
  95. try
  96. {
  97. string url = string.Format("{0}{1}?Source={2}&Id={3}", businessFile.BaseUrl, businessFile.DownloadUrlAddress, businessFile.Source, fileId);
  98. using (var client = _httpClientFactory.CreateClient())
  99. using (var response = await client.GetAsync(url))
  100. {
  101. if (response.StatusCode == HttpStatusCode.OK)
  102. {
  103. using var memoryStream = new MemoryStream();
  104. await response.Content.CopyToAsync(memoryStream);
  105. memoryStream.Seek(0, SeekOrigin.Begin);
  106. fileContentBytes = memoryStream.ToArray();
  107. }
  108. }
  109. if (fileContentBytes != null)
  110. {
  111. //获取附件上传Token
  112. string strToken = await _xieTongClient.GetTokenAsync(cancellationToken);
  113. ////组装请求参数
  114. //var model = new GetCaseMaterialInfoRequest
  115. //{
  116. // paras = request,
  117. // token = new ClientInfo(configProvince.ClientId, configProvince.ClientSecret)
  118. //};
  119. Dictionary<string, object> dicParam = new()
  120. {
  121. { "params", request } // 第一个接口参数,json格式字符串
  122. };
  123. // 构造字典文件数据
  124. // 接口参数名称为files
  125. CFormUpload.FileParameter fileParameter = new CFormUpload.FileParameter("files", fileContentBytes, fileName, null);
  126. dicParam.Add(fileName, fileParameter);
  127. string strUrl = configProvince.HuiJu + "get_case_material_info";
  128. // 上传附件
  129. string strResult = CFormUpload.MultipartFormDataPost(strUrl, null, dicParam, strToken);
  130. if (!string.IsNullOrEmpty(strResult))
  131. return JsonConvert.DeserializeObject<ProvinceResponse>(strResult);
  132. }
  133. return new ProvinceResponse();
  134. }
  135. catch (Exception)
  136. {
  137. return new ProvinceResponse();
  138. }
  139. }
  140. }
  141. }