fix 优化考勤记录上传

This commit is contained in:
2024-03-09 13:17:40 +08:00
parent 553332e204
commit f9a7152c2b
4 changed files with 88 additions and 14 deletions

View File

@@ -36,6 +36,12 @@ public class DeviceUploadLogEntity : SugarBaseEntity, ITenantId
[SugarColumn(ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))]
public DeviceUploadLogTypeEnumType Type { get; set; }
/// <summary>
/// 同步数据
/// </summary>
[SugarColumn(ColumnDataType = "bit")]
public bool Sync { get; set; }
[SugarColumn(IndexGroupNameList = new string[] { "idx_DeviceUploadLog_TenantId" })]
public string TenantId { get; set; }

View File

@@ -48,10 +48,10 @@ public class JobPack : BaseAppPack
TimeZone = TimeZoneInfo.Local
});
RecurringJob.AddOrUpdate<SyncOriginCardInfoJob>("SyncOriginCardInfo", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions
//上传考勤记录
RecurringJob.AddOrUpdate<SyncOriginCardInfoJob>("SyncOriginCardInfo", s => s.RunAsync(), "0/20 * * * * ?", new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
}
}

View File

@@ -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<SyncOriginCardInfoJob> _logger;
public SyncOriginCardInfoJob(IJinkoJIPService jinkoJIPService)
public SyncOriginCardInfoJob(IJinkoJIPService jinkoJIPService,
IDbContext dbContext,
ISqlSugarOptionsService sqlSugarOptionsService,
ILogger<SyncOriginCardInfoJob> logger
)
{
_jinkoJIPService = jinkoJIPService;
_dbContext = dbContext;
_sqlSugarOptionsService = sqlSugarOptionsService;
_logger = logger;
}
public async Task RunAsync()
{
await _jinkoJIPService.UploadAttendancesAsync(new List<UploadAttendanceRequest>()
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
_sqlSugarOptionsService.IgnoreTenant = true;
try
{
new UploadAttendanceRequest()
//考勤设备(不需要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 logs = await _dbContext.Queryable<DeviceUploadLogEntity>()
.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<GainfoEntity>().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<DeviceUploadLogEntity>()
.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();
}
}
}

View File

@@ -1,23 +1,29 @@
namespace Dlhis.ScheduledTask.SyncSapInfo
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.SyncSapInfo
{
public class UploadAttendanceRequest
{
[JsonIgnore]
public long Id { get; set; }
/// <summary>
/// item.CardNo.TrimEnd('\0')
/// </summary>
public string ZWORKCODE { get; set; }
public DateTime RaiseTime { get; set; }
[JsonIgnore]
public DateTime RaiseDateTime { get; set; }
/// <summary>
/// yyyy-MM-dd
/// </summary>
public string ZDATE => RaiseTime.ToString("yyyy-MM-dd");
public string ZDATE => RaiseDateTime.ToString("yyyy-MM-dd");
/// <summary>
/// HH:mm:ss
/// </summary>
public string ZTIME => RaiseTime.ToString("HH:mm:ss");
public string ZTIME => RaiseDateTime.ToString("HH:mm:ss");
/// <summary>
/// 设备位置