diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/SapBusinessTripService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/SapBusinessTripService.cs index db8d0445..27b4c38e 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/SapBusinessTripService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/SapBusinessTripService.cs @@ -6,6 +6,7 @@ using Dlhis.Entity.Sys.Enums; using Dlhis.Entity.Visitor.Entity; using Dlhis.Entity.Visitor.Enums; using Dlhis.Jip.Api; +using Dlhis.Jip.Api.Responses; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; using Mapster; @@ -40,47 +41,16 @@ public class SapBusinessTripService : ISapBusinessTripService try { var tripResponse = await _jinkoJipApi.GetBusinessTripInfosByFactoryAsync(factory, today, today); - if (!tripResponse.Items.Any()) - { - return; - } - foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent())) - { - if (await _dbContext.Queryable().Where(x => x.Code == tripInfoItem.Code - && x.BeginDate == tripInfoItem.BeginDate - && x.EndDate == tripInfoItem.EndDate - && x.Destination == factory).AnyAsync()) - { - continue; - } + //先写入BusinessTripEntity表,保留原始数据 + await PersistBusinessTripDataAsync(factory, tripResponse); - var entity = tripInfoItem.Adapt(); - entity.Status = BusinessTripStatusEnumType.Wait; - await _dbContext.InsertAsync(entity); - } - - //获取今天和昨天的出差数据 - var tripInfos = await _dbContext.Queryable() - .Where(x => SqlFunc.DateIsSame(x.BeginDate, today) || SqlFunc.DateIsSame(x.BeginDate, yesterday)) - .Where(x => !SqlFunc.IsNullOrEmpty(x.Destination)) - //.Where(x => !SqlFunc.IsNullOrEmpty(x.BeginDateTime) && !SqlFunc.IsNullOrEmpty(x.EndDateTime)) - .Where(x => x.Destination == factory) - .ToListAsync(); + //获取昨天和今天的出差数据 + var tripInfos = await GetHandleBusinessTripDataAsync(factory, yesterday, today); //获取分组信息 - var tripGroup = await _dbContext.Queryable() - .Where(x => x.Type == DeviceTypeEnumType.Door) - .Where(x => x.TenantId == factory) - .Where(x => x.IsBusinessTrip) - .Where(x => x.IsDeleted == false) - .SingleAsync(); - - var deviceCodes = await _dbContext.Queryable() - .Where(x => x.GroupCode == tripGroup.Code) - .Where(x => x.IsDeleted == false) - .Select(x => x.DeviceCode) - .ToListAsync(); + var tripGroup = await GetTripGroupAsync(factory); + var deviceCodes = await GetTripGroupUnderDeviceCodesAsync(tripGroup.Code); if (deviceCodes.Any()) { @@ -149,7 +119,7 @@ public class SapBusinessTripService : ISapBusinessTripService .Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group) .Where(x => x.UniqueValue == tripGroup.Code) .Where(x => x.Type == tripGroup.Type) - .Where(x => x.TenantId == factory) + .Where(x => x.TenantId == factory && x.IsDeleted == false) .AnyAsync()) { await _dbContext.InsertAsync(new DevicePermissionEntity @@ -174,6 +144,8 @@ public class SapBusinessTripService : ISapBusinessTripService _logger.LogError(e, "出差自动下发任务失败:{message}", e.Message); } } + + await HandleDirtyDataAsync(factory, tripGroup, deviceCodes); } } catch (Exception e) @@ -181,4 +153,116 @@ public class SapBusinessTripService : ISapBusinessTripService _logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message); } } + + + /// + /// 持久化数据 + /// + private async Task PersistBusinessTripDataAsync(string factory, SapBusinessTripInfoResponse tripResponse) + { + foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent())) + { + if (await _dbContext.Queryable().Where(x => x.Code == tripInfoItem.Code + && x.BeginDate == tripInfoItem.BeginDate + && x.EndDate == tripInfoItem.EndDate + && x.Destination == factory).AnyAsync()) + { + continue; + } + + var entity = tripInfoItem.Adapt(); + entity.Status = BusinessTripStatusEnumType.Wait; + await _dbContext.InsertAsync(entity); + } + } + + + private async Task> GetHandleBusinessTripDataAsync(string factory, DateTime yesterday, DateTime today) + { + return await _dbContext.Queryable() + .Where(x => SqlFunc.DateIsSame(x.BeginDate, yesterday) || SqlFunc.DateIsSame(x.BeginDate, today)) + .Where(x => !SqlFunc.IsNullOrEmpty(x.Destination)) //必须有目的地 + //.Where(x => !SqlFunc.IsNullOrEmpty(x.BeginDateTime) && !SqlFunc.IsNullOrEmpty(x.EndDateTime)) + .Where(x => x.Destination == factory) + .ToListAsync(); + } + + + private async Task GetTripGroupAsync(string factory) + { + return await _dbContext.Queryable() + .Where(x => x.Type == DeviceTypeEnumType.Door) + .Where(x => x.TenantId == factory) + .Where(x => x.IsBusinessTrip) + .Where(x => x.IsDeleted == false) + .SingleAsync(); + } + + private async Task> GetTripGroupUnderDeviceCodesAsync(Guid tripGroupCode) + { + return await _dbContext.Queryable() + .Where(x => x.GroupCode == tripGroupCode) + .Where(x => x.IsDeleted == false) + .Select(x => x.DeviceCode) + .ToListAsync(); + } + + private async Task HandleDirtyDataAsync(string factory, DeviceGroupEntity tripGroup, List deviceCodes) + { + //确保是真实脏数据 + var yesterday = DateTime.Now.Date.AddDays(-2); + try + { + var dirtyData = await _dbContext.Queryable() + .Where(x => x.Destination == factory) + .Where(x => !SqlFunc.IsNullOrEmpty(x.Destination)) + .Where(x => x.Status == BusinessTripStatusEnumType.Generated) + .Where(x => x.BeginDate < yesterday) + .Select(x => new + { + num = SqlFunc.RowNumber(SqlFunc.Desc(x.BeginDate), x.Code), + x.Id, x.Code, x.BeginDate + }).MergeTable() + .Where(x => x.num == 1) + .ToListAsync(); + + + foreach (var item in dirtyData) + { + try + { + var currentGainfo = await _dbContext.Queryable() + .Where(x => x.Code == item.Code) + .Select(x => new { x.No, x.Code }) + .SingleAsync(); + + await _dbContext.Updateable() + .SetColumns(x => x.IsDeleted == true) + .Where(x => x.AssignType == AssignTypeEnumType.User) + .Where(x => x.AssignValue == currentGainfo.No) + .Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group) + .Where(x => x.UniqueValue == tripGroup.Code) + .Where(x => x.Type == tripGroup.Type) + .Where(x => x.TenantId == factory) + .ExecuteCommandAsync(); + + await _dbContext.Updateable() + .SetColumns(x => x.Status == BusinessTripStatusEnumType.Deleted) + .Where(x => x.Id == item.Id) + .ExecuteCommandAsync(); + + + await _gainfoPermissionService.ResetPermissionsAsync(currentGainfo.No, DeviceTaskEnumType.Delete, deviceCodes, "出差遗留脏数据删除"); + } + catch (Exception e) + { + _logger.LogError(e, "处理出差遗留脏数据删除失败:工号{code}", item.Code); + } + } + } + catch (Exception e) + { + _logger.LogError(e, "处理{factory}遗留出差数据失败:{message}", factory, e.Message); + } + } } \ No newline at end of file