using System.Reflection; using Dlhis.Common.Response; using Dlhis.Entity.Basic.Entity; using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.SqlSugar.Services; 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 /// [HttpGet] public List GetEnums(string code) { var types = Assembly.Load("Dlhis.Entity") .GetTypes() .Where(x => x.IsEnum) .WhereIf(code.IsPresent(), x => x.Name == code); 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) }; }).ToList() }; }) .ToList(); //todo cache 速度 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.Code, Value = x.Display }) .ToList() }; }); result.AddRange(dbResult); return result; } }