123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using DataSharing.RawData;
- using DataSharing.SendTask;
- using DataSharing.Share.Dtos.Province;
- using DataSharing.Share.Dtos.Province.XieTong.Receive;
- using MapsterMapper;
- using MediatR;
- using Newtonsoft.Json;
- using System.Net;
- using XF.Domain.Dependency;
- using XF.Domain.Repository;
- namespace DataSharing.Province
- {
- public class PusherProviderService : IPusherProviderService, IScopeDependency
- {
- private readonly IMapper _mapper;
- private readonly IMediator _mediator;
- private readonly IChannelConfigurationManager _channelConfigurationManager;
- private readonly PusherProvider _pusherProvider;
- private readonly IHttpClientFactory _httpClientFactory;
- private readonly XieTongClient _xieTongClient;
- private readonly IRepository<DsOrder> _dsOrderRepository;
- private readonly IRepository<DsSendTask> _dsSendTaskRepository;
- /// <summary>
- ///
- /// </summary>
- /// <param name="mapper"></param>
- /// <param name="mediator"></param>
- /// <param name="channelConfigurationManager"></param>
- /// <param name="pusherProvider"></param>
- /// <param name="httpClientFactory"></param>
- /// <param name="xieTongClient"></param>
- /// <param name="dsOrderRepository"></param>
- /// <param name="dsSendTaskRepository"></param>
- public PusherProviderService(IMapper mapper, IMediator mediator,
- IChannelConfigurationManager channelConfigurationManager,
- PusherProvider pusherProvider,
- IHttpClientFactory httpClientFactory,
- XieTongClient xieTongClient,
- IRepository<DsOrder> dsOrderRepository,
- IRepository<DsSendTask> dsSendTaskRepository)
- {
- _mapper = mapper;
- _mediator = mediator;
- _channelConfigurationManager = channelConfigurationManager;
- _pusherProvider = pusherProvider;
- _httpClientFactory = httpClientFactory;
- _xieTongClient = xieTongClient;
- _dsOrderRepository = dsOrderRepository;
- _dsSendTaskRepository = dsSendTaskRepository;
- }
- /// <summary>
- ///附件上传处理
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task GetCaseMaterialInfoPusher(DsSendTask dto, CancellationToken cancellationToken)
- {
- if (dto is null)
- return;
- bool isRight = true;
- //读取每一条数据中的附件参数
- if (dto.FileJson != null && dto.FileJson.Count > 0)
- {
- foreach (var itemFile in dto.FileJson)
- {
- var result = await SendFileData(dto.Request, itemFile.FileId, itemFile.FileId, cancellationToken);
- if (result != null && result.ReturnInfo != null)
- {
- //结果处理
- if (result.ReturnInfo.Code != "1")
- isRight = false;
- }
- }
- }
- if (isRight)
- {
- dto.IsSuccess = true;
- await _dsSendTaskRepository.UpdateAsync(dto, cancellationToken);
- }
- }
- /// <summary>
- /// 附件读取以及附件上传
- /// </summary>
- /// <param name="dto"></param>
- /// <param name="fileName"></param>
- /// <param name="fileId"></param>
- /// <returns></returns>
- private async Task<ProvinceResponse> SendFileData(string request, string fileName, string fileId, CancellationToken cancellationToken)
- {
- //获取配置信息
- var configProvince = _channelConfigurationManager.GetConfigurationProvince();
- var businessFile = _channelConfigurationManager.GetConfigurationBusinessFile();
- byte[] fileContentBytes = null;
- try
- {
- string url = string.Format("{0}{1}?Source={2}&Id={3}", businessFile.BaseUrl, businessFile.DownloadUrlAddress, businessFile.Source, fileId);
- using (var client = _httpClientFactory.CreateClient())
- using (var response = await client.GetAsync(url))
- {
- if (response.StatusCode == HttpStatusCode.OK)
- {
- using var memoryStream = new MemoryStream();
- await response.Content.CopyToAsync(memoryStream);
- memoryStream.Seek(0, SeekOrigin.Begin);
- fileContentBytes = memoryStream.ToArray();
- }
- }
- if (fileContentBytes != null)
- {
- //获取附件上传Token
- string strToken = await _xieTongClient.GetTokenAsync(cancellationToken);
- ////组装请求参数
- //var model = new GetCaseMaterialInfoRequest
- //{
- // paras = request,
- // token = new ClientInfo(configProvince.ClientId, configProvince.ClientSecret)
- //};
- Dictionary<string, object> dicParam = new()
- {
- { "params", request } // 第一个接口参数,json格式字符串
- };
- // 构造字典文件数据
- // 接口参数名称为files
- CFormUpload.FileParameter fileParameter = new CFormUpload.FileParameter("files", fileContentBytes, fileName, null);
- dicParam.Add(fileName, fileParameter);
- string strUrl = configProvince.HuiJu + "get_case_material_info";
- // 上传附件
- string strResult = CFormUpload.MultipartFormDataPost(strUrl, null, dicParam, strToken);
- if (!string.IsNullOrEmpty(strResult))
- return JsonConvert.DeserializeObject<ProvinceResponse>(strResult);
- }
- return new ProvinceResponse();
- }
- catch (Exception)
- {
- return new ProvinceResponse();
- }
- }
- }
- }
|