Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs

195 lines
8.2 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.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Basic.Organization.Services.Implements;
public class GainfoPermissionService : IGainfoPermissionService
{
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
private readonly IDbContext _dbContext;
public GainfoPermissionService(IDevicePermissionAnalyzeService devicePermissionAnalyzeService, IDbContext dbContext)
{
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
_dbContext = dbContext;
}
public async Task<List<GetDevicesUnderGroupByUserNoResponse>> GetDevicesUnderGroupByUserNoAsync(string userNo, Guid deviceGroupCode)
{
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
var currentGroup = deviceGroups.FirstOrDefault(x => x.Code == deviceGroupCode);
if (currentGroup == null)
{
return new List<GetDevicesUnderGroupByUserNoResponse>();
}
var groups = RecursionHelper.HandlerDeviceGroupNextCodes(currentGroup.Code, deviceGroups);
return groups.Select(groupCode =>
{
var nos = _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.LeftJoin<DeviceEntity>((x, d) => x.DeviceCode == d.Code)
.Where(x => groupCode == x.GroupCode)
.Select((x, d) => d.UniqueNo)
.ToList();
var page = _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((d, dt) => d.TemplateCode == dt.Code)
.LeftJoin<DeviceRegistryEntity>((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code)
.Where(d => nos.Contains(d.UniqueNo))
.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,
UniqueNo = d.UniqueNo,
Supplier = d.Supplier,
DeployPlace = d.DeployPlace,
DeployPosition = d.DeployPosition,
DeployDirection = d.DeployDirection,
TemplateCode = dt.Code,
ConnectionInfo = d.ConnectionInfo,
Enable = d.Enable,
})
.ToList();
return new GetDevicesUnderGroupByUserNoResponse
{
GroupCode = groupCode,
Devices = page
};
}).ToList();
}
public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
{
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { request.UserNo });
var permissions = result[request.UserNo].ToList();
var codes = 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 => codes.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.First(x => x.Tenant == item.Factory);
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.First(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, List<Guid> deviceCodes, string source)
{
var gainfo = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.No == userNo)
.SingleAsync();
if (gainfo == null)
{
gainfo = await _dbContext.Queryable<GainfoEntity>().AS("GainfoLeave")
.Where(x => x.No == userNo)
.SingleAsync();
if (gainfo != null)
{
gainfo.Id = gainfo.IdentityUserId;
}
}
gainfo.ThrowIfNull("档案数据不存在");
//获取人员的设备权限
if (deviceCodes.IsEmpty())
{
deviceCodes = await _devicePermissionAnalyzeService.GetAssignDeviceCodesAsync(gainfo!.No);
}
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceCodes.Contains(x.Code)).ToListAsync();
var tasks = devices.Select(x => new DeviceTaskEntity
{
UserId = gainfo!.Id,
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
Version = 0,
UserNo = userNo,
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);
}
}