fix common合并
This commit is contained in:
110
Src/Dlhis.Basic/Common/EnumSetting/EnumsController.cs
Normal file
110
Src/Dlhis.Basic/Common/EnumSetting/EnumsController.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Reflection;
|
||||
using Dlhis.Basic.Common.FileOperation.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Kurisu.AspNetCore.CustomClass;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.UnifyResultAndValidation.Aop;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Basic.Common.EnumSetting;
|
||||
|
||||
/// <summary>
|
||||
/// 枚举
|
||||
/// </summary>
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class EnumsController : ControllerBase
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public EnumsController(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有枚举/基础类型
|
||||
/// </summary>
|
||||
/// <param name="code">code</param>
|
||||
/// <returns></returns>
|
||||
[DisableApiLog]
|
||||
[HttpGet]
|
||||
public List<CommonEnumResponse> GetEnums(string code)
|
||||
{
|
||||
var types = Assembly.Load("Dlhis.Entity")
|
||||
.GetTypes()
|
||||
.Where(x => x.IsEnum);
|
||||
|
||||
var result = types.Select(x =>
|
||||
{
|
||||
var d = x.GetDescription();
|
||||
return new CommonEnumResponse
|
||||
{
|
||||
Code = x.Name,
|
||||
Name = d,
|
||||
Data = x.GetFields(BindingFlags.Public | BindingFlags.Static).Select(item =>
|
||||
{
|
||||
var e = (Enum)Enum.Parse(item.FieldType, item.GetValue(null).ToString());
|
||||
var fd = e.GetDescription();
|
||||
return new KeyValueDto<string, object>
|
||||
{
|
||||
Key = fd,
|
||||
Value = (int)item.GetValue(null)
|
||||
};
|
||||
})
|
||||
.OrderBy(x => x.Value)
|
||||
.ToList()
|
||||
};
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var dbTypes = _dbContext.Queryable<BasicTypeEntity>().OrderBy(x => x.Sequence).ToList();
|
||||
var dbResult = dbTypes.GroupBy(x => x.Type).Select(g =>
|
||||
{
|
||||
var type = g.Key;
|
||||
return new CommonEnumResponse
|
||||
{
|
||||
Code = type,
|
||||
Name = type,
|
||||
Data = g.AsEnumerable().Select(x => new KeyValueDto<string, object>
|
||||
{
|
||||
Key = x.Display,
|
||||
Value = x.Code
|
||||
})
|
||||
.OrderBy(x => x.Value)
|
||||
.ToList()
|
||||
};
|
||||
});
|
||||
|
||||
result.AddRange(dbResult);
|
||||
|
||||
result.Add(GetCurrentEnums("LogLevel", "日志级别", typeof(LogLevel)));
|
||||
|
||||
return result.WhereIf(code.IsPresent(), x => x.Code == code).ToList();
|
||||
}
|
||||
|
||||
|
||||
private static CommonEnumResponse GetCurrentEnums(string code, string name, Type enumType)
|
||||
{
|
||||
return new CommonEnumResponse
|
||||
{
|
||||
Code = code,
|
||||
Name = name,
|
||||
Data = enumType.GetFields(BindingFlags.Public | BindingFlags.Static).Select(item =>
|
||||
{
|
||||
var e = (Enum)Enum.Parse(item.FieldType, item.GetValue(null).ToString());
|
||||
var fd = e.GetDescription();
|
||||
return new KeyValueDto<string, object>
|
||||
{
|
||||
Key = fd,
|
||||
Value = (int)item.GetValue(null)
|
||||
};
|
||||
})
|
||||
.OrderBy(x => x.Value)
|
||||
.ToList()
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user