fix: 人员同步按最新数据处理

This commit is contained in:
2026-05-27 11:47:12 +08:00
parent 05f0dfb312
commit cbd375fbb4
2 changed files with 15 additions and 12 deletions

View File

@@ -469,7 +469,7 @@ public class GainfoUpdateService : IGainfoUpdateService
var lastMap = last
.Where(x => !string.IsNullOrWhiteSpace(x.ZWORKCODE))
.GroupBy(x => x.ZWORKCODE)
.ToDictionary(x => x.Key, x => x.ToList());
.ToDictionary(x => x.Key, x => x.First());
var modifiedPersons = new List<SapPersonItem>();
foreach (var person in sapList)
@@ -480,16 +480,14 @@ public class GainfoUpdateService : IGainfoUpdateService
continue;
}
if (!lastMap.TryGetValue(person.ZWORKCODE, out var historyPersons) || historyPersons.Count <= 0)
if (!lastMap.TryGetValue(person.ZWORKCODE, out var latestHistory))
{
modifiedPersons.Add(person);
continue;
}
var exactHistory = historyPersons.FirstOrDefault(x => comparer.Equals(person, x));
if (exactHistory != null)
if (comparer.Equals(person, latestHistory))
{
historyPersons.Remove(exactHistory);
continue;
}
@@ -500,13 +498,13 @@ public class GainfoUpdateService : IGainfoUpdateService
{
var first = modifiedPersons[0];
var historyCandidates = !string.IsNullOrWhiteSpace(first.ZWORKCODE)
&& lastMap.TryGetValue(first.ZWORKCODE, out var currentHistoryPersons)
? currentHistoryPersons
: new List<SapPersonItem>();
var hasHistory = historyCandidates.Count > 0;
&& lastMap.TryGetValue(first.ZWORKCODE, out var currentHistoryPerson)
? currentHistoryPerson
: null;
var hasHistory = historyCandidates != null;
var diffType = hasHistory ? "Update" : "Insert";
var diffMessage = hasHistory
? string.Join("; ", GetFirstDiffFields(first, historyCandidates[0]))
? string.Join("; ", GetFirstDiffFields(first, historyCandidates))
: "历史记录不存在";
_logger.LogWarning("SAP首条差异 DiffType:{diffType}, ZWORKCODE:{workCode}, HasHistory:{hasHistory}, Diffs:{diffs}",
diffType, first.ZWORKCODE, hasHistory, diffMessage);
@@ -535,7 +533,13 @@ public class GainfoUpdateService : IGainfoUpdateService
var items = await _dbContext.Queryable<GainfoSapEntity>()
.Where(x => chunk.Contains(x.ename))
.ToListAsync();
gainfos.AddRange(items);
var latestItems = items
.OrderByDescending(x => x.logdate)
.ThenByDescending(x => x.id)
.GroupBy(x => x.ename)
.Select(x => x.First())
.ToList();
gainfos.AddRange(latestItems);
}
return gainfos.Select(ToSapPersonItem).ToList();