You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

91 lines
3.2 KiB

using AutoMapper;
using Easy.DDD.Domain.Repositories;
using Easy.Guids;
using Easy.Result;
using Identity.Api.Clean.Contracts.Inputs;
using Identity.Api.Clean.Contracts.Models;
using Identity.Api.Clean.Domain.Entites;
using Identity.Api.Clean.Shared.IServices;
namespace Identity.Api.Clean.Application;
public class OrganizationUnitAppService
{
protected IRepository<OrganizationUnit> OrganizationUnitRepository { get; }
protected IRepository<IdentityUserOrganizationUnit> IdentityUserOrganizationUnitRepository { get; }
protected IOrganizationUnitManager OrganizationUnitManager { get; }
protected IMapper Mapper { get; }
protected IGuidGenerator GuidGenerator { get; }
protected IHttpContextAccessor HttpContextAccessor { get; }
public virtual async Task<ApiResultValue<OrganizationUnitModel>> CreateAsync(OrganizationUnitCreateInput input)
{
var orgEntity = new OrganizationUnit(GuidGenerator.Create(), input.DisplayName, input.ParentId, input.TenantId);
await OrganizationUnitManager.CreateAsync(orgEntity);
await OrganizationUnitRepository.AddAsync(orgEntity, true);
// 在token中获取sub
var sub = HttpContextAccessor.HttpContext.User.FindFirst("sub");
if (sub != null && Guid.TryParse(sub.Value, out Guid userId))
{
await IdentityUserOrganizationUnitRepository
.AddAsync(new IdentityUserOrganizationUnit(GuidGenerator.Create(), userId, orgEntity.Id, input.TenantId), true);
}
var value = Mapper.Map<OrganizationUnitModel>(orgEntity);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResultValue<OrganizationUnitModel>> UpdateAsync(Guid id, OrganizationUnitUpdateInput organizationUnit)
{
var orgEntity = Mapper.Map<OrganizationUnit>(organizationUnit);
await OrganizationUnitManager.ValidateOrganizationUnitAsync(orgEntity);
await OrganizationUnitRepository.UpdateAsync(orgEntity, true);
var value = Mapper.Map<OrganizationUnitModel>(orgEntity);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResult> DeleteAsync(Guid id)
{
await OrganizationUnitManager.DeleteAsync(id);
return ApiResult.Success();
}
public virtual async Task<ApiResultValue<OrganizationUnitDescriptionModel>> GetDescriptionAsync(Guid id)
{
var org = await OrganizationUnitRepository.FirstOrDefaultAsync(o => o.Id == id);
var units = await OrganizationUnitRepository.ToListAsync(o => o.ParentId == id);
var value = Mapper.Map<OrganizationUnitDescriptionModel>(org);
value.OrganizationUnitDescriptions = Mapper.Map<List<OrganizationUnitDescription>>(units);
return ApiResult.ValueSuccess(value);
}
public virtual async Task<ApiResult> AddRoleAsync(OrganizationUnitRoleCreateInput input)
{
await OrganizationUnitManager.AddRoleToOrganizationUnitAsync(input.RoleId, input.Id);
return ApiResult.Success();
}
public virtual async Task<ApiResult> RemoveRoleAsync(Guid id, OrganizationUnitRoleDeleteInput input)
{
await OrganizationUnitManager.RemoveRoleFromOrganizationUnitAsync(input.RoleId, id);
return ApiResult.Success();
}
}