Files
jinko.yikatong/Src/Dlhis.ScheduledTask/SyncSapInfo/Job/SyncOriginCardInfoJob.cs
2024-03-09 13:53:12 +08:00

92 lines
3.4 KiB
C#

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,
IDbContext dbContext,
ISqlSugarOptionsService sqlSugarOptionsService,
ILogger<SyncOriginCardInfoJob> logger
)
{
_jinkoJIPService = jinkoJIPService;
_dbContext = dbContext;
_sqlSugarOptionsService = sqlSugarOptionsService;
_logger = logger;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
_sqlSugarOptionsService.IgnoreTenant = true;
try
{
//考勤设备(不需要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
{
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();
if (ids.Any())
{
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();
}
}
}