Merge branch 'development' of https://git.dlhis.com/liguoyang/jinko.yikatong into development
This commit is contained in:
@@ -28,4 +28,8 @@
|
|||||||
<ProjectReference Include="..\Dlhis.Sys.Api\Dlhis.Sys.Api.csproj" />
|
<ProjectReference Include="..\Dlhis.Sys.Api\Dlhis.Sys.Api.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Summary\Request\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
29
Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs
Normal file
29
Src/Dlhis.Basic/Summary/Controllers/SummariesController.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Dlhis.Basic.Summary.Response;
|
||||||
|
using Dlhis.Basic.Summary.Services;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Controllers;
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class SummariesController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ISummaryService _summaryService;
|
||||||
|
|
||||||
|
public SummariesController(ISummaryService summaryService)
|
||||||
|
{
|
||||||
|
_summaryService = summaryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 考勤首页
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("attendance-summary")]
|
||||||
|
public async Task<AttendanceSummaryResponse> GetKaoQingSummary()
|
||||||
|
{
|
||||||
|
return await _summaryService.GetAttendanceSummaryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Src/Dlhis.Basic/Summary/Dto/TrendDto.cs
Normal file
8
Src/Dlhis.Basic/Summary/Dto/TrendDto.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Dlhis.Basic.Summary.Dto;
|
||||||
|
|
||||||
|
public class TrendDto
|
||||||
|
{
|
||||||
|
public DateTime Date { get; set; }
|
||||||
|
|
||||||
|
public int Count { get; set; }
|
||||||
|
}
|
||||||
21
Src/Dlhis.Basic/Summary/Response/AttendanceDetailSummary.cs
Normal file
21
Src/Dlhis.Basic/Summary/Response/AttendanceDetailSummary.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using Dlhis.Basic.Summary.Dto;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Response;
|
||||||
|
|
||||||
|
public class AttendanceDetailSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 在职人数
|
||||||
|
/// </summary>
|
||||||
|
public int Employed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 非在职人数
|
||||||
|
/// </summary>
|
||||||
|
public int Unemployed { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 人员趋势 7天
|
||||||
|
/// </summary>
|
||||||
|
public GainfoTrendResponse GainfoTrend { get; set; }
|
||||||
|
}
|
||||||
26
Src/Dlhis.Basic/Summary/Response/AttendanceDeviceSummary.cs
Normal file
26
Src/Dlhis.Basic/Summary/Response/AttendanceDeviceSummary.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using Dlhis.Basic.Summary.Dto;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Response;
|
||||||
|
|
||||||
|
public class AttendanceDeviceSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 总数
|
||||||
|
/// </summary>
|
||||||
|
public int Total { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在线数
|
||||||
|
/// </summary>
|
||||||
|
public int OnlineCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用数
|
||||||
|
/// </summary>
|
||||||
|
public int EnableCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增设备趋势 30天内
|
||||||
|
/// </summary>
|
||||||
|
public List<TrendDto> Trend { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Dlhis.Basic.Summary.Response
|
||||||
|
{
|
||||||
|
public class AttendanceSummaryResponse
|
||||||
|
{
|
||||||
|
public AttendanceDeviceSummary DeviceSummary { get; set; }
|
||||||
|
|
||||||
|
public AttendanceDetailSummary AttendanceDetailSummary { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Src/Dlhis.Basic/Summary/Response/GainfoTrendResponse.cs
Normal file
10
Src/Dlhis.Basic/Summary/Response/GainfoTrendResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using Dlhis.Basic.Summary.Dto;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Response
|
||||||
|
{
|
||||||
|
public class GainfoTrendResponse
|
||||||
|
{
|
||||||
|
public List<TrendDto> EmployedTrend { get; set; }
|
||||||
|
public List<TrendDto> UnemployedTrend { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Src/Dlhis.Basic/Summary/Services/ISummaryService.cs
Normal file
8
Src/Dlhis.Basic/Summary/Services/ISummaryService.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using Dlhis.Basic.Summary.Response;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Services;
|
||||||
|
|
||||||
|
public interface ISummaryService : IScopeDependency
|
||||||
|
{
|
||||||
|
Task<AttendanceSummaryResponse> GetAttendanceSummaryAsync();
|
||||||
|
}
|
||||||
106
Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs
Normal file
106
Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
using Dlhis.Basic.Organization.Services;
|
||||||
|
using Dlhis.Basic.Summary.Dto;
|
||||||
|
using Dlhis.Basic.Summary.Response;
|
||||||
|
using Dlhis.Entity.Basic.Entity;
|
||||||
|
using Dlhis.Entity.Basic.Enums;
|
||||||
|
using Dlhis.Entity.Device.Entities;
|
||||||
|
using Kurisu.SqlSugar.Services;
|
||||||
|
|
||||||
|
namespace Dlhis.Basic.Summary.Services.Implements;
|
||||||
|
|
||||||
|
public class SummaryService : ISummaryService
|
||||||
|
{
|
||||||
|
private readonly IDbContext _dbContext;
|
||||||
|
private readonly IGainfoStore _gainfoStore;
|
||||||
|
|
||||||
|
public SummaryService(IDbContext dbContext, IGainfoStore gainfoStore)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
_gainfoStore = gainfoStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<AttendanceSummaryResponse> GetAttendanceSummaryAsync()
|
||||||
|
{
|
||||||
|
var now = DateTime.Now;
|
||||||
|
|
||||||
|
var currentDay = DateTime.Today.AddDays(1).AddSeconds(-1);
|
||||||
|
var before30Day = DateTime.Today.AddDays(-29);
|
||||||
|
var before7Day = DateTime.Today.AddDays(-6);
|
||||||
|
|
||||||
|
var devices = await _dbContext.Queryable<DeviceEntity>().ToListAsync();
|
||||||
|
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
||||||
|
|
||||||
|
var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>()
|
||||||
|
.LeftJoin<DeviceEntity>((x, d) => x.DeviceUniqueNo == d.UniqueNo)
|
||||||
|
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
||||||
|
.Select((x, d) => new
|
||||||
|
{
|
||||||
|
d.HeartbeatInterval,
|
||||||
|
x.LastHeartbeatTime
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).ToListAsync();
|
||||||
|
var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).ToListAsync();
|
||||||
|
|
||||||
|
return new AttendanceSummaryResponse
|
||||||
|
{
|
||||||
|
DeviceSummary = new AttendanceDeviceSummary
|
||||||
|
{
|
||||||
|
Total = devices.Count,
|
||||||
|
EnableCount = devices.Where(x => x.Enable).Count(),
|
||||||
|
OnlineCount = deviceRegistries.Where(x =>
|
||||||
|
{
|
||||||
|
if (x.HeartbeatInterval <= 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now;
|
||||||
|
|
||||||
|
}).Count(),
|
||||||
|
Trend = FixTrend(devices.Where(x => x.CreateTime >= before30Day && x.CreateTime <= currentDay)
|
||||||
|
.GroupBy(x => x.CreateTime.Date)
|
||||||
|
.Select(x => new TrendDto { Date = x.Key, Count = x.Count() }), before30Day, currentDay)
|
||||||
|
},
|
||||||
|
AttendanceDetailSummary = new AttendanceDetailSummary
|
||||||
|
{
|
||||||
|
Employed = workingGainfos.Count,
|
||||||
|
Unemployed = leaveGainfos.Count,
|
||||||
|
GainfoTrend = new GainfoTrendResponse
|
||||||
|
{
|
||||||
|
EmployedTrend = FixTrend(workingGainfos.Where(x => x.CreateTime >= before7Day && x.CreateTime <= currentDay)
|
||||||
|
.GroupBy(x => x.CreateTime.Date)
|
||||||
|
.Select(x => new TrendDto { Date = x.Key, Count = x.Count() }), before7Day, currentDay),
|
||||||
|
UnemployedTrend = FixTrend(leaveGainfos.Where(x => x.CreateTime >= before7Day && x.CreateTime <= currentDay)
|
||||||
|
.GroupBy(x => x.CreateTime.Date)
|
||||||
|
.Select(x => new TrendDto { Date = x.Key, Count = x.Count() }), before7Day, currentDay),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<TrendDto> FixTrend(IEnumerable<TrendDto> dtos, DateTime start, DateTime end)
|
||||||
|
{
|
||||||
|
var result = dtos.ToList();
|
||||||
|
|
||||||
|
start = start.Date;
|
||||||
|
end = end.Date;
|
||||||
|
|
||||||
|
var currentDate = start;
|
||||||
|
while (currentDate <= end)
|
||||||
|
{
|
||||||
|
if (result.Any(x => x.Date == currentDate))
|
||||||
|
{
|
||||||
|
currentDate = currentDate.AddDays(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Add(new TrendDto { Date = currentDate, Count = 0 });
|
||||||
|
|
||||||
|
currentDate = currentDate.AddDays(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,10 +20,15 @@ public enum CanteenBusinessPlaceCatalogEnumType
|
|||||||
[Description("食堂")]
|
[Description("食堂")]
|
||||||
Cafeteria = 2,
|
Cafeteria = 2,
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 超市
|
/// 超市
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Description("超市")]
|
[Description("超市")]
|
||||||
Supermarket = 3,
|
Supermarket = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 水吧
|
||||||
|
/// </summary>
|
||||||
|
[Description("水吧")]
|
||||||
|
Bar = 4,
|
||||||
}
|
}
|
||||||
@@ -17,5 +17,5 @@ public class RoleEntity
|
|||||||
|
|
||||||
public string NormalizedName { get; set; }
|
public string NormalizedName { get; set; }
|
||||||
|
|
||||||
public string ConcurrencyStamp { get; set; }
|
public Guid ConcurrencyStamp { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
13
Src/Dlhis.Entity/Sys/Entities/UserRoleEntity.cs
Normal file
13
Src/Dlhis.Entity/Sys/Entities/UserRoleEntity.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using SqlSugar;
|
||||||
|
|
||||||
|
namespace Dlhis.Entity.Sys.Entities;
|
||||||
|
|
||||||
|
[SkipScan]
|
||||||
|
[SugarTable(TableName = "UserRoles")]
|
||||||
|
public class UserRoleEntity
|
||||||
|
{
|
||||||
|
public int UserId { get; set; }
|
||||||
|
|
||||||
|
public int RoleId { get; set; }
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ namespace Dlhis.Sys.Identity.Controllers;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 角色
|
/// 角色
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Authorize(Roles = "admin")]
|
[Authorize]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class RolesController : ControllerBase
|
public class RolesController : ControllerBase
|
||||||
@@ -27,9 +27,55 @@ public class RolesController : ControllerBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[Authorize(Roles = "admin")]
|
||||||
public async Task<Pagination<RoleRespone>> GetPageAsync(RolePageRequest request)
|
[HttpGet("page")]
|
||||||
|
public async Task<Pagination<RoleRespone>> GetPageAsync([FromQuery] RolePageRequest request)
|
||||||
{
|
{
|
||||||
return await _roleService.GetPageAsync(request);
|
return await _roleService.GetPageAsync(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 分配角色
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[Authorize(Roles = "admin")]
|
||||||
|
[HttpPost("assign")]
|
||||||
|
public async Task AssignRole(AssignRoleRequest request)
|
||||||
|
{
|
||||||
|
await _roleService.AssignRoleAsync(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存角色
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[Authorize(Roles = "admin")]
|
||||||
|
[HttpPost("save")]
|
||||||
|
public async Task SaveRole(RoleSaveRequest request)
|
||||||
|
{
|
||||||
|
await _roleService.SaveRoleAsync(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 移除角色
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[Authorize(Roles = "admin")]
|
||||||
|
[HttpPost("delete")]
|
||||||
|
public async Task RemoveRole(RoleRemoveRequest request)
|
||||||
|
{
|
||||||
|
await _roleService.RemoveRoleAsync(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 我的角色
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("my")]
|
||||||
|
public async Task<RoleRespone> GetMyRoleAsync()
|
||||||
|
{
|
||||||
|
return await _roleService.GetMyRoleAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
Src/Dlhis.Sys/Identity/Request/AssignRoleRequest.cs
Normal file
8
Src/Dlhis.Sys/Identity/Request/AssignRoleRequest.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Dlhis.Sys.Identity.Request;
|
||||||
|
|
||||||
|
public class AssignRoleRequest
|
||||||
|
{
|
||||||
|
public int UserId { get; set; }
|
||||||
|
|
||||||
|
public int? RoleId { get; set; }
|
||||||
|
}
|
||||||
6
Src/Dlhis.Sys/Identity/Request/RoleRemoveRequest.cs
Normal file
6
Src/Dlhis.Sys/Identity/Request/RoleRemoveRequest.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Dlhis.Sys.Identity.Request;
|
||||||
|
|
||||||
|
public class RoleRemoveRequest
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
}
|
||||||
11
Src/Dlhis.Sys/Identity/Request/RoleSaveRequest.cs
Normal file
11
Src/Dlhis.Sys/Identity/Request/RoleSaveRequest.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Dlhis.Sys.Identity.Request;
|
||||||
|
|
||||||
|
public class RoleSaveRequest
|
||||||
|
{
|
||||||
|
public int? Id { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "角色名不能为空")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
@@ -7,4 +7,12 @@ namespace Dlhis.User.Identity.Services;
|
|||||||
public interface IRoleService : IScopeDependency
|
public interface IRoleService : IScopeDependency
|
||||||
{
|
{
|
||||||
Task<Pagination<RoleRespone>> GetPageAsync(RolePageRequest request);
|
Task<Pagination<RoleRespone>> GetPageAsync(RolePageRequest request);
|
||||||
|
|
||||||
|
Task SaveRoleAsync(RoleSaveRequest request);
|
||||||
|
|
||||||
|
Task<RoleRespone> GetMyRoleAsync();
|
||||||
|
|
||||||
|
Task RemoveRoleAsync(RoleRemoveRequest request);
|
||||||
|
|
||||||
|
Task AssignRoleAsync(AssignRoleRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,11 @@
|
|||||||
using Dlhis.Sys.Identity.Request;
|
using Dlhis.Sys.Identity.Request;
|
||||||
using Dlhis.Sys.Identity.Response;
|
using Dlhis.Sys.Identity.Response;
|
||||||
using Dlhis.User.Identity.Services;
|
using Dlhis.User.Identity.Services;
|
||||||
|
using Kurisu.AspNetCore.Scope;
|
||||||
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
||||||
using Kurisu.AspNetCore.Utils.Extensions;
|
using Kurisu.AspNetCore.Utils.Extensions;
|
||||||
using Kurisu.Core.DataAccess.Dto;
|
using Kurisu.Core.DataAccess.Dto;
|
||||||
|
using Kurisu.Core.User.Abstractions;
|
||||||
using Kurisu.SqlSugar.Extensions;
|
using Kurisu.SqlSugar.Extensions;
|
||||||
using Kurisu.SqlSugar.Services;
|
using Kurisu.SqlSugar.Services;
|
||||||
|
|
||||||
@@ -11,20 +14,162 @@ namespace Dlhis.Sys.Identity.Services.Implements;
|
|||||||
|
|
||||||
public class RoleService : IRoleService
|
public class RoleService : IRoleService
|
||||||
{
|
{
|
||||||
private readonly IDbContext _dbContext;
|
private readonly IConfiguration _configuration;
|
||||||
|
private readonly ICurrentUser _currentUser;
|
||||||
|
|
||||||
public RoleService(IDbContext dbContext, IConfiguration configuration)
|
public RoleService(IConfiguration configuration, ICurrentUser currentUser)
|
||||||
{
|
{
|
||||||
var identityConnection = configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
_configuration = configuration;
|
||||||
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
_currentUser = currentUser;
|
||||||
_dbContext = dbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Pagination<RoleRespone>> GetPageAsync(RolePageRequest request)
|
public async Task<Pagination<RoleRespone>> GetPageAsync(RolePageRequest request)
|
||||||
{
|
{
|
||||||
return await _dbContext.Queryable<RoleEntity>()
|
return await Scoped.Request.Value.CreateAsync(async sp =>
|
||||||
|
{
|
||||||
|
var dbContext = sp.GetService<IDbContext>();
|
||||||
|
var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
||||||
|
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
||||||
|
|
||||||
|
|
||||||
|
return await dbContext.Queryable<RoleEntity>()
|
||||||
.WhereIF(request.Search.IsPresent(), x => x.Name.Contains(request.Search) || x.NormalizedName.Contains(request.Search))
|
.WhereIF(request.Search.IsPresent(), x => x.Name.Contains(request.Search) || x.NormalizedName.Contains(request.Search))
|
||||||
.Select<RoleRespone>()
|
.Select<RoleRespone>()
|
||||||
.ToPageListAsync(request);
|
.ToPageListAsync(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<RoleRespone> GetMyRoleAsync()
|
||||||
|
{
|
||||||
|
var userId = _currentUser.GetIntSubjectId();
|
||||||
|
|
||||||
|
return await Scoped.Request.Value.CreateAsync(async sp =>
|
||||||
|
{
|
||||||
|
var dbContext = sp.GetService<IDbContext>();
|
||||||
|
var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
||||||
|
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
||||||
|
|
||||||
|
|
||||||
|
return await dbContext.Queryable<UserRoleEntity>()
|
||||||
|
.LeftJoin<RoleEntity>((x, r) => x.RoleId == r.Id)
|
||||||
|
.Where(x => x.UserId == userId)
|
||||||
|
.Select((x, r) => new RoleRespone
|
||||||
|
{
|
||||||
|
Id = r.Id,
|
||||||
|
Name = r.Name,
|
||||||
|
NormalizedName = r.NormalizedName,
|
||||||
|
})
|
||||||
|
.FirstAsync();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SaveRoleAsync(RoleSaveRequest request)
|
||||||
|
{
|
||||||
|
await Scoped.Request.Value.CreateAsync(async sp =>
|
||||||
|
{
|
||||||
|
var dbContext = sp.GetService<IDbContext>();
|
||||||
|
var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
||||||
|
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
||||||
|
|
||||||
|
|
||||||
|
await CheckDuplicateNameAsync(dbContext, request);
|
||||||
|
|
||||||
|
if (!request.Id.HasValue)
|
||||||
|
{
|
||||||
|
await dbContext.InsertAsync(new RoleEntity
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
NormalizedName = request.Name.ToUpperInvariant(),
|
||||||
|
ConcurrencyStamp = Guid.NewGuid(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var entity = await dbContext.Queryable<RoleEntity>().Where(x => x.Id == request.Id).FirstAsync();
|
||||||
|
entity.ThrowIfNull("角色不存在");
|
||||||
|
|
||||||
|
entity.Name = request.Name;
|
||||||
|
entity.NormalizedName = request.Name.ToUpperInvariant();
|
||||||
|
entity.ConcurrencyStamp = Guid.NewGuid();
|
||||||
|
|
||||||
|
await dbContext.UpdateAsync(entity);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task CheckDuplicateNameAsync(IDbContext db, RoleSaveRequest request)
|
||||||
|
{
|
||||||
|
var exists = await db.Queryable<RoleEntity>()
|
||||||
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
||||||
|
.Where(x => x.Name == request.Name)
|
||||||
|
.AnyAsync();
|
||||||
|
|
||||||
|
exists.ThrowIfTrue($"重复角色名称{request.Name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AssignRoleAsync(AssignRoleRequest request)
|
||||||
|
{
|
||||||
|
var myUserId = _currentUser.GetIntSubjectId();
|
||||||
|
|
||||||
|
await Scoped.Request.Value.CreateAsync(async sp =>
|
||||||
|
{
|
||||||
|
//不能被其他人修改
|
||||||
|
if (request.UserId == 1 && request.UserId != myUserId)
|
||||||
|
{
|
||||||
|
throw new UserFriendlyException("无法修改admin角色");
|
||||||
|
}
|
||||||
|
|
||||||
|
var dbContext = sp.GetService<IDbContext>();
|
||||||
|
var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
||||||
|
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
||||||
|
|
||||||
|
var existsUser = await dbContext.Queryable<UserEntity>().Where(x => x.Id == request.UserId).AnyAsync();
|
||||||
|
existsUser.ThrowIfFalse("分配的用户不存在");
|
||||||
|
|
||||||
|
var userRole = await dbContext.Queryable<UserRoleEntity>().Where(x => x.UserId == request.UserId).FirstAsync();
|
||||||
|
|
||||||
|
if (request.RoleId.HasValue)
|
||||||
|
{
|
||||||
|
var existsRole = await dbContext.Queryable<RoleEntity>().Where(x => x.Id == request.RoleId).AnyAsync();
|
||||||
|
existsUser.ThrowIfFalse("分配的角色不存在");
|
||||||
|
|
||||||
|
if (userRole == null)
|
||||||
|
{
|
||||||
|
await dbContext.InsertAsync(new UserRoleEntity
|
||||||
|
{
|
||||||
|
UserId = request.UserId,
|
||||||
|
RoleId = request.RoleId.Value,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await dbContext.Updateable<UserRoleEntity>()
|
||||||
|
.SetColumns(x => x.RoleId == request.RoleId)
|
||||||
|
.Where(x => x.UserId == request.UserId)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await dbContext.DeleteReallyAsync(userRole);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RemoveRoleAsync(RoleRemoveRequest request)
|
||||||
|
{
|
||||||
|
await Scoped.Request.Value.CreateAsync(async sp =>
|
||||||
|
{
|
||||||
|
var dbContext = sp.GetService<IDbContext>();
|
||||||
|
var identityConnection = _configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value;
|
||||||
|
dbContext.Client.CurrentConnectionConfig.ConnectionString = identityConnection;
|
||||||
|
|
||||||
|
var role = await dbContext.Queryable<RoleEntity>().FirstAsync(x => x.Id == request.Id);
|
||||||
|
role.ThrowIfNull("角色不存在");
|
||||||
|
|
||||||
|
//删除角色,自动删除UserRoles对应关系
|
||||||
|
await dbContext.DeleteReallyAsync(role);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user