Abp组织架构

在abp中,已有组织架构部分的接口,但是根据实际需要进行更改还是有必要。

终端实体类Player,联系集PlayerOrganizationUnit,组织架构类OrganizationUnit,派生自OrganizationUnit的TypeOrganiationUnit类添加属性OUType来区分不同的组织架构树。
Abp组织架构_第1张图片

实体Entity

[Table("NovelPlayer")]
public class Player : Entity<long>, IMustHaveTenant, IMustHaveOrganizationUnit
{
    public string Title { get; set; }
    public string DisplayName { get; set; }
    public string MAC { get; set; }
    public bool IsUsing { get; set; }
    // IMustHaveTenant, IMustHaveOrganizationUnit
    public int TenantId { get; set; }
    public long OrganizationUnitId { get; set; }
}
  
[Table("NovelPlayerOrganizationUnit")]
[AutoMap(typeof(Player))]
public class PlayerOrganizationUnit : CreationAuditedEntity<long>, IMayHaveTenant, ISoftDelete
{
    // player
    public long PlayerId { get; set; }
    public string Title { get; set; }
    public string DisplayName { get; set; }
    public string MAC { get; set; }
    public bool IsUsing { get; set; }
    // organizationUnit
    public long OrganizationUnitId { get; set; }
    // IMayHaveTenant, ISoftDelete
    public bool IsDeleted { get; set; }
    public int? TenantId { get; set; }

    public PlayerOrganizationUnit() { }
    public PlayerOrganizationUnit(int? tenantId, long playerId, long organizationUnitId)
    {
        TenantId = tenantId;
        PlayerId = playerId;
        OrganizationUnitId = organizationUnitId;
    }
}

拓展组织架构类OrganizaitionUnit

public class TypeOrganizationUnit : OrganizationUnit
{
    // 1-player,2-user
    [Range(1, 100)]
    public int OUType { get; set; }
    public TypeOrganizationUnit()
    {
    }
    //构造函数中添加OUType
    public TypeOrganizationUnit(int tp, int? tenantId, string displayName, long? parentId = null)
        : base(tenantId, displayName, parentId)
    {
        OUType = tp;
    }
}

对应的Dto

[AutoMap(typeof(TypeOrganizationUnit))]
public class TypeOrganizationUnitDto : OrganizationUnitDto
{
    public int OUType { get; set; }
    public TypeOrganizationUnitDto()
    {
    }
}

EF Core

public DbSet<Player> Players { get; set; }
public DbSet<PlayerOrganizationUnit> PlayerOrganizationUnits { get; set; }

从上一篇派生类在EntityFramework Core中不生成表的探讨中可知,organizationUnit表中会新增一个OUType的类型。我们用这个字段来筛选出我们需要的组织架构成员
Abp组织架构_第2张图片

应用服务

//获取对应的组织架构树成员
public async Task<ListResultDto<TypeOrganizationUnitDto>> GetOrganizationUnits(int tp)
{
    var query = from ou in _typeOrganizationUnitRepository.GetAll()
                where ou.OUType == tp
                select new
                {
                ou
                };
    var items = await query.ToListAsync();

    return new ListResultDto<TypeOrganizationUnitDto>(
        items.Select(item =>
        {
        var organizationUnitDto = ObjectMapper.Map<TypeOrganizationUnitDto>(item.ou);
        return organizationUnitDto;
        }).ToList());
}
//将终端添加入指定的组织架构中
public async Task AddPlayersToOrganizationUnit(PlayersToOrganizationUnitInput input)
{
    foreach (var playerId in input.PlayerIds)
    {
    await _playerManager.AddPlayerToOrganizationUnitAsync(playerId, input.OrganizationUnitId);
    }
}
//添加指定树的组织架构
public async Task<TypeOrganizationUnitDto> CreateOrganizationUnit(CreateOrganizationUnitInput input)
{
    var organizationUnit = new TypeOrganizationUnit(AbpSession.TenantId, input.DisplayName, input.ParentId);
    await _organizationUnitManager.CreateAsync(organizationUnit);
    await CurrentUnitOfWork.SaveChangesAsync();

    return ObjectMapper.Map<TypeOrganizationUnitDto>(organizationUnit);
}

领域层服务

这里我们比照UserManager在领域层写PlayerManager的服务

public virtual async Task<Player> GetPlayerByIdAsync(long playerId)
{
    var player = await _playerRepositry.GetAsync(playerId);
    if (player == null)
    {
    throw new AbpException("There is no player with id: " + playerId);
    }
    return player;
}

public virtual async Task AddPlayerToOrganizationUnitAsync(long playerId, long ouId)
{
    await AddToOrganizationUnitAsync(
        await GetPlayerByIdAsync(playerId),
        await _typeOrganizationUnitRepositry.GetAsync(ouId)
        );
}

public virtual async Task AddToOrganizationUnitAsync(Player player, TypeOrganizationUnit ou)
{
    var currentOus = await GetOrganizationUnitsAsync(player);

    if (currentOus.Any(cou => cou.Id == ou.Id))
    {
    return;
    }

    //await CheckMaxUserOrganizationUnitMembershipCountAsync(player.TenantId, currentOus.Count + 1);

    await _playerOrganizationUnitRepository.InsertAsync(new PlayerOrganizationUnit(player.TenantId, player.Id, ou.Id));
}

[UnitOfWork]
public virtual Task<List<PlayerOrganizationUnit>> GetOrganizationUnitsAsync(Player player)
{
    var query = from uou in _playerOrganizationUnitRepository.GetAll()
                join ou in _typeOrganizationUnitRepositry.GetAll() on uou.OrganizationUnitId equals ou.Id
                where uou.Id == player.Id
                select uou;

    return Task.FromResult(query.ToList());
}

运行程序
Abp组织架构_第3张图片

你可能感兴趣的:(Abp组织架构)