Files
jinko.yikatong/Src/Dlhis.ScheduledTask/SyncSapInfo/Services/Implements/JinkoSapService.cs
2024-03-15 13:59:18 +08:00

139 lines
4.7 KiB
C#

using System.Net.Http.Headers;
using System.Text;
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.SyncSapInfo.Dto;
using Dlhis.ScheduledTask.SyncSapInfo.Response;
using Kurisu.SqlSugar.Services;
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.SyncSapInfo.Services.Implements;
public class JinkoSapService : IJinkoSapService
{
private readonly ILogger<JinkoSapService> _logger;
private readonly IDbContext _dbContext;
private readonly IHttpClientFactory _httpClientFactory;
public JinkoSapService(IHttpClientFactory httpClientFactory, ILogger<JinkoSapService> logger, IDbContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
_httpClientFactory = httpClientFactory;
}
private HttpClient GetHttpClient()
{
var httpClient = _httpClientFactory.CreateClient("jinko:sap");
httpClient.BaseAddress = new Uri("http://sappipap01.jinkosolar.cn:50100");
httpClient.Timeout = TimeSpan.FromMinutes(10);
var credentials = string.Format("{0}:{1}", "EHS_USER", "y3R1hd2h*RICAC");
var headerValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);
return httpClient;
}
public async Task<SapOrganizationResponse> GetSapOrganizationsAsync()
{
string apiUrl = "/RESTAdapter/Rest/EHR_ORGANIZATION/";
var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
string responseJson = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SapOrganizationResponse>(responseJson);
return result;
}
public async Task<SapPositionResponse> GetPositionsAsync()
{
string apiUrl = "/RESTAdapter/Rest/EHR_POSITION/";
var response = await GetHttpClient().PostAsync(apiUrl, new StringContent("{}", Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
string responseJson = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SapPositionResponse>(responseJson);
return result;
}
public async Task<SapPersonResponse> GetPersonsAsync()
{
var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/";
var werks = await _dbContext.Queryable<WerkEntity>().Where(x => x.Enable).ToListAsync();
List<SapPersonItem> persons = new();
List<SapPersonJobStatus> jobInfos = new();
List<SapPersonDepartmentStatus> departmentInfos = new();
foreach (var item in werks)
{
try
{
var body = new
{
T_WERKS = new
{
item = new List<object>
{
new
{
WERKS = item.Werk
}
}
},
T_PERSON = Array.Empty<string>(),
T_PNO = Array.Empty<string>(),
};
var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
var response = await GetHttpClient().PostAsync(apiUrl, content);
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
var personResp = JsonConvert.DeserializeObject<SapPersonResponse>(jsonResponse);
if (personResp?.Person?.Items?.Any() == true)
{
persons.AddRange(personResp.Person.Items);
}
if (personResp?.JobStatusInfo?.Items?.Any() == true)
{
jobInfos.AddRange(personResp.JobStatusInfo.Items);
}
if (personResp?.DepartmentInfo?.Items?.Any() == true)
{
departmentInfos.AddRange(personResp.DepartmentInfo.Items);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "获取人事范围{werk}失败:{message}", ex.Message, item.Werk);
}
}
return new SapPersonResponse()
{
Person = new SapPerson<SapPersonItem>
{
Items = persons
},
JobStatusInfo = new SapPerson<SapPersonJobStatus>
{
Items = jobInfos,
},
DepartmentInfo = new SapPerson<SapPersonDepartmentStatus>
{
Items = departmentInfos
}
};
}
}