namespace CloudGaming.Shared.Admin.ApplicationServices; /// /// 服务父类 /// /// 数据库实体 /// 数据库实体主键类型 /// 查询检索Dto /// 表单保存Dto public abstract class ApplicationService(IServiceProvider serviceProvider) : ApplicationService(serviceProvider) where TEntity : class, new() where TSearchDto : class, new() where TSaveFormDto : class, new() { /// /// 获取列表数据 /// /// /// public virtual async Task FindListAsync(PagingSearchInput 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; } /// /// 获取列表数据 /// /// /// public virtual async Task> 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; } /// /// 根据id数组删除 /// /// /// public virtual Task DeleteListAsync(List ids) { return Repository.DeleteByIdsAsync(ids); } /// /// 查询表单数据 /// /// /// public virtual async Task> FindFormAsync(TKey id) { var res = new Dictionary(); 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; } /// /// 保存数据 /// /// /// public virtual Task SaveFormAsync(TSaveFormDto form) { return Repository.InsertOrUpdateAsync(form as TEntity ?? throw MessageBox.Show("保存函数参数Dto不是数据库类型,请对保存函数重写!")); } /// /// 导出Excel /// /// /// public virtual async Task ExportExcelAsync(PagingSearchInput pagingSearchInput) { pagingSearchInput.Page = -1; var tableViewModel = await FindListAsync(pagingSearchInput); return ExcelUtil.ExportExcelByPagingView(tableViewModel, null, "Id"); } /// /// /// /// /// /// /// 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>(condition, parameter); var filteredQuery = query.Where(whereExpression); // 构建排序表达式:it => (int?)it.[orderIdName] var orderByExpression = Expression.Lambda>( Expression.Convert(Expression.Property(parameter, orderIdName), typeof(int?)), parameter ); // 执行查询并获取最大值 var epg = filteredQuery.Max(orderByExpression); if (epg == null) { return 1; } return (epg ?? 0) + 1; } }