180 lines
7.6 KiB
C#
180 lines
7.6 KiB
C#
using Dlhis.Basic.Api;
|
|
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.ScheduledTask.Attributes;
|
|
using Dlhis.ScheduledTask.Common.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.ScheduledTask.Jobs;
|
|
|
|
[RecurringJob(nameof(SyncBusinessTripJob), "0 0 1 1/1 * ? ", Name = "SAP同步出差信息")]
|
|
public class SyncBusinessTripJob : BaseJob
|
|
{
|
|
private readonly IDevicePermissionApi _devicePermissionApi;
|
|
|
|
/// <inheritdoc />
|
|
public SyncBusinessTripJob(IServiceProvider serviceProvider, IDevicePermissionApi devicePermissionApi) : base(serviceProvider)
|
|
{
|
|
_devicePermissionApi = devicePermissionApi;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override async Task RunAsync(IServiceProvider serviceProvider)
|
|
{
|
|
var jipService = serviceProvider.GetService<IJinkoJIPService>();
|
|
var today = DateTime.Today;
|
|
var lastDay = today.AddDays(-1);
|
|
|
|
var departments = await DbContext.Queryable<DepartmentEntity>().ToListAsync();
|
|
var tops = departments.Where(x => x.PNo == "00001000").ToList();
|
|
var validDepartments = tops.ToList();
|
|
tops.ForEach(x => RecursionGetNext(x, departments, validDepartments));
|
|
|
|
foreach (var departmentNos in validDepartments.Select(x => x.No).Chunk(20))
|
|
{
|
|
try
|
|
{
|
|
var tripResponse = await jipService.GetTravelInfosByDepartmentsAsync(today, today, departmentNos.ToList());
|
|
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).AnyAsync())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
|
entity.Status = BusinessTripStatusEnumType.Wait;
|
|
await DbContext.InsertAsync(entity);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.LogError(e, "获取部门出差数据执行失败:{message}", e.Message);
|
|
}
|
|
}
|
|
|
|
|
|
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)
|
|
.ToListAsync();
|
|
|
|
var factoryIds = tripInfos.Select(x => x.Destination).Distinct().ToList();
|
|
var tripGroups = await DbContext.Queryable<DeviceGroupEntity>()
|
|
.Where(x => x.Type == DeviceTypeEnumType.Door)
|
|
.Where(x => factoryIds.Contains(x.TenantId)).Where(x => x.IsBusinessTrip)
|
|
.ToListAsync();
|
|
|
|
var userCodes = tripInfos.Select(x => x.Code).Distinct().ToList();
|
|
var gainfos = await DbContext.Queryable<GainfoEntity>().Where(x => userCodes.Contains(x.Code)).ToListAsync();
|
|
|
|
foreach (var code in userCodes)
|
|
{
|
|
var gainfo = gainfos.FirstOrDefault(x => x.Code == code);
|
|
if (gainfo == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (var factoryTripInfos in tripInfos.Where(x => x.Code == code).GroupBy(x => x.Destination))
|
|
{
|
|
var tripGroup = tripGroups.FirstOrDefault(x => x.TenantId == factoryTripInfos.Key);
|
|
if (tripGroup == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var todayTripInfo = factoryTripInfos.FirstOrDefault(x => x.BeginDate == today && x.EndDate == today);
|
|
var lastDayTripInfo = factoryTripInfos.FirstOrDefault(x => x.BeginDate == lastDay && x.EndDate == lastDay);
|
|
|
|
bool isGenerateOrDeleted = false;
|
|
|
|
if (lastDayTripInfo != null && todayTripInfo != null)
|
|
{
|
|
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
|
await DbContext.UpdateAsync(todayTripInfo);
|
|
continue;
|
|
}
|
|
|
|
|
|
if (lastDayTripInfo == null && todayTripInfo != null)
|
|
{
|
|
isGenerateOrDeleted = true;
|
|
todayTripInfo.Status = BusinessTripStatusEnumType.Generated;
|
|
}
|
|
|
|
if (todayTripInfo == null && lastDayTripInfo != null)
|
|
{
|
|
isGenerateOrDeleted = false;
|
|
lastDayTripInfo.Status = BusinessTripStatusEnumType.Deleted;
|
|
}
|
|
|
|
try
|
|
{
|
|
var beforeResult = await _devicePermissionApi.GetPermissionUsersInfoAsync(new List<string> { code });
|
|
|
|
if (isGenerateOrDeleted)
|
|
{
|
|
await DbContext.InsertAsync(new DevicePermissionEntity
|
|
{
|
|
AssignType = AssignTypeEnumType.User,
|
|
AssignValue = gainfo.No,
|
|
UniqueType = DeviceAssignmentTypeEnumType.Group,
|
|
UniqueValue = tripGroup.Code,
|
|
TenantId = factoryTripInfos.Key,
|
|
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 == factoryTripInfos.Key)
|
|
.ExecuteCommandAsync();
|
|
}
|
|
|
|
await _devicePermissionApi.RePermissionUsersInfoAsync(beforeResult.Data);
|
|
|
|
if (lastDayTripInfo != null)
|
|
await DbContext.UpdateAsync(lastDayTripInfo);
|
|
|
|
if (todayTripInfo != null)
|
|
await DbContext.UpdateAsync(todayTripInfo);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.LogError(e, "出差自动下发任务失败:{message}", e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//************************************************************************************************************************************************
|
|
|
|
private static void RecursionGetNext(DepartmentEntity current, List<DepartmentEntity> data, List<DepartmentEntity> result)
|
|
{
|
|
var next = data.Where(x => x.PNo == current.No).ToList();
|
|
foreach (var item in next)
|
|
{
|
|
result.Add(item);
|
|
RecursionGetNext(item, data, result);
|
|
}
|
|
}
|
|
} |