Browse Source

租户服务分页查询新增套餐列,新增和修改接口新增租户套餐数据保存

zhontai 2 years ago
parent
commit
acc3906378

+ 9 - 0
src/platform/ZhonTai.Admin/Domain/Tenant/TenantEntity.cs

@@ -3,6 +3,9 @@ using FreeSql;
 using FreeSql.DataAnnotations;
 using ZhonTai.Admin.Domain.User;
 using ZhonTai.Admin.Domain.Org;
+using System.Collections.Generic;
+using ZhonTai.Admin.Domain.TenantPkg;
+using ZhonTai.Admin.Domain.Pkg;
 
 namespace ZhonTai.Admin.Domain.Tenant;
 
@@ -65,4 +68,10 @@ public partial class TenantEntity : EntityBase
     /// </summary>
     [Column(StringLength = 500)]
     public string Description { get; set; }
+
+    /// <summary>
+    /// 套餐列表
+    /// </summary>
+    [Navigate(ManyToMany = typeof(TenantPkgEntity))]
+    public ICollection<PkgEntity> Pkgs { get; set; }
 }

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

@@ -24,6 +24,11 @@ public class TenantAddInput
     [Required(ErrorMessage = "请输入编码")]
     public string Code { get; set; }
 
+    /// <summary>
+    /// 套餐Ids
+    /// </summary>
+    public virtual long[] PkgIds { get; set; }
+
     /// <summary>
     /// 姓名
     /// </summary>

+ 15 - 0
src/platform/ZhonTai.Admin/Services/Tenant/Dto/TenantGetOutput.cs

@@ -1,6 +1,21 @@
 
+using Newtonsoft.Json;
+using System.Collections.Generic;
+using System.Linq;
+using ZhonTai.Admin.Domain.Pkg;
+
 namespace ZhonTai.Admin.Services.Tenant.Dto;
 
 public class TenantGetOutput : TenantUpdateInput
 {
+    /// <summary>
+    /// 套餐列表
+    /// </summary>
+    [JsonIgnore]
+    public ICollection<PkgEntity> Pkgs { get; set; }
+
+    /// <summary>
+    /// 套餐Ids
+    /// </summary>
+    public override long[] PkgIds => Pkgs?.Select(a => a.Id)?.ToArray();
 }

+ 7 - 0
src/platform/ZhonTai.Admin/Services/Tenant/Dto/TenantGetPkgDto.cs

@@ -0,0 +1,7 @@
+namespace ZhonTai.Admin.Services.Pkg.Dto;
+
+public class TenantGetPkgDto
+{
+    public long Id { get; set; }
+    public string Name { get; set; }
+}

+ 10 - 0
src/platform/ZhonTai.Admin/Services/Tenant/Dto/TenantListOutput.cs

@@ -1,5 +1,7 @@
 using System;
+using System.Collections.Generic;
 using Newtonsoft.Json;
+using ZhonTai.Admin.Domain.Pkg;
 
 namespace ZhonTai.Admin.Services.Tenant.Dto;
 
@@ -20,6 +22,14 @@ public class TenantListOutput
     /// </summary>
     public string Code { get; set; }
 
+    [JsonIgnore]
+    public ICollection<PkgEntity> Pkgs { get; set; }
+
+    /// <summary>
+    /// 套餐
+    /// </summary>
+    public string[] PkgNames { get; set; }
+
     /// <summary>
     /// 姓名
     /// </summary>

+ 38 - 5
src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs

@@ -22,10 +22,11 @@ using Microsoft.AspNetCore.Identity;
 using System.Linq.Expressions;
 using System;
 using System.Collections.Generic;
-using System.Security.Policy;
-using System.Xml.Linq;
 using Yitter.IdGenerator;
-using ZhonTai.Admin.Repositories;
+using ZhonTai.Admin.Domain.Pkg;
+using ZhonTai.Admin.Domain.TenantPkg;
+using ZhonTai.Admin.Core.Auth;
+using ZhonTai.Admin.Core.Entities;
 
 namespace ZhonTai.Admin.Services.Tenant;
 
@@ -46,6 +47,7 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
     private IUserStaffRepository _userStaffRepository => LazyGetRequiredService<IUserStaffRepository>();
     private IUserOrgRepository _userOrgRepository => LazyGetRequiredService<IUserOrgRepository>();
     private IPasswordHasher<UserEntity> _passwordHasher => LazyGetRequiredService<IPasswordHasher<UserEntity>>();
+    private ITenantPkgRepository _tenantPkgRepository => LazyGetRequiredService<ITenantPkgRepository>();
 
     public TenantService()
     {
@@ -62,10 +64,12 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
         {
             var tenant = await _tenantRepository.Select
             .WhereDynamic(id)
+            .IncludeMany(a => a.Pkgs.Select(b => new PkgEntity { Id = b.Id, Name = b.Name }))
             .FirstAsync(a => new TenantGetOutput
             {
                 Name = a.Org.Name,
                 Code = a.Org.Code,
+                Pkgs = a.Pkgs,
                 UserName = a.User.UserName,
                 RealName = a.User.Name,
                 Phone = a.User.Mobile,
@@ -85,7 +89,6 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
     {
         using var _ = _tenantRepository.DataFilter.Disable(FilterNames.Tenant);
 
-
         var key = input.Filter?.Name;
 
         var list = await _tenantRepository.Select
@@ -93,6 +96,7 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
         .WhereIf(key.NotNull(), a => a.Org.Name.Contains(key))
         .Count(out var total)
         .OrderByDescending(true, a => a.Id)
+        .IncludeMany(a => a.Pkgs.Select(b => new PkgEntity { Name = b.Name }))
         .Page(input.CurrentPage, input.PageSize)
         .ToListAsync(a => new TenantListOutput
         {
@@ -102,11 +106,12 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
             RealName = a.User.Name,
             Phone = a.User.Mobile,
             Email = a.User.Email,
+            Pkgs = a.Pkgs,
         });
 
         var data = new PageOutput<TenantListOutput>()
         {
-            List = list,
+            List = Mapper.Map<List<TenantListOutput>>(list),
             Total = total
         };
 
@@ -170,6 +175,18 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
             TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
             long tenantId = tenant.Id;
 
+            //添加租户套餐
+            if (input.PkgIds != null && input.PkgIds.Any())
+            {
+                var pkgs = input.PkgIds.Select(pkgId => new TenantPkgEntity
+                {
+                    TenantId = tenantId,
+                    PkgId = pkgId
+                }).ToList();
+
+                await _tenantPkgRepository.InsertAsync(pkgs);
+            }
+
             //添加部门
             var org = new OrgEntity
             {
@@ -376,6 +393,19 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
                 Description = input.Description,
             })
             .UpdateColumns(a => new { a.Description, a.ModifiedTime }).ExecuteAffrowsAsync();
+
+            //更新租户套餐
+            await _tenantPkgRepository.DeleteAsync(a => a.TenantId == tenant.Id);
+            if (input.PkgIds != null && input.PkgIds.Any())
+            {
+                var pkgs = input.PkgIds.Select(pkgId => new TenantPkgEntity
+                {
+                    TenantId = tenant.Id,
+                    PkgId = pkgId
+                }).ToList();
+
+                await _tenantPkgRepository.InsertAsync(pkgs);
+            }
         }
     }
 
@@ -416,6 +446,9 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
             //删除角色
             await _roleRepository.Where(a => a.TenantId == id).DisableGlobalFilter(FilterNames.Tenant).ToDelete().ExecuteAffrowsAsync();
 
+            //删除租户套餐
+            await _tenantPkgRepository.DeleteAsync(a => a.TenantId == id);
+
             //删除租户
             await _tenantRepository.DeleteAsync(id);
         }

+ 18 - 0
src/platform/ZhonTai.Admin/Services/Tenant/_MapConfig.cs

@@ -0,0 +1,18 @@
+using Mapster;
+using System.Linq;
+using ZhonTai.Admin.Services.Tenant.Dto;
+
+namespace ZhonTai.Admin.Services.Tenant;
+
+/// <summary>
+/// 映射配置
+/// </summary>
+public class MapConfig : IRegister
+{
+    public void Register(TypeAdapterConfig config)
+    {
+        config
+        .NewConfig<TenantListOutput, TenantListOutput>()
+        .Map(dest => dest.PkgNames, src => src.Pkgs.Select(a => a.Name));
+    }
+}

+ 1 - 3
src/platform/ZhonTai.Admin/Services/User/Dto/UserAddInput.cs

@@ -1,6 +1,4 @@
-using System.ComponentModel.DataAnnotations;
-
-namespace ZhonTai.Admin.Services.User.Dto;
+namespace ZhonTai.Admin.Services.User.Dto;
 
 /// <summary>
 /// 添加

+ 3 - 3
src/platform/ZhonTai.Admin/Services/User/Dto/UserGetPageOutput.cs

@@ -38,6 +38,9 @@ public class UserGetPageOutput
     /// </summary>
     public UserType Type { get; set; }
 
+    [JsonIgnore]
+    public ICollection<RoleEntity> Roles { get; set; }
+
     /// <summary>
     /// 角色
     /// </summary>
@@ -53,9 +56,6 @@ public class UserGetPageOutput
     /// </summary>
     public bool Enabled { get; set; }
 
-    [JsonIgnore]
-    public ICollection<RoleEntity> Roles { get; set; }
-
     /// <summary>
     /// 创建时间
     /// </summary>

+ 0 - 3
src/platform/ZhonTai.Admin/Services/User/UserService.cs

@@ -19,7 +19,6 @@ using ZhonTai.Admin.Services.Auth.Dto;
 using ZhonTai.Admin.Services.User.Dto;
 using ZhonTai.DynamicApi;
 using ZhonTai.DynamicApi.Attributes;
-using ZhonTai.Admin.Core.Helpers;
 using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Domain.UserStaff;
 using ZhonTai.Admin.Domain.Org;
@@ -31,10 +30,8 @@ using ZhonTai.Admin.Domain.RoleOrg;
 using ZhonTai.Admin.Domain.UserOrg;
 using Microsoft.AspNetCore.Identity;
 using ZhonTai.Admin.Services.File;
-using ZhonTai.Admin.Core.Auth;
 using System.Linq.Expressions;
 using System;
-using ZhonTai.Admin.Core.Entities;
 
 namespace ZhonTai.Admin.Services.User;
 

+ 30 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -3110,6 +3110,11 @@
             说明
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Domain.Tenant.TenantEntity.Pkgs">
+            <summary>
+            套餐列表
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Domain.Tenant.TenantType">
             <summary>
             租户类型
@@ -6710,6 +6715,11 @@
             编码
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantAddInput.PkgIds">
+            <summary>
+            套餐Ids
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantAddInput.RealName">
             <summary>
             姓名
@@ -6755,6 +6765,16 @@
             说明
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantGetOutput.Pkgs">
+            <summary>
+            套餐列表
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantGetOutput.PkgIds">
+            <summary>
+            套餐Ids
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantListOutput.Id">
             <summary>
             主键
@@ -6770,6 +6790,11 @@
             企业编码
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantListOutput.PkgNames">
+            <summary>
+            套餐
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Services.Tenant.Dto.TenantListOutput.RealName">
             <summary>
             姓名
@@ -6906,6 +6931,11 @@
             <param name="input"></param>
             <returns></returns>
         </member>
+        <member name="T:ZhonTai.Admin.Services.Tenant.MapConfig">
+            <summary>
+            映射配置
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Services.User.Dto.UserAddInput">
             <summary>
             添加