114 lines
4.2 KiB
C#
114 lines
4.2 KiB
C#
using System.Security.Claims;
|
|
using Dlhis.Basic.Places.Dto;
|
|
using Dlhis.Basic.Places.Request;
|
|
using Dlhis.Basic.Places.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Sys.Api;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
|
|
namespace Dlhis.Basic.Places.Services.Implements;
|
|
|
|
public class BusinessPlacePermissionsService : IBusinessPlacePermissionsService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IUserApi _userApi;
|
|
|
|
public BusinessPlacePermissionsService(IDbContext dbContext, ICurrentUser currentUser, IUserApi userApi)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_userApi = userApi;
|
|
}
|
|
|
|
public async Task<Pagination<BusinessPlacePermissionPageResponse>> GetPageAsync(BusinessPlacePermissionPageRequest request)
|
|
{
|
|
var page = await _dbContext.Queryable<BusinessPlaceManagerRelationshipEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, bplace) => x.BusinessPlaceCode == bplace.Code)
|
|
.Select((x, bplace) => new BusinessPlacePermissionPageResponse
|
|
{
|
|
UserId = x.ManagerUserId,
|
|
BusinessPlaceCode = x.BusinessPlaceCode,
|
|
BusinessPlaceName = bplace.Name,
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
var usersResult = await _userApi.GetUserByIdsAsync(page.Data.Select(x => x.UserId).Distinct().ToList());
|
|
var users = usersResult.Data;
|
|
|
|
page.Data.ForEach(item => { item.UserName = users.FirstOrDefault(x => x.Id == item.UserId)?.UserName; });
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task<List<BusinessPlaceDto>> GetMyAsync()
|
|
{
|
|
var userId = _currentUser.GetIntSubjectId();
|
|
var role = _currentUser.GetUserClaim(ClaimTypes.Role);
|
|
var isAdmin = role == "admin" || role == "tenant-admin";
|
|
|
|
var codes = new List<Guid>();
|
|
if (isAdmin)
|
|
{
|
|
codes = await _dbContext.Queryable<BusinessPlaceEntity>()
|
|
.Select(x => x.Code)
|
|
.ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
codes = await _dbContext.Queryable<BusinessPlaceManagerRelationshipEntity>()
|
|
.Where(x => x.ManagerUserId == userId)
|
|
.Select(x => x.BusinessPlaceCode)
|
|
.Distinct()
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
return await _dbContext.Queryable<BusinessPlaceEntity>()
|
|
.Where(x => codes.Contains(x.Code))
|
|
.Select<BusinessPlaceDto>()
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task SaveBusinessPlacePermissionAsync(BusinessPlacePermissionSaveRequest request)
|
|
{
|
|
await _dbContext.Client.Deleteable<BusinessPlaceManagerRelationshipEntity>()
|
|
.Where(x => x.ManagerUserId == request.UserId)
|
|
.ExecuteCommandAsync();
|
|
|
|
await _dbContext.InsertAsync(request.BusinessPlaceCodes.Select(x => new BusinessPlaceManagerRelationshipEntity
|
|
{
|
|
BusinessPlaceCode = x,
|
|
ManagerUserId = request.UserId
|
|
}).ToList());
|
|
}
|
|
|
|
public async Task<List<BusinessPlacePermissionPageResponse>> GetBusinessPlacePermissionByUserIdAsyc(int id)
|
|
{
|
|
var infos = await _dbContext.Queryable<BusinessPlaceManagerRelationshipEntity>()
|
|
.LeftJoin<BusinessPlaceEntity>((x, bplace) => x.BusinessPlaceCode == bplace.Code)
|
|
.Where(x => x.ManagerUserId == id)
|
|
.Select((x, bplace) => new BusinessPlacePermissionPageResponse
|
|
{
|
|
UserId = x.ManagerUserId,
|
|
BusinessPlaceCode = x.BusinessPlaceCode,
|
|
BusinessPlaceName = bplace.Name,
|
|
})
|
|
.ToListAsync();
|
|
|
|
var userResult = await _userApi.GetUserByIdAsync(id);
|
|
var user = userResult.Data;
|
|
|
|
infos.ForEach(item => { item.UserName = user?.UserName; });
|
|
|
|
return infos;
|
|
}
|
|
|
|
public async Task DeleteAssignmentAsync(int id)
|
|
{
|
|
await _dbContext.Client.Deleteable<BusinessPlaceManagerRelationshipEntity>().Where(x => x.Id == id).ExecuteCommandAsync();
|
|
}
|
|
} |