0
0
فهرست منبع

租户新增优化

zhontai 2 سال پیش
والد
کامیت
cce5dd87de

+ 1 - 8
src/platform/ZhonTai.Admin/Domain/Tenant/TenantEntity.cs

@@ -52,13 +52,6 @@ public partial class TenantEntity : EntityFull
 
     public UserEntity User { get; set; }
 
-    /// <summary>
-    /// 授权角色
-    /// </summary>
-    public long? RoleId { get; set; }
-
-    public RoleEntity Role { get; set; }
-
     /// <summary>
     /// 租户类型
     /// </summary>
@@ -67,7 +60,7 @@ public partial class TenantEntity : EntityFull
     /// <summary>
     /// 数据隔离类型
     /// </summary>
-    public DataIsolationType DataIsolationType { get; set; } = DataIsolationType.OwnDb;
+    public DataIsolationType DataIsolationType { get; set; } = DataIsolationType.Share;
 
     /// <summary>
     /// 数据库

+ 10 - 5
src/platform/ZhonTai.Admin/Services/Tenant/Dto/TenantAddInput.cs

@@ -1,4 +1,5 @@
-using ZhonTai.Admin.Core.Entities;
+using System.ComponentModel.DataAnnotations;
+using ZhonTai.Admin.Core.Entities;
 
 namespace ZhonTai.Admin.Services.Tenant.Dto;
 
@@ -8,23 +9,27 @@ namespace ZhonTai.Admin.Services.Tenant.Dto;
 public class TenantAddInput
 {
     /// <summary>
-    /// 编码
+    /// 企业名称
     /// </summary>
-    public string Code { get; set; }
+    [Required(ErrorMessage = "请输入企业名称")]
+    public string Name { get; set; }
 
     /// <summary>
-    /// 企业名称
+    /// 编码
     /// </summary>
-    public string Name { get; set; }
+    [Required(ErrorMessage = "请输入编码")]
+    public string Code { get; set; }
 
     /// <summary>
     /// 姓名
     /// </summary>
+    [Required(ErrorMessage = "请输入姓名")]
     public string RealName { get; set; }
 
     /// <summary>
     /// 手机号码
     /// </summary>
+    [Required(ErrorMessage = "请输入手机号码")]
     public string Phone { get; set; }
 
     /// <summary>

+ 28 - 11
src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs

@@ -35,7 +35,7 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
     private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
     private IOrgRepository _orgRepository => LazyGetRequiredService<IOrgRepository>();
     private IStaffRepository _staffRepository => LazyGetRequiredService<IStaffRepository>();
-    private IRepositoryBase<UserOrgEntity> _empOrgRepository => LazyGetRequiredService<IRepositoryBase<UserOrgEntity>>();
+    private IRepositoryBase<UserOrgEntity> _userOrgRepository => LazyGetRequiredService<IRepositoryBase<UserOrgEntity>>();
 
     private AppConfig _appConfig => LazyGetRequiredService<AppConfig>();
 
@@ -100,6 +100,16 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
     [Transaction]
     public virtual async Task<IResultOutput> AddAsync(TenantAddInput input)
     {
+        if (await _tenantRepository.Select.AnyAsync(a => a.Name == input.Name))
+        {
+            return ResultOutput.NotOk($"企业名称已存在");
+        }
+
+        if (await _tenantRepository.Select.AnyAsync(a => a.Code == input.Code))
+        {
+            return ResultOutput.NotOk($"企业编码已存在");
+        }
+
         //添加租户
         var entity = Mapper.Map<TenantEntity>(input);
         var tenant = await _tenantRepository.InsertAsync(entity);
@@ -118,8 +128,9 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
 
         //添加主管理员
         var pwd = MD5Encrypt.Encrypt32(_appConfig.DefaultPassword);
-        var user = new UserEntity { 
-            TenantId = tenantId, 
+        var user = new UserEntity
+        {
+            TenantId = tenantId,
             UserName = input.Phone,
             Password = pwd,
             Name = input.RealName,
@@ -145,10 +156,11 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
             UserId = userId,
             OrgId = org.Id
         };
-        await _empOrgRepository.InsertAsync(userOrg);
+        await _userOrgRepository.InsertAsync(userOrg);
 
         //添加角色
-        var role = new RoleEntity { 
+        var role = new RoleEntity
+        {
             TenantId = tenantId,
             Name = "主管理员",
             Code = "admin"
@@ -156,16 +168,15 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
         await _roleRepository.InsertAsync(role);
 
         //添加用户角色
-        var userRole = new UserRoleEntity() 
-        { 
-            UserId = userId, 
-            RoleId = role.Id 
+        var userRole = new UserRoleEntity()
+        {
+            UserId = userId,
+            RoleId = role.Id
         };
         await _userRoleRepository.InsertAsync(userRole);
 
-        //更新租户的用户和角色
+        //更新租户的用户
         tenant.UserId = userId;
-        tenant.RoleId = role.Id;
         await _tenantRepository.UpdateAsync(tenant);
 
         return ResultOutput.Ok();
@@ -208,6 +219,12 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
         //删除用户角色
         await _userRoleRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
 
+        //删除员工
+        await _staffRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
+
+        //删除用户部门
+        await _userOrgRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
+
         //删除用户
         await _userRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();