fix 调试大华设备对接

This commit is contained in:
2024-01-24 20:46:55 +08:00
parent a36ff46722
commit 51c80a242e
19 changed files with 368 additions and 74 deletions

View File

@@ -145,7 +145,7 @@ public class DevicesController : ControllerBase
/// <param name="request"></param>
/// <returns></returns>
[HttpGet]
public async Task<Pagination<DeviceResponse>> GetDeviceByNoAsync([FromQuery] DevicePageRequest request)
public async Task<DeviceResponse> GetDeviceByNoAsync([FromQuery] DeviceByNoRequest request)
{
return await _deviceService.GetDeviceByNoAsync(request);
}

View File

@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Dlhis.Basic.Device.Request;
public class DeviceByNoRequest
{
[Required]
public string UniqueNo { get; set; }
}

View File

@@ -1,6 +1,7 @@
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.Utils.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Dlhis.Basic.Device.Response;
@@ -72,7 +73,26 @@ public class DeviceResponse
/// <summary>
/// 连接信息
/// </summary>
public string ConnectionInfo { get; set; }
public object ConnectionInfo
{
get
{
try
{
return JObject.Parse(ConnectionInfoStr);
}
catch (Exception)
{
return JObject.Parse("{}");
}
}
}
/// <summary>
/// 连接信息
/// </summary>
[JsonIgnore]
public string ConnectionInfoStr { get; set; }
/// <summary>
/// 是否启用

View File

@@ -12,7 +12,7 @@ public interface IDeviceService : IScopeDependency
Task<bool> CheckExistsAsync(string deviceUniqueNo);
Task<Pagination<DeviceResponse>> GetDeviceByNoAsync(DevicePageRequest request);
Task<DeviceResponse> GetDeviceByNoAsync(DeviceByNoRequest request);
Task<List<DeviceResponse>> GetAttendanceDevicesAsync();
}

View File

@@ -152,18 +152,19 @@ public class DeviceService : IDeviceService
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<Pagination<DeviceResponse>> GetDeviceByNoAsync(DevicePageRequest request)
public async Task<DeviceResponse> GetDeviceByNoAsync(DeviceByNoRequest request)
{
var page = await _dbContext.Queryable<DeviceEntity>()
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == request.UniqueNo)
.Select(device => new DeviceResponse
{
FuncsStr = device.Funcs.ToString()
.Select(device => new DeviceResponse
{
FuncsStr = device.Funcs.ToString(),
ConnectionInfoStr = device.ConnectionInfo
}, true)
.ToPageListAsync(request);
}, true)
.SingleAsync();
return page;
return device;
}
public async Task<List<DeviceResponse>> GetAttendanceDevicesAsync()

View File

@@ -18,14 +18,14 @@ public class SqlSugarPack : BaseAppPack
{
#if DEBUG
//using var scope = app.ApplicationServices.CreateScope();
//var db = scope.ServiceProvider.GetService<ISqlSugarClient>();
//var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
// .Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
// .ToArray();
using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetService<ISqlSugarClient>();
var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
.Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
.ToArray();
//db.DbMaintenance.CreateDatabase();
//db.CodeFirst.InitTables(tables);
db.DbMaintenance.CreateDatabase();
db.CodeFirst.InitTables(tables);
//using var scopeFace = app.ApplicationServices.CreateScope();
//var dbFace = scopeFace.ServiceProvider.GetService<ISqlSugarClient>();

View File

@@ -1,5 +1,7 @@
using Kurisu.Core.DataAccess.Entity;
using Dlhis.Entity.Device.Enums;
using Kurisu.Core.DataAccess.Entity;
using SqlSugar;
using SqlSugar.DbConvert;
namespace Dlhis.Entity.Device.Entities;
@@ -12,6 +14,10 @@ public class DeviceUploadLogEntity : ITenantId
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public long Id { get; set; }
[SugarColumn(IsNullable = true)]
public string SerialNo { get; set; }
/// <summary>
/// 设备号
/// </summary>
@@ -20,17 +26,20 @@ public class DeviceUploadLogEntity : ITenantId
/// <summary>
/// 人员唯一号
/// </summary>
[SugarColumn(IsNullable = true)]
public string UserUniqueNo { get; set; }
/// <summary>
/// 上传IP
/// 产生时间
/// </summary>
public string Ip { get; set; }
public DateTime RaiseDateTime { get; set; }
/// <summary>
/// 上传时间
/// 日志类型
/// </summary>
public DateTime UploadTime { get; set; }
[SugarColumn(ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))]
public DeviceUploadLogTypeEnumType Type { get; set; }
[SugarColumn(IndexGroupNameList = new string[] { "idx_DeviceUploadLog_TenantId" })]

View File

@@ -50,4 +50,17 @@ public enum DeviceTaskEnumType
/// </summary>
[Description("删除计划")]
ScheduleDelete,
/// <summary>
/// 同步时间
/// </summary>
[Description("同步时间")]
SyncDateTime = 400,
/// <summary>
/// 同步时间
/// </summary>
[Description("上传记录")]
UploadRecord = 500,
}

View File

@@ -0,0 +1,29 @@
using System.ComponentModel;
namespace Dlhis.Entity.Device.Enums;
/// <summary>
/// 设别上传记录类型
/// </summary>
[Description("设别上传记录类型")]
public enum DeviceUploadLogTypeEnumType
{
/// <summary>
/// 门记录
/// </summary>
[Description("门记录")]
Door = 1,
/// <summary>
/// 考勤记录
/// </summary>
[Description("考勤记录")]
Attendance = 2,
/// <summary>
/// 触发获取记录,对业务无作用
/// </summary>
[Description("触发获取记录")]
Raise = 100
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using Dlhis.Entity.Device.Enums;
namespace Dlhis.ScheduledTask.DeviceUploadLog.Request;
@@ -13,11 +14,22 @@ public class DeviceUploadLogUploadRequest
/// <summary>
/// 用户学工号
/// </summary>
[Required(ErrorMessage = "人员唯一号不能为空")]
public string UserUniqueNo { get; set; }
/// <summary>
/// 上传时间
/// 产生时间
/// </summary>
public DateTime UploadTime { get; set; }
public DateTime RaiseDateTime { get; set; }
/// <summary>
/// 记录序号
/// </summary>
public string SerialNo { get; set; }
/// <summary>
/// 日志类型
/// </summary>
[EnumDataType(typeof(DeviceUploadLogTypeEnumType))]
public DeviceUploadLogTypeEnumType Type { get; set; } = DeviceUploadLogTypeEnumType.Door;
}

View File

@@ -25,13 +25,8 @@ public class DeviceUploadLogResponse
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 上传IP
/// </summary>
public string Ip { get; set; }
/// <summary>
/// 上传时间
/// </summary>
public DateTime UploadTime { get; set; }
public DateTime RaiseDateTime { get; set; }
}

View File

@@ -23,14 +23,13 @@ public class DeviceUploadLogService : IDeviceUploadLogService
{
var page = await _dbContext.Queryable<DeviceUploadLogEntity>()
.LeftJoin<GainfoEntity>((deviceUp, ga) => deviceUp.UserUniqueNo == ga.No)
.WhereIF(request.SearchDateTimePeriod?.Start.HasValue == true, deviceUp => deviceUp.UploadTime >= request.SearchDateTimePeriod.Start)
.WhereIF(request.SearchDateTimePeriod?.End.HasValue == true, deviceUp => deviceUp.UploadTime <= request.SearchDateTimePeriod.End)
.WhereIF(request.SearchDateTimePeriod?.Start.HasValue == true, deviceUp => deviceUp.RaiseDateTime >= request.SearchDateTimePeriod.Start)
.WhereIF(request.SearchDateTimePeriod?.End.HasValue == true, deviceUp => deviceUp.RaiseDateTime <= request.SearchDateTimePeriod.End)
.Select((deviceUp, ga) => new DeviceUploadLogResponse
{
DeviceUniqueNo = deviceUp.DeviceUniqueNo,
Id = deviceUp.Id,
Ip = deviceUp.Ip,
UploadTime = deviceUp.UploadTime,
RaiseDateTime = deviceUp.RaiseDateTime,
UserNo = ga.No,
UserCode = ga.Code,
UserName = ga.Name,
@@ -49,7 +48,7 @@ public class DeviceUploadLogService : IDeviceUploadLogService
var entity = new DeviceUploadLogEntity
{
DeviceUniqueNo = request.DeviceUniqueNo,
UploadTime = request.UploadTime,
// UploadTime = request.UploadTime,
UserUniqueNo = request.UserUniqueNo,
TenantId = device.TenantId,
};
@@ -63,23 +62,26 @@ public class DeviceUploadLogService : IDeviceUploadLogService
var nos = requests.Select(x => x.DeviceUniqueNo).ToList();
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => nos.Contains(x.UniqueNo)).ToListAsync();
var records = requests.GroupBy(x => x.DeviceUniqueNo).Select(x =>
{
var device = devices.FirstOrDefault(item => item.UniqueNo == x.Key);
if (device != null)
{
return x.AsEnumerable().Select(item => new DeviceUploadLogEntity
{
DeviceUniqueNo = x.Key,
UserUniqueNo = item.UserUniqueNo,
UploadTime = item.UploadTime,
TenantId = device.TenantId,
var records = requests.GroupBy(x => x.DeviceUniqueNo).SelectMany(x =>
{
var device = devices.FirstOrDefault(item => item.UniqueNo == x.Key);
if (device != null)
{
return x.AsEnumerable().Select(item => new DeviceUploadLogEntity
{
DeviceUniqueNo = x.Key,
UserUniqueNo = item.UserUniqueNo,
RaiseDateTime = item.RaiseDateTime,
SerialNo = item.SerialNo,
Type = item.Type,
TenantId = device.TenantId,
}).ToList();
}
}).ToList();
}
return null;
}).Where(x => x != null).ToList();
return null;
}).Where(x => x != null).ToList();
await _dbContext.InsertAsync(records);
}

View File

@@ -1,4 +1,5 @@
using Dlhis.ScheduledTask.SyncSapInfo.Job;
using Dlhis.ScheduledTask.TaskAssignment.Job;
using Hangfire;
using Kurisu.AspNetCore.Startup;
@@ -16,5 +17,16 @@ public class JobPack : BaseAppPack
{
TimeZone = TimeZoneInfo.Local
});
RecurringJob.AddOrUpdate<CreateSyncServerDateTimeJob>("SyncServerDateTime", s => s.RunAsync(), "0 0 0/1 * * ?", new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
RecurringJob.AddOrUpdate<CreateGetAttendanceJob>("CreateGetAttendance", s => s.RunAsync(), "0 0/5 * * * ?", new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
}
}

View File

@@ -0,0 +1,58 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.SqlSugar.Services;
namespace Dlhis.ScheduledTask.TaskAssignment.Job
{
public class CreateGetAttendanceJob
{
private readonly IDbContext _dbContext;
private readonly ILogger<CreateSyncServerDateTimeJob> _logger;
public CreateGetAttendanceJob(IDbContext dbContext, ILogger<CreateSyncServerDateTimeJob> logger)
{
_logger = logger;
_dbContext = dbContext;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup();
try
{
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
devices = devices.Where(x => x.Funcs.Contains(DeviceFuncEnumType.Attendance)).ToList();
var tasks = devices.Select(x => new DeviceTaskEntity
{
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
TenantId = x.TenantId,
Version = 0,
UserNo = string.Empty,
UserCode = string.Empty,
Type = DeviceTaskEnumType.UploadRecord,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
}).ToList();
await _dbContext.InsertAsync(tasks);
}
catch (Exception ex)
{
_logger.LogError(ex, $"生成{DeviceTaskEnumType.UploadRecord.GetDescription()}任务失败:" + ex.Message);
}
finally
{
_dbContext.Client.QueryFilter.Restore();
}
}
}
}

View File

@@ -0,0 +1,57 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.SqlSugar.Services;
namespace Dlhis.ScheduledTask.TaskAssignment.Job
{
public class CreateSyncServerDateTimeJob
{
private readonly IDbContext _dbContext;
private readonly ILogger<CreateSyncServerDateTimeJob> _logger;
public CreateSyncServerDateTimeJob(IDbContext dbContext, ILogger<CreateSyncServerDateTimeJob> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup();
try
{
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Enable).ToListAsync();
var tasks = devices.Select(x => new DeviceTaskEntity
{
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
TenantId = x.TenantId,
Version = 0,
UserNo = string.Empty,
UserCode = string.Empty,
Type = DeviceTaskEnumType.SyncDateTime,
CreatedBy = 1,
CreateTime = DateTime.Now,
ModifiedBy = 1,
ModifiedTime = DateTime.Now,
}).ToList();
await _dbContext.InsertAsync(tasks);
}
catch (Exception ex)
{
_logger.LogError(ex, "生成同步时间任务失败:" + ex.Message);
}
finally
{
_dbContext.Client.QueryFilter.Restore();
}
}
}
}

View File

@@ -9,13 +9,19 @@ public class TimePeriodToDeviceResponse
/// </summary>
public int Id { get; set; }
/// <summary>
/// 星期
/// </summary>
public List<DayOfWeek> Weeks { get; set; }
/// <summary>
/// 时段区间
/// 时段名称
/// </summary>
public TimePeriod TimePeriod { get; set; }
public string Name { get; set; }
/// <summary>
/// 使能
/// </summary>
public bool Enable { get; set; }
/// <summary>
/// 星期时段
/// </summary>
public List<TimePeriodWeekDaySection> Sections { get; set; }
}

View File

@@ -0,0 +1,16 @@
using Kurisu.Core.CustomClass;
namespace Dlhis.ScheduledTask.TaskAssignment.Response;
public class TimePeriodWeekDaySection
{
/// <summary>
/// 星期
/// </summary>
public DayOfWeek DayOfWeek { get; set; }
/// <summary>
/// 时段区间 最多4个
/// </summary>
public List<TimePeriod> TimePeriods { get; set; }
}

View File

@@ -0,0 +1,8 @@
using Kurisu.Core.CustomClass;
namespace Dlhis.ScheduledTask.TaskAssignment.Response;
public class UploadRecordToDeviceResponse
{
public DateTimePeriod DateTimePeriod { get; set; }
}

View File

@@ -106,6 +106,30 @@ public class AssignmentService : IAssignmentService
return new List<DeviceTaskToDeviceByDeviceResponse>();
}
Dictionary<string, DateTime?> deviceUploadInfos = null;
if (data.Any(x => x.Type == DeviceTaskEnumType.UploadRecord))
{
var deviceNos = data.Select(x => x.DeviceUniqueNo).Distinct();
deviceUploadInfos = new Dictionary<string, DateTime?>(deviceNos.Count());
var logInfos = await _dbContext.Queryable<DeviceUploadLogEntity>().Select(it => new
{
Index2 = SqlFunc.RowNumber(SqlFunc.Desc(it.Id), it.DeviceUniqueNo),
it.DeviceUniqueNo,
it.RaiseDateTime
})
.MergeTable()
.Where(it => it.Index2 == 1)
.ToListAsync();
foreach (var item in deviceNos)
{
var logInfo = logInfos.FirstOrDefault(x => x.DeviceUniqueNo == item);
deviceUploadInfos.Add(item, logInfo?.RaiseDateTime);
}
}
//申请新的请求域
return Scoped.Request.Value.Create((sp) =>
{
var faceUrlOptions = sp.GetService<IOptions<FaceUrlOptions>>().Value;
@@ -137,9 +161,9 @@ public class AssignmentService : IAssignmentService
Tasks = x.AsEnumerable().Select(item =>
{
var key = $"face:{item.UserCode}";
var token = item.Type == DeviceTaskEnumType.Add
? timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(faceUrlOptions.Limit))
: string.Empty;
var token = item.Type == DeviceTaskEnumType.Delete
? string.Empty
: timeLimitedDataProtector.Protect(key, TimeSpan.FromSeconds(faceUrlOptions.Limit));
var dto = new TaskCombineDto
{
@@ -162,11 +186,11 @@ public class AssignmentService : IAssignmentService
UserName = item.UserName,
DepartmentNo = item.DepartmentNo,
DepartmentName = item.DepartmentName,
CardNo = item.Type == DeviceTaskEnumType.Add ? item.CardNo : string.Empty,
Face = item.Type == DeviceTaskEnumType.Add ? faceUrlOptions.Url + token : string.Empty,//下发才附带照片信息
CardNo = item.Type == DeviceTaskEnumType.Delete ? string.Empty : item.CardNo,
Face = item.Type == DeviceTaskEnumType.Delete ? string.Empty : faceUrlOptions.Url + token,//下发才附带照片信息
ValidDatePeriod = new DatePeriod
{
Start = DateOnly.FromDateTime(DateTime.Now),
Start = DateOnly.FromDateTime(DateTime.Now.AddDays(-1)),
End = DateOnly.FromDateTime(DateTime.Now.AddYears(20)),
},
TimePeriods = new List<int> { 1 },
@@ -181,15 +205,23 @@ public class AssignmentService : IAssignmentService
dto.Info = new TimePeriodToDeviceResponse
{
Id = 1,
TimePeriod = new TimePeriod
Name = "小白领",
Enable = true,
Sections = new List<TimePeriodWeekDaySection>()
{
Start = TimeOnly.Parse("08:00:00"),
End = TimeOnly.Parse("17:30:00"),
},
Weeks = new List<DayOfWeek>
{
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday
},
new TimePeriodWeekDaySection()
{
DayOfWeek= DayOfWeek.Monday,
TimePeriods = new List<TimePeriod>()
{
new TimePeriod
{
Start = TimeOnly.Parse("08:00:00"),
End = TimeOnly.Parse("17:30:00"),
},
}
}
}
};
}
break;
@@ -205,16 +237,30 @@ public class AssignmentService : IAssignmentService
{
new ScheduleDto
{
Name = "国庆节",
Name = "国庆节",
DateTimePeriod = new DateTimePeriod
{
Start = new DateTime(2024,10,1), End = new DateTime(2024,10,7)
Start = new DateTime(2024,10,1),
End = new DateTime(2024,10,7)
}
}
}
};
}
break;
case DeviceTaskEnumType.UploadRecord:
{
dto.Info = new UploadRecordToDeviceResponse
{
DateTimePeriod = new DateTimePeriod
{
Start = deviceUploadInfos[d] ?? DateTime.Now,
End = DateTime.Now.AddDays(1)
}
};
}
break;
default:
break;
}