208 lines
6.8 KiB
C#
208 lines
6.8 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.ScheduledTask.Common.Dto;
|
|
using Dlhis.ScheduledTask.Common.Response;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Dlhis.ScheduledTask.Common.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> GetPersonsByWerkAsync(string werk)
|
|
{
|
|
var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/";
|
|
|
|
var werks = await _dbContext.Queryable<WerkEntity>()
|
|
.Where(x => x.Enable)
|
|
.WhereIF(werk.IsPresent(), x => x.Werk == werk)
|
|
.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);
|
|
}
|
|
|
|
_logger.LogInformation("获取人事范围{werk}完成", item.Werk);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "获取人事范围{werk}失败:{message}", item.Werk, ex.Message);
|
|
}
|
|
}
|
|
|
|
return new SapPersonResponse()
|
|
{
|
|
Person = new SapPerson<SapPersonItem>
|
|
{
|
|
Items = persons
|
|
},
|
|
JobStatusInfo = new SapPerson<SapPersonJobStatus>
|
|
{
|
|
Items = jobInfos,
|
|
},
|
|
DepartmentInfo = new SapPerson<SapPersonDepartmentStatus>
|
|
{
|
|
Items = departmentInfos
|
|
}
|
|
};
|
|
}
|
|
|
|
public async Task<SapPersonResponse> GetPersonsByCodeAsync(string code)
|
|
{
|
|
var apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/";
|
|
|
|
List<SapPersonItem> persons = new();
|
|
List<SapPersonJobStatus> jobInfos = new();
|
|
List<SapPersonDepartmentStatus> departmentInfos = new();
|
|
|
|
var body = new
|
|
{
|
|
T_WERKS = Array.Empty<string>(),
|
|
T_PERSON = Array.Empty<string>(),
|
|
T_PNO = new
|
|
{
|
|
item = new List<object>
|
|
{
|
|
new
|
|
{
|
|
ZWORKCODE = code
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
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);
|
|
}
|
|
|
|
return new SapPersonResponse()
|
|
{
|
|
Person = new SapPerson<SapPersonItem>
|
|
{
|
|
Items = persons
|
|
},
|
|
JobStatusInfo = new SapPerson<SapPersonJobStatus>
|
|
{
|
|
Items = jobInfos,
|
|
},
|
|
DepartmentInfo = new SapPerson<SapPersonDepartmentStatus>
|
|
{
|
|
Items = departmentInfos
|
|
}
|
|
};
|
|
}
|
|
}
|