Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/GainfoPermissionService.cs
2024-12-07 10:41:08 +08:00

226 lines
8.6 KiB
C#

using Dlhis.Basic.Device.Request;
using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Basic.Places.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 IGainfoStore _gainfoStore;
private readonly IDevicePermissionService _devicePermissionService;
private readonly IDbContext _dbContext;
public GainfoPermissionService(IGainfoStore gainfoStore, IDevicePermissionService devicePermissionService, IDbContext dbContext)
{
_gainfoStore = gainfoStore;
_devicePermissionService = devicePermissionService;
_dbContext = dbContext;
}
public async Task<Pagination<BusinessPlaceResponse>> GetPlaceByGainfoAsync(DeviceAssRequest request)
{
var gainfo = await _gainfoStore.Queryable()
.Where(x => x.No == request.UserNo)
.FirstAsync();
if (gainfo == null)
{
return new Pagination<BusinessPlaceResponse>();
}
//用户所有deviceCode权限
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(gainfo.Code);
return await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((x, dr) => x.UniqueNo == dr.DeviceUniqueNo)
.LeftJoin<BusinessPlaceEntity>((x, dr, place) => dr.BusinessPlaceCode == place.Code)
.LeftJoin<BusinessPlaceShiftRelationshipEntity>((x, dr, place, ps) => place.Code == ps.PlaceCode)
.LeftJoin<BusinessShiftEntity>((x, dr, place, ps, s) => s.Code == ps.ShiftCode)
.Where(x => x.Type == DeviceTypeEnumType.Meal)
.Where(x => deviceCodes.Contains(x.Code))
.Where((x, dr) => dr.BusinessPlaceCode.HasValue)
.Select((x, dr, place, ps, s) => new BusinessPlaceResponse
{
Id = place.Id,
Code = place.Code,
Status = place.Status,
Name = place.Name,
Catalog = place.Catalog,
CustomCode = place.CustomCode,
Source = place.Source,
ShiftCode = s.Code,
ShiftName = s.Name
})
.Distinct()
.ToPageListAsync(request);
}
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 = RecursionGetNext(currentGroup, deviceGroups).ToList();
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<DevicePageResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
{
var ids = await _devicePermissionService.GetAssignDeviceCodesAsync(request.UserNo);
var page = await _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)
.WhereIF(request.DeviceType.HasValue, d => d.Type == request.DeviceType)
.Where(d => ids.Contains(d.Code))
.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
}, true)
.ToPageListAsync(request);
return page;
}
public async Task ResetPermissionsAsync(string userNo, DeviceTaskEnumType type, List<Guid> deviceCodes)
{
var gainfo = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.No == userNo)
.SingleAsync();
var msg = gainfo == null ? "不存在职数据" : string.Empty;
//如果找不到就为离职人员
if (gainfo == null)
{
gainfo = await _dbContext.Queryable<GainfoEntity>().AS("GainfoLeave")
.Where(x => x.No == userNo)
.SingleAsync();
if (gainfo != null)
{
gainfo.Id = gainfo.IdentityUserId;
}
msg += gainfo == null ? "并且不存在离职数据" : string.Empty;
}
gainfo.ThrowIfNull($"档案数据有误:{msg}");
//获取人员的设备权限
if (deviceCodes.IsEmpty())
{
deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(gainfo!.Code);
}
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 = DeviceTaskSourceEnumType.Manually
}).ToList();
await _dbContext.InsertAsync(tasks);
}
public async Task<List<DeviceGroupTreeResponse>> GetAssignGroupAsync(string userNo)
{
//(DeviceAssignmentTypeEnumType.Group, AssignTypeEnumType.User, userNo
var groups = await _devicePermissionService.GetAssignmentsAsync(new DeviceGetAssignmentsRequest
{
UniqueType = DeviceAssignmentTypeEnumType.Group,
AssignType = AssignTypeEnumType.User,
AssignValue = userNo
});
return await _dbContext.Queryable<DeviceGroupEntity>()
.Where(x => groups.Contains(x.Code))
.Select<DeviceGroupTreeResponse>()
.ToListAsync();
}
//**********************************************************************************************************************************************
private static IEnumerable<Guid> RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups)
{
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
foreach (var item in next)
{
RecursionGetNext(item, deviceGroups);
}
yield return current.Code;
}
}