147 lines
6.6 KiB
C#
147 lines
6.6 KiB
C#
using Dlhis.Basic.Device.Services;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Dlhis.Entity.Sys.Entities;
|
|
using Dlhis.Entity.Sys.Enums;
|
|
using Dlhis.Entity.Visitor.Entity;
|
|
using Dlhis.Entity.Visitor.Enums;
|
|
using Dlhis.Jip.Api;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class SapBusinessTripService : ISapBusinessTripService
|
|
{
|
|
private readonly IJinkoJipApi _jinkoJipApi;
|
|
private readonly IDbContext _dbContext;
|
|
private readonly IDevicePermissionService _devicePermissionService;
|
|
private readonly ILogger<SapBusinessTripService> _logger;
|
|
|
|
public SapBusinessTripService(IJinkoJipApi jinkoJipApi,
|
|
IDbContext dbContext,
|
|
IDevicePermissionService devicePermissionService,
|
|
ILogger<SapBusinessTripService> logger)
|
|
{
|
|
_jinkoJipApi = jinkoJipApi;
|
|
_dbContext = dbContext;
|
|
_devicePermissionService = devicePermissionService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task HandlerBusinessTripsByFactoryAsync(string factory)
|
|
{
|
|
var today = DateTime.Today;
|
|
var yesterday = today.AddDays(-1);
|
|
|
|
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<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);
|
|
}
|
|
|
|
//获取今天和昨天的出差数据
|
|
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.Destination == factory)
|
|
.ToListAsync();
|
|
|
|
//获取分组信息
|
|
var tripGroup = await _dbContext.Queryable<DeviceGroupEntity>()
|
|
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
|
.Where(x => x.TenantId == factory)
|
|
.Where(x => x.IsBusinessTrip)
|
|
.SingleAsync();
|
|
|
|
var userCodes = tripInfos.Select(x => x.Code).Distinct().ToList();
|
|
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userCodes.Contains(x.Code)).Select(x => new { x.No, x.Code }).ToListAsync();
|
|
|
|
foreach (var gainfo in gainfos)
|
|
{
|
|
try
|
|
{
|
|
var myTripInfos = tripInfos.Where(x => x.Code == gainfo.Code).ToList();
|
|
|
|
var yesterdayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == yesterday && x.EndDate == yesterday); //昨天出差数据
|
|
var todayTripInfo = myTripInfos.FirstOrDefault(x => x.BeginDate == today && x.EndDate == today); //今天出差数据
|
|
|
|
//都为空不处理
|
|
if (yesterdayTripInfo == null && todayTripInfo == null) continue;
|
|
|
|
//都不为空则更新数据状态
|
|
if (yesterdayTripInfo != null && todayTripInfo != null)
|
|
{
|
|
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
|
await _dbContext.UpdateAsync(todayTripInfo);
|
|
continue;
|
|
}
|
|
|
|
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
|
|
{
|
|
if (yesterdayTripInfo != null)
|
|
{
|
|
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);
|
|
}
|
|
|
|
if (yesterdayTripInfo == null)
|
|
{
|
|
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
|
await _dbContext.InsertAsync(new DevicePermissionEntity
|
|
{
|
|
AssignType = AssignTypeEnumType.User,
|
|
AssignValue = gainfo.No,
|
|
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
|
UniqueValue = tripGroup.Code,
|
|
TenantId = factory,
|
|
Type = tripGroup.Type,
|
|
IsDeleted = false
|
|
});
|
|
await _dbContext.UpdateAsync(todayTripInfo);
|
|
}
|
|
}, new List<string> { gainfo.No });
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, "处理{factory}出差数据失败:{message}", factory, e.Message);
|
|
}
|
|
}
|
|
} |