From f9a7152c2bb7a33e4b7646bf26e3d508729ce10c Mon Sep 17 00:00:00 2001 From: liguoyang Date: Sat, 9 Mar 2024 13:17:40 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BC=98=E5=8C=96=E8=80=83=E5=8B=A4?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Device/Entities/DeviceUploadLogEntity.cs | 6 ++ Src/Dlhis.ScheduledTask/Packs/JobPack.cs | 4 +- .../SyncSapInfo/Job/SyncOriginCardInfoJob.cs | 78 +++++++++++++++++-- .../SyncSapInfo/UploadAttendanceRequest.cs | 14 +++- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/Src/Dlhis.Entity/Device/Entities/DeviceUploadLogEntity.cs b/Src/Dlhis.Entity/Device/Entities/DeviceUploadLogEntity.cs index 39904219..2b9c7aef 100644 --- a/Src/Dlhis.Entity/Device/Entities/DeviceUploadLogEntity.cs +++ b/Src/Dlhis.Entity/Device/Entities/DeviceUploadLogEntity.cs @@ -36,6 +36,12 @@ public class DeviceUploadLogEntity : SugarBaseEntity, ITenantId [SugarColumn(ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))] public DeviceUploadLogTypeEnumType Type { get; set; } + /// + /// 同步数据 + /// + [SugarColumn(ColumnDataType = "bit")] + public bool Sync { get; set; } + [SugarColumn(IndexGroupNameList = new string[] { "idx_DeviceUploadLog_TenantId" })] public string TenantId { get; set; } diff --git a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs index 59f37b22..e74aa037 100644 --- a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs +++ b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs @@ -48,10 +48,10 @@ public class JobPack : BaseAppPack TimeZone = TimeZoneInfo.Local }); - RecurringJob.AddOrUpdate("SyncOriginCardInfo", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions + //上传考勤记录 + RecurringJob.AddOrUpdate("SyncOriginCardInfo", s => s.RunAsync(), "0/20 * * * * ?", new RecurringJobOptions { TimeZone = TimeZoneInfo.Local }); - } } diff --git a/Src/Dlhis.ScheduledTask/SyncSapInfo/Job/SyncOriginCardInfoJob.cs b/Src/Dlhis.ScheduledTask/SyncSapInfo/Job/SyncOriginCardInfoJob.cs index b7161013..4587d2bc 100644 --- a/Src/Dlhis.ScheduledTask/SyncSapInfo/Job/SyncOriginCardInfoJob.cs +++ b/Src/Dlhis.ScheduledTask/SyncSapInfo/Job/SyncOriginCardInfoJob.cs @@ -1,26 +1,88 @@ -using Dlhis.ScheduledTask.SyncSapInfo.Services; +using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Device.Entities; +using Dlhis.Entity.Device.Enums; +using Dlhis.ScheduledTask.SyncSapInfo.Services; +using Kurisu.AspNetCore.Utils.Extensions; +using Kurisu.Core.DataAccess.Entity; +using Kurisu.SqlSugar.Services; +using SqlSugar; namespace Dlhis.ScheduledTask.SyncSapInfo.Job; public class SyncOriginCardInfoJob { private readonly IJinkoJIPService _jinkoJIPService; + private readonly IDbContext _dbContext; + private readonly ISqlSugarOptionsService _sqlSugarOptionsService; + private readonly ILogger _logger; - public SyncOriginCardInfoJob(IJinkoJIPService jinkoJIPService) + public SyncOriginCardInfoJob(IJinkoJIPService jinkoJIPService, + IDbContext dbContext, + ISqlSugarOptionsService sqlSugarOptionsService, + ILogger logger + ) { _jinkoJIPService = jinkoJIPService; + _dbContext = dbContext; + _sqlSugarOptionsService = sqlSugarOptionsService; + _logger = logger; } public async Task RunAsync() { - await _jinkoJIPService.UploadAttendancesAsync(new List() + _dbContext.Client.QueryFilter.ClearAndBackup(); + _sqlSugarOptionsService.IgnoreTenant = true; + try { - new UploadAttendanceRequest() + //考勤设备(不需要Enable参数) + var devices = await _dbContext.Queryable() + .Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)) + .ToListAsync(); + + //获取上传日志(有效的) + var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList(); + var logs = await _dbContext.Queryable() + .Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo)) + .Where(x => !string.IsNullOrEmpty(x.UserUniqueNo)) + .Where(x => x.Sync == false) + .ToListAsync(); + + //获取人员信息 + var userCodes = logs.Select(x => x.UserUniqueNo).ToList(); + var gainfos = await _dbContext.Queryable().Where(x => userCodes.Contains(x.Code)).Select(x => new { x.Code, x.CardNo }).ToListAsync(); + + var requests = logs.Select(x => new UploadAttendanceRequest { - RaiseTime = DateTime.Now, - ZWORKCODE = "000001", - ZNOTE = "正门1" + Id = x.Id, + RaiseDateTime = x.RaiseDateTime, + ZNOTE = devices.First(item => item.UniqueNo == x.DeviceUniqueNo).DeployPlace, + ZWORKCODE = gainfos.FirstOrDefault(item => item.Code == x.UserUniqueNo)?.CardNo?.TrimEnd('\0'), + + }).Where(x => x.ZWORKCODE.IsPresent()).ToList(); + + var ids = requests.Select(x => x.Id).ToList(); + var r = await _jinkoJIPService.UploadAttendancesAsync(requests); + if (r.OLX == "0") + { + await _dbContext.Updateable() + .SetColumns(x => x.Sync == true) + .SetColumns(x => x.ModifiedTime == DateTime.Now) + .Where(x => ids.Contains(x.Id)) + .ExecuteCommandAsync(); } - }); + else + { + throw new Exception(r.OMS); + } + } + catch (Exception ex) + { + _logger.LogError("上传考勤记录失败:", ex.Message); + } + finally + { + _sqlSugarOptionsService.IgnoreTenant = false; + _dbContext.Client.QueryFilter.Restore(); + } } } diff --git a/Src/Dlhis.ScheduledTask/SyncSapInfo/UploadAttendanceRequest.cs b/Src/Dlhis.ScheduledTask/SyncSapInfo/UploadAttendanceRequest.cs index ac552237..05f9833c 100644 --- a/Src/Dlhis.ScheduledTask/SyncSapInfo/UploadAttendanceRequest.cs +++ b/Src/Dlhis.ScheduledTask/SyncSapInfo/UploadAttendanceRequest.cs @@ -1,23 +1,29 @@ -namespace Dlhis.ScheduledTask.SyncSapInfo +using Newtonsoft.Json; + +namespace Dlhis.ScheduledTask.SyncSapInfo { public class UploadAttendanceRequest { + [JsonIgnore] + public long Id { get; set; } + /// /// item.CardNo.TrimEnd('\0') /// public string ZWORKCODE { get; set; } - public DateTime RaiseTime { get; set; } + [JsonIgnore] + public DateTime RaiseDateTime { get; set; } /// /// yyyy-MM-dd /// - public string ZDATE => RaiseTime.ToString("yyyy-MM-dd"); + public string ZDATE => RaiseDateTime.ToString("yyyy-MM-dd"); /// /// HH:mm:ss /// - public string ZTIME => RaiseTime.ToString("HH:mm:ss"); + public string ZTIME => RaiseDateTime.ToString("HH:mm:ss"); /// /// 设备位置