fix 增加脏数据处理
- 优化第二天没有出差数据(基地),导致前一天需要被删除的数据没有执行
This commit is contained in:
@@ -6,6 +6,7 @@ using Dlhis.Entity.Sys.Enums;
|
|||||||
using Dlhis.Entity.Visitor.Entity;
|
using Dlhis.Entity.Visitor.Entity;
|
||||||
using Dlhis.Entity.Visitor.Enums;
|
using Dlhis.Entity.Visitor.Enums;
|
||||||
using Dlhis.Jip.Api;
|
using Dlhis.Jip.Api;
|
||||||
|
using Dlhis.Jip.Api.Responses;
|
||||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
@@ -40,47 +41,16 @@ public class SapBusinessTripService : ISapBusinessTripService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var tripResponse = await _jinkoJipApi.GetBusinessTripInfosByFactoryAsync(factory, today, today);
|
var tripResponse = await _jinkoJipApi.GetBusinessTripInfosByFactoryAsync(factory, today, today);
|
||||||
if (!tripResponse.Items.Any())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent()))
|
//先写入BusinessTripEntity表,保留原始数据
|
||||||
{
|
await PersistBusinessTripDataAsync(factory, tripResponse);
|
||||||
if (await _dbContext.Queryable<BusinessTripEntity>().Where(x => x.Code == tripInfoItem.Code
|
|
||||||
&& x.BeginDate == tripInfoItem.BeginDate
|
|
||||||
&& x.EndDate == tripInfoItem.EndDate
|
|
||||||
&& x.Destination == factory).AnyAsync())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
//获取昨天和今天的出差数据
|
||||||
entity.Status = BusinessTripStatusEnumType.Wait;
|
var tripInfos = await GetHandleBusinessTripDataAsync(factory, yesterday, today);
|
||||||
await _dbContext.InsertAsync(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取今天和昨天的出差数据
|
|
||||||
var tripInfos = await _dbContext.Queryable<BusinessTripEntity>()
|
|
||||||
.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 tripGroup = await _dbContext.Queryable<DeviceGroupEntity>()
|
var tripGroup = await GetTripGroupAsync(factory);
|
||||||
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
var deviceCodes = await GetTripGroupUnderDeviceCodesAsync(tripGroup.Code);
|
||||||
.Where(x => x.TenantId == factory)
|
|
||||||
.Where(x => x.IsBusinessTrip)
|
|
||||||
.Where(x => x.IsDeleted == false)
|
|
||||||
.SingleAsync();
|
|
||||||
|
|
||||||
var deviceCodes = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
||||||
.Where(x => x.GroupCode == tripGroup.Code)
|
|
||||||
.Where(x => x.IsDeleted == false)
|
|
||||||
.Select(x => x.DeviceCode)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
if (deviceCodes.Any())
|
if (deviceCodes.Any())
|
||||||
{
|
{
|
||||||
@@ -149,7 +119,7 @@ public class SapBusinessTripService : ISapBusinessTripService
|
|||||||
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
.Where(x => x.UniqueValue == tripGroup.Code)
|
.Where(x => x.UniqueValue == tripGroup.Code)
|
||||||
.Where(x => x.Type == tripGroup.Type)
|
.Where(x => x.Type == tripGroup.Type)
|
||||||
.Where(x => x.TenantId == factory)
|
.Where(x => x.TenantId == factory && x.IsDeleted == false)
|
||||||
.AnyAsync())
|
.AnyAsync())
|
||||||
{
|
{
|
||||||
await _dbContext.InsertAsync(new DevicePermissionEntity
|
await _dbContext.InsertAsync(new DevicePermissionEntity
|
||||||
@@ -174,6 +144,8 @@ public class SapBusinessTripService : ISapBusinessTripService
|
|||||||
_logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
_logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await HandleDirtyDataAsync(factory, tripGroup, deviceCodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -181,4 +153,116 @@ public class SapBusinessTripService : ISapBusinessTripService
|
|||||||
_logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message);
|
_logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 持久化数据
|
||||||
|
/// </summary>
|
||||||
|
private async Task PersistBusinessTripDataAsync(string factory, SapBusinessTripInfoResponse tripResponse)
|
||||||
|
{
|
||||||
|
foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent()))
|
||||||
|
{
|
||||||
|
if (await _dbContext.Queryable<BusinessTripEntity>().Where(x => x.Code == tripInfoItem.Code
|
||||||
|
&& x.BeginDate == tripInfoItem.BeginDate
|
||||||
|
&& x.EndDate == tripInfoItem.EndDate
|
||||||
|
&& x.Destination == factory).AnyAsync())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
||||||
|
entity.Status = BusinessTripStatusEnumType.Wait;
|
||||||
|
await _dbContext.InsertAsync(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task<List<BusinessTripEntity>> GetHandleBusinessTripDataAsync(string factory, DateTime yesterday, DateTime today)
|
||||||
|
{
|
||||||
|
return await _dbContext.Queryable<BusinessTripEntity>()
|
||||||
|
.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<DeviceGroupEntity> GetTripGroupAsync(string factory)
|
||||||
|
{
|
||||||
|
return await _dbContext.Queryable<DeviceGroupEntity>()
|
||||||
|
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
||||||
|
.Where(x => x.TenantId == factory)
|
||||||
|
.Where(x => x.IsBusinessTrip)
|
||||||
|
.Where(x => x.IsDeleted == false)
|
||||||
|
.SingleAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<List<Guid>> GetTripGroupUnderDeviceCodesAsync(Guid tripGroupCode)
|
||||||
|
{
|
||||||
|
return await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||||
|
.Where(x => x.GroupCode == tripGroupCode)
|
||||||
|
.Where(x => x.IsDeleted == false)
|
||||||
|
.Select(x => x.DeviceCode)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleDirtyDataAsync(string factory, DeviceGroupEntity tripGroup, List<Guid> deviceCodes)
|
||||||
|
{
|
||||||
|
//确保是真实脏数据
|
||||||
|
var yesterday = DateTime.Now.Date.AddDays(-2);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var dirtyData = await _dbContext.Queryable<BusinessTripEntity>()
|
||||||
|
.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<GainfoEntity>()
|
||||||
|
.Where(x => x.Code == item.Code)
|
||||||
|
.Select(x => new { x.No, x.Code })
|
||||||
|
.SingleAsync();
|
||||||
|
|
||||||
|
await _dbContext.Updateable<DevicePermissionEntity>()
|
||||||
|
.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<BusinessTripEntity>()
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user