using System.Reflection; using Dlhis.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.Common.EnumSetting; /// /// 枚举 /// [Authorize] [Route("api/[controller]")] [ApiController] public class EnumsController : ControllerBase { private readonly IDbContext _dbContext; public EnumsController(IDbContext dbContext) { _dbContext = dbContext; } /// /// 获取所有枚举/基础类型 /// /// code /// [DisableApiLog] [HttpGet] public List 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 { Key = fd, Value = (int)item.GetValue(null) }; }) .OrderBy(x => x.Value) .ToList() }; }) .ToList(); var dbTypes = _dbContext.Queryable().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 { 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 { Key = fd, Value = (int)item.GetValue(null) }; }) .OrderBy(x => x.Value) .ToList() }; } }