180 lines
5.8 KiB
C#
180 lines
5.8 KiB
C#
namespace CloudGaming.Shared.Admin.ApplicationServices;
|
||
|
||
/// <summary>
|
||
/// 服务父类
|
||
/// </summary>
|
||
/// <typeparam name="TEntity">数据库实体</typeparam>
|
||
/// <typeparam name="TKey">数据库实体主键类型</typeparam>
|
||
/// <typeparam name="TSearchDto">查询检索Dto</typeparam>
|
||
/// <typeparam name="TSaveFormDto">表单保存Dto</typeparam>
|
||
public abstract class ApplicationService<TEntity, TKey, TSearchDto, TSaveFormDto>(IServiceProvider serviceProvider)
|
||
: ApplicationService<TEntity>(serviceProvider)
|
||
where TEntity : class, new()
|
||
where TSearchDto : class, new()
|
||
where TSaveFormDto : class, new()
|
||
{
|
||
/// <summary>
|
||
/// 获取列表数据
|
||
/// </summary>
|
||
/// <param name="pagingSearchInput"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<PagingView> FindListAsync(PagingSearchInput<TSearchDto> pagingSearchInput)
|
||
{
|
||
var query = Repository.SelectNoTracking;
|
||
|
||
var result = await Repository.AsPagingViewAsync(query, pagingSearchInput);
|
||
// //覆盖值
|
||
// result
|
||
// .FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd"))
|
||
// .FormatValue(query, w => w.LastModificationTime, (oldValue) => oldValue?.ToString("yyyy-MM-dd"))
|
||
// ;
|
||
return result;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取列表数据
|
||
/// </summary>
|
||
/// <param name="pagingSearchInput"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<List<TEntity>> FindListAsync()
|
||
{
|
||
var query = Repository.SelectNoTracking;
|
||
|
||
var list = await query.ToListAsync();
|
||
// //覆盖值
|
||
// result
|
||
// .FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd"))
|
||
// .FormatValue(query, w => w.LastModificationTime, (oldValue) => oldValue?.ToString("yyyy-MM-dd"))
|
||
// ;
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据id数组删除
|
||
/// </summary>
|
||
/// <param name="ids"></param>
|
||
/// <returns></returns>
|
||
public virtual Task DeleteListAsync(List<TKey> ids)
|
||
{
|
||
return Repository.DeleteByIdsAsync(ids);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询表单数据
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<Dictionary<string, object?>> FindFormAsync(TKey id)
|
||
{
|
||
var res = new Dictionary<string, object?>();
|
||
var form = await Repository.FindByIdAsync(id);
|
||
form = form.NullSafe();
|
||
|
||
if (id is Guid newId)
|
||
{
|
||
res[nameof(id)] = newId == Guid.Empty ? "" : newId;
|
||
}
|
||
else if (id is int newIntId)
|
||
{
|
||
res[nameof(id)] = newIntId == 0 ? "" : newIntId;
|
||
}
|
||
else if (id is long newLongId)
|
||
{
|
||
res[nameof(id)] = newLongId == 0 ? "" : newLongId;
|
||
}
|
||
else
|
||
{
|
||
res[nameof(id)] = id;
|
||
}
|
||
|
||
res[nameof(form)] = form;
|
||
return res;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存数据
|
||
/// </summary>
|
||
/// <param name="form"></param>
|
||
/// <returns></returns>
|
||
public virtual Task SaveFormAsync(TSaveFormDto form)
|
||
{
|
||
return Repository.InsertOrUpdateAsync(form as TEntity ?? throw MessageBox.Show("保存函数参数Dto不是数据库类型,请对保存函数重写!"));
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 导出Excel
|
||
/// </summary>
|
||
/// <param name="pagingSearchInput"></param>
|
||
/// <returns></returns>
|
||
public virtual async Task<byte[]> ExportExcelAsync(PagingSearchInput<TSearchDto> pagingSearchInput)
|
||
{
|
||
pagingSearchInput.Page = -1;
|
||
var tableViewModel = await FindListAsync(pagingSearchInput);
|
||
return ExcelUtil.ExportExcelByPagingView(tableViewModel, null, "Id");
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="selectName"></param>
|
||
/// <param name="orderIdName"></param>
|
||
/// <param name="value"></param>
|
||
/// <returns></returns>
|
||
public virtual int? GetOrderId(string selectName, string orderIdName, string value)
|
||
{
|
||
// 获取数据源
|
||
var query = Repository.Select;
|
||
|
||
// 构建表达式树
|
||
var parameter = Expression.Parameter(typeof(TEntity), "it");
|
||
|
||
// 获取字段类型
|
||
var propertyInfo = typeof(TEntity).GetProperty(selectName);
|
||
if (propertyInfo == null)
|
||
{
|
||
throw new ArgumentException($"Property '{selectName}' not found on type '{typeof(TEntity).Name}'.");
|
||
}
|
||
|
||
// 根据字段类型转换value
|
||
object convertedValue;
|
||
if (propertyInfo.PropertyType == typeof(int))
|
||
{
|
||
convertedValue = Convert.ToInt32(value);
|
||
}
|
||
else if (propertyInfo.PropertyType == typeof(string))
|
||
{
|
||
convertedValue = value;
|
||
}
|
||
else
|
||
{
|
||
convertedValue = value; // 如果是其他类型,保持原样
|
||
}
|
||
|
||
// 构建条件表达式:it => it.[selectName] == convertedValue
|
||
var condition = Expression.Equal(
|
||
Expression.Property(parameter, selectName),
|
||
Expression.Constant(convertedValue, propertyInfo.PropertyType)
|
||
);
|
||
|
||
// 构建查询表达式:query.Where(it => it.[selectName] == convertedValue)
|
||
var whereExpression = Expression.Lambda<Func<TEntity, bool>>(condition, parameter);
|
||
var filteredQuery = query.Where(whereExpression);
|
||
|
||
// 构建排序表达式:it => (int?)it.[orderIdName]
|
||
var orderByExpression = Expression.Lambda<Func<TEntity, int?>>(
|
||
Expression.Convert(Expression.Property(parameter, orderIdName), typeof(int?)), parameter
|
||
);
|
||
|
||
// 执行查询并获取最大值
|
||
var epg = filteredQuery.Max(orderByExpression);
|
||
|
||
if (epg == null)
|
||
{
|
||
return 1;
|
||
}
|
||
return (epg ?? 0) + 1;
|
||
}
|
||
|
||
} |