Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs
2026-03-13 15:42:36 +08:00

332 lines
15 KiB
C#

using Dlhis.Basic.Device.Dtos;
using Dlhis.Basic.Device.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 Dlhis.Entity.Sys.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using SqlSugar;
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,
IDevicePermissionService devicePermissionService,
IDbContext dbContext)
{
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
_devicePermissionService = devicePermissionService;
_dbContext = dbContext;
}
/// <inheritdoc />
public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodesAsync(DeviceAssUserCodeRequest request)
{
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == request.UserCode).SingleAsync();
gainfo.ThrowIfNull($"{request.UserCode}人员不存在");
return await GetAssignDeviceCodesAsync(new DeviceAssRequest
{
PageIndex = request.PageIndex,
PageSize = request.PageSize,
Search = request.Search,
UserNo = gainfo.No,
DeviceType = request.DeviceType
});
}
public async Task<Dictionary<string, List<TenantAssignTypeDeviceCodeExpanded>>> ExpandUserAssignDeviceCodesWithTypeInTenantAsync(Dictionary<string, List<TenantAssignTypeDeviceCode>> values)
{
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
var departments = await _dbContext.Queryable<DepartmentEntity>().Select(x => new { x.No, x.Name }).ToListAsync();
var userGroups = await _dbContext.Queryable<GainfoGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync();
var deviceGroupRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
return values.ToDictionary(x => x.Key, x => x.Value.SelectMany(item =>
{
return item.AssignTypes.SelectMany(p =>
{
if (p.UniqueType == DeviceAssignmentTypeEnumType.Group)
{
var currentDeviceGroupCode = p.OriginalCodes.First();
var link = new List<Guid> { currentDeviceGroupCode };
var r = deviceGroupRelationships.Where(x => x.GroupCode == currentDeviceGroupCode).Select(x => new TenantAssignTypeDeviceCodeExpanded
{
Tenant = item.Tenant,
AssignType = p.AssignType,
AssignValue = p.AssignValue,
UniqueType = p.UniqueType,
DeviceCode = x.DeviceCode,
DeviceGroupCodes = link
}).ToList();
HandleNext(currentDeviceGroupCode, link, deviceGroups, deviceGroupRelationships, r);
r.ForEach(x =>
{
x.Tenant = item.Tenant;
x.AssignType = p.AssignType;
x.AssignValue = p.AssignValue;
x.UniqueType = p.UniqueType;
});
return r;
}
if (p.UniqueType == DeviceAssignmentTypeEnumType.Device)
{
return p.Codes.Select(x => new TenantAssignTypeDeviceCodeExpanded()
{
Tenant = item.Tenant,
AssignType = p.AssignType,
AssignValue = p.AssignValue,
UniqueType = p.UniqueType,
DeviceCode = x
}).ToList();
}
return new List<TenantAssignTypeDeviceCodeExpanded>();
}).ToList();
}).ToList());
}
//public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodes2Async(DeviceAssRequest request)
//{
// var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { request.UserNo });
// if (!result.TryGetValue(request.UserNo, out var permissions))
// {
// return new Pagination<GainfoDevicePermissionResponse>();
// }
// var expandResult = await ExpandUserAssignDeviceCodesWithTypeInTenantAsync(result);
// var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
// var list = await _dbContext.Queryable<DeviceEntity>()
// .LeftJoin<DeviceRegistryEntity>((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<DeviceGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync();
// var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department))
// ? await _dbContext.Queryable<DepartmentEntity>().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<GainfoGroupEntity>().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;
// 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;
// })
// };
// }
// });
// var page = list.Where(x => x.Type == request.DeviceType).ToPage(request);
// page.Total = list.Where(x => x.Type == request.DeviceType).Count();
// return page;
//}
private void HandleNext(Guid current, List<Guid> links, List<DeviceGroupEntity> deviceGroups, List<DeviceGroupRelationshipEntity> deviceGroupRelationships, List<TenantAssignTypeDeviceCodeExpanded> result)
{
var items = deviceGroups.Where(x => x.PCode == current).ToList();
foreach (var item in items)
{
var newLinks = links.ToList();
newLinks.Add(item.Code);
var deviceCodes = deviceGroupRelationships.Where(x => x.GroupCode == item.Code).ToList();
if (deviceCodes.Any())
{
result.AddRange(deviceCodes.Select(x => new TenantAssignTypeDeviceCodeExpanded
{
DeviceCode = x.DeviceCode,
DeviceGroupCodes = newLinks
}).ToList());
}
HandleNext(item.Code, newLinks, deviceGroups, deviceGroupRelationships, result);
}
}
public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
{
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { request.UserNo });
if (!result.TryGetValue(request.UserNo, out var permissions))
{
return new Pagination<GainfoDevicePermissionResponse>();
}
var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((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 => deviceCodes.Contains(d.Code))
.Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true)
.ToPageListAsync(request);
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync();
var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department))
? await _dbContext.Queryable<DepartmentEntity>().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<GainfoGroupEntity>().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<DevicePermissionAssignResponse> ResetPermissionsAsync(string userNo, DeviceTaskEnumType type, DeviceTypeEnumType? deviceType, List<Guid> deviceCodes, string source)
{
var gainfo = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.No == userNo || x.Code == userNo)
.Select(x => new
{
UserId = x.Id,
UserNo = x.No,
UserCode = x.Code
})
.SingleAsync()
?? await _dbContext.Queryable<GainfoLeaveEntity>()
.Where(x => x.No == userNo || x.Code == userNo)
.Select(x => new
{
UserId = (long)x.IdentityUserId,
UserNo = x.No,
UserCode = x.Code
})
.SingleAsync();
gainfo.ThrowIfNull("档案数据不存在");
//获取人员的设备权限
if (deviceCodes.IsEmpty())
{
deviceCodes = await _devicePermissionService.GetUserDeviceCodesByNoOrCodeAsync(gainfo.UserCode);
}
var devices = await _dbContext.Queryable<DeviceEntity>()
.WhereIF(deviceType.HasValue, x => x.Type == deviceType)
.Where(x => deviceCodes.Contains(x.Code))
.ToListAsync();
var tasks = devices.Select(x => new DeviceTaskEntity
{
UserId = gainfo!.UserId,
UserNo = userNo,
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Version = 0,
Type = type,
DeviceType = x.Type,
TenantId = x.TenantId,
Priority = 10,
Source = source,
}).ToList();
await _dbContext.InsertAsync(tasks);
var addCount = type == DeviceTaskEnumType.Add ? tasks.Count : 0;
var deleteCount = type == DeviceTaskEnumType.Delete ? tasks.Count : 0;
return new DevicePermissionAssignResponse(addCount, deleteCount);
}
}