diff --git a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs index f3a029bc..ea54f645 100644 --- a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs +++ b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs @@ -130,7 +130,7 @@ public class DevicePermissionsController : ControllerBase [IgnoreTenant] public async Task> GetAssignDeviceCodesAsync(string userCode) { - return await _devicePermissionService.GetUserDeviceCodesByNoAsync(userCode); + return await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(userCode); } diff --git a/Src/Dlhis.Basic/Device/Controllers/DevicesConnectController.cs b/Src/Dlhis.Basic/Device/Controllers/DevicesConnectController.cs index 6d7fcdcf..4c4a3303 100644 --- a/Src/Dlhis.Basic/Device/Controllers/DevicesConnectController.cs +++ b/Src/Dlhis.Basic/Device/Controllers/DevicesConnectController.cs @@ -76,7 +76,7 @@ public class DevicesConnectController : ControllerBase /// /// /// - [DisableApiLog] + [DisableApiLog] [HttpGet("brand-devices")] [IgnoreTenant] public async Task> GetDeviceUniqueInfoByBrandAsync( diff --git a/Src/Dlhis.Basic/Device/Controllers/ElevatorAcsController.cs b/Src/Dlhis.Basic/Device/Controllers/ElevatorAcsController.cs new file mode 100644 index 00000000..945a9d16 --- /dev/null +++ b/Src/Dlhis.Basic/Device/Controllers/ElevatorAcsController.cs @@ -0,0 +1,48 @@ +using Dlhis.Basic.Device.Request; +using Dlhis.Basic.Device.Response; +using Dlhis.Basic.Device.Services; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Aop; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Dlhis.Basic.Device.Controllers; + +/// +/// 梯控设备 +/// +[Authorize] +[Route("api/[controller]")] +[ApiController] +public class ElevatorAcsController : ControllerBase +{ + private readonly IElevatorAcsService _elevatorAcsService; + + public ElevatorAcsController(IElevatorAcsService elevatorAcsService) + { + _elevatorAcsService = elevatorAcsService; + } + + + /// + /// 设备保存 + /// + /// + /// + [HttpPost("save")] + [Transactional] + public async Task SaveAsync(ElevatorAcsSaveRequest request) + { + await _elevatorAcsService.SaveAsync(request); + } + + /// + /// 获取设备详情 + /// + /// + /// + [HttpGet("detail")] + public async Task GetDetailsAsync(string deviceUniqueNo) + { + return await _elevatorAcsService.GetDetailsAsync(deviceUniqueNo); + } +} diff --git a/Src/Dlhis.Basic/Device/Request/DevicePageRequest.cs b/Src/Dlhis.Basic/Device/Request/DevicePageRequest.cs index 6d0894d7..fc3f6960 100644 --- a/Src/Dlhis.Basic/Device/Request/DevicePageRequest.cs +++ b/Src/Dlhis.Basic/Device/Request/DevicePageRequest.cs @@ -23,4 +23,9 @@ public class DevicePageRequest : PageDto /// 启用状态 /// public bool? Enable { get; set; } + + /// + /// 是否为梯控 + /// + public bool IsElevatorAcs { get; set; } } diff --git a/Src/Dlhis.Basic/Device/Request/DevicePermissionSaveRequest.cs b/Src/Dlhis.Basic/Device/Request/DevicePermissionSaveRequest.cs index c47e060c..0bb6b80b 100644 --- a/Src/Dlhis.Basic/Device/Request/DevicePermissionSaveRequest.cs +++ b/Src/Dlhis.Basic/Device/Request/DevicePermissionSaveRequest.cs @@ -1,7 +1,5 @@ using System.ComponentModel.DataAnnotations; -using System.Linq.Expressions; using Dlhis.Entity.Device.Enums; -using Dlhis.Entity.Sys.Entities; using Dlhis.Entity.Sys.Enums; using Kurisu.AspNetCore.Utils.Extensions; diff --git a/Src/Dlhis.Basic/Device/Request/ElevatorAcsSaveRequest.cs b/Src/Dlhis.Basic/Device/Request/ElevatorAcsSaveRequest.cs new file mode 100644 index 00000000..6217c4d5 --- /dev/null +++ b/Src/Dlhis.Basic/Device/Request/ElevatorAcsSaveRequest.cs @@ -0,0 +1,78 @@ +using System.ComponentModel.DataAnnotations; +using Dlhis.Entity.Device.Enums; + +namespace Dlhis.Basic.Device.Request; + +public class ElevatorAcsSaveRequest +{ + public bool IsEdit { get; set; } + + /// + /// 设备唯一号 + /// + [Required(ErrorMessage = "设备唯一号不能为空")] + public string UniqueNo { get; set; } + + /// + /// 模板code + /// + [Required(ErrorMessage = "模板不能为空")] + public Guid TemplateCode { get; set; } + + /// + /// 供应商信息 + /// + [Required(ErrorMessage = "供应商信息不能为空")] + public string Supplier { get; set; } + + /// + /// 设备功能 DeviceFuncEnumType + /// + [MinLength(1, ErrorMessage = "设备功能不能为空")] + [Required(ErrorMessage = "设备功能不能为空")] + public List Funcs { get; set; } + + /// + /// 心跳间隔 + /// + public int HeartbeatInterval { get; set; } + + /// + /// 部署点 + /// + public string DeployPlace { get; set; } + + /// + /// 部署坐标 + /// + public string DeployPosition { get; set; } + + /// + /// 部署方向 DeviceDeployDirectionEnumType + /// + public DeviceDeployDirectionEnumType? DeployDirection { get; set; } + + /// + /// 连接信息 + /// + [Required(ErrorMessage = "连接信息不能为空")] + public string ConnectionInfo { get; set; } + + /// + /// 是否启用 + /// + public bool Enable { get; set; } + + /// + /// 楼层列表 + /// + public List Floors { get; set; } +} + +public class FloorInfo +{ + /// + /// 楼层名称 + /// + public string FloorName { get; set; } +} diff --git a/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs b/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs index c9cb14ca..b4ed1075 100644 --- a/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs +++ b/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs @@ -102,6 +102,33 @@ public class DevicePageResponse /// public bool EnableTimePeriod { get; set; } + /// + /// 是否作为梯控 + /// + public bool IsElevatorAcs { get; set; } + + /// + /// 同源标记 + /// + public int SourceFlag + { + get + { + if (string.IsNullOrEmpty(RealDeviceUniqueNo)) + { + return UniqueNo.GetHashCode(); + } + + return RealDeviceUniqueNo.GetHashCode(); + } + } + + /// + /// + /// + [JsonIgnore] + public string RealDeviceUniqueNo { get; set; } + /// /// 同步标志 diff --git a/Src/Dlhis.Basic/Device/Response/ElevatorAcsDetailResponse.cs b/Src/Dlhis.Basic/Device/Response/ElevatorAcsDetailResponse.cs new file mode 100644 index 00000000..223e6539 --- /dev/null +++ b/Src/Dlhis.Basic/Device/Response/ElevatorAcsDetailResponse.cs @@ -0,0 +1,112 @@ +using Dlhis.DataPermission.Model; +using Dlhis.Entity.Device.Enums; +using Mapster; +using SqlSugar; +using SqlSugar.DbConvert; + +namespace Dlhis.Basic.Device.Response; + +public class ElevatorAcsDetailResponse +{ + /// + /// 设备code + /// + [DeviceCode] + [SugarColumn(IsNullable = false, ColumnDescription = "设备code")] + public Guid Code { get; set; } + + /// + /// 设备唯一码,IP/MAC/SNO + /// + [AdaptIgnore(MemberSide.Destination)] + [SugarColumn(IsNullable = false, ColumnDescription = "设备唯一码", UniqueGroupNameList = new[] { "Idx_Device_UniqueNo" })] + public string UniqueNo { get; set; } + + /// + /// 心跳间隔 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "心跳检查间隔")] + public int HeartbeatInterval { get; set; } = 30; + + /// + /// 供应商 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "供应商")] + public string Supplier { get; set; } + + /// + /// 设备功能 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "varchar(150)", IsJson = true, ColumnDescription = "设备功能", DefaultValue = "[]")] + public List Funcs { get; set; } + + /// + /// 设备类型 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "设备类型", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))] + public DeviceTypeEnumType Type { get; set; } + + /// + /// 部署点 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "部署点")] + public string DeployPlace { get; set; } + + /// + /// 部署坐标 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "部署坐标")] + public string DeployPosition { get; set; } + + /// + /// 部署方向 + /// + [SugarColumn(IsNullable = true, ColumnDescription = "部署方向", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))] + public DeviceDeployDirectionEnumType? DeployDirection { get; set; } + + /// + /// 模板code + /// + [SugarColumn(IsNullable = false, ColumnDescription = "模板code")] + public Guid TemplateCode { get; set; } + + /// + /// 连接信息 + /// + [SugarColumn(IsNullable = false, ColumnDescription = "连接信息")] + public string ConnectionInfo { get; set; } + + /// + /// 是否启用 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用")] + public bool Enable { get; set; } + + + /// + /// 是否作为考勤设备 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否作为考勤设备")] + public bool AsAttendance { get; set; } + + + /// + /// 是否启用时段码 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用时段码", DefaultValue = "0")] + public bool EnableTimePeriod { get; set; } = false; + + /// + /// 是否为电梯门禁设备 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否为电梯门禁设备", DefaultValue = "0")] + public bool IsElevatorAcs { get; set; } + + public List Floors { get; set; } +} + +public class ElevatorAcsFloorResponse +{ + public string FloorName { get; set; } + public string AcsValue { get; set; } +} diff --git a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs index b8b39e45..b82c16f5 100644 --- a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs @@ -27,18 +27,18 @@ public interface IDevicePermissionService : IScopeDependency /// /// 生成下发任务(主动生成) /// - /// + /// /// /// /// - Task GenerateDeviceTasksAsync(string userNo, DeviceTaskEnumType taskType, string source); + Task GenerateDeviceTasksAsync(string userNoOrCode, DeviceTaskEnumType taskType, string source); /// /// 获取用户所有设备权限 /// - /// + /// /// - Task> GetUserDeviceCodesByNoAsync(string userNo); + Task> GetUserDeviceCodesByNoOrCodeAsync(string userNoOrCode); Task> GetGainfoPageByDeviceAsync(DeviceWithGainfoRequest request); @@ -64,5 +64,6 @@ public interface IDevicePermissionService : IScopeDependency Task GenerateDeleteDeviceTasksAsync(string deviceUniqueNo); + [Obsolete("不建议使用,建议使用GetUserDeviceCodesByNoOrCodeAsync方法")] Task> GetUserDeviceCodesByNoAsync(List userCodes); } \ No newline at end of file diff --git a/Src/Dlhis.Basic/Device/Services/IElevatorAcsService.cs b/Src/Dlhis.Basic/Device/Services/IElevatorAcsService.cs new file mode 100644 index 00000000..96850476 --- /dev/null +++ b/Src/Dlhis.Basic/Device/Services/IElevatorAcsService.cs @@ -0,0 +1,10 @@ +using Dlhis.Basic.Device.Request; +using Dlhis.Basic.Device.Response; + +namespace Dlhis.Basic.Device.Services; + +public interface IElevatorAcsService : IScopeDependency +{ + Task SaveAsync(ElevatorAcsSaveRequest request); + Task GetDetailsAsync(string deviceUniqueNo); +} diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DeviceConnectService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DeviceConnectService.cs index bf400a26..dae83d13 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DeviceConnectService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DeviceConnectService.cs @@ -139,15 +139,35 @@ public class DeviceConnectService : IDeviceConnectService { var factories = factory.Split(",").ToList(); - return await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId)) - .Select(d => new DeviceUniqueWithConnectionInfoDto - { - DeviceUnique = d.UniqueNo, - ConnectionInfo = d.ConnectionInfo - }) - .ToListAsync(); + var results = await _dbContext.Queryable() + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId)) + .Where(d => d.IsElevatorAcs == false)//先排除梯控 + .Where(d => d.Enable == true) + .Select(d => new DeviceUniqueWithConnectionInfoDto + { + DeviceUnique = d.UniqueNo, + ConnectionInfo = d.ConnectionInfo + }) + .ToListAsync(); + + + var elevatorAcses = await _dbContext.Queryable() + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .LeftJoin((d, dt, f) => d.UniqueNo == f.VirtualDeviceUniqueNo) + .Where((d, dt) => dt.Brand == brand && factories.Contains(d.TenantId)) + .Where(d => d.IsElevatorAcs == true) + .Where(d => d.Enable == true) + .Select((d, dt, f) => new DeviceUniqueWithConnectionInfoDto + { + DeviceUnique = f.RealDeviceUniqueNo, + ConnectionInfo = d.ConnectionInfo + }) + .ToListAsync(); + + results.AddRange(elevatorAcses.DistinctBy(x => x.DeviceUnique).ToList()); + + return results; } diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs index 5a7f9641..3e9fff71 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs @@ -298,9 +298,9 @@ public class DevicePermissionService : IDevicePermissionService } - public async Task GenerateDeviceTasksAsync(string userNo, DeviceTaskEnumType taskType, string source) + public async Task GenerateDeviceTasksAsync(string userNoOrCode, DeviceTaskEnumType taskType, string source) { - var deviceCodes = await GetUserDeviceCodesByNoAsync(userNo); + var deviceCodes = await GetUserDeviceCodesByNoOrCodeAsync(userNoOrCode); if (deviceCodes.IsEmpty()) return; var devices = await _dbContext.Queryable() @@ -308,13 +308,33 @@ public class DevicePermissionService : IDevicePermissionService .Where(x => deviceCodes.Contains(x.Code)) .ToListAsync(); + var gainfo = await _dbContext.Queryable() + .Where(x => x.No == userNoOrCode || x.Code == userNoOrCode) + .Select(x => new + { + UserId = x.Id, + UserNo = x.No, + UserCode = x.Code + }) + .SingleAsync() + ?? await _dbContext.Queryable() + .Where(x => x.No == userNoOrCode || x.Code == userNoOrCode) + .Select(x => new + { + UserId = (long)x.IdentityUserId, + UserNo = x.No, + UserCode = x.Code + }) + .SingleAsync(); + await _dbContext.InsertAsync(devices.Select(x => new DeviceTaskEntity { DeviceUniqueNo = x.UniqueNo, Status = DeviceTaskStatusEnumType.Wait, Type = taskType, DeviceType = x.Type, - UserNo = userNo, + UserNo = gainfo.UserNo, + UserId = gainfo.UserId, Priority = 0, Version = 0, TenantId = x.TenantId, @@ -326,11 +346,14 @@ public class DevicePermissionService : IDevicePermissionService /// /// 获取用户所有设备权限 /// - /// + /// /// - public async Task> GetUserDeviceCodesByNoAsync(string userNo) + public async Task> GetUserDeviceCodesByNoOrCodeAsync(string userNoOrCode) { - var no = await _dbContext.Queryable().Where(x => x.No == userNo || x.Code == userNo).Select(x => x.No).SingleAsync(); + var no = await _dbContext.Queryable().Where(x => x.No == userNoOrCode || x.Code == userNoOrCode).Select(x => x.No).SingleAsync() + ?? + await _dbContext.Queryable().Where(x => x.No == userNoOrCode || x.Code == userNoOrCode).Select(x => x.No).FirstAsync(); + var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List { no }); var deviceCodes = result.First().Value.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList(); diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs index 9d05e95a..990f8b37 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs @@ -38,24 +38,29 @@ public class DeviceService : IDeviceService public async Task> GetPageAsync(DevicePageRequest request) { var page = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .LeftJoin((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo) - .LeftJoin((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code) - .Where((d, dt) => d.Type == request.Type) - .WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable) - .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search)) - .WhereIF(request.Supplier.IsPresent(), d => d.Supplier.Contains(request.Supplier)) - .Select((d, dt, dr, bp) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - BusinessPlaceCode = bp.Code, - BusinessPlaceName = bp.Name, - LastHeartbeatTime = dr.LastHeartbeatTime, - Factory = d.TenantId - }, true) - .ToPageListAsync(request); + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .LeftJoin((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo) + .LeftJoin((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code) + .LeftJoin((d, dt, dr, bp, ef) => d.UniqueNo == ef.VirtualDeviceUniqueNo) + .Where((d, dt) => d.Type == request.Type) + .Where(d => d.IsElevatorAcs == request.IsElevatorAcs) + .WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable) + .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search)) + .WhereIF(request.Supplier.IsPresent(), d => d.Supplier.Contains(request.Supplier)) + .Select((d, dt, dr, bp, ef) => new DevicePageResponse + { + IsElevatorAcs = d.IsElevatorAcs, + FuncsStr = d.Funcs.ToString(), + Brand = dt.Brand, + Type = d.Type, + BusinessPlaceCode = bp.Code, + BusinessPlaceName = bp.Name, + LastHeartbeatTime = dr.LastHeartbeatTime, + RealDeviceUniqueNo = ef.RealDeviceUniqueNo, + Factory = d.TenantId, + }, true) + .ToPageListAsync(request); + //获取当前用户角色 var role = _currentUser.GetRole(); @@ -77,7 +82,6 @@ public class DeviceService : IDeviceService } } - var deviceUniqueNos = page.Data.Select(x => x.UniqueNo).ToList(); var deviceUsers = await _dbContext.Queryable() .Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo)) diff --git a/Src/Dlhis.Basic/Device/Services/Implements/ElevatorAcsService.cs b/Src/Dlhis.Basic/Device/Services/Implements/ElevatorAcsService.cs new file mode 100644 index 00000000..76cabd3c --- /dev/null +++ b/Src/Dlhis.Basic/Device/Services/Implements/ElevatorAcsService.cs @@ -0,0 +1,216 @@ +using Dlhis.Basic.Device.Request; +using Dlhis.Basic.Device.Response; +using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Device.Enums; +using Dlhis.Sys.Api; +using Kurisu.AspNetCore.Authentication.Abstractions; +using Kurisu.AspNetCore.CustomClass; +using Kurisu.AspNetCore.DataAccess.Entity; +using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; +using Kurisu.AspNetCore.Utils.Extensions; +using Mapster; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Dlhis.Basic.Device.Services.Implements; + +public class ElevatorAcsService : IElevatorAcsService +{ + private readonly IDbContext _dbContext; + private readonly ICurrentUser _currentUser; + private readonly ITenantApi _tenantApi; + + private const string DefaultPassword = "********"; + private const string DefaultPasswordKey = "password"; + + public ElevatorAcsService(IDbContext dbContext, ICurrentUser currentUser, ITenantApi tenantApi) + { + _dbContext = dbContext; + _currentUser = currentUser; + _tenantApi = tenantApi; + } + + public async Task SaveAsync(ElevatorAcsSaveRequest request) + { + await CheckUniqueNoExistsAsync(request); + await CheckTemplateExistsAsync(request.TemplateCode); + await CheckTemplateFieldAsync(request.TemplateCode, request.ConnectionInfo); + + if (!request.IsEdit) + { + var password = GetJsonValue(request.ConnectionInfo, DefaultPasswordKey); + if (password.IsPresent()) + { + (password == DefaultPassword).ThrowIfTrue("不支持错误的密码类型"); + } + + for (int i = 0; i < request.Floors.Count; i++) + { + var floor = request.Floors[i]; + + DeviceEntity entity = request.Adapt(); + entity.Code = Guid.NewGuid(); + entity.Type = DeviceTypeEnumType.Door; + entity.IsElevatorAcs = true; + entity.Enable = true; + + var virtualDeviceUniqueNo = $"{request.UniqueNo}-EACS-{floor.FloorName}"; + entity.UniqueNo = virtualDeviceUniqueNo; + + var elevatorAcsFloor = new ElevatorAcsFloorEntity + { + RealDeviceUniqueNo = request.UniqueNo, + FloorName = floor.FloorName, + AcsValue = i, + VirtualDeviceUniqueNo = virtualDeviceUniqueNo, + }; + + await _dbContext.InsertAsync(entity); + await _dbContext.InsertAsync(elevatorAcsFloor); + } + } + else + { + var prefix = request.UniqueNo + "-EACS-"; + List entities = await _dbContext.Queryable().Where(x => x.UniqueNo.StartsWith(prefix)).ToListAsync(); + + foreach (var entity in entities) + { + //检查更新的密码字段是否为默认密码,如果是,则将原有密码从数据库实体中取出并替换到请求的连接信息中,防止默认密码被保存。 + // 这样可以避免用户在未修改密码时,前端传递默认密码导致密码被覆盖为不可用的默认值。 + var updatePassword = GetJsonValue(request.ConnectionInfo, DefaultPasswordKey); + if (updatePassword.IsPresent()) + { + if (updatePassword == DefaultPassword) + { + var jObj = JObject.Parse(request.ConnectionInfo); + var password = GetJsonValue(entity.ConnectionInfo, DefaultPasswordKey); + + jObj[DefaultPasswordKey] = password; + request.ConnectionInfo = jObj.ToString(); + } + } + + request.Adapt(entity); + await _dbContext.UpdateAsync(entity); + } + } + } + + public async Task GetDetailsAsync(string deviceUniqueNo) + { + var floor = await _dbContext.Queryable().Where(x => x.VirtualDeviceUniqueNo == deviceUniqueNo).SingleAsync(); + var floors = await _dbContext.Queryable().Where(x => x.RealDeviceUniqueNo == floor.RealDeviceUniqueNo).ToListAsync(); + var deviceUniqueNos = floors.Select(x => x.VirtualDeviceUniqueNo).ToList(); + + var device = await _dbContext.Queryable().Where(x => x.UniqueNo == deviceUniqueNo).SingleAsync(); + + + var result = device.Adapt(); + result.UniqueNo = floor.RealDeviceUniqueNo; + result.Floors = floors.Adapt>(); + + return result; + } + + /// + /// 获取json的字段值 + /// + /// + /// + /// + /// + private static string GetJsonValue(string json, string key) + { + try + { + var jObj = JObject.Parse(json); + return jObj.TryGetValue(key, out var value) + ? value.Value() + : string.Empty; + } + catch (JsonReaderException) + { + throw new UserFriendlyException("连接信息参数格式不正确"); + } + } + + /// + /// 检查设备模板是否存在 + /// + /// + /// + private async Task CheckTemplateExistsAsync(Guid templateCode) + { + var entity = await _dbContext.Queryable().Where(x => x.Code == templateCode).FirstAsync(); + entity.ThrowIfNull("设备模板不存在"); + } + + + /// + /// 检查设备连接参数是否与模板设定一致 + /// + /// + /// + /// + private async Task CheckTemplateFieldAsync(Guid templateCode, string jsonContent) + { + var template = await _dbContext.Queryable().Where(x => x.Code == templateCode).SingleAsync(); + template.ThrowIfNull("设备模板不存在"); + + try + { + var templateJObj = JObject.Parse(template.Template); + var jObj = JObject.Parse(jsonContent); + (templateJObj.Count == jObj.Count).ThrowIfFalse("连接参数字段数不一致"); + + foreach (var item in templateJObj) + { + jObj.ContainsKey(item.Key).ThrowIfFalse($"设备连接参数需要和模板一致.缺失{item.Key}"); + } + } + catch (Exception ex) when (ex is JsonReaderException) + { + throw new UserFriendlyException("连接信息参数格式不正确"); + } + } + + /// + /// 检查唯一号是否存在 + /// + /// + /// + private async Task CheckUniqueNoExistsAsync(ElevatorAcsSaveRequest request) + { + await _dbContext.IgnoreAsync(async () => + { + var currentTenantId = _currentUser.GetStringTenantId(); + + var prefix = request.UniqueNo + "-EACS-"; + + //查询设备是否存在 + var device = await _dbContext.Queryable() + .Where(x => x.UniqueNo == request.UniqueNo) + .WhereIF(!request.IsEdit, x => x.UniqueNo.StartsWith(prefix)) + .SingleAsync(); + + if (device != null) + { + (device.TenantId == currentTenantId).ThrowIfTrue("设备已经存在,请检查"); + + var factory = device.TenantId; + try + { + var tenant = await _tenantApi.GetTenantByCodeAsync(device.TenantId); + factory = tenant?.Name ?? string.Empty; + } + catch (Exception) + { + //ignore + } + + true.ThrowIfTrue($"设备已添加{factory}厂区"); + } + }); + } +} diff --git a/Src/Dlhis.Basic/Organization/Controllers/GainfoCardsController.cs b/Src/Dlhis.Basic/Organization/Controllers/GainfoCardsController.cs index 7bebfd90..bc412643 100644 --- a/Src/Dlhis.Basic/Organization/Controllers/GainfoCardsController.cs +++ b/Src/Dlhis.Basic/Organization/Controllers/GainfoCardsController.cs @@ -29,6 +29,7 @@ public class GainfoCardsController : ControllerBase /// /// [HttpPost("change")] + [IgnoreTenant] [Transactional] public async Task ChangeCardAsync(GainfoChangeCardRequest request) { diff --git a/Src/Dlhis.Basic/Organization/Controllers/GainfoFingerprintsController.cs b/Src/Dlhis.Basic/Organization/Controllers/GainfoFingerprintsController.cs index f8930e88..d16a5cba 100644 --- a/Src/Dlhis.Basic/Organization/Controllers/GainfoFingerprintsController.cs +++ b/Src/Dlhis.Basic/Organization/Controllers/GainfoFingerprintsController.cs @@ -35,6 +35,19 @@ public class GainfoFingerprintsController : ControllerBase await _gainfoFingerprintService.CollectFingerprintAsync(request.UserCode, request.FingerNum, request.Fingerprint); } + /// + /// 批量采集 + /// + /// + /// + [HttpPost("batchCollect")] + [IgnoreTenant] + [Transactional] + public async Task CollectFingerprintAsync(FingerprintBatchCollectRequest request) + { + await _gainfoFingerprintService.CollectFingerprintAsync(request); + } + /// /// 获取人员指纹数据 /// diff --git a/Src/Dlhis.Basic/Organization/Request/FingerprintBatchCollectRequest.cs b/Src/Dlhis.Basic/Organization/Request/FingerprintBatchCollectRequest.cs new file mode 100644 index 00000000..716a80db --- /dev/null +++ b/Src/Dlhis.Basic/Organization/Request/FingerprintBatchCollectRequest.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; + +namespace Dlhis.Basic.Organization.Request +{ + public class FingerprintBatchCollectRequest + { + [Required(ErrorMessage = "工号不能为空")] + public string UserCode { get; set; } + + [MinLength(1, ErrorMessage = "指纹列表不能为空")] + public List Fingerprints { get; set; } + } + + + public class FingerprintItem + { + public int FingerNum { get; set; } + public string Fingerprint { get; set; } + } +} diff --git a/Src/Dlhis.Basic/Organization/Services/IGainfoFingerprintService.cs b/Src/Dlhis.Basic/Organization/Services/IGainfoFingerprintService.cs index 7e6ff0ef..0f2578b7 100644 --- a/Src/Dlhis.Basic/Organization/Services/IGainfoFingerprintService.cs +++ b/Src/Dlhis.Basic/Organization/Services/IGainfoFingerprintService.cs @@ -1,10 +1,12 @@ -using Dlhis.Basic.Organization.Response; +using Dlhis.Basic.Organization.Request; +using Dlhis.Basic.Organization.Response; namespace Dlhis.Basic.Organization.Services; public interface IGainfoFingerprintService : IScopeDependency { Task CollectFingerprintAsync(string userCode, int fingerNum, string fingerprint); - + Task CollectFingerprintAsync(FingerprintBatchCollectRequest request); + Task GetFingerprintAsync(string userCode, int? fingerNum); } diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoCardService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoCardService.cs index 06cacc02..ab5abeeb 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoCardService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoCardService.cs @@ -29,13 +29,23 @@ public class GainfoCardService : IGainfoCardService public async Task ChangeCardAsync(GainfoChangeCardRequest request) { + if (await _gainfoStore.Queryable().Where(x => x.CardNo == request.CardNo).AnyAsync()) + { + true.ThrowIfTrue("卡号已分配"); + } + var gainfo = await _gainfoStore.Queryable().Where(x => x.No == request.UserNo).FirstAsync(); gainfo.ThrowIfNull("档案不存在"); gainfo.CardNo = request.CardNo; await _gainfoStore.UpdateAsync(gainfo); - await _dbContext.InsertAsync(request.Adapt()); + + var cardRecord = request.Adapt(); + cardRecord.TenantId = gainfo.Factory; + await _dbContext.InsertAsync(cardRecord); + + await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Update, null, null, "更新卡号下发"); } public async Task> GetChangeRecordsAsync(string userNo) diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFingerprintService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFingerprintService.cs index da30343c..32644e31 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFingerprintService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoFingerprintService.cs @@ -1,72 +1,117 @@ -using Dlhis.Basic.Organization.Response; +using Dlhis.Basic.Device.Services; +using Dlhis.Basic.Organization.Request; +using Dlhis.Basic.Organization.Response; using Dlhis.Entity.Basic.Entity; +using Dlhis.Entity.Device.Entities; +using Dlhis.Entity.Device.Enums; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; -namespace Dlhis.Basic.Organization.Services.Implements +namespace Dlhis.Basic.Organization.Services.Implements; + +public class GainfoFingerprintService : IGainfoFingerprintService { - public class GainfoFingerprintService : IGainfoFingerprintService + private readonly IDbContext _dbContext; + private readonly IDevicePermissionService _devicePermissionService; + + public GainfoFingerprintService(IDbContext dbContext, IDevicePermissionService devicePermissionService) { - private readonly IDbContext _dbContext; - - public GainfoFingerprintService(IDbContext dbContext) + _dbContext = dbContext; + _devicePermissionService = devicePermissionService; + } + public async Task CollectFingerprintAsync(string userCode, int fingerNum, string fingerprint) + { + await CollectFingerprintAsync(new FingerprintBatchCollectRequest { - _dbContext = dbContext; - } - public async Task CollectFingerprintAsync(string userCode, int fingerNum, string fingerprint) - { - if (!await _dbContext.Queryable().Where(x => x.Code == userCode).AnyAsync()) + UserCode = userCode, + Fingerprints = new List { - true.ThrowIfTrue("人员不存在"); + new FingerprintItem + { + FingerNum = fingerNum, + Fingerprint = fingerprint + } } + }); + } - var gainfoFingerprint = await _dbContext.Queryable().Where(x => x.UserCode == userCode && x.FingerNum == fingerNum).FirstAsync(); + public async Task CollectFingerprintAsync(FingerprintBatchCollectRequest request) + { + var gainfo = await _dbContext.Queryable().Where(x => x.Code == request.UserCode).SingleAsync(); + gainfo.ThrowIfNull("人员不存在"); + gainfo.CardNo.IsEmpty().ThrowIfTrue("人员未绑定卡号,无法采集指纹"); + + + foreach (var item in request.Fingerprints) + { + var gainfoFingerprint = await _dbContext.Queryable().Where(x => x.UserCode == request.UserCode && x.FingerNum == item.FingerNum).FirstAsync(); if (gainfoFingerprint == null) { gainfoFingerprint = new GainfoFingerprintEntity { - UserCode = userCode, - FingerNum = fingerNum, - Fingerprint = fingerprint + UserCode = request.UserCode, + FingerNum = item.FingerNum, + Fingerprint = item.Fingerprint }; await _dbContext.InsertAsync(gainfoFingerprint); } else { - gainfoFingerprint.Fingerprint = fingerprint; + gainfoFingerprint.Fingerprint = item.Fingerprint; await _dbContext.UpdateAsync(gainfoFingerprint); } } - public async Task GetFingerprintAsync(string userCode, int? fingerNum) + var deviceCodes = await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(request.UserCode); + var devices = await _dbContext.Queryable() + .Where(x => x.IsElevatorAcs) + .Where(x => deviceCodes.Contains(x.Code)) + .ToListAsync(); + + await _dbContext.InsertAsync(devices.Select(x => new DeviceTaskEntity { - var gainfo = await _dbContext.Queryable() - .LeftJoin((x, dept) => x.DepartmentNo == dept.No) - .Where(x => x.Code == userCode) - .Select((x, dept) => new - { - x.Code, - x.Name, - x.DepartmentNo, - DepartmentName = dept.Name - }) - .FirstAsync(); - gainfo.ThrowIfNull("人员不存在"); + DeviceUniqueNo = x.UniqueNo, + Status = DeviceTaskStatusEnumType.Wait, + Type = DeviceTaskEnumType.Add, + DeviceType = x.Type, + UserNo = gainfo.No, + UserId = gainfo.Id, + Priority = 0, + Version = 0, + TenantId = x.TenantId, + Source = "指纹采集下发" + }).ToList()); + } - var fingerprins = await _dbContext.Queryable().Where(x => x.UserCode == userCode).OrderBy(x => x.FingerNum).ToListAsync(); - return new GainfoFingerprintResponse + public async Task GetFingerprintAsync(string userCode, int? fingerNum) + { + var gainfo = await _dbContext.Queryable() + .LeftJoin((x, dept) => x.DepartmentNo == dept.No) + .Where(x => x.Code == userCode) + .Select((x, dept) => new { - UserCode = gainfo.Code, - UserName = gainfo.Name, - DepartmentNo = gainfo.DepartmentNo, - DepartmentName = gainfo.DepartmentName, - Fingerprints = fingerprins.Select(x => new FingerprintResponse - { - FingerNum = x.FingerNum, - Fingerprint = x.Fingerprint - }).ToList() - }; - } + x.Code, + x.Name, + x.DepartmentNo, + DepartmentName = dept.Name + }) + .FirstAsync(); + gainfo.ThrowIfNull("人员不存在"); + + var fingerprins = await _dbContext.Queryable().Where(x => x.UserCode == userCode).OrderBy(x => x.FingerNum).ToListAsync(); + + return new GainfoFingerprintResponse + { + UserCode = gainfo.Code, + UserName = gainfo.Name, + DepartmentNo = gainfo.DepartmentNo, + DepartmentName = gainfo.DepartmentName, + Fingerprints = fingerprins.Select(x => new FingerprintResponse + { + FingerNum = x.FingerNum, + Fingerprint = x.Fingerprint + }).ToList() + }; } } diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs index f621f57c..8cd92d19 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs @@ -19,11 +19,15 @@ namespace Dlhis.Basic.Organization.Services.Implements; public class GainfoPermissionService : IGainfoPermissionService { private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService; + private readonly IDevicePermissionService _devicePermissionService; private readonly IDbContext _dbContext; - public GainfoPermissionService(IDevicePermissionAnalyzeService devicePermissionAnalyzeService, IDbContext dbContext) + public GainfoPermissionService(IDevicePermissionAnalyzeService devicePermissionAnalyzeService, + IDevicePermissionService devicePermissionService, + IDbContext dbContext) { _devicePermissionAnalyzeService = devicePermissionAnalyzeService; + _devicePermissionService = devicePermissionService; _dbContext = dbContext; } @@ -102,81 +106,81 @@ public class GainfoPermissionService : IGainfoPermissionService }).ToList()); } - public async Task> GetAssignDeviceCodes2Async(DeviceAssRequest request) - { - var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List { request.UserNo }); - if (!result.TryGetValue(request.UserNo, out var permissions)) - { - return new Pagination(); - } + //public async Task> GetAssignDeviceCodes2Async(DeviceAssRequest request) + //{ + // var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List { request.UserNo }); + // if (!result.TryGetValue(request.UserNo, out var permissions)) + // { + // return new Pagination(); + // } - var expandResult = await ExpandUserAssignDeviceCodesWithTypeInTenantAsync(result); + // var expandResult = await ExpandUserAssignDeviceCodesWithTypeInTenantAsync(result); - var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList(); + // var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList(); - var list = await _dbContext.Queryable() - .LeftJoin((d, dr) => d.UniqueNo == dr.DeviceUniqueNo) - .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search) - .Where(d => deviceCodes.Contains(d.Code)) - .Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true) - .ToListAsync(); + // var list = await _dbContext.Queryable() + // .LeftJoin((d, dr) => d.UniqueNo == dr.DeviceUniqueNo) + // .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search) + // .Where(d => deviceCodes.Contains(d.Code)) + // .Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true) + // .ToListAsync(); - var deviceGroups = await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync(); - var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department)) - ? await _dbContext.Queryable().Select(x => new { x.No, x.Name }).ToListAsync() - : new[] { new { No = string.Empty, Name = string.Empty } }.ToList(); + // var deviceGroups = await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync(); + // var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department)) + // ? await _dbContext.Queryable().Select(x => new { x.No, x.Name }).ToListAsync() + // : new[] { new { No = string.Empty, Name = string.Empty } }.ToList(); - var userGroups = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Group)) - ? await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync() - : new[] { new { Code = Guid.Empty, Name = string.Empty } }.ToList(); + // var userGroups = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Group)) + // ? await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync() + // : new[] { new { Code = Guid.Empty, Name = string.Empty } }.ToList(); - list.ForEach(item => - { - var permission = permissions.FirstOrDefault(x => x.Tenant == item.Factory); - if (permission != null) - { - item.Source = new DevicePermissionSourceDto - { - Factory = item.Factory, - Types = permission.AssignTypes.Where(x => x.Codes.Any(p => p == item.Code)).Select(x => - { - var codeDesc = x.UniqueType == DeviceAssignmentTypeEnumType.Device - ? item.DeployPlace - : deviceGroups.FirstOrDefault(p => p.Code == x.OriginalCodes.First())?.Name; + // list.ForEach(item => + // { + // var permission = permissions.FirstOrDefault(x => x.Tenant == item.Factory); + // if (permission != null) + // { + // item.Source = new DevicePermissionSourceDto + // { + // Factory = item.Factory, + // Types = permission.AssignTypes.Where(x => x.Codes.Any(p => p == item.Code)).Select(x => + // { + // var codeDesc = x.UniqueType == DeviceAssignmentTypeEnumType.Device + // ? item.DeployPlace + // : deviceGroups.FirstOrDefault(p => p.Code == x.OriginalCodes.First())?.Name; - var currentAssignValue = x.AssignValue; + // var currentAssignValue = x.AssignValue; - currentAssignValue = x.AssignType switch - { - AssignTypeEnumType.Department => departments.FirstOrDefault(p => p.No == x.AssignValue.Split(":")[1])?.Name, - AssignTypeEnumType.Group => userGroups.FirstOrDefault(p => p.Code == Guid.Parse(currentAssignValue))?.Name, - AssignTypeEnumType.User => "自己", - _ => currentAssignValue - }; + // currentAssignValue = x.AssignType switch + // { + // AssignTypeEnumType.Department => departments.FirstOrDefault(p => p.No == x.AssignValue.Split(":")[1])?.Name, + // AssignTypeEnumType.Group => userGroups.FirstOrDefault(p => p.Code == Guid.Parse(currentAssignValue))?.Name, + // AssignTypeEnumType.User => "自己", + // _ => currentAssignValue + // }; - var r = new DevicePermissionSourceTypeDto - { - AssignType = x.AssignType, - AssignValue = currentAssignValue, - UniqueType = x.UniqueType, - Codes = x.UniqueType == DeviceAssignmentTypeEnumType.Device - ? x.Codes.Where(p => p == item.Code) - : x.Codes.Take(1), - CodeDesc = codeDesc - }; + // var r = new DevicePermissionSourceTypeDto + // { + // AssignType = x.AssignType, + // AssignValue = currentAssignValue, + // UniqueType = x.UniqueType, + // Codes = x.UniqueType == DeviceAssignmentTypeEnumType.Device + // ? x.Codes.Where(p => p == item.Code) + // : x.Codes.Take(1), + // CodeDesc = codeDesc + // }; - return r; - }) - }; - } - }); + // return r; + // }) + // }; + // } + // }); - var page = list.Where(x => x.Type == request.DeviceType).ToPage(request); - page.Total = list.Where(x => x.Type == request.DeviceType).Count(); + // var page = list.Where(x => x.Type == request.DeviceType).ToPage(request); + // page.Total = list.Where(x => x.Type == request.DeviceType).Count(); - return page; - } + // return page; + //} private void HandleNext(Guid current, List links, List deviceGroups, List deviceGroupRelationships, List result) { @@ -270,99 +274,34 @@ public class GainfoPermissionService : IGainfoPermissionService } - // - // public async Task> GetAllUserAssignDeviceCodesAsync() - // { - // var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(null); - // - // var codes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList(); - // - // var page = await _dbContext.Queryable() - // .LeftJoin((d, dr) => d.UniqueNo == dr.DeviceUniqueNo) - // .WhereIF(request.DeviceType.HasValue, d => d.Type == request.DeviceType) - // .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search) - // .Where(d => codes.Contains(d.Code)) - // .Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true) - // .ToPageListAsync(request); - // - // var deviceGroups = await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync(); - // var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department)) - // ? await _dbContext.Queryable().Select(x => new { x.No, x.Name }).ToListAsync() - // : new[] { new { No = string.Empty, Name = string.Empty } }.ToList(); - // - // var userGroups = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Group)) - // ? await _dbContext.Queryable().Select(x => new { x.Code, x.Name }).ToListAsync() - // : new[] { new { Code = Guid.Empty, Name = string.Empty } }.ToList(); - // - // page.Data.ForEach(item => - // { - // var permission = permissions.FirstOrDefault(x => x.Tenant == item.Factory); - // if (permission != null) - // { - // item.Source = new DevicePermissionSourceDto - // { - // Factory = item.Factory, - // Types = permission.AssignTypes.Where(x => x.Codes.Any(p => p == item.Code)).Select(x => - // { - // var codeDesc = x.UniqueType == DeviceAssignmentTypeEnumType.Device - // ? item.DeployPlace - // : deviceGroups.FirstOrDefault(p => p.Code == x.OriginalCodes.First())?.Name; - // - // var currentAssignValue = x.AssignValue; - // - // currentAssignValue = x.AssignType switch - // { - // AssignTypeEnumType.Department => departments.FirstOrDefault(p => p.No == x.AssignValue.Split(":")[1])?.Name, - // AssignTypeEnumType.Group => userGroups.FirstOrDefault(p => p.Code == Guid.Parse(currentAssignValue))?.Name, - // AssignTypeEnumType.User => "自己", - // _ => currentAssignValue - // }; - // - // var r = new DevicePermissionSourceTypeDto - // { - // AssignType = x.AssignType, - // AssignValue = currentAssignValue, - // UniqueType = x.UniqueType, - // Codes = x.UniqueType == DeviceAssignmentTypeEnumType.Device - // ? x.Codes.Where(p => p == item.Code) - // : x.Codes.Take(1), - // CodeDesc = codeDesc - // }; - // - // return r; - // }) - // }; - // } - // }); - // - // return page; - // } - public async Task ResetPermissionsAsync(string userNo, DeviceTaskEnumType type, DeviceTypeEnumType? deviceType, List deviceCodes, string source) { var gainfo = await _dbContext.Queryable() - .Where(x => x.No == userNo) - .SingleAsync(); - - if (gainfo == null) - { - gainfo = await _dbContext.Queryable().AS("GainfoLeave") - .Where(x => x.No == userNo) - .SingleAsync(); - - if (gainfo != null) + .Where(x => x.No == userNo || x.Code == userNo) + .Select(x => new + { + UserId = x.Id, + UserNo = x.No, + UserCode = x.Code + }) + .SingleAsync() + ?? await _dbContext.Queryable() + .Where(x => x.No == userNo || x.Code == userNo) + .Select(x => new { - gainfo.Id = gainfo.IdentityUserId; - } - } + UserId = (long)x.IdentityUserId, + UserNo = x.No, + UserCode = x.Code + }) + .SingleAsync(); gainfo.ThrowIfNull("档案数据不存在"); //获取人员的设备权限 if (deviceCodes.IsEmpty()) { - deviceCodes = await _devicePermissionAnalyzeService.GetUserPermissionDeviceCodesAsync(gainfo!.No); + deviceCodes = await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(gainfo.UserCode); } var devices = await _dbContext.Queryable() @@ -372,11 +311,11 @@ public class GainfoPermissionService : IGainfoPermissionService var tasks = devices.Select(x => new DeviceTaskEntity { - UserId = gainfo!.Id, + UserId = gainfo!.UserId, + UserNo = userNo, DeviceUniqueNo = x.UniqueNo, Status = DeviceTaskStatusEnumType.Wait, Version = 0, - UserNo = userNo, Type = type, DeviceType = x.Type, TenantId = x.TenantId, diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoService.cs index 3c3a59e4..494e6269 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoService.cs @@ -313,7 +313,7 @@ public class GainfoService : IGainfoService await _dbContext.UpdateAsync(gainfo); - var deviceCodes = await _devicePermissionService.GetUserDeviceCodesByNoAsync(gainfo.No); + var deviceCodes = await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(gainfo.No); var dhDevices = await _dbContext.Queryable().LeftJoin((x, xt) => x.TemplateCode == xt.Code) .Where((x, xt) => xt.Name == "大华人脸机") diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs index d769b24b..d47a4ebe 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoUpdateService.cs @@ -365,7 +365,7 @@ public class GainfoUpdateService : IGainfoUpdateService } } } - else + else { await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Delete, null, null, "人员离职生成"); diff --git a/Src/Dlhis.Basic/Properties/PublishProfiles/FolderProfile.pubxml b/Src/Dlhis.Basic/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 00000000..19f43e16 --- /dev/null +++ b/Src/Dlhis.Basic/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,15 @@ + + + + + false + false + true + Release + Any CPU + FileSystem + bin\Release\net6.0\publish\ + FileSystem + <_TargetId>Folder + + \ No newline at end of file diff --git a/Src/Dlhis.Entity/Basic/Entity/DeviceEntity.cs b/Src/Dlhis.Entity/Basic/Entity/DeviceEntity.cs index 2a73f65f..dd01c55d 100644 --- a/Src/Dlhis.Entity/Basic/Entity/DeviceEntity.cs +++ b/Src/Dlhis.Entity/Basic/Entity/DeviceEntity.cs @@ -15,82 +15,82 @@ namespace Dlhis.Entity.Basic.Entity; public class DeviceEntity : SugarBaseEntity, ITenantId { /// - /// 设备code + /// 设备code /// [DeviceCode] [SugarColumn(IsNullable = false, ColumnDescription = "设备code")] public Guid Code { get; set; } /// - /// 设备唯一码,IP/MAC/SNO + /// 设备唯一码,IP/MAC/SNO /// [AdaptIgnore(MemberSide.Destination)] [SugarColumn(IsNullable = false, ColumnDescription = "设备唯一码", UniqueGroupNameList = new[] { "Idx_Device_UniqueNo" })] public string UniqueNo { get; set; } /// - /// 心跳间隔 + /// 心跳间隔 /// [SugarColumn(IsNullable = false, ColumnDescription = "心跳检查间隔")] public int HeartbeatInterval { get; set; } = 30; /// - /// 供应商 + /// 供应商 /// [SugarColumn(IsNullable = true, ColumnDescription = "供应商")] public string Supplier { get; set; } /// - /// 设备功能 + /// 设备功能 /// [SugarColumn(IsNullable = false, ColumnDataType = "varchar(150)", IsJson = true, ColumnDescription = "设备功能", DefaultValue = "[]")] public List Funcs { get; set; } /// - /// 设备类型 + /// 设备类型 /// [SugarColumn(IsNullable = false, ColumnDescription = "设备类型", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))] public DeviceTypeEnumType Type { get; set; } /// - /// 部署点 + /// 部署点 /// [SugarColumn(IsNullable = true, ColumnDescription = "部署点")] public string DeployPlace { get; set; } /// - /// 部署坐标 + /// 部署坐标 /// [SugarColumn(IsNullable = true, ColumnDescription = "部署坐标")] public string DeployPosition { get; set; } /// - /// 部署方向 + /// 部署方向 /// [SugarColumn(IsNullable = true, ColumnDescription = "部署方向", ColumnDataType = "varchar(50)", SqlParameterDbType = typeof(EnumToStringConvert))] public DeviceDeployDirectionEnumType? DeployDirection { get; set; } /// - /// 模板code + /// 模板code /// [SugarColumn(IsNullable = false, ColumnDescription = "模板code")] public Guid TemplateCode { get; set; } /// - /// 连接信息 + /// 连接信息 /// [SugarColumn(IsNullable = false, ColumnDescription = "连接信息")] public string ConnectionInfo { get; set; } /// - /// 是否启用 + /// 是否启用 /// [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用")] public bool Enable { get; set; } /// - /// 是否作为考勤设备 + /// 是否作为考勤设备 /// [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否作为考勤设备")] public bool AsAttendance { get; set; } @@ -102,5 +102,11 @@ public class DeviceEntity : SugarBaseEntity, ITenantId [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否启用时段码", DefaultValue = "0")] public bool EnableTimePeriod { get; set; } = false; + /// + /// 是否为电梯门禁设备 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "bit", ColumnDescription = "是否为电梯门禁设备", DefaultValue = "0")] + public bool IsElevatorAcs { get; set; } + public string TenantId { get; set; } } \ No newline at end of file diff --git a/Src/Dlhis.Entity/Basic/Entity/ElevatorAcsFloorEntity.cs b/Src/Dlhis.Entity/Basic/Entity/ElevatorAcsFloorEntity.cs new file mode 100644 index 00000000..6eae44e6 --- /dev/null +++ b/Src/Dlhis.Entity/Basic/Entity/ElevatorAcsFloorEntity.cs @@ -0,0 +1,37 @@ +using Kurisu.AspNetCore.DataAccess.Entity; +using Kurisu.AspNetCore.DataAccess.SqlSugar; +using SqlSugar; + +namespace Dlhis.Entity.Basic.Entity; + +/// +/// 梯控楼层对照表 +/// +[SugarTable(TableName = "ElevatorAcsFloors", TableDescription = "梯控楼层对照表")] +public class ElevatorAcsFloorEntity : SugarBaseEntity, ITenantId +{ + /// + /// 实际设备唯一编号 + /// + public string RealDeviceUniqueNo { get; set; } + + /// + /// 虚拟设备唯一编号,用于区分同一设备的不同楼层 + /// + public string VirtualDeviceUniqueNo { get; set; } + + /// + /// 关联的控制器对应的值(access control system value) + /// + public int AcsValue { get; set; } + + /// + /// 楼层名称 + /// + public string FloorName { get; set; } + + /// + /// 租户ID + /// + public string TenantId { get; set; } +} diff --git a/Src/Dlhis.Entity/Basic/Entity/GainfoCardRecordEntity.cs b/Src/Dlhis.Entity/Basic/Entity/GainfoCardRecordEntity.cs index ccef0313..a98ebed4 100644 --- a/Src/Dlhis.Entity/Basic/Entity/GainfoCardRecordEntity.cs +++ b/Src/Dlhis.Entity/Basic/Entity/GainfoCardRecordEntity.cs @@ -8,24 +8,24 @@ namespace Dlhis.Entity.Basic.Entity public class GainfoCardRecordEntity : SugarBaseEntity, ITenantId { /// - /// 档案No + /// 档案No /// [SugarColumn(IsNullable = false, IndexGroupNameList = new[] { "idx_GainfoCardRecord_userno" })] public string UserNo { get; set; } /// - /// 卡号 + /// 卡号 /// [SugarColumn(IsNullable = false)] public string CardNo { get; set; } /// - /// 更换原因 + /// 更换原因 /// public string Reason { get; set; } /// - /// 备注 + /// 备注 /// public string Remark { get; set; } diff --git a/Src/Dlhis.Entity/Basic/Entity/GainfoFingerprintEntity.cs b/Src/Dlhis.Entity/Basic/Entity/GainfoFingerprintEntity.cs index 22b7c837..d4399293 100644 --- a/Src/Dlhis.Entity/Basic/Entity/GainfoFingerprintEntity.cs +++ b/Src/Dlhis.Entity/Basic/Entity/GainfoFingerprintEntity.cs @@ -25,5 +25,6 @@ public class GainfoFingerprintEntity : SugarBaseEntity /// /// 指纹数据 /// + [SugarColumn(ColumnDataType = "text")] public string Fingerprint { get; set; } } diff --git a/Src/Dlhis.ScheduledTask/Jobs/ExternalAutoResignJob.cs b/Src/Dlhis.ScheduledTask/Jobs/ExternalAutoResignJob.cs index bde769c7..36bd4e15 100644 --- a/Src/Dlhis.ScheduledTask/Jobs/ExternalAutoResignJob.cs +++ b/Src/Dlhis.ScheduledTask/Jobs/ExternalAutoResignJob.cs @@ -4,9 +4,9 @@ using Dlhis.ScheduledTask.Attributes; namespace Dlhis.ScheduledTask.Jobs; /// -/// 自动处理外包离职 +/// 自动处理访客离场 /// -[RecurringJob(nameof(ExternalAutoResignJob), "0 0 1 1/1 * ? ", Name = "自动处理外部人员离职")] +[RecurringJob(nameof(ExternalAutoResignJob), "0 0 1 1/1 * ? ", Name = "自动处理访客离场")] public class ExternalAutoResignJob : BaseJob { private readonly ISyncApi _syncApi; diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs index 2cf21b14..7dd08c04 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs @@ -51,6 +51,32 @@ public class AssignmentsController : ControllerBase return await _assignmentService.DeviceGetTasksAsync(request); } + /// + /// 获取设备任务关联人员 + /// + /// + [DisableApiLog] + [IgnoreTenant] + [AllowAnonymous] + [HttpGet("elevatorAcs/users")] + public async Task> ElevatorAcsDeviceGetTasksGetAsync(string deviceUniqueNo) + { + return await _assignmentService.ElevatorAcsDeviceGetTasksGetAsync(deviceUniqueNo); + } + + + /// + /// 获取设备任务 + /// + /// + [DisableApiLog] + [IgnoreTenant] + [HttpGet("elevatorAcs/users/deviceTasks")] + public async Task ElevatorAcsDeviceGetTasksGetAsync(string deviceUniqueNo, string userCode) + { + return await _assignmentService.GetElevatorAcsTaskAsync(deviceUniqueNo, userCode); + } + /// /// 获取设备任务(按设备) /// @@ -71,6 +97,7 @@ public class AssignmentsController : ControllerBase [DisableApiLog] [HttpPost("taskStatus")] [IgnoreTenant] + [Transactional] public async Task UpdateDeviceTaskStatusAsync(List requests) { await _assignmentService.UpdateDeviceTaskStatusAsync(requests); @@ -262,9 +289,9 @@ public class AssignmentsController : ControllerBase { x.OpeningBalance = Math.Abs(x.OpeningBalance); x.CostAmount = Math.Abs(x.CostAmount); - x.ClosingAmount = Math.Abs(x.ClosingAmount); + x.ClosingAmount = Math.Abs(x.ClosingAmount); }); - + return page; } @@ -293,12 +320,12 @@ public class AssignmentsController : ControllerBase catch (Exception) { } - + page.Data.ForEach(x => { x.OpeningBalance = Math.Abs(x.OpeningBalance); x.CostAmount = Math.Abs(x.CostAmount); - x.ClosingAmount = Math.Abs(x.ClosingAmount); + x.ClosingAmount = Math.Abs(x.ClosingAmount); }); return page; diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Dto/ElevatorTaskDto.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Dto/ElevatorTaskDto.cs new file mode 100644 index 00000000..72c4932a --- /dev/null +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Dto/ElevatorTaskDto.cs @@ -0,0 +1,7 @@ +namespace Dlhis.ScheduledTask.TaskAssignment.Dto +{ + public class ElevatorTaskDto : TaskDto + { + public int FloorAcsValue { get; set; } + } +} diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Response/ElevatorAcsDeviceTaskToDeviceByDeviceResponse.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Response/ElevatorAcsDeviceTaskToDeviceByDeviceResponse.cs new file mode 100644 index 00000000..f4ef8dc9 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Response/ElevatorAcsDeviceTaskToDeviceByDeviceResponse.cs @@ -0,0 +1,16 @@ +namespace Dlhis.ScheduledTask.TaskAssignment.Response; + +public class ElevatorAcsDeviceTaskToDeviceByDeviceResponse +{ + public string DeviceUniqueNo { get; set; } + + public long? UserId { get; set; } + public string UserCode { get; set; } + public string UserName { get; set; } + + public string CardNo { get; set; } + + public List TaskIds { get; set; } + + public List FloorAcsValues { get; set; } +} diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/IAssignmentService.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/IAssignmentService.cs index e8f07cd5..970ac92f 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/IAssignmentService.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/IAssignmentService.cs @@ -12,6 +12,10 @@ public interface IAssignmentService : IScopeDependency /// Task> DeviceGetTasksAsync(DeviceTaskGetRequest request); + + Task> ElevatorAcsDeviceGetTasksGetAsync(string userCode); + Task GetElevatorAcsTaskAsync(string deviceUniqueNo, string userCode); + /// /// 更新设备任务状态 /// diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs index 1c727860..540f6962 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs @@ -1,19 +1,20 @@ -using Dlhis.Entity.Basic.Entity; +using Dlhis.Basic.Api; +using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Dlhis.Entity.Device.Models; +using Dlhis.ScheduledTask.TaskAssignment.Dto; using Dlhis.ScheduledTask.TaskAssignment.Request; using Dlhis.ScheduledTask.TaskAssignment.Response; -using Kurisu.AspNetCore.Utils.Extensions; -using Microsoft.AspNetCore.DataProtection; -using Microsoft.Extensions.Options; -using SqlSugar; -using Newtonsoft.Json.Linq; -using Kurisu.AspNetCore.Utils; -using Dlhis.ScheduledTask.TaskAssignment.Dto; using Kurisu.AspNetCore.CustomClass; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; +using Kurisu.AspNetCore.Utils; +using Kurisu.AspNetCore.Utils.Extensions; +using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Options; +using Newtonsoft.Json.Linq; +using SqlSugar; namespace Dlhis.ScheduledTask.TaskAssignment.Services.Implements; @@ -22,6 +23,7 @@ public class AssignmentService : IAssignmentService private readonly IDbContext _dbContext; private readonly IDataProtectionProvider _dataProtectionProvider; private readonly IMemoryCache _memoryCache; + private readonly IGainfoPermissionApi _gainfoPermissionApi; private readonly ILogger _logger; private readonly FaceUrlOptions _faceUrlOptions; @@ -29,11 +31,13 @@ public class AssignmentService : IAssignmentService IDataProtectionProvider dataProtectionProvider, IOptions options, IMemoryCache memoryCache, + IGainfoPermissionApi gainfoPermissionApi, ILogger logger) { _dbContext = dbContext; _dataProtectionProvider = dataProtectionProvider; _memoryCache = memoryCache; + _gainfoPermissionApi = gainfoPermissionApi; _logger = logger; _faceUrlOptions = options.Value; } @@ -85,60 +89,60 @@ public class AssignmentService : IAssignmentService case DeviceTaskEnumType.Add: case DeviceTaskEnumType.Delete: case DeviceTaskEnumType.Update: - { - dto.Info = new UserToDeviceResponse { - UserId = item.UserId ?? 0, - UserNo = item.UserNo, - OldUserNo = item.OldUserNo, - UserCode = item.UserCode, - UserType = item.UserType, - UserName = item.UserName, - DepartmentNo = item.DepartmentNo, - DepartmentName = item.DepartmentName, - CardNo = item.CardNo, - Face = item.Type == DeviceTaskEnumType.Delete ? string.Empty : _faceUrlOptions.Url + token, //下发才附带照片信息 - ValidDatePeriod = new DatePeriod + dto.Info = new UserToDeviceResponse { - Start = DateOnly.FromDateTime(DateTime.Now.AddDays(-1)), - End = DateOnly.FromDateTime(DateTime.Now.AddYears(20)), - }, - TimePeriods = new List { item.CardType }, - Doors = new List { 1 }, - Schedule = 1 - }; - } + UserId = item.UserId ?? 0, + UserNo = item.UserNo, + OldUserNo = item.OldUserNo, + UserCode = item.UserCode, + UserType = item.UserType, + UserName = item.UserName, + DepartmentNo = item.DepartmentNo, + DepartmentName = item.DepartmentName, + CardNo = item.CardNo, + Face = item.Type == DeviceTaskEnumType.Delete ? string.Empty : _faceUrlOptions.Url + token, //下发才附带照片信息 + ValidDatePeriod = new DatePeriod + { + Start = DateOnly.FromDateTime(DateTime.Now.AddDays(-1)), + End = DateOnly.FromDateTime(DateTime.Now.AddYears(20)), + }, + TimePeriods = new List { item.CardType }, + Doors = new List { 1 }, + Schedule = 1 + }; + } break; case DeviceTaskEnumType.TimePeriodAdd: case DeviceTaskEnumType.TimePeriodDelete: - { - dto.Info = item.TimePeriods.Select(t => new TimePeriodToDeviceResponse { - Id = t.CardType, - Name = t.CardName, - Enable = item.EnableTimePeriod, - Sections = t.Sections.Select(s => new TimePeriodWeekDaySection + dto.Info = item.TimePeriods.Select(t => new TimePeriodToDeviceResponse { - DayOfWeek = s.DayOfWeek, - TimePeriods = s.TimePeriods.Select(p => new DeviceTimePeriod + Id = t.CardType, + Name = t.CardName, + Enable = item.EnableTimePeriod, + Sections = t.Sections.Select(s => new TimePeriodWeekDaySection { - Enable = p.Enable, - Start = p.Enable ? p.Start : new TimeOnly(00, 00, 00), - End = p.Enable ? p.End : new TimeOnly(00, 00, 00) + DayOfWeek = s.DayOfWeek, + TimePeriods = s.TimePeriods.Select(p => new DeviceTimePeriod + { + Enable = p.Enable, + Start = p.Enable ? p.Start : new TimeOnly(00, 00, 00), + End = p.Enable ? p.End : new TimeOnly(00, 00, 00) + }).ToList() }).ToList() - }).ToList() - }); - } + }); + } break; case DeviceTaskEnumType.ScheduleAdd: case DeviceTaskEnumType.ScheduleDelete: - { - dto.Info = new ScheduleToDeviceResponse { - Id = 1, - Enable = true, - Name = "计划的一部分", - Schedules = new List + dto.Info = new ScheduleToDeviceResponse + { + Id = 1, + Enable = true, + Name = "计划的一部分", + Schedules = new List { new() { @@ -150,22 +154,22 @@ public class AssignmentService : IAssignmentService } } } - }; - } + }; + } break; case DeviceTaskEnumType.UploadRecord: - { - var timeInfo = deviceUploadInfos[d]; - dto.Info = new UploadRecordToDeviceResponse { - DateTimePeriod = new DateTimePeriod + var timeInfo = deviceUploadInfos[d]; + dto.Info = new UploadRecordToDeviceResponse { - Start = timeInfo.Item1, - End = timeInfo.Item2 - } - }; - } + DateTimePeriod = new DateTimePeriod + { + Start = timeInfo.Item1, + End = timeInfo.Item2 + } + }; + } break; } @@ -175,6 +179,109 @@ public class AssignmentService : IAssignmentService }).ToList(); } + public async Task> ElevatorAcsDeviceGetTasksGetAsync(string deviceUniqueNo) + { + //梯控 + var elevatorAcsFloors = await _dbContext.Queryable().Where(x => x.RealDeviceUniqueNo == deviceUniqueNo).ToListAsync(); + if (elevatorAcsFloors.IsPresent()) + { + var virtualDeviceUniqueNos = elevatorAcsFloors.Select(x => x.VirtualDeviceUniqueNo).ToList(); + + var allUserNos = new List(); + foreach (var virtualDeviceUniqueNo in virtualDeviceUniqueNos) + { + var userNos = await _dbContext.Queryable().Where(x => x.DeviceUniqueNo == virtualDeviceUniqueNo) + .Where(x => (x.Status == DeviceTaskStatusEnumType.Wait || x.Status == DeviceTaskStatusEnumType.Process) && !string.IsNullOrEmpty(x.UserNo)) + .OrderBy(x => new { x.Id, Priority = SqlFunc.Desc(x.Priority) }) + .Select(x => x.UserNo) + .ToListAsync(); + allUserNos.AddRange(userNos); + } + allUserNos = allUserNos.Distinct().ToList(); + if (allUserNos.IsEmpty()) + { + return new List(); + } + var userCodes = await _dbContext.Queryable().Where(x => allUserNos.Contains(x.No)).Select(x => x.Code).ToListAsync(); + var leaveUserCodes = await _dbContext.Queryable().Where(x => allUserNos.Contains(x.No)).Select(x => x.Code).ToListAsync(); + return userCodes.Concat(leaveUserCodes).Distinct().ToList(); + } + + return new List(); + } + + public async Task GetElevatorAcsTaskAsync(string deviceUniqueNo, string userCode) + { + var elevatorAcsFloors = await _dbContext.Queryable().Where(x => x.RealDeviceUniqueNo == deviceUniqueNo).ToListAsync(); + var virtualDeviceUniqueNos = elevatorAcsFloors.Select(x => x.VirtualDeviceUniqueNo).ToList(); + + var deviceCodes = await _gainfoPermissionApi.GetPermissionsDeviceCodesAsync(userCode); + var floorAcsValues = await _dbContext.Queryable() + .LeftJoin((x, f) => x.UniqueNo == f.VirtualDeviceUniqueNo) + .Where(x => virtualDeviceUniqueNos.Contains(x.UniqueNo) && deviceCodes.Contains(x.Code)) + .Select((x, f) => f.AcsValue) + .ToListAsync(); + + + var all = await CommonHelper.RetryAsync(async () => + { + var internalAll = new List(); + + foreach (var virtualDeviceUniqueNo in virtualDeviceUniqueNos) + { + var taskIds = await _dbContext.Queryable() + .LeftJoin((deviceTask, device) => deviceTask.DeviceUniqueNo == device.UniqueNo) + .Where((deviceTask, device) => deviceTask.DeviceUniqueNo == virtualDeviceUniqueNo) + .Where((deviceTask, device) => device.Enable) + .Where(deviceTask => (deviceTask.Status == DeviceTaskStatusEnumType.Wait || deviceTask.Status == DeviceTaskStatusEnumType.Process) && !string.IsNullOrEmpty(deviceTask.UserNo)) + .OrderBy(deviceTask => new { deviceTask.Id, Priority = SqlFunc.Desc(deviceTask.Priority) }) + .Select((deviceTask, device) => deviceTask.Id) + .ToListAsync(); + + internalAll.AddRange(taskIds); + } + + var effect = await _dbContext.Updateable() + .SetColumns(x => x.Status == DeviceTaskStatusEnumType.Process) + .SetColumns(x => x.ModifiedTime == DateTime.Now) + .Where(x => internalAll.Contains(x.Id) && (x.Status == DeviceTaskStatusEnumType.Wait || x.Status == DeviceTaskStatusEnumType.Process)) + .ExecuteCommandAsync(); + + if (effect != internalAll.Count) + { + throw new VersionExceptions("version exceptions"); + } + + return internalAll; + + }, 5); + + if (!all.Any()) + { + return null; + } + + var userInfo = await _dbContext.Queryable() + .Where(x => x.Code == userCode) + .Select(x => new { UserId = x.Id, UserCode = x.Code, UserName = x.Name, x.CardNo }) + .FirstAsync() + ?? + await _dbContext.Queryable() + .Where(x => x.Code == userCode) + .Select(x => new { UserId = x.Id, UserCode = x.Code, UserName = x.Name, x.CardNo }) + .FirstAsync(); + + return new ElevatorAcsDeviceTaskToDeviceByDeviceResponse + { + DeviceUniqueNo = deviceUniqueNo, + UserId = userInfo.UserId, + UserCode = userInfo.UserCode, + CardNo = userInfo.CardNo, + UserName = userInfo.UserName, + FloorAcsValues = floorAcsValues, + TaskIds = all + }; + } public async Task UpdateDeviceTaskStatusAsync(List requests) {