fix 同步接口
This commit is contained in:
96
Src/Dlhis.ScheduledTask/Job/SyncOrgInfoJob.cs
Normal file
96
Src/Dlhis.ScheduledTask/Job/SyncOrgInfoJob.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.ScheduledTask.Comparers;
|
||||
using Dlhis.ScheduledTask.Entities;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Dlhis.ScheduledTask.Job
|
||||
{
|
||||
/// <summary>
|
||||
/// 部门同步定时任务
|
||||
/// </summary>
|
||||
public class SyncOrgInfoJob
|
||||
{
|
||||
private List<ZHRS_ORG> lastUpdatedOrgList = new List<ZHRS_ORG>();
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public SyncOrgInfoJob(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task SyncOrgInfo()
|
||||
{
|
||||
HttpClient httpClient = new HttpClient();
|
||||
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);
|
||||
|
||||
string apiUrl =
|
||||
"http://sappidap.jinkosolar.cn:51000/RESTAdapter/Rest/EHR_ORGANIZATION/";
|
||||
|
||||
|
||||
string requestBody = "{\"I_DATE\":\"2021-01-01\",\"I_DATE_TO\":\"2022-05-29\",\"I_LANGU\":\"1\",\"T_ID\":{\"item\":[{\"OBJID\":\"50007967\"},{\"OBJID\":\"50008308\"}]}}";
|
||||
|
||||
HttpResponseMessage response = await httpClient.PostAsync(apiUrl, new StringContent(requestBody, Encoding.UTF8, "application/json"));
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
string responseData = await response.Content.ReadAsStringAsync();
|
||||
ZHRS_ORGResult orgResult = JsonSerializer.Deserialize<ZHRS_ORGResult>(responseData);
|
||||
|
||||
if (orgResult.T_ORG != null && orgResult.T_ORG.item.Count > 0)
|
||||
{
|
||||
var modifiedOrgList = GetModifiedOrg(orgResult.T_ORG?.item);
|
||||
|
||||
// 使用 SQLSugar 将新数据同步到数据库
|
||||
await SaveOrg(modifiedOrgList);
|
||||
}
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// Console.WriteLine($"HTTP请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
|
||||
private List<ZHRS_ORG> GetModifiedOrg(List<ZHRS_ORG> sapList)
|
||||
{
|
||||
if (lastUpdatedOrgList.Count <= 0)
|
||||
{
|
||||
return sapList;
|
||||
}
|
||||
|
||||
var needRemove = sapList.Intersect(lastUpdatedOrgList, new ZHRS_ORGComparer());
|
||||
return sapList.Except(needRemove, new ZHRS_ORGComparer()).ToList();
|
||||
}
|
||||
|
||||
private async Task SaveOrg(List<ZHRS_ORG> modifiedOrgList)
|
||||
{
|
||||
if (modifiedOrgList == null || modifiedOrgList.Count == 0) return;
|
||||
|
||||
foreach (ZHRS_ORG item in modifiedOrgList)
|
||||
{
|
||||
|
||||
|
||||
var existingOrgs = await _dbContext.Queryable<ZHRF_SYN_01Entity>().Where(x => x.OBJID == item.OBJID).FirstAsync();
|
||||
|
||||
if (existingOrgs != null)
|
||||
{
|
||||
var org = item.Adapt(existingOrgs);
|
||||
await _dbContext.UpdateAsync(org);
|
||||
}
|
||||
else
|
||||
{
|
||||
var org = item.Adapt<ZHRF_SYN_01Entity>();
|
||||
await _dbContext.InsertAsync(org);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user