Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/SyncOriginCardInfoJob.cs
2024-08-07 20:52:40 +08:00

80 lines
3.0 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Request;
using Dlhis.ScheduledTask.Common.Services;
using SqlSugar;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 上传考勤记录
/// </summary>
[RecurringJob(nameof(SyncOriginCardInfoJob), "0 0/5 * * * ?", Name = "上传考勤记录")]
public class SyncOriginCardInfoJob : BaseJob
{
private readonly IJinkoJIPService _jinkoJIPService;
public SyncOriginCardInfoJob(IJinkoJIPService jinkoJIPService, IServiceProvider serviceProvider) : base(serviceProvider)
{
_jinkoJIPService = jinkoJIPService;
}
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//考勤设备(不需要Enable参数)
var devices = await DbContext.Queryable<DeviceEntity>()
.Where(x => x.Type == DeviceTypeEnumType.Attendance || x.Type == DeviceTypeEnumType.Door && x.AsAttendance)
.ToListAsync();
//获取上传日志(有效的)
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
var requests = await DbContext.Queryable<DeviceUploadLogEntity>()
.LeftJoin<DeviceEntity>((x, device) => x.DeviceUniqueNo == device.UniqueNo)
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
.Where(x => !string.IsNullOrEmpty(x.UserUniqueNo))
.Where(x => x.Sync == false)
//.Where(x => x.Status) todo
.Where(x => SqlFunc.Subqueryable<OutsourceEntity>().Where(p => p.Code == x.UserUniqueNo).NotAny())
.Select((x, device) => new UploadAttendanceRequest
{
Id = x.Id,
RaiseDateTime = x.RaiseDateTime,
ZNOTE = device.DeployPlace,
ZWORKCODE = x.UserUniqueNo
})
.ToPageListAsync(1, 500);
Logger.LogInformation("获取待上传考勤记录数:{count}", requests.Count);
if (requests.Any())
{
foreach (var items in requests.Chunk(500))
{
try
{
var ids = items.Select(x => x.Id).ToList();
var r = await _jinkoJIPService.UploadAttendancesAsync(items.ToList());
if (r.OLX == "0")
{
await DbContext.Updateable<DeviceUploadLogEntity>()
.SetColumns(x => x.Sync == true)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => ids.Contains(x.Id))
.ExecuteCommandAsync();
}
else
{
Logger.LogError("考勤上传异常:{message}", r.OMS);
}
}
catch (Exception ex)
{
Logger.LogError(ex, "考勤上传异常:{message}", ex.Message);
}
}
}
}
}