fix 优化处理逻辑
This commit is contained in:
@@ -11,139 +11,129 @@ using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Mapster;
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.ScheduledTask.Common.Services.Implements
|
||||
namespace Dlhis.ScheduledTask.Common.Services.Implements;
|
||||
|
||||
public class SapBusinessTripService : ISapBusinessTripService
|
||||
{
|
||||
public class SapBusinessTripService : ISapBusinessTripService
|
||||
private readonly IJinkoJIPService _jinkoJipService;
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IDevicePermissionApi _devicePermissionApi;
|
||||
private readonly ILogger<SapBusinessTripService> _logger;
|
||||
|
||||
public SapBusinessTripService(IJinkoJIPService jinkoJipService, IDbContext dbContext, IDevicePermissionApi devicePermissionApi, ILogger<SapBusinessTripService> logger)
|
||||
{
|
||||
private readonly IJinkoJIPService _jinkoJipService;
|
||||
private readonly IDbContext _dbContext;
|
||||
private readonly IDevicePermissionApi _devicePermissionApi;
|
||||
private readonly ILogger<SapBusinessTripService> _logger;
|
||||
_jinkoJipService = jinkoJipService;
|
||||
_dbContext = dbContext;
|
||||
_devicePermissionApi = devicePermissionApi;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public SapBusinessTripService(IJinkoJIPService jinkoJipService, IDbContext dbContext, IDevicePermissionApi devicePermissionApi, ILogger<SapBusinessTripService> logger)
|
||||
/// <inheritdoc />
|
||||
public async Task HandlerBusinessTripsByFactoryAsync(string factory)
|
||||
{
|
||||
var today = DateTime.Today;
|
||||
var yesterday = today.AddDays(-1);
|
||||
|
||||
try
|
||||
{
|
||||
_jinkoJipService = jinkoJipService;
|
||||
_dbContext = dbContext;
|
||||
_devicePermissionApi = devicePermissionApi;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task HandlerBusinessTripsByFactoryAsync(string factory)
|
||||
{
|
||||
var today = DateTime.Today;
|
||||
var lastDay = today.AddDays(-1);
|
||||
|
||||
try
|
||||
var tripResponse = await _jinkoJipService.GetTravelInfosByFactoryAsync(today, today, factory);
|
||||
foreach (var tripInfoItem in tripResponse.Items.Where(x => x.Destination.IsPresent()))
|
||||
{
|
||||
var tripResponse = await _jinkoJipService.GetTravelInfosByFactoryAsync(today, today, factory);
|
||||
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())
|
||||
{
|
||||
if (await _dbContext.Queryable<BusinessTripEntity>().Where(x => x.Code == tripInfoItem.Code
|
||||
&& x.BeginDate == tripInfoItem.BeginDate
|
||||
&& x.EndDate == tripInfoItem.EndDate).AnyAsync())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
||||
entity.Status = BusinessTripStatusEnumType.Wait;
|
||||
await _dbContext.InsertAsync(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
//获取今天和昨天的出差数据
|
||||
var tripInfos = await _dbContext.Queryable<BusinessTripEntity>()
|
||||
.Where(x => (SqlFunc.DateIsSame(x.BeginDate, today) && SqlFunc.DateIsSame(x.BeginDate, today))
|
||||
|| (SqlFunc.DateIsSame(x.BeginDate, lastDay) && SqlFunc.DateIsSame(x.BeginDate, lastDay)))
|
||||
.Where(x => !SqlFunc.IsNullOrEmpty(x.Destination))
|
||||
.Where(x => x.Status == BusinessTripStatusEnumType.Wait)
|
||||
.Where(x => x.Destination == factory)
|
||||
.ToListAsync();
|
||||
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
||||
entity.Status = BusinessTripStatusEnumType.Wait;
|
||||
await _dbContext.InsertAsync(entity);
|
||||
}
|
||||
|
||||
var tripGroup = await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
||||
.Where(x => x.TenantId == factory)
|
||||
.Where(x => x.IsBusinessTrip)
|
||||
.SingleAsync();
|
||||
//获取今天和昨天的出差数据
|
||||
var tripInfos = await _dbContext.Queryable<BusinessTripEntity>()
|
||||
.Where(x => (SqlFunc.DateIsSame(x.BeginDate, today) && SqlFunc.DateIsSame(x.BeginDate, today))
|
||||
|| (SqlFunc.DateIsSame(x.BeginDate, yesterday) && SqlFunc.DateIsSame(x.BeginDate, yesterday)))
|
||||
.Where(x => !SqlFunc.IsNullOrEmpty(x.Destination))
|
||||
.Where(x => x.Status == BusinessTripStatusEnumType.Wait)
|
||||
.Where(x => x.Destination == factory)
|
||||
.ToListAsync();
|
||||
|
||||
var userCodes = tripInfos.Select(x => x.Code).Distinct().ToList();
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userCodes.Contains(x.Code)).ToListAsync();
|
||||
//获取分组信息
|
||||
var tripGroup = await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
||||
.Where(x => x.TenantId == factory)
|
||||
.Where(x => x.IsBusinessTrip)
|
||||
.SingleAsync();
|
||||
|
||||
foreach (var gainfo in gainfos)
|
||||
var userCodes = tripInfos.Select(x => x.Code).Distinct().ToList();
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userCodes.Contains(x.Code)).ToListAsync();
|
||||
|
||||
foreach (var gainfo in gainfos)
|
||||
{
|
||||
try
|
||||
{
|
||||
var myTripInfos = tripInfos.Where(x => x.Code == gainfo.Code).ToList();
|
||||
|
||||
var todayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == today && x.EndDate == today);
|
||||
var lastDayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == lastDay && x.EndDate == lastDay);
|
||||
var yesterdayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == yesterday && x.EndDate == yesterday); //昨天出差数据
|
||||
var todayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == today && x.EndDate == today); //今天出差数据
|
||||
|
||||
bool isGenerateOrDeleted = false;
|
||||
//都为空不处理
|
||||
if (yesterdayTripInfo == null && todayTripInfo == null) continue;
|
||||
|
||||
if (lastDayTripInfo == null && todayTripInfo != null)
|
||||
{
|
||||
isGenerateOrDeleted = true;
|
||||
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
||||
}
|
||||
|
||||
if (lastDayTripInfo != null && todayTripInfo == null)
|
||||
{
|
||||
lastDayTripInfo.Status = BusinessTripStatusEnumType.Deleted;
|
||||
}
|
||||
|
||||
if (lastDayTripInfo != null && todayTripInfo != null)
|
||||
//都不为空则更新数据状态
|
||||
if (yesterdayTripInfo != null && todayTripInfo != null)
|
||||
{
|
||||
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
||||
await _dbContext.UpdateAsync(todayTripInfo);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
if (yesterdayTripInfo == null)
|
||||
{
|
||||
var beforeResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(new List<string> { gainfo.Code });
|
||||
|
||||
if (isGenerateOrDeleted)
|
||||
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
||||
await _dbContext.InsertAsync(new DevicePermissionEntity
|
||||
{
|
||||
await _dbContext.InsertAsync(new DevicePermissionEntity
|
||||
{
|
||||
AssignType = AssignTypeEnumType.User,
|
||||
AssignValue = gainfo.No,
|
||||
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
||||
UniqueValue = tripGroup.Code,
|
||||
TenantId = factory,
|
||||
Type = tripGroup.Type,
|
||||
IsDeleted = false
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
await _dbContext.Updateable<DevicePermissionEntity>()
|
||||
.SetColumns(x => x.IsDeleted == true)
|
||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Where(x => x.AssignValue == gainfo.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 _devicePermissionApi.RePermissionUsersInfoAsync(beforeResult.Data);
|
||||
|
||||
if (lastDayTripInfo != null)
|
||||
await _dbContext.UpdateAsync(lastDayTripInfo);
|
||||
|
||||
if (todayTripInfo != null)
|
||||
await _dbContext.UpdateAsync(todayTripInfo);
|
||||
AssignType = AssignTypeEnumType.User,
|
||||
AssignValue = gainfo.No,
|
||||
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
||||
UniqueValue = tripGroup.Code,
|
||||
TenantId = factory,
|
||||
Type = tripGroup.Type,
|
||||
IsDeleted = false
|
||||
});
|
||||
await _dbContext.UpdateAsync(todayTripInfo);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
if (yesterdayTripInfo != null)
|
||||
{
|
||||
_logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
||||
yesterdayTripInfo.Status = BusinessTripStatusEnumType.Deleted;
|
||||
await _dbContext.Updateable<DevicePermissionEntity>()
|
||||
.SetColumns(x => x.IsDeleted == true)
|
||||
.Where(x => x.AssignType == AssignTypeEnumType.User)
|
||||
.Where(x => x.AssignValue == gainfo.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.UpdateAsync(yesterdayTripInfo);
|
||||
}
|
||||
|
||||
var beforeResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(new List<string> { gainfo.Code });
|
||||
await _devicePermissionApi.RePermissionUsersInfoAsync(beforeResult.Data);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user