feat 任务下发生成
This commit is contained in:
@@ -23,7 +23,7 @@ public class DeviceGroupsController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设备分组分页
|
/// 设备组分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@@ -34,7 +34,7 @@ public class DeviceGroupsController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取设备分组树
|
/// 获取设备组树
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="showDevices">是否显示设备</param>
|
/// <param name="showDevices">是否显示设备</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|||||||
@@ -38,4 +38,16 @@ public class DevicePermissionsController : ControllerBase
|
|||||||
{
|
{
|
||||||
return await _devicePermissionService.GetAssignmentsAsync(request.UserNo, request.UniqueType);
|
return await _devicePermissionService.GetAssignmentsAsync(request.UserNo, request.UniqueType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重置
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("reset")]
|
||||||
|
public async Task ResetAssignmentsAsync(DeviceResetAssignmentsRequest request)
|
||||||
|
{
|
||||||
|
await _devicePermissionService.ResetAssignmentsAsync(request);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Device.Request;
|
||||||
|
|
||||||
|
public class DeviceResetAssignmentsRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 人员档案号
|
||||||
|
/// </summary>
|
||||||
|
[Required(ErrorMessage = "人员不能为空")]
|
||||||
|
public string UserNo { get; set; }
|
||||||
|
}
|
||||||
@@ -19,4 +19,20 @@ public interface IDevicePermissionService : IScopeDependency
|
|||||||
/// <param name="uniqueType"></param>
|
/// <param name="uniqueType"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<List<Guid>> GetAssignmentsAsync(string userNo, DeviceAssignmentTypeEnumType? uniqueType);
|
Task<List<Guid>> GetAssignmentsAsync(string userNo, DeviceAssignmentTypeEnumType? uniqueType);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 重置分配
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task ResetAssignmentsAsync(DeviceResetAssignmentsRequest request);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 生成下发任务
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="func"></param>
|
||||||
|
/// <param name="userNos"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using Dlhis.Entity.Basic.Entity;
|
|||||||
using Dlhis.Entity.Device.Entities;
|
using Dlhis.Entity.Device.Entities;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Kurisu.Core.DataAccess.Dto;
|
using Kurisu.Core.DataAccess.Dto;
|
||||||
|
using Kurisu.SqlSugar.Attributes;
|
||||||
using Kurisu.SqlSugar.Extensions;
|
using Kurisu.SqlSugar.Extensions;
|
||||||
using Kurisu.SqlSugar.Services;
|
using Kurisu.SqlSugar.Services;
|
||||||
using Mapster;
|
using Mapster;
|
||||||
@@ -13,10 +14,12 @@ namespace Dlhis.Basic.Device.Services.Implements;
|
|||||||
public class DeviceGroupService : IDeviceGroupService
|
public class DeviceGroupService : IDeviceGroupService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IDbContext _dbContext;
|
||||||
|
private readonly IDevicePermissionService _devicePermissionService;
|
||||||
|
|
||||||
public DeviceGroupService(IDbContext dbContext)
|
public DeviceGroupService(IDbContext dbContext, IDevicePermissionService devicePermissionService)
|
||||||
{
|
{
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
|
_devicePermissionService = devicePermissionService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Pagination<DeviceGroupPageResponse>> GetPageAsync(DeviceGroupPageRequest request)
|
public async Task<Pagination<DeviceGroupPageResponse>> GetPageAsync(DeviceGroupPageRequest request)
|
||||||
@@ -70,6 +73,7 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
await _dbContext.DeleteAsync(deviceGroup);
|
await _dbContext.DeleteAsync(deviceGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Transactional]
|
||||||
public async Task AssignDevicesAsync(DeviceAssignRequest request)
|
public async Task AssignDevicesAsync(DeviceAssignRequest request)
|
||||||
{
|
{
|
||||||
var group = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
var group = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
||||||
@@ -77,15 +81,13 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
|
|
||||||
await CheckNoNextGroupAsync(group.Code);
|
await CheckNoNextGroupAsync(group.Code);
|
||||||
|
|
||||||
if (!request.DeviceCodes.Any())
|
//下发任务
|
||||||
|
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
|
||||||
{
|
{
|
||||||
//todo 删除时候判断权限是否分配
|
|
||||||
|
|
||||||
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
var relationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
||||||
await _dbContext.DeleteAsync(relationships);
|
await _dbContext.DeleteAsync(relationships);
|
||||||
}
|
|
||||||
else
|
request.DeviceCodes = await _dbContext.Queryable<DeviceEntity>().Where(x => request.DeviceCodes.Contains(x.Code)).Select(x => x.Code).ToListAsync();
|
||||||
{
|
|
||||||
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
var entities = request.DeviceCodes.Select(x => new DeviceGroupRelationshipEntity
|
||||||
{
|
{
|
||||||
GroupCode = request.GroupCode,
|
GroupCode = request.GroupCode,
|
||||||
@@ -94,8 +96,8 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
await _dbContext.InsertAsync(entities);
|
await _dbContext.InsertAsync(entities);
|
||||||
}
|
|
||||||
//todo 生成下发任务
|
}, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code)
|
public async Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code)
|
||||||
@@ -165,6 +167,7 @@ public class DeviceGroupService : IDeviceGroupService
|
|||||||
Name = device.UniqueNo,
|
Name = device.UniqueNo,
|
||||||
PCode = relation.GroupCode
|
PCode = relation.GroupCode
|
||||||
})
|
})
|
||||||
|
.Distinct()
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
using Dlhis.Basic.Device.Request;
|
using Dlhis.Basic.Device.Request;
|
||||||
|
using Dlhis.Entity.Basic.Entity;
|
||||||
using Dlhis.Entity.Device.Entities;
|
using Dlhis.Entity.Device.Entities;
|
||||||
|
using Dlhis.Entity.Device.Enums;
|
||||||
using Dlhis.Entity.Sys.Entities;
|
using Dlhis.Entity.Sys.Entities;
|
||||||
using Dlhis.Entity.Sys.Enums;
|
using Dlhis.Entity.Sys.Enums;
|
||||||
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
|
using Kurisu.SqlSugar.Attributes;
|
||||||
using Kurisu.SqlSugar.Services;
|
using Kurisu.SqlSugar.Services;
|
||||||
|
|
||||||
namespace Dlhis.Basic.Device.Services.Implements;
|
namespace Dlhis.Basic.Device.Services.Implements;
|
||||||
@@ -15,26 +19,32 @@ public class DevicePermissionService : IDevicePermissionService
|
|||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Transactional]
|
||||||
public async Task AssignmentAsync(List<DevicePermissionSaveReuqest> reuqests)
|
public async Task AssignmentAsync(List<DevicePermissionSaveReuqest> reuqests)
|
||||||
{
|
{
|
||||||
foreach (var item in reuqests)
|
foreach (var grp in reuqests.GroupBy(x => x.UserNo))
|
||||||
{
|
{
|
||||||
var permissions = await _dbContext.Queryable<DevicePermissionEntity>()
|
var userNo = grp.Key;
|
||||||
.Where(x => x.UniqueType == item.UniqueType)
|
var data = grp.AsEnumerable().DistinctBy(x => x.UniqueValue).ToList();
|
||||||
.Where(x => x.UniqueValue == item.UniqueValue)
|
|
||||||
.Where(x => x.UserNo == item.UserNo)
|
|
||||||
.ToListAsync();
|
|
||||||
|
|
||||||
//todo 差异数据
|
await GenerateDeviceTasksAsync(async () =>
|
||||||
await _dbContext.Client.Deleteable(permissions).ExecuteCommandAsync();
|
|
||||||
|
|
||||||
await _dbContext.InsertAsync(new DevicePermissionEntity
|
|
||||||
{
|
{
|
||||||
UserNo = item.UserNo,
|
await _dbContext.Client.Deleteable<DevicePermissionEntity>()
|
||||||
UniqueType = item.UniqueType,
|
.Where(x => x.UserNo == userNo)
|
||||||
UniqueValue = item.UniqueValue,
|
.ExecuteCommandAsync();
|
||||||
});
|
|
||||||
}
|
var devicePermissions = data.Select(x => new DevicePermissionEntity
|
||||||
|
{
|
||||||
|
UserNo = x.UserNo,
|
||||||
|
UniqueType = x.UniqueType,
|
||||||
|
UniqueValue = x.UniqueValue,
|
||||||
|
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
await _dbContext.InsertAsync(devicePermissions);
|
||||||
|
|
||||||
|
}, new List<string> { userNo });
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Guid>> GetAssignmentsAsync(string userNo, DeviceAssignmentTypeEnumType? uniqueType)
|
public async Task<List<Guid>> GetAssignmentsAsync(string userNo, DeviceAssignmentTypeEnumType? uniqueType)
|
||||||
@@ -45,61 +55,141 @@ public class DevicePermissionService : IDevicePermissionService
|
|||||||
.Select(x => x.UniqueValue)
|
.Select(x => x.UniqueValue)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return assignments;
|
if (uniqueType.HasValue && uniqueType == DeviceAssignmentTypeEnumType.Group)
|
||||||
|
{
|
||||||
|
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
||||||
|
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
||||||
|
tops.ForEach(x =>
|
||||||
|
{
|
||||||
|
RecursionGetNext(x, deviceGroups, assignments, assignments.Contains(x.Code));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return assignments.Distinct().ToList();
|
||||||
|
|
||||||
|
void RecursionGetNext(DeviceGroupEntity current, List<DeviceGroupEntity> deviceGroups, List<Guid> result, bool add)
|
||||||
|
{
|
||||||
|
var next = deviceGroups.Where(x => x.PCode == current.Code).ToList();
|
||||||
|
foreach (var item in next)
|
||||||
|
{
|
||||||
|
if (add) assignments.Add(item.Code);
|
||||||
|
RecursionGetNext(item, deviceGroups, result, assignments.Contains(item.Code));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
|
|
||||||
|
public async Task ResetAssignmentsAsync(DeviceResetAssignmentsRequest request)
|
||||||
{
|
{
|
||||||
|
await GenerateDeviceTasksAsync(async () =>
|
||||||
|
{
|
||||||
|
await _dbContext.Client.Deleteable<DevicePermissionEntity>()
|
||||||
|
.Where(x => x.UserNo == request.UserNo)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
|
||||||
|
}, new List<string> { request.UserNo });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task GenerateDeviceTasksAsync(Func<Task> func, List<string> userNos)
|
||||||
|
{
|
||||||
|
if (userNos.IsEmpty())
|
||||||
|
{
|
||||||
|
userNos = await _dbContext.Queryable<DevicePermissionEntity>().Select(x => x.UserNo).Distinct().ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
var beforeDic = new Dictionary<string, List<Guid>>(userNos.Count);
|
||||||
|
foreach (var userNo in userNos)
|
||||||
|
{
|
||||||
|
beforeDic.TryAdd(userNo, await GetAssignDeviceCodesAsync(userNo));
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取处理
|
||||||
|
await func();
|
||||||
|
|
||||||
|
//获取当前的权限code
|
||||||
|
var currentDic = new Dictionary<string, List<Guid>>(userNos.Count);
|
||||||
|
foreach (var userNo in userNos)
|
||||||
|
{
|
||||||
|
currentDic.TryAdd(userNo, await GetAssignDeviceCodesAsync(userNo));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var userNo in userNos)
|
||||||
|
{
|
||||||
|
var beforeCodes = beforeDic[userNo];
|
||||||
|
var currentCodes = currentDic[userNo];
|
||||||
|
|
||||||
|
//被删除
|
||||||
|
var deletedItems = beforeCodes.Where(x => !currentCodes.Contains(x)).ToList();
|
||||||
|
//新增
|
||||||
|
var addItems = currentCodes.Where(x => !beforeCodes.Contains(x)).ToList();
|
||||||
|
|
||||||
|
if (!deletedItems.Any() && !addItems.Any())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var deivces = await _dbContext.Queryable<DeviceEntity>().Where(x => deletedItems.Contains(x.Code) || addItems.Contains(x.Code)).ToListAsync();
|
||||||
|
var deviceCodes = deivces.Select(x => x.Code).ToList();
|
||||||
|
deletedItems = deletedItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||||
|
addItems = addItems.Where(x => deviceCodes.Contains(x)).ToList();
|
||||||
|
|
||||||
|
var userInfo = await _dbContext.Queryable<GainfoEntity>()
|
||||||
|
.LeftJoin<DepartmentEntity>((ga, department) => department.No == ga.DepartmentNo)
|
||||||
|
.Where(ga => ga.No == userNo)
|
||||||
|
.Select(ga => new
|
||||||
|
{
|
||||||
|
ga.Code
|
||||||
|
})
|
||||||
|
.SingleAsync();
|
||||||
|
|
||||||
|
//下发任务创建
|
||||||
|
var toDeleted = deletedItems.Select(x => new DeviceTaskEntity
|
||||||
|
{
|
||||||
|
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||||
|
Status = DeviceTaskStatusEnumType.Wait,
|
||||||
|
Type = DeviceTaskEnumType.Delete,
|
||||||
|
UserNo = userNo,
|
||||||
|
Version = 0,
|
||||||
|
UserCode = userInfo.Code,
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
var toAdd = addItems.Select(x => new DeviceTaskEntity
|
||||||
|
{
|
||||||
|
DeviceUniqueNo = deivces.First(item => item.Code == x).UniqueNo,
|
||||||
|
Status = DeviceTaskStatusEnumType.Wait,
|
||||||
|
Type = DeviceTaskEnumType.Add,
|
||||||
|
UserNo = userNo,
|
||||||
|
Version = 0,
|
||||||
|
UserCode = userInfo.Code,
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
var tasks = toDeleted.Concat(toAdd).ToList();
|
||||||
|
await _dbContext.InsertAsync(tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//************************************************ help methods *******************************************************
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户所有设备权限
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userNo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task<List<Guid>> GetAssignDeviceCodesAsync(string userNo)
|
||||||
|
{
|
||||||
|
var deviceCodes = await GetAssignmentsAsync(userNo, DeviceAssignmentTypeEnumType.Device);
|
||||||
|
|
||||||
var groupCodes = await GetAssignmentsAsync(userNo, DeviceAssignmentTypeEnumType.Group);
|
var groupCodes = await GetAssignmentsAsync(userNo, DeviceAssignmentTypeEnumType.Group);
|
||||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
|
var deviceCodeUnderGroups = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||||
|
.Where(x => groupCodes.Contains(x.GroupCode))
|
||||||
|
.Select(x => x.DeviceCode)
|
||||||
|
.Distinct()
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
return null;
|
deviceCodes.AddRange(deviceCodeUnderGroups);
|
||||||
//var deviceCodes = await GetAssignmentsAsync(userNo, DeviceAssignmentTypeEnumType.Device);
|
|
||||||
|
|
||||||
|
|
||||||
//var permissionCodes = await _dbContext.Queryable<MenuPermissionEntity>()
|
return deviceCodes.Distinct().ToList();
|
||||||
// .Where(x => x.UniqueType == uniqueType)
|
|
||||||
// .Where(x => x.UniqueValue == uniqueData)
|
|
||||||
// .Select(x => x.Code)
|
|
||||||
// .ToListAsync();
|
|
||||||
|
|
||||||
//var menus = await GetListAsync(PlatformEnumType.PCAdmin);
|
|
||||||
|
|
||||||
//foreach (var menu in menus)
|
|
||||||
//{
|
|
||||||
// if (menu.Items.Any())
|
|
||||||
// {
|
|
||||||
// var codes = RecursionHandler(menu.Items, permissionCodes);
|
|
||||||
// permissionCodes.RemoveAll(x => codes.Contains(x));
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
//return permissionCodes;
|
|
||||||
|
|
||||||
//static List<string> RecursionHandler(List<MenuResponse> menus, List<string> innerPermissions)
|
|
||||||
//{
|
|
||||||
// var codes = new HashSet<string>();
|
|
||||||
// foreach (var menu in menus)
|
|
||||||
// {
|
|
||||||
// if (menu.Items.Any())
|
|
||||||
// {
|
|
||||||
// var innerPermissionCodes = RecursionHandler(menu.Items, innerPermissions);
|
|
||||||
// foreach (var item in innerPermissionCodes)
|
|
||||||
// {
|
|
||||||
// codes.Add(item);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (!innerPermissions.Contains(menu.Code))
|
|
||||||
// {
|
|
||||||
// foreach (var item in menu.Items.Select(x => x.Code))
|
|
||||||
// {
|
|
||||||
// codes.Add(item);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return codes.ToList();
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ public class SqlSugarPack : BaseAppPack
|
|||||||
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
|
||||||
//using var scope = app.ApplicationServices.CreateScope();
|
//using var scope = app.ApplicationServices.CreateScope();
|
||||||
//var db = scope.ServiceProvider.GetService<ISqlSugarClient>();
|
//var db = scope.ServiceProvider.GetService<ISqlSugarClient>();
|
||||||
//var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
|
//var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false))
|
||||||
|
|||||||
@@ -25,8 +25,4 @@
|
|||||||
<ProjectReference Include="..\Dlhis.User.Api\Dlhis.User.Api.csproj" />
|
<ProjectReference Include="..\Dlhis.User.Api\Dlhis.User.Api.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="新文件夹\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user