feat: 😎优化租户字典
This commit is contained in:
parent
55a1082c2e
commit
8cb4cf74eb
@ -12,7 +12,7 @@ namespace Admin.NET.Core;
|
||||
[SugarTable(null, "系统字典值表")]
|
||||
[SysTable]
|
||||
[SugarIndex("index_{table}_C", nameof(Code), OrderByType.Asc)]
|
||||
public partial class SysDictData : EntityTenant
|
||||
public partial class SysDictData : EntityBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典类型Id
|
||||
|
||||
97
Admin.NET/Admin.NET.Core/Entity/SysDictDataTenant.cs
Normal file
97
Admin.NET/Admin.NET.Core/Entity/SysDictDataTenant.cs
Normal file
@ -0,0 +1,97 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 系统租户字典值表
|
||||
/// </summary>
|
||||
[SugarTable(null, "系统租户字典值表")]
|
||||
[SysTable]
|
||||
[SugarIndex("index_{table}_C", nameof(Code), OrderByType.Asc)]
|
||||
public partial class SysDictDataTenant : EntityTenant
|
||||
{
|
||||
/// <summary>
|
||||
/// 字典类型Id
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "字典类型Id")]
|
||||
public long DictTypeId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 字典类型
|
||||
/// </summary>
|
||||
[Newtonsoft.Json.JsonIgnore]
|
||||
[System.Text.Json.Serialization.JsonIgnore]
|
||||
[Navigate(NavigateType.OneToOne, nameof(DictTypeId))]
|
||||
public SysDictType DictType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 值
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "值", Length = 256)]
|
||||
[Required, MaxLength(256)]
|
||||
public virtual string Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编码
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "编码", Length = 256)]
|
||||
[Required, MaxLength(256)]
|
||||
public virtual string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "名称", Length = 256)]
|
||||
[MaxLength(256)]
|
||||
public virtual string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示样式-标签颜色
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "显示样式-标签颜色", Length = 16)]
|
||||
[MaxLength(16)]
|
||||
public string? TagType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示样式-Style(控制显示样式)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "显示样式-Style", Length = 512)]
|
||||
[MaxLength(512)]
|
||||
public string? StyleSetting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示样式-Class(控制显示样式)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "显示样式-Class", Length = 512)]
|
||||
[MaxLength(512)]
|
||||
public string? ClassSetting { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "排序", DefaultValue = "100")]
|
||||
public int OrderNo { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "备注", Length = 2048)]
|
||||
[MaxLength(2048)]
|
||||
public string? Remark { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 拓展数据(保存业务功能的配置项)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "拓展数据(保存业务功能的配置项)", ColumnDataType = StaticConfig.CodeFirst_BigString)]
|
||||
public string? ExtData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 状态
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "状态", DefaultValue = "1")]
|
||||
public StatusEnum Status { get; set; } = StatusEnum.Enable;
|
||||
}
|
||||
@ -54,6 +54,12 @@ public partial class SysDictType : EntityBase
|
||||
[SugarColumn(ColumnDescription = "是否是内置字典", DefaultValue = "1")]
|
||||
public virtual YesNoEnum SysFlag { get; set; } = YesNoEnum.Y;
|
||||
|
||||
/// <summary>
|
||||
/// 是否是租户字典(Y-是,N-否)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnDescription = "是否是租户字典", DefaultValue = "2")]
|
||||
public virtual YesNoEnum IsTenant { get; set; } = YesNoEnum.N;
|
||||
|
||||
/// <summary>
|
||||
/// 字典值集合
|
||||
/// </summary>
|
||||
|
||||
@ -367,6 +367,12 @@ public enum ErrorCodeEnum
|
||||
[ErrorCodeItemMetadata("禁止删除系统内置字典")]
|
||||
D3010,
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典值集合入参有误
|
||||
/// </summary>
|
||||
[ErrorCodeItemMetadata("获取字典值集合入参有误")]
|
||||
D3011,
|
||||
|
||||
/// <summary>
|
||||
/// 菜单已存在
|
||||
/// </summary>
|
||||
|
||||
@ -13,6 +13,7 @@ namespace Admin.NET.Core.Service;
|
||||
public class SysDictDataService : IDynamicApiController, ITransient
|
||||
{
|
||||
private readonly SqlSugarRepository<SysDictData> _sysDictDataRep;
|
||||
public readonly ISugarQueryable<SysDictData> VSysDictData;
|
||||
private readonly SysCacheService _sysCacheService;
|
||||
private readonly UserManager _userManager;
|
||||
|
||||
@ -23,6 +24,10 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
_userManager = userManager;
|
||||
_sysDictDataRep = sysDictDataRep;
|
||||
_sysCacheService = sysCacheService;
|
||||
VSysDictData = _sysDictDataRep.Context.UnionAll(
|
||||
_sysDictDataRep.AsQueryable(),
|
||||
_sysDictDataRep.Change<SysDictDataTenant>().AsQueryable()
|
||||
.Select<SysDictData>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -33,11 +38,11 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("获取字典值分页列表")]
|
||||
public async Task<SqlSugarPagedList<SysDictData>> Page(PageDictDataInput input)
|
||||
{
|
||||
return await _sysDictDataRep.AsQueryable()
|
||||
return await VSysDictData
|
||||
.Where(u => u.DictTypeId == input.DictTypeId)
|
||||
.WhereIF(!string.IsNullOrEmpty(input.Code?.Trim()), u => u.Code.Contains(input.Code))
|
||||
.WhereIF(!string.IsNullOrEmpty(input.Value?.Trim()), u => u.Value.Contains(input.Value))
|
||||
.OrderBy(u => new { u.TenantId, u.OrderNo, u.Code })
|
||||
.OrderBy(u => new { u.OrderNo, u.Code })
|
||||
.ToPagedListAsync(input.Page, input.PageSize);
|
||||
}
|
||||
|
||||
@ -60,14 +65,16 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("增加字典值")]
|
||||
public async Task AddDictData(AddDictDataInput input)
|
||||
{
|
||||
var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId);
|
||||
var isExist = await VSysDictData.AnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
|
||||
if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3008);
|
||||
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
await _sysDictDataRep.InsertAsync(input.Adapt<SysDictData>());
|
||||
|
||||
dynamic dictData = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
|
||||
await _sysDictDataRep.Context.Insertable(dictData).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -80,17 +87,18 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("更新字典值")]
|
||||
public async Task UpdateDictData(UpdateDictDataInput input)
|
||||
{
|
||||
var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Id == input.Id);
|
||||
var isExist = await VSysDictData.AnyAsync(u => u.Id == input.Id);
|
||||
if (!isExist) throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
|
||||
isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
|
||||
isExist = await VSysDictData.AnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id);
|
||||
if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(input.DictTypeId);
|
||||
if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3009);
|
||||
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
await _sysDictDataRep.UpdateAsync(input.Adapt<SysDictData>());
|
||||
dynamic dictData = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
|
||||
await _sysDictDataRep.Context.Updateable(dictData).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -103,13 +111,14 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("删除字典值")]
|
||||
public async Task DeleteDictData(DeleteDictDataInput input)
|
||||
{
|
||||
var dictData = await _sysDictDataRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
var dictData = await VSysDictData.FirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(dictData.DictTypeId);
|
||||
if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3010);
|
||||
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
await _sysDictDataRep.DeleteAsync(dictData);
|
||||
dynamic entity = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
|
||||
await _sysDictDataRep.Context.Deleteable(entity).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -120,7 +129,7 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("获取字典值详情")]
|
||||
public async Task<SysDictData> GetDetail([FromQuery] DictDataInput input)
|
||||
{
|
||||
return await _sysDictDataRep.GetByIdAsync(input.Id);
|
||||
return (await VSysDictData.FirstAsync(u => u.Id == input.Id))?.Adapt<SysDictData>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -132,7 +141,7 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("修改字典值状态")]
|
||||
public async Task SetStatus(DictDataInput input)
|
||||
{
|
||||
var dictData = await _sysDictDataRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
var dictData = await VSysDictData.FirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D3004);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(dictData.DictTypeId);
|
||||
if (dictType.SysFlag == YesNoEnum.Y && !_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.D3009);
|
||||
@ -140,7 +149,8 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
|
||||
dictData.Status = input.Status;
|
||||
await _sysDictDataRep.AsUpdateable(dictData).UpdateColumns(u => new { u.Status }, true).ExecuteCommandAsync();
|
||||
dynamic entity = dictType.IsTenant == YesNoEnum.Y ? input.Adapt<SysDictDataTenant>() : input.Adapt<SysDictData>();
|
||||
await _sysDictDataRep.Context.Updateable(entity).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -151,18 +161,7 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[NonAction]
|
||||
public async Task<List<SysDictData>> GetDictDataListByDictTypeId(long dictTypeId)
|
||||
{
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().GetByIdAsync(dictTypeId);
|
||||
var dictDataList = _sysCacheService.Get<List<SysDictData>>($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
|
||||
if (dictDataList == null)
|
||||
{
|
||||
dictDataList = await _sysDictDataRep.AsQueryable().ClearFilter()
|
||||
.InnerJoin<SysDictType>((u, a) => u.DictTypeId == a.Id)
|
||||
.Where((u, a) => a.SysFlag == YesNoEnum.Y || u.TenantId == _userManager.TenantId)
|
||||
.Where(u => u.DictTypeId == dictTypeId).OrderBy(u => new { u.OrderNo, u.Code }).ToListAsync();
|
||||
_sysCacheService.Set($"{CacheConst.KeyDict}{dictType.Code}", dictDataList);
|
||||
}
|
||||
return dictDataList;
|
||||
return await GetDataListByIdOrCode(dictTypeId, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -173,16 +172,41 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("根据字典类型编码获取字典值集合")]
|
||||
public async Task<List<SysDictData>> GetDataList(string code)
|
||||
{
|
||||
var dictDataList = _sysCacheService.Get<List<SysDictData>>($"{CacheConst.KeyDict}{code}");
|
||||
return await GetDataListByIdOrCode(null, code);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典值集合 🔖
|
||||
/// </summary>
|
||||
/// <param name="typeId"></param>
|
||||
/// <param name="code"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
public async Task<List<SysDictData>> GetDataListByIdOrCode(long? typeId, string code)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(code) && typeId == null ||
|
||||
!string.IsNullOrWhiteSpace(code) && typeId != null)
|
||||
throw Oops.Oh(ErrorCodeEnum.D3011);
|
||||
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().AsQueryable()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(code), u => u.Code == code)
|
||||
.WhereIF(typeId != null, u => u.Id == typeId)
|
||||
.FirstAsync();
|
||||
if (dictType == null) return null;
|
||||
|
||||
var dictDataList = _sysCacheService.Get<List<SysDictData>>($"{CacheConst.KeyDict}{dictType.Code}");
|
||||
if (dictDataList == null)
|
||||
{
|
||||
dictDataList = await _sysDictDataRep.Change<SysDictType>().AsQueryable()
|
||||
.LeftJoin<SysDictData>((u, a) => u.Id == a.DictTypeId).ClearFilter()
|
||||
.Where((u, a) => u.SysFlag == YesNoEnum.Y || a.TenantId == _userManager.TenantId)
|
||||
.Where((u, a) => u.Code == code && u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable)
|
||||
.OrderBy((u, a) => new { a.OrderNo, a.Code })
|
||||
.Select((u, a) => a).ToListAsync();
|
||||
_sysCacheService.Set($"{CacheConst.KeyDict}{code}", dictDataList);
|
||||
|
||||
dictDataList = await VSysDictData.InnerJoin<SysDictType>((u, a) => u.DictTypeId == a.Id)
|
||||
.Where(u => u.DictTypeId == dictType.Id)
|
||||
.Select((u, a) => new SysDictData
|
||||
{
|
||||
Status = u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable ? StatusEnum.Enable : StatusEnum.Disable,
|
||||
}, true)
|
||||
.OrderBy(u => new { u.OrderNo, u.Code })
|
||||
.ToListAsync();
|
||||
_sysCacheService.Set($"{CacheConst.KeyDict}{dictType.Code}", dictDataList);
|
||||
}
|
||||
return dictDataList;
|
||||
}
|
||||
@ -195,13 +219,9 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[DisplayName("根据查询条件获取字典值集合")]
|
||||
public async Task<List<SysDictData>> GetDataList([FromQuery] QueryDictDataInput input)
|
||||
{
|
||||
return await _sysDictDataRep.Change<SysDictType>().AsQueryable()
|
||||
.LeftJoin<SysDictData>((u, a) => u.Id == a.DictTypeId).ClearFilter()
|
||||
.Where((u, a) => u.SysFlag == YesNoEnum.Y || a.TenantId == _userManager.TenantId)
|
||||
.Where((u, a) => u.Code == input.Code)
|
||||
.WhereIF(input.Status.HasValue, (u, a) => u.Status == (StatusEnum)input.Status.Value && a.Status == (StatusEnum)input.Status.Value)
|
||||
.OrderBy((u, a) => new { a.OrderNo, a.Code })
|
||||
.Select((u, a) => a).ToListAsync();
|
||||
var dataList = await GetDataList(input.Code);
|
||||
if (input.Status.HasValue) return dataList.Where(u => u.Status == (StatusEnum)input.Status.Value).ToList();
|
||||
return dataList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -212,9 +232,12 @@ public class SysDictDataService : IDynamicApiController, ITransient
|
||||
[NonAction]
|
||||
public async Task DeleteDictData(long dictTypeId)
|
||||
{
|
||||
var dictTypeCode = await _sysDictDataRep.AsQueryable().Where(u => u.DictTypeId == dictTypeId).Select(u => u.DictType.Code).FirstAsync();
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictTypeCode}");
|
||||
var dictType = await _sysDictDataRep.Change<SysDictType>().AsQueryable().Where(u => u.Id == dictTypeId).FirstAsync();
|
||||
_sysCacheService.Remove($"{CacheConst.KeyDict}{dictType?.Code}");
|
||||
|
||||
await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId);
|
||||
if (dictType?.IsTenant == YesNoEnum.Y)
|
||||
await _sysDictDataRep.Change<SysDictDataTenant>().DeleteAsync(u => u.DictTypeId == dictTypeId);
|
||||
else
|
||||
await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId);
|
||||
}
|
||||
}
|
||||
@ -36,7 +36,7 @@ public class SysDictTypeService : IDynamicApiController, ITransient
|
||||
public async Task<SqlSugarPagedList<SysDictType>> Page(PageDictTypeInput input)
|
||||
{
|
||||
return await _sysDictTypeRep.AsQueryable()
|
||||
.WhereIF(!_userManager.SuperAdmin, u => u.SysFlag == YesNoEnum.N)
|
||||
.WhereIF(!_userManager.SuperAdmin, u => u.IsTenant == YesNoEnum.Y)
|
||||
.WhereIF(!string.IsNullOrEmpty(input.Code?.Trim()), u => u.Code.Contains(input.Code))
|
||||
.WhereIF(!string.IsNullOrEmpty(input.Name?.Trim()), u => u.Name.Contains(input.Name))
|
||||
.OrderBy(u => new { u.OrderNo, u.Code })
|
||||
@ -161,9 +161,8 @@ public class SysDictTypeService : IDynamicApiController, ITransient
|
||||
public async Task<dynamic> GetAllDictList()
|
||||
{
|
||||
var ds = await _sysDictTypeRep.AsQueryable()
|
||||
.InnerJoin<SysDictData>((u, a) => u.Id == a.DictTypeId).ClearFilter()
|
||||
.Where((u, a) => u.SysFlag == YesNoEnum.Y || a.TenantId == _userManager.TenantId)
|
||||
.Where((u, a) => u.IsDelete == false && a.IsDelete == false && u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable)
|
||||
.InnerJoin(_sysDictDataService.VSysDictData, (u, a) => u.Id == a.DictTypeId)
|
||||
.Where((u, a) => u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable)
|
||||
.Select((u, a) => new { TypeCode = u.Code, a.Code, a.Name, a.Value, a.Remark, a.OrderNo, a.TagType, a.ExtData })
|
||||
.ToListAsync();
|
||||
return ds.OrderBy(u => u.OrderNo).GroupBy(u => u.TypeCode).ToDictionary(u => u.Key, u => u);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user