Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Common/Services/Implements/JinkoSapService.cs

222 lines
7.0 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.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.Common.Services.Implements;
public class JinkoSapService : IJinkoSapService
{
private readonly IDbContext _dbContext;
private readonly IHttpClientFactory _httpClientFactory;
public JinkoSapService(IHttpClientFactory httpClientFactory, IDbContext dbContext)
{
_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);
const string credentials = $"{"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()
{
const 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()
{
const 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)
{
const string apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/";
var werkInfo = await _dbContext.Queryable<WerkEntity>()
.Where(x => x.Enable)
.Where(x => x.Werk == werk)
.SingleAsync();
var body = new
{
I_ALLINFO = "X",
T_WERKS = new
{
item = new List<object>
{
new
{
WERKS = werkInfo.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);
List<SapPersonItem> persons = new();
List<SapPersonJobStatus> jobInfos = new();
List<SapPersonDepartmentStatus> departmentInfos = new();
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
}
};
}
public async Task<SapPersonResponse> GetPersonsByCodeAsync(string code)
{
const string apiUrl = "/RESTAdapter/Rest/EHR_PERSONNEL/";
var body = new
{
I_ALLINFO = "X",
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);
List<SapPersonItem> persons = new();
List<SapPersonJobStatus> jobInfos = new();
List<SapPersonDepartmentStatus> departmentInfos = new();
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
}
};
}
public async Task<SapTravelInfoResponse> GetTravelInfosAsync(DateTime begin, DateTime end)
{
const string apiUrl = "/JIP/HCM/HCM_TRAVELINFO";
var beginDay = begin.ToString("yyyyMMdd");
var endDay = end.ToString("yyyyMMdd");
var obj = new
{
IV_BEGDA = beginDay,
IV_ENDDA = endDay
};
var body = obj.ToJson();
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();
return JsonConvert.DeserializeObject<SapTravelInfoResponse>(jsonResponse);
}
}