feat 人脸检测

This commit is contained in:
2024-08-26 15:08:30 +08:00
parent f80d36c9b2
commit 7493e5a3a3
29 changed files with 422 additions and 205 deletions

View File

@@ -8,4 +8,6 @@ public interface IFactorySettingService : IScopeDependency
Task<List<FactorySettingResponse>> GetListAsync();
Task SaveAsync(FactorySettingSaveRequest request);
Task<FactorySettingResponse> GetFactorySettingAsync(string factory);
}

View File

@@ -1,10 +0,0 @@
using Dlhis.Basic.Organization.Request;
namespace Dlhis.Basic.Organization.Services;
public interface IGainfoOutsourceSettingService : IScopeDependency
{
Task SaveAsync(GainfoOutsourceSettingSaveRequest request);
Task<object> GetSettingAsync();
}

View File

@@ -1,8 +1,13 @@
using System.Reflection;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Entity.Basic.Dto;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Sys.Api;
using Dlhis.Sys.Api.Response;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
namespace Dlhis.Basic.Organization.Services.Implements;
@@ -20,24 +25,7 @@ public class FactorySettingService : IFactorySettingService
public async Task<List<FactorySettingResponse>> GetListAsync()
{
var apiResult = await _tenantApi.GetMyTenantsAsync();
var factories = apiResult.Data;
var factoryCodes = factories.Select(x => x.Code).ToList();
var settings = await _dbContext.Queryable<FactorySettingEntity>().Where(x => factoryCodes.Contains(x.TenantId))
.ToListAsync();
return factories.Select(item =>
{
var factory = factories.FirstOrDefault(x => x.Code == item.Code);
return new FactorySettingResponse
{
Factory = factory?.Code,
FactoryName = factory?.Name,
EnableOutsourceDown = settings.FirstOrDefault(x => x.TenantId == item.Code)?.EnableOutsourceDown ?? false,
UserQrCodeExpiration = settings.FirstOrDefault(x => x.TenantId == item.Code)?.UserQrCodeExpiration
};
}).ToList();
return await GetFactorySettingsAsync();
}
public async Task SaveAsync(FactorySettingSaveRequest request)
@@ -58,4 +46,60 @@ public class FactorySettingService : IFactorySettingService
await _dbContext.UpdateAsync(setting);
}
}
public async Task<FactorySettingResponse> GetFactorySettingAsync(string factory)
{
var settings = await GetFactorySettingsAsync(factory);
var setting = settings.FirstOrDefault() ?? new FactorySettingResponse();
setting.UserQrCodeExpiration ??= 60 * 1000 * 60;
setting.FaceImageDetection ??= new FaceImageDetectionDto
{
Enable = true,
Detections = typeof(FaceQualityTypeEnumType).GetFields(BindingFlags.Public | BindingFlags.Static).Select(x => new FaceImageDetection
{
Type = Enum.Parse<FaceQualityTypeEnumType>(x!.GetValue(null)!.ToString()!),
Level = FaceQualityLevelEnumType.Medium
}).ToList()
};
return setting;
}
//************************************************************************************************************************************************************
private async Task<List<FactorySettingResponse>> GetFactorySettingsAsync(string factory = null)
{
List<TenantResponse> factories;
if (factory.IsPresent())
{
factories = new List<TenantResponse> { new() { Code = factory } };
}
else
{
var apiResult = await _tenantApi.GetMyTenantsAsync();
factories = apiResult.Data;
}
var factoryCodes = factories.Select(x => x.Code).ToList();
var settings = await _dbContext.Queryable<FactorySettingEntity>()
.Where(x => factoryCodes.Contains(x.TenantId))
.ToListAsync();
return factories.Select(item =>
{
var currentFactory = factories.FirstOrDefault(x => x.Code == item.Code);
var currentSetting = settings.FirstOrDefault(x => x.TenantId == item.Code);
return new FactorySettingResponse
{
Factory = currentFactory?.Code,
FactoryName = currentFactory?.Name,
EnableOutsourceDown = currentSetting?.EnableOutsourceDown ?? false,
UserQrCodeExpiration = currentSetting?.UserQrCodeExpiration,
FaceImageDetection = currentSetting?.FaceImageDetection
};
}).ToList();
}
}

View File

@@ -1,9 +1,12 @@
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Entity.Basic.Dto;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Device.Enums;
using Dlhis.InfoCheck.Api;
using Dlhis.InfoCheck.Api.Requests;
using Dlhis.Scheduled.Api;
using Dlhis.Scheduled.Api.Request;
using Kurisu.AspNetCore.Authentication.Abstractions;
@@ -26,6 +29,8 @@ public class GainfoFaceService : IGainfoFaceService
private readonly ICurrentUser _currentUser;
private readonly IHangfireApi _hangfireApi;
private readonly IDevicePermissionService _devicePermissionService;
private readonly IFactorySettingService _factorySettingService;
private readonly IInfoCheckApi _infoCheckApi;
private readonly IDataProtectionProvider _dataProtectionProvider;
public GainfoFaceService(IGainfoStore gainfoStore,
@@ -35,6 +40,8 @@ public class GainfoFaceService : IGainfoFaceService
ICurrentUser currentUser,
IHangfireApi hangfireApi,
IDevicePermissionService devicePermissionService,
IFactorySettingService factorySettingService,
IInfoCheckApi infoCheckApi,
IDataProtectionProvider dataProtectionProvider)
{
_gainfoStore = gainfoStore;
@@ -45,10 +52,11 @@ public class GainfoFaceService : IGainfoFaceService
_currentUser = currentUser;
_hangfireApi = hangfireApi;
_devicePermissionService = devicePermissionService;
_factorySettingService = factorySettingService;
_infoCheckApi = infoCheckApi;
_dataProtectionProvider = dataProtectionProvider;
}
public async Task<GainfoFacePageSummaryResponse> GetPageAsync(GainfoFacePageRequest request)
{
var factories = request.Factories;
@@ -96,11 +104,12 @@ public class GainfoFaceService : IGainfoFaceService
var gainfo = await _gainfoStore.Queryable(GainfoStatusEnumType.External)
.Where(x => x.IdNo == request.UserNo)
.FirstAsync();
.SingleAsync();
gainfo ??= await _gainfoStore.Queryable(GainfoStatusEnumType.Working)
.Where(x => x.IdNo == request.UserNo)
.FirstAsync();
.SingleAsync();
gainfo.ThrowIfNull("人员信息不存在");
var face = await _faceDbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
@@ -186,25 +195,20 @@ public class GainfoFaceService : IGainfoFaceService
public async Task UploadFaceAsync(GainfoUploadFaceRequest request)
{
var gainfo = await _gainfoStore.Queryable().Where(x => x.No == request.UserNo).FirstAsync();
var gainfo = await _gainfoStore.Queryable().Where(x => x.No == request.UserNo || x.Code == request.UserNo).SingleAsync();
gainfo.ThrowIfNull("人员档案信息不存在");
var userId = _currentUser.GetIntSubjectId();
await DetectFaceImageQualityAsync(request.Face);
var face = await _faceDbContext.Queryable<GainfoFaceEntity>().Where(x => x.UserCode == gainfo.Code).SingleAsync();
var taskType = face == null ? DeviceTaskEnumType.Add : DeviceTaskEnumType.Update;
if (face == null)
{
face = new GainfoFaceEntity
{
UserCode = gainfo.Code,
Face = request.Face,
CreateTime = DateTime.Now,
ModifiedTime = DateTime.Now,
CreatedBy = userId,
ModifiedBy = userId
Face = request.Face
};
await _faceDbContext.InsertAsync(face);
@@ -218,8 +222,6 @@ public class GainfoFaceService : IGainfoFaceService
}
face.Face = request.Face;
face.ModifiedBy = userId;
face.ModifiedTime = DateTime.Now;
await _faceDbContext.UpdateAsync(face);
}
@@ -236,9 +238,6 @@ public class GainfoFaceService : IGainfoFaceService
_logger.LogError(e, "上传face数据到sap失败:{message}", e.Message);
}
// var prefix = gainfo.Factory == "501" ? "jschuneng" : "jingke";
// var url = $"https://pay.aizhuhotel.com/{prefix}/api/face";
//生成任务
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, taskType, DeviceTaskSourceEnumType.Photo);
}
@@ -328,4 +327,42 @@ public class GainfoFaceService : IGainfoFaceService
return faceDictionary;
}
//*********************************************************************************************************************************************
/// <summary>
/// 人脸质量检测
/// </summary>
/// <param name="base64Image"></param>
/// <param name="factory"></param>
/// <exception cref="UserFriendlyException"></exception>
private async Task DetectFaceImageQualityAsync(string base64Image, string factory = null)
{
var currentFactory = factory ?? _currentUser.GetStringTenantId();
var factorySetting = await _factorySettingService.GetFactorySettingAsync(currentFactory);
if (factorySetting.FaceImageDetection.Enable)
{
List<string> r;
try
{
var result = await _infoCheckApi.FaceDetectAsync(new FaceDetectRequest
{
Base64 = base64Image,
CheckList = factorySetting.FaceImageDetection.Detections.Select(x => new FaceDetectCheckRequest
{
Type = (int)x.Type,
Level = (int)x.Level
}).ToList()
});
r = result.Data;
}
catch (Exception)
{
return;
}
r.IsPresent().ThrowIfTrue($"{r.Join("\r\n")}");
}
}
}

View File

@@ -1,71 +0,0 @@
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.Organization.Request;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using SqlSugar;
namespace Dlhis.Basic.Organization.Services.Implements;
public class GainfoOutsourceSettingService : IGainfoOutsourceSettingService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly IDevicePermissionService _devicePermissionService;
public GainfoOutsourceSettingService(IDbContext dbContext, ICurrentUser currentUser, IDevicePermissionService devicePermissionService)
{
_dbContext = dbContext;
_currentUser = currentUser;
_devicePermissionService = devicePermissionService;
}
public async Task SaveAsync(GainfoOutsourceSettingSaveRequest request)
{
var userNos = await _dbContext.Queryable<GainfoEntity>()
.Where(x => SqlFunc.Subqueryable<OutsourceEntity>().Where(p => p.Code == x.Code).Any())
.Select(x => x.No)
.ToListAsync();
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
{
var currentTenantId = _currentUser.GetStringTenantId();
(!request.Id.HasValue && await _dbContext.Queryable<FactorySettingEntity>()
.Where(x => x.TenantId == currentTenantId)
.AnyAsync()).ThrowIfTrue("保存失败,请检查参数");
if (!request.Id.HasValue)
{
var entity = new FactorySettingEntity
{
EnableOutsourceDown = request.EnableFactory,
TenantId = currentTenantId
};
await _dbContext.InsertAsync(entity);
}
else
{
var entity = await _dbContext.Queryable<FactorySettingEntity>().Where(x => x.Id == request.Id).SingleAsync();
entity.ThrowIfNull("数据不存在,保存失败");
entity.EnableOutsourceDown = request.EnableFactory;
await _dbContext.UpdateAsync(entity);
}
}, userNos);
}
public async Task<object> GetSettingAsync()
{
var currentTenantId = _currentUser.GetStringTenantId();
return await _dbContext.Queryable<FactorySettingEntity>().Where(x => x.TenantId == currentTenantId).Select(x => new
{
x.Id, EnableFactory = x.EnableOutsourceDown
}).SingleAsync();
}
}

View File

@@ -10,21 +10,26 @@ public class GainfoQrCodeService : IGainfoQrCodeService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
private readonly IFactorySettingService _factorySettingService;
public GainfoQrCodeService(IDbContext dbContext, ICurrentUser currentUser)
public GainfoQrCodeService(IDbContext dbContext, ICurrentUser currentUser, IFactorySettingService factorySettingService)
{
_dbContext = dbContext;
_currentUser = currentUser;
_factorySettingService = factorySettingService;
}
public async Task<string> GetMyQrCodeAsync(bool isNeedRefresh = false)
{
var jinkoCode = _currentUser.GetUserClaim("jinko-code");
jinkoCode = "0123456";
if (jinkoCode.IsEmpty())
{
return null;
}
var factory = _currentUser.GetStringTenantId();
var now = DateTime.Now;
var gainfoQrCode = await _dbContext.Queryable<GainfoQrCodeEntity>().SingleAsync(x => x.UserCode == jinkoCode);
@@ -39,7 +44,8 @@ public class GainfoQrCodeService : IGainfoQrCodeService
await _dbContext.InsertAsync(gainfoQrCode);
}
if (gainfoQrCode.ModifiedTime.AddMinutes(60) >= now && !isNeedRefresh)
var setting = await _factorySettingService.GetFactorySettingAsync(factory);
if (gainfoQrCode.ModifiedTime.AddMinutes(setting.UserQrCodeExpiration!.Value) >= now && !isNeedRefresh)
{
return gainfoQrCode.QrCode;
}