Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加部门管理 #52

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.D3Shop.Admin.Shared.Dtos.Identity
{

/// <summary>
/// 创建部门用户信息
/// </summary>
/// <param name="UserId"></param>
/// <param name="UserName"></param>
public record CreateDepartmentUserInfoDto(AdminUserId UserId, string UserName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.D3Shop.Admin.Shared.Dtos.Identity
{

/// <summary>
/// 更新部门用户信息
/// </summary>
/// <param name="UserId"></param>
/// <param name="UserName"></param>
public record UpdateDepartmentUserInfoDto(AdminUserId UserId, string UserName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.10"/>
<PackageReference Include="NetCorePal.Extensions.Dto" Version="$(NetCorePalVersion)"/>
<PackageReference Include="NetCorePal.Extensions.NewtonsoftJson" Version="$(NetCorePalVersion)"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.10" />
<PackageReference Include="NetCorePal.Extensions.Dto" Version="$(NetCorePalVersion)" />
<PackageReference Include="NetCorePal.Extensions.NewtonsoftJson" Version="$(NetCorePalVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetCorePal.D3Shop.Domain\NetCorePal.D3Shop.Domain.csproj"/>
<ProjectReference Include="..\NetCorePal.D3Shop.Domain\NetCorePal.D3Shop.Domain.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ public static class PermissionCodes
public const string RoleView = nameof(RoleView);

#endregion

#region DepartmentManagement
public const string DepartmentManagement = nameof(DepartmentManagement);
public const string DepartmentCreate = nameof(DepartmentCreate);
public const string DepartmentEdit = nameof(DepartmentEdit);
public const string DepartmentView = nameof(DepartmentView);
public const string DepartmentDelete = nameof(DepartmentDelete);

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NetCorePal.D3Shop.Admin.Shared.Dtos.Identity;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using System.ComponentModel.DataAnnotations;

namespace NetCorePal.D3Shop.Admin.Shared.Requests;

public class CreateDepartmentRequest
{
[Required] public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public DeptId ParentId { get; set; } = new DeptId(0);

public IEnumerable<CreateDepartmentUserInfoDto> Users { get; set; } = [];
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using NetCorePal.Extensions.Dto;

namespace NetCorePal.D3Shop.Admin.Shared.Requests;

public class DepartmentQueryRequest : PageRequest
{
public string? Name { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using NetCorePal.D3Shop.Admin.Shared.Dtos.Identity;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using System.ComponentModel.DataAnnotations;

namespace NetCorePal.D3Shop.Admin.Shared.Requests;

public class UpdateDepartmentInfoRequest
{
[Required] public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public IEnumerable<CreateDepartmentUserInfoDto> Users { get; set; } = [];
}
11 changes: 11 additions & 0 deletions src/NetCorePal.D3Shop.Admin.Shared/Responses/DepartmentResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;

namespace NetCorePal.D3Shop.Admin.Shared.Responses;

public class DepartmentResponse(DeptId id, string name, string description)
{
public DeptId Id { get; } = id;
public string Name { get; set; } = name;
public string Description { get; set; } = description;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using NetCorePal.Extensions.Domain;
using NetCorePal.Extensions.Primitives;

Expand All @@ -19,6 +20,11 @@ protected AdminUser()
public string Password { get; private set; } = string.Empty;
public DateTime CreatedAt { get; init; }
public virtual ICollection<AdminUserRole> Roles { get; } = [];

public virtual ICollection<UserDept> UserDepts { get; } = [];



public virtual ICollection<AdminUserPermission> Permissions { get; } = [];
public bool IsDeleted { get; private set; }
public DateTime? DeletedAt { get; private set; }
Expand Down Expand Up @@ -47,6 +53,12 @@ public void UpdateRoleInfo(RoleId roleId, string roleName)
savedRole?.UpdateRoleInfo(roleName);
}

public void SetUserDepts(DeptId deptId, string deptName)
{
var savedDept = UserDepts.FirstOrDefault(r => r.DeptId == deptId);
savedDept?.UpdateDeptInfo(deptName);
}

public void UpdateRoles(IEnumerable<AdminUserRole> rolesToBeAssigned,
IEnumerable<AdminUserPermission> permissions)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate
{
public class UserDept
{
protected UserDept() { }

public AdminUserId AdminUserId { get; private set; } = default!;
public DeptId DeptId { get; private set; } = default!;
public string DeptName { get; private set; } = string.Empty;

public UserDept(DeptId deptId, string deptName)
{
DeptId = deptId;
DeptName = deptName;
}

public void UpdateDeptInfo(string deptName)
{
DeptName = deptName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using NetCorePal.D3Shop.Domain.DomainEvents.Identity;
using NetCorePal.Extensions.Domain;
using NetCorePal.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate
{
public partial record DeptId : IInt64StronglyTypedId;

/// <summary>
/// 部门
/// </summary>
public class Department : Entity<DeptId>, IAggregateRoot
{

/// <summary>
/// 部门名称
/// </summary>

public string Name { get; private set; } = string.Empty;

/// <summary>
/// 描述
/// </summary>
public string Description { get; private set; } = string.Empty;

/// <summary>
/// 父部门id
/// </summary>

public DeptId ParentId { get; private set; } = new DeptId(0);

public DateTime CreatedAt { get; init; }

public bool IsDeleted { get; private set; }
public DateTime? DeletedAt { get; private set; }

public virtual ICollection<DepartmentUser> Users { get; } = [];


protected Department()
{

}

public Department(string name, string description, DeptId parentId, IEnumerable<DepartmentUser> deptUsers)
{
Name = name;
Description = description;
ParentId = parentId;
CreatedAt = DateTime.Now;
foreach (var user in deptUsers)
{
Users.Add(user);
}
}

/// <summary>
/// 修改部门信息
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="deptUsers"></param>
public void UpdateDepartInfo(string name, string description, IEnumerable<DepartmentUser> deptUsers)
{
Name = name;
Description = description;

var currentUserMap = Users.ToDictionary(r => r.UserId);
var targetUserMap = deptUsers.ToDictionary(r => r.UserId);

var userIdsToRemove = currentUserMap.Keys.Except(targetUserMap.Keys);
foreach (var userId in userIdsToRemove)
{
Users.Remove(currentUserMap[userId]);
}

var userIdsToAdd = targetUserMap.Keys.Except(currentUserMap.Keys);
foreach (var userId in userIdsToAdd)
{
var targetUser = targetUserMap[userId];
Users.Add(targetUser);
}

AddDomainEvent(new DepartmentInfoChangedDomainEvent(this));
}


public void UpdateDepartmentUserName(AdminUserId userId, string userName)
{
var savedUser = Users.FirstOrDefault(r => r.UserId == userId);
savedUser?.UpdateUserInfo(userName);
}


public void Delete()
{
if (IsDeleted) throw new KnownException("部门已经被删除!");
IsDeleted = true;
DeletedAt = DateTime.Now;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate
{

/// <summary>
/// 部门用户
/// </summary>
public class DepartmentUser
{
protected DepartmentUser()
{

}

/// <summary>
/// 部门id
/// </summary>
public DeptId DeptId { get; set; } = default!;

/// <summary>
/// 用户名称
/// </summary>
public string UserName { get; private set; } = string.Empty;

/// <summary>
/// 部门id
/// </summary>
public AdminUserId UserId { get; set; } = default!;

public DepartmentUser(string userName, AdminUserId userId)
{
UserName = userName;
UserId = userId;
}

public void UpdateUserInfo(string userName)
{
UserName = userName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using NetCorePal.Extensions.Domain;

namespace NetCorePal.D3Shop.Domain.DomainEvents.Identity;

public record DepartmentInfoChangedDomainEvent(Department Department) : IDomainEvent;
3 changes: 3 additions & 0 deletions src/NetCorePal.D3Shop.Infrastructure/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NetCorePal.D3Shop.Domain.AggregatesModel.DeliverAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.AdminUserAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.RoleAggregate;
using NetCorePal.D3Shop.Domain.AggregatesModel.Identity.DepartmentAggregate;

namespace NetCorePal.D3Shop.Infrastructure
{
Expand Down Expand Up @@ -37,6 +38,8 @@ protected override void ConfigureConventions(ModelConfigurationBuilder configura
#region Identity
public DbSet<AdminUser> AdminUsers => Set<AdminUser>();
public DbSet<Role> Roles => Set<Role>();

public DbSet<Department> Departments => Set<Department>();
#endregion
}
}
Loading
Loading