diff --git a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs index c3cc2e7..1dcfec3 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobInputPageController.cs @@ -18,7 +18,7 @@ using TaskManager.EntityFramework.Repository; namespace TaskManager.Controllers { - public class CheryRecurringJobInputPageController : RecurringJobBaseController where T : BaseEntity + public class CheryRecurringJobInputPageController : RecurringJobBaseController where T : BaseEntity ,new() { protected readonly IRepository _repository; public CheryRecurringJobInputPageController(HttpClient httpClient, JobDbContext jobDbContext, LogController log, IRepository repository) : base(httpClient, jobDbContext, log) @@ -256,19 +256,14 @@ namespace TaskManager.Controllers }; // 可以在这里构建表达式树过滤条件 - Expression> filter = null; + //Expression> filter = null; var pagedResult = await _repository.GetPagedAsync(null, pagingParams); - - var dataTable = pagedResult.Data.ToDataTable(); - - - - return await ExportFile(dataTable, Guid.NewGuid().ToString() + ".xlsx"); + return await ExportFile(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx"); } - protected async Task ExportFile(DataTable dtos, string fileName) + protected async Task ExportFile(ICollection dtos, string fileName) { var excelExporter = HttpContext.RequestServices.GetRequiredService(); var res = await excelExporter.ExportAsByteArray(dtos); @@ -278,6 +273,44 @@ namespace TaskManager.Controllers + [HttpGet] + + public async Task GetImportTemplate() + { + try + { + // 创建导入模板生成器 + var importer = new ExcelImporter(); + + // 生成导入模板流(这里假设使用 YourModel 作为导入模型) + var bytes = await importer.GenerateTemplateBytes(); + + + using var stream = new MemoryStream(bytes); + stream.Seek(0, SeekOrigin.Begin); + + // 设置友好的文件名,例如:"导入模板_20250530.xlsx" + var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx"; + + // 返回文件流结果 + return File( + fileStream: stream, + contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + fileDownloadName: fileName); + } + catch (Exception ex) + { + // 记录异常日志 + Console.WriteLine($"生成导入模板时出错: {ex.Message}"); + + // 返回错误响应 + return StatusCode(500, "生成导入模板时发生错误"); + } + } + + + + diff --git a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs index cbff2ca..9908581 100644 --- a/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs +++ b/API/Wood.Service/Controllers/CheryRecurringJobOutPageController.cs @@ -28,32 +28,7 @@ namespace TaskManager.Controllers _repository = repository; } - protected async Task ExportFile(ICollection dtos, string fileName) where T : class, new() - { - var excelExporter = HttpContext.RequestServices.GetRequiredService(); - var res = await excelExporter.ExportAsByteArray(dtos); - return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + fileName }; - } - - public async Task Export([FromQuery] int pageNumber = 1, - [FromQuery] int pageSize = 10, - [FromQuery] string sortBy = "", - [FromQuery] bool isAscending = true, - [FromQuery] Dictionary filters = null) - { - var pagingParams = new PagingParams - { - PageNumber = pageNumber, - PageSize = pageSize, - SortBy = sortBy, - IsAscending = isAscending, - Filters = filters - }; - // 可以在这里构建表达式树过滤条件 - Expression> filter = null; - var pagedResult = await _repository.GetPagedAsync(filter, pagingParams); - return await ExportFile(pagedResult.Data.ToDataTable(), Guid.NewGuid().ToString() + ".xlsx"); - } + [NonAction] public async Task> FetchAllDataAsync(string inputdate) @@ -381,8 +356,33 @@ namespace TaskManager.Controllers return Ok(pagedResult); } - - protected async Task ExportFile(DataTable dtos, string fileName) + + + [HttpGet] + public async Task Export([FromQuery] int pageNumber = 1, + [FromQuery] int pageSize = 10, + [FromQuery] string sortBy = "", + [FromQuery] bool isAscending = true, + [FromQuery] Dictionary filters = null) + { + var pagingParams = new PagingParams + { + PageNumber = pageNumber, + PageSize = pageSize, + SortBy = sortBy, + IsAscending = isAscending, + Filters = filters + }; + + // 可以在这里构建表达式树过滤条件 + //Expression> filter = null; + + var pagedResult = await _repository.GetPagedAsync(null, pagingParams); + return await ExportFile(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx"); + + + } + protected async Task ExportFile(ICollection dtos, string fileName) { var excelExporter = HttpContext.RequestServices.GetRequiredService(); var res = await excelExporter.ExportAsByteArray(dtos); @@ -392,6 +392,42 @@ namespace TaskManager.Controllers + [HttpGet] + + public async Task GetImportTemplate() + { + try + { + // 创建导入模板生成器 + var importer = new ExcelImporter(); + + // 生成导入模板流(这里假设使用 YourModel 作为导入模型) + var bytes = await importer.GenerateTemplateBytes(); + + + using var stream = new MemoryStream(bytes); + stream.Seek(0, SeekOrigin.Begin); + + // 设置友好的文件名,例如:"导入模板_20250530.xlsx" + var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx"; + + // 返回文件流结果 + return File( + fileStream: stream, + contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + fileDownloadName: fileName); + } + catch (Exception ex) + { + // 记录异常日志 + Console.WriteLine($"生成导入模板时出错: {ex.Message}"); + + // 返回错误响应 + return StatusCode(500, "生成导入模板时发生错误"); + } + } + + diff --git a/API/Wood.Service/Controllers/NormalBaseController.cs b/API/Wood.Service/Controllers/NormalBaseController.cs index 9a5bfd8..978aa11 100644 --- a/API/Wood.Service/Controllers/NormalBaseController.cs +++ b/API/Wood.Service/Controllers/NormalBaseController.cs @@ -22,7 +22,7 @@ using Magicodes.ExporterAndImporter.Core.Extension; namespace Wood.Service.Controllers { [AllowAnonymous] - public class NormalBaseController:ControllerBase where T:BaseEntity + public class NormalBaseController:ControllerBase where T:BaseEntity ,new() { protected readonly JobDbContext _context; @@ -120,12 +120,14 @@ namespace Wood.Service.Controllers return Ok(pagedResult); } + + [HttpGet] public async Task Export([FromQuery] int pageNumber = 1, - [FromQuery] int pageSize = 10, - [FromQuery] string sortBy = "", - [FromQuery] bool isAscending = true, - [FromQuery] Dictionary filters = null) + [FromQuery] int pageSize = 10, + [FromQuery] string sortBy = "", + [FromQuery] bool isAscending = true, + [FromQuery] Dictionary filters = null) { var pagingParams = new PagingParams { @@ -137,19 +139,14 @@ namespace Wood.Service.Controllers }; // 可以在这里构建表达式树过滤条件 - Expression> filter = null; + //Expression> filter = null; var pagedResult = await _repository.GetPagedAsync(null, pagingParams); - - var dataTable = pagedResult.Data.ToDataTable(); - - - - return await ExportFile(dataTable, Guid.NewGuid().ToString() + ".xlsx"); + return await ExportFile(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx"); } - protected async Task ExportFile(DataTable dtos, string fileName) + protected async Task ExportFile(ICollection dtos, string fileName) { var excelExporter = HttpContext.RequestServices.GetRequiredService(); var res = await excelExporter.ExportAsByteArray(dtos); @@ -157,38 +154,42 @@ namespace Wood.Service.Controllers } - //[HttpGet] - - //public async Task GetImportTemplate() - //{ - // try - // { - // // 创建导入模板生成器 - // var importer = new ExcelImporter(); - - // // 生成导入模板流(这里假设使用 YourModel 作为导入模型) - // byte[] by = await importer.GenerateTemplate; - // using var stream = new MemoryStream(by); - // stream.Seek(0, SeekOrigin.Begin); - - // // 设置友好的文件名,例如:"导入模板_20250530.xlsx" - // var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx"; - - // // 返回文件流结果 - // return File( - // fileStream: stream, - // contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - // fileDownloadName: fileName); - // } - // catch (Exception ex) - // { - // // 记录异常日志 - // Console.WriteLine($"生成导入模板时出错: {ex.Message}"); - - // // 返回错误响应 - // return StatusCode(500, "生成导入模板时发生错误"); - // } - //} + + + [HttpGet] + + public async Task GetImportTemplate() + { + try + { + // 创建导入模板生成器 + var importer = new ExcelImporter(); + + // 生成导入模板流(这里假设使用 YourModel 作为导入模型) + var bytes = await importer.GenerateTemplateBytes(); + + + using var stream = new MemoryStream(bytes); + stream.Seek(0, SeekOrigin.Begin); + + // 设置友好的文件名,例如:"导入模板_20250530.xlsx" + var fileName = $"导入模板_{DateTime.Now:yyyyMMdd}.xlsx"; + + // 返回文件流结果 + return File( + fileStream: stream, + contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + fileDownloadName: fileName); + } + catch (Exception ex) + { + // 记录异常日志 + Console.WriteLine($"生成导入模板时出错: {ex.Message}"); + + // 返回错误响应 + return StatusCode(500, "生成导入模板时发生错误"); + } + } diff --git a/API/Wood.Service/Controllers/TaskConifgureController.cs b/API/Wood.Service/Controllers/TaskConifgureController.cs index cbd9952..eccf650 100644 --- a/API/Wood.Service/Controllers/TaskConifgureController.cs +++ b/API/Wood.Service/Controllers/TaskConifgureController.cs @@ -1,20 +1,17 @@ -using Hangfire; +using System; +using System.Data; +using System.IO; +using System.Linq.Expressions; +using System.Security.Policy; +using System.Threading.Tasks; +using Hangfire; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - - - //using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; - -using System; -using System.IO; -using System.Linq.Expressions; -using System.Security.Policy; -using System.Threading.Tasks; using TaskManager.Controllers; using TaskManager.Entity; using TaskManager.EntityFramework; @@ -723,9 +720,44 @@ namespace TaskManager.Controllers } - - - + + [HttpGet] + public async Task Exportextend([FromQuery] int pageNumber = 1, + [FromQuery] int pageSize = 10, + [FromQuery] string sortBy = "", + [FromQuery] bool isAscending = true, + [FromQuery] Dictionary filters = null) + { + var pagingParams = new PagingParams + { + PageNumber = pageNumber, + PageSize = pageSize, + SortBy = sortBy, + IsAscending = isAscending, + Filters = filters + }; + + // 可以在这里构建表达式树过滤条件 + //Expression> filter = null; + + var pagedResult = await _repository.GetPagedAsync(null, pagingParams); + + + + + + return await ExportFile(pagedResult.Data, Guid.NewGuid().ToString() + ".xlsx"); + + + } + protected async Task ExportFile(ICollection dtos, string fileName) where T : class, new() + { + var excelExporter = HttpContext.RequestServices.GetRequiredService(); + var res = await excelExporter.ExportAsByteArray(dtos); + return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "_" + fileName }; + } + + // 使用 Hangfire 注册定时任务 diff --git a/API/Wood.Util/EntityHelper.cs b/API/Wood.Util/EntityHelper.cs index 38b266b..d0ddf9e 100644 --- a/API/Wood.Util/EntityHelper.cs +++ b/API/Wood.Util/EntityHelper.cs @@ -71,157 +71,157 @@ namespace Wood.Util } - public static class DataTableHelper - { - /// - /// 将泛型列表转换为DataTable - /// - /// 实体类型 - /// 泛型列表 - /// 转换配置(可选) - /// DataTable - public static DataTable ToDataTable(this List list, Action> config = null) - { - if (list == null || list.Count == 0) - return new DataTable(); - - var tableConfig = new DataTableConfig(); - config?.Invoke(tableConfig); - - var dataTable = new DataTable(); - var properties = GetProperties(typeof(T), tableConfig); - - // 创建列 - foreach (var property in properties) - { - var columnType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; - var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) - ? mappedName - : property.Name; - - var column = new DataColumn(columnName, columnType); - dataTable.Columns.Add(column); - } - - // 填充数据 - foreach (var item in list) - { - var row = dataTable.NewRow(); - foreach (var property in properties) - { - var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) - ? mappedName - : property.Name; - - var value = property.GetValue(item); - row[columnName] = value ?? DBNull.Value; - } - dataTable.Rows.Add(row); - } - - return dataTable; - } - - /// - /// 获取需要转换的属性列表 - /// - private static PropertyInfo[] GetProperties(Type type, DataTableConfig config) - { - var bindingFlags = BindingFlags.Public | BindingFlags.Instance; - if (config.IgnoreNonPublicProperties) - bindingFlags &= ~BindingFlags.NonPublic; - - var properties = type.GetProperties(bindingFlags); - - if (config.IgnoreProperties?.Count > 0) - { - properties = Array.FindAll(properties, p => !config.IgnoreProperties.Contains(p.Name)); - } - - if (config.OnlyIncludeProperties?.Count > 0) - { - properties = Array.FindAll(properties, p => config.OnlyIncludeProperties.Contains(p.Name)); - } - - return properties; - } - - /// - /// 数据转换配置类 - /// - public class DataTableConfig : DataTableConfig - { - public new Dictionary ColumnMappings { get; } = new Dictionary(); - - /// - /// 设置列映射(属性名 -> 列名) - /// - public DataTableConfig MapColumn(string propertyName, string columnName) - { - ColumnMappings[propertyName] = columnName; - return this; - } - - /// - /// 设置忽略的属性 - /// - public new DataTableConfig IgnoreProperty(params string[] propertyNames) - { - base.IgnoreProperty(propertyNames); - return this; - } - - /// - /// 设置只包含的属性 - /// - public new DataTableConfig OnlyIncludeProperty(params string[] propertyNames) - { - base.OnlyIncludeProperty(propertyNames); - return this; - } - - /// - /// 设置是否忽略非公共属性(默认忽略) - /// - public new DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true) - { - base.SetIgnoreNonPublicProperties(ignore); - return this; - } - } - - /// - /// 数据转换配置基类 - /// - public class DataTableConfig - { - public HashSet IgnoreProperties { get; } = new HashSet(); - public HashSet OnlyIncludeProperties { get; } = new HashSet(); - public Dictionary ColumnMappings { get; } = new Dictionary(); - public bool IgnoreNonPublicProperties { get; private set; } = true; - - public DataTableConfig IgnoreProperty(params string[] propertyNames) - { - foreach (var name in propertyNames) - IgnoreProperties.Add(name); - return this; - } - - public DataTableConfig OnlyIncludeProperty(params string[] propertyNames) - { - OnlyIncludeProperties.Clear(); - foreach (var name in propertyNames) - OnlyIncludeProperties.Add(name); - return this; - } - - public DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true) - { - IgnoreNonPublicProperties = ignore; - return this; - } - } - } + //public static class DataTableHelper + //{ + // /// + // /// 将泛型列表转换为DataTable + // /// + // /// 实体类型 + // /// 泛型列表 + // /// 转换配置(可选) + // /// DataTable + // public static DataTable ToDataTable(this List list, Action> config = null) + // { + // if (list == null || list.Count == 0) + // return new DataTable(); + + // var tableConfig = new DataTableConfig(); + // config?.Invoke(tableConfig); + + // var dataTable = new DataTable(); + // var properties = GetProperties(typeof(T), tableConfig); + + // // 创建列 + // foreach (var property in properties) + // { + // var columnType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; + // var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) + // ? mappedName + // : property.Name; + + // var column = new DataColumn(columnName, columnType); + // dataTable.Columns.Add(column); + // } + + // // 填充数据 + // foreach (var item in list) + // { + // var row = dataTable.NewRow(); + // foreach (var property in properties) + // { + // var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) + // ? mappedName + // : property.Name; + + // var value = property.GetValue(item); + // row[columnName] = value ?? DBNull.Value; + // } + // dataTable.Rows.Add(row); + // } + + // return dataTable; + // } + + // /// + // /// 获取需要转换的属性列表 + // /// + // private static PropertyInfo[] GetProperties(Type type, DataTableConfig config) + // { + // var bindingFlags = BindingFlags.Public | BindingFlags.Instance; + // if (config.IgnoreNonPublicProperties) + // bindingFlags &= ~BindingFlags.NonPublic; + + // var properties = type.GetProperties(bindingFlags); + + // if (config.IgnoreProperties?.Count > 0) + // { + // properties = Array.FindAll(properties, p => !config.IgnoreProperties.Contains(p.Name)); + // } + + // if (config.OnlyIncludeProperties?.Count > 0) + // { + // properties = Array.FindAll(properties, p => config.OnlyIncludeProperties.Contains(p.Name)); + // } + + // return properties; + // } + + // /// + // /// 数据转换配置类 + // /// + // public class DataTableConfig : DataTableConfig + // { + // public new Dictionary ColumnMappings { get; } = new Dictionary(); + + // /// + // /// 设置列映射(属性名 -> 列名) + // /// + // public DataTableConfig MapColumn(string propertyName, string columnName) + // { + // ColumnMappings[propertyName] = columnName; + // return this; + // } + + // /// + // /// 设置忽略的属性 + // /// + // public new DataTableConfig IgnoreProperty(params string[] propertyNames) + // { + // base.IgnoreProperty(propertyNames); + // return this; + // } + + // /// + // /// 设置只包含的属性 + // /// + // public new DataTableConfig OnlyIncludeProperty(params string[] propertyNames) + // { + // base.OnlyIncludeProperty(propertyNames); + // return this; + // } + + // /// + // /// 设置是否忽略非公共属性(默认忽略) + // /// + // public new DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true) + // { + // base.SetIgnoreNonPublicProperties(ignore); + // return this; + // } + // } + + // /// + // /// 数据转换配置基类 + // /// + // public class DataTableConfig + // { + // public HashSet IgnoreProperties { get; } = new HashSet(); + // public HashSet OnlyIncludeProperties { get; } = new HashSet(); + // public Dictionary ColumnMappings { get; } = new Dictionary(); + // public bool IgnoreNonPublicProperties { get; private set; } = true; + + // public DataTableConfig IgnoreProperty(params string[] propertyNames) + // { + // foreach (var name in propertyNames) + // IgnoreProperties.Add(name); + // return this; + // } + + // public DataTableConfig OnlyIncludeProperty(params string[] propertyNames) + // { + // OnlyIncludeProperties.Clear(); + // foreach (var name in propertyNames) + // OnlyIncludeProperties.Add(name); + // return this; + // } + + // public DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true) + // { + // IgnoreNonPublicProperties = ignore; + // return this; + // } + // } + //}