fix 出差人员任务生成
This commit is contained in:
@@ -1,17 +1,28 @@
|
||||
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) : base(serviceProvider)
|
||||
public SyncBusinessTripJob(IServiceProvider serviceProvider, IDevicePermissionApi devicePermissionApi) : base(serviceProvider)
|
||||
{
|
||||
_devicePermissionApi = devicePermissionApi;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -19,6 +30,7 @@ public class SyncBusinessTripJob : BaseJob
|
||||
{
|
||||
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();
|
||||
@@ -30,7 +42,7 @@ public class SyncBusinessTripJob : BaseJob
|
||||
try
|
||||
{
|
||||
var tripResponse = await jipService.GetTravelInfosByDepartmentsAsync(today, today, departmentNos.ToList());
|
||||
foreach (var tripInfoItem in tripResponse.Items)
|
||||
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
|
||||
@@ -40,6 +52,7 @@ public class SyncBusinessTripJob : BaseJob
|
||||
}
|
||||
|
||||
var entity = tripInfoItem.Adapt<BusinessTripEntity>();
|
||||
entity.Status = BusinessTripStatusEnumType.Wait;
|
||||
await DbContext.InsertAsync(entity);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +61,107 @@ public class SyncBusinessTripJob : BaseJob
|
||||
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,
|
||||
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.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//************************************************************************************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user