You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.5 KiB
C#

using EntrustSettle.Common;
using EntrustSettle.Common.Caches;
using EntrustSettle.Common.Const;
using EntrustSettle.IServices;
using EntrustSettle.Model;
using EntrustSettle.Model.Dtos;
8 months ago
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
8 months ago
using NLog;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
8 months ago
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace EntrustSettle.Services
{
public class HYDService : IHYDService
{
private readonly ICaching caching;
8 months ago
private readonly ILogger logger;
private readonly IRestClient client;
8 months ago
public HYDService(ICaching caching,
7 months ago
//ILoggerFactory logFactory,
ILogger<HYDService> logger,
8 months ago
[FromKeyedServices(HttpEnum.Hyd)] IRestClient client)
{
this.caching = caching;
8 months ago
this.client = client;
7 months ago
//logger = logFactory.CreateLogger("BigDataLogger");
this.logger = logger;
}
public async Task<long> Submit(HydSubmitDto submitDto)
{
7 months ago
try
{
7 months ago
string token = await GetToken();
7 months ago
var url = AppSettings.app("Apis", "Hyd", "SubmitUrl");
var customer = AppSettings.app("Apis", "Hyd", "CUSTOMER");
var headers = new Dictionary<string, string>()
{
{ "PDK", token },
{ "CUSTOMER", customer }
};
var request = new RestRequest(url);
request.AddHeaders(headers);
request.AddJsonBody(submitDto);
7 months ago
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
logger.LogRestSharp("海运达下单接口", client, request, resp);
8 months ago
7 months ago
if (resp.IsSuccessful)
8 months ago
{
7 months ago
var jobj = JObject.Parse(resp.Content);
var code = jobj.GetValue("code").ObjToInt();
if (code == 200)
8 months ago
{
7 months ago
var bsno = jobj.GetValue("data").ObjToLong();
return bsno;
}
else
{
if (code > 400 && code < 500)
{
await caching.DelCacheKeyAsync(CacheConst.HydAccessToken);
}
8 months ago
}
}
7 months ago
throw new Exception("调用外部下单接口失败");
}
catch (Exception ex)
{
throw new Exception("下单失败,原因:" + ex.Message);
}
}
public async Task<List<HydQueryResultDto>> Query(string mblno)
{
string token = await GetToken();
var url = AppSettings.app("Apis", "Hyd", "QueryUrl");
var customer = AppSettings.app("Apis", "Hyd", "CUSTOMER");
var headers = new Dictionary<string, string>()
{
{ "PDK", token },
{ "CUSTOMER", customer }
};
string[] param = [mblno];
8 months ago
var request = new RestRequest(url);
request.AddHeaders(headers);
request.AddJsonBody(param);
8 months ago
var resp = await client.ExecuteGetAsync(request); // 以Execute开头的方法不会引发异常
logger.LogRestSharp("海运达状态查询接口", client, request, resp);
if (resp.IsSuccessful)
{
8 months ago
var jobj = JObject.Parse(resp.Content);
var code = jobj.GetValue("code").ObjToInt();
if (code == 200)
{
var result = JsonConvert.DeserializeObject<List<HydQueryResultDto>>(jobj.GetValue("rows").ToString());
return result;
}
else
{
8 months ago
if (code > 400 && code < 500)
{
await caching.DelCacheKeyAsync(CacheConst.HydAccessToken);
}
}
}
7 months ago
throw new Exception("调用外部状态查询接口失败");
}
public async Task<bool> FeedBack(List<HydFeedbackDto> hydFeedbackDtoList)
{
7 months ago
try
{
7 months ago
string token = await GetToken();
7 months ago
var url = AppSettings.app("Apis", "Hyd", "FeedbackUrl");
var customer = AppSettings.app("Apis", "Hyd", "CUSTOMER");
var headers = new Dictionary<string, string>()
{
7 months ago
{ "PDK", token },
{ "CUSTOMER", customer }
};
foreach (var item in hydFeedbackDtoList)
{
var request = new RestRequest(url);
request.AddHeaders(headers);
request.AddJsonBody(item);
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
logger.LogRestSharp("海运达反馈接口", client, request, resp);
if (resp.IsSuccessful)
8 months ago
{
7 months ago
var jobj = JObject.Parse(resp.Content);
var code = jobj.GetValue("code").ObjToInt();
if (code == 200)
8 months ago
{
7 months ago
continue;
}
else
{
if (code > 400 && code < 500)
{
await caching.DelCacheKeyAsync(CacheConst.HydAccessToken);
}
var msg = jobj.GetValue("msg").ToString();
8 months ago
}
}
7 months ago
throw new Exception("调用外部反馈接口失败");
}
7 months ago
return true;
}
catch (Exception ex)
{
throw new Exception("反馈失败,原因:" + ex.Message);
}
}
private async Task<string> GetToken()
{
var token = await caching.GetAsync<string>(CacheConst.HydAccessToken);
if (!string.IsNullOrEmpty(token))
{
return token;
}
var url = AppSettings.app("Apis", "Hyd", "LoginUrl");
8 months ago
var request = new RestRequest(url);
var body = new
{
8 months ago
username = AppSettings.app("Apis", "Hyd", "username"),
password = AppSettings.app("Apis", "Hyd", "password")
};
8 months ago
request.AddJsonBody(body);
8 months ago
var resp = await client.ExecutePostAsync(request); // 以Execute开头的方法不会引发异常
logger.LogRestSharp("海运达登录接口", client, request, resp);
8 months ago
if (resp.IsSuccessful)
{
8 months ago
var jobj = JObject.Parse(resp.Content);
var code = jobj.GetValue("code");
if (code.ObjToInt() == 200)
{
token = jobj.GetValue("token").ToString();
await caching.SetAsync(CacheConst.HydAccessToken, token, TimeSpan.FromHours(3));
return token;
}
}
7 months ago
throw new Exception($"调用外部登录接口失败({resp.ErrorException?.InnerException?.Message})");
}
}
}