fix 基础数据代码调整

This commit is contained in:
2024-01-09 17:56:24 +08:00
parent 2ac9e4ceb2
commit 4b7a0f8682
5 changed files with 102 additions and 101 deletions

View File

@@ -1,5 +1,8 @@
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;
@@ -13,37 +16,67 @@ namespace Dlhis.Common.EnumSetting;
[ApiController]
public class EnumsController : ControllerBase
{
private readonly IDbContext _dbContext;
public EnumsController(IDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// 获取所有枚举
/// 获取所有枚举/基础类型
/// </summary>
/// <param name="code">枚举code</param>
/// <param name="code">code</param>
/// <returns></returns>
[HttpGet]
public object GetEnums(string code)
public List<CommonEnumResponse> GetEnums(string code)
{
var types = Assembly.Load("Dlhis.Entity")
.GetTypes()
.Where(x => x.IsEnum)
.WhereIf(code.IsPresent(), x => x.Name == code);
.WhereIf(code.IsPresent(), x => x.Name == code);
return types.Select(x =>
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<BasicTypeEntity>().OrderBy(x => x.Sequence).ToList();
var dbResult = dbTypes.GroupBy(x => x.Type).Select(g =>
{
var d = x.GetDescription();
return new
var type = g.Key;
return new CommonEnumResponse
{
Code = x.Name,
Name = d,
Data = x.GetFields(BindingFlags.Public | BindingFlags.Static).Select(item =>
Code = type,
Name = type,
Data = g.AsEnumerable().Select(x => new KeyValueDto
{
var e = (Enum)Enum.Parse(item.FieldType, item.GetValue(null).ToString());
var fd = e.GetDescription();
return new
{
Key = fd,
Value = (int)item.GetValue(null)
};
Key = x.Code,
Value = x.Display
})
.ToList()
};
});
result.AddRange(dbResult);
return result;
}
}

View File

@@ -1,48 +0,0 @@
using Dlhis.Entity.Basic.Entity;
using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Dlhis.Common.EnumSetting;
/// <summary>
/// 类型
/// </summary>
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class TypesController : ControllerBase
{
private readonly IDbContext _dbContext;
public TypesController(IDbContext dbContext)
{
_dbContext = dbContext;
}
/// <summary>
/// Basic类型表
/// </summary>
/// <returns></returns>
[HttpGet]
public object GetTypes()
{
var types = _dbContext.Queryable<BasicTypeEntity>().ToList().AsEnumerable();
var distinctTypes = types.Select(x => x.Type).Distinct();
return distinctTypes.Select(type =>
{
var date = types.Where(item => item.Type == type)
.Select(item => new
{
Key = item.Code,
Value = item.Descript
});
return new
{
Type = type,
Date = date
};
});
}
}

View File

@@ -0,0 +1,9 @@
namespace Dlhis.Common.Response
{
public class CommonEnumResponse
{
public string Code { get; set; }
public string Name { get; set; }
public List<KeyValueDto> Data { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace Dlhis.Common.Response
{
public class KeyValueDto
{
public string Key { get; set; }
public object Value { get; set; }
}
}

View File

@@ -1,48 +1,47 @@
using SqlSugar;
namespace Dlhis.Entity.Basic.Entity
namespace Dlhis.Entity.Basic.Entity;
/// <summary>
/// Basic类型表
/// </summary>
[SugarTable(TableName = "BasicType", TableDescription = "Basic类型表")]
public class BasicTypeEntity : SugarBaseEntity
{
/// <summary>
/// Basic类型表
/// 类型
/// </summary>
[SugarTable(TableName = "BasicType", TableDescription = "Basic类型表")]
public class BasicTypeEntity : SugarBaseEntity
{
/// <summary>
/// 类型
/// </summary>
[SugarColumn(ColumnDescription = "类型")]
public string Type { get; set; }
[SugarColumn(ColumnDescription = "类型")]
public string Type { get; set; }
/// <summary>
/// 编码
/// </summary>
[SugarColumn(ColumnDescription = "编码")]
public string Code { get; set; }
/// <summary>
/// 编码
/// </summary>
[SugarColumn(ColumnDescription = "编码")]
public string Code { get; set; }
/// <summary>
/// 中文描述
/// </summary>
[SugarColumn(ColumnDescription = "中文描述")]
public string Descript { get; set; }
/// <summary>
/// 中文描述
/// </summary>
[SugarColumn(ColumnDescription = "中文描述")]
public string Display { get; set; }
/// <summary>
/// 英文描述
/// </summary>
[SugarColumn(ColumnDescription = "英文描述")]
public string DescriptEn { get; set; }
/// <summary>
/// 英文描述
/// </summary>
[SugarColumn(ColumnDescription = "英文描述")]
public string DisplayEn { get; set; }
/// <summary>
/// 备注
/// </summary>
[SugarColumn(ColumnDescription = "备注")]
public string Remark { get; set; }
/// <summary>
/// 备注
/// </summary>
[SugarColumn(ColumnDescription = "备注")]
public string Remark { get; set; }
/// <summary>
/// 排序
/// </summary>
[SugarColumn(ColumnDescription = "排序")]
public string Sort { get; set; }
/// <summary>
/// 排序
/// </summary>
[SugarColumn(ColumnDescription = "排序", DefaultValue = "0")]
public int Sequence { get; set; }
}
}