188 lines
8.2 KiB
C#
188 lines
8.2 KiB
C#
//using System.Collections;
|
||
//using System.Reflection;
|
||
//using Dlhis.Basic.Common.DataExport.Request;
|
||
//using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||
//using Kurisu.AspNetCore.Utils.Extensions;
|
||
//using Mapster;
|
||
//using Microsoft.AspNetCore.Mvc;
|
||
//using Newtonsoft.Json.Linq;
|
||
//using OfficeOpenXml;
|
||
//using SqlSugar;
|
||
|
||
//namespace Dlhis.Basic.Common.DataExport.Services.Implements
|
||
//{
|
||
// public class DataExportService : IDataExportService
|
||
// {
|
||
// protected IDbContext DbContext { get; }
|
||
|
||
// public DataExportService(IDbContext dbContext)
|
||
// {
|
||
// DbContext = dbContext;
|
||
// ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||
// }
|
||
|
||
// private readonly static MethodInfo _queryMethod = typeof(IAdo).GetMethod(nameof(IAdo.SqlQueryAsync), 1, new[] { typeof(string), typeof(object) }, null);
|
||
// private readonly static MethodInfo _handlerTaskMethod = typeof(DataExportService).GetMethod(nameof(HandlerResult), BindingFlags.NonPublic | BindingFlags.Instance);
|
||
|
||
// public async Task<FileContentResult> GetDataExportAsync(DataExportRequest request)
|
||
// {
|
||
// var types = Assembly.Load("Dlhis.Entity")
|
||
// .GetTypes()
|
||
// .Where(x => x.IsClass && x.GetCustomAttribute<SugarTable>() != null);
|
||
|
||
// var type = types.FirstOrDefault(x => x.GetCustomAttribute<SugarTable>()?.TableName == request.TableName)!;
|
||
// type.ThrowIfNull("该表不存在");
|
||
|
||
// var columns = type.GetProperties().Where(x => request.ColumnNames.Contains(x.Name)).ToList();
|
||
// var excelHeaderNames = columns.Select(x => x.GetCustomAttribute<SugarColumn>()?.ColumnDescription ?? x.Name)
|
||
// .ToList();
|
||
|
||
|
||
// using (var package = new ExcelPackage())
|
||
// {
|
||
// ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(request.TableName);
|
||
|
||
// // 写入表头
|
||
// for (int i = 0; i < excelHeaderNames.Count; i++)
|
||
// {
|
||
// worksheet.Cells[1, i + 1].Value = excelHeaderNames[i];
|
||
// }
|
||
|
||
// var tableName = type.GetCustomAttribute<SugarTable>().TableName;
|
||
// var fields = columns.Select(x => x.Name).Join(",");
|
||
|
||
// var task = _queryMethod.MakeGenericMethod(type).Invoke(DbContext.Client.Ado, new object[] { @$"SELECT {fields} FROM {tableName}", null });
|
||
// var data = _handlerTaskMethod.MakeGenericMethod(type).Invoke(this, new object[] { task });
|
||
|
||
// int row = 2;
|
||
// foreach (var item in data.ToJson().ToObject<List<JObject>>())
|
||
// {
|
||
// for (int col = 0; col < columns.Count; col++)
|
||
// {
|
||
// var column = columns[col];
|
||
// var value = item[column.Name]!.Value<string>();
|
||
|
||
// if (column.PropertyType.IsEnum)
|
||
// {
|
||
// value = column.PropertyType.GetFields(BindingFlags.Public | BindingFlags.Static).Where(p => (int)p.GetValue(null) == item[column.Name]!.Value<int>()).Select(p =>
|
||
// {
|
||
// var e = (Enum)Enum.Parse(p.FieldType, p.GetValue(null).ToString());
|
||
// var fd = e.GetDescription();
|
||
|
||
// return fd;
|
||
// }).First();
|
||
// }
|
||
|
||
// worksheet.Cells[row, col + 1].Value = value;
|
||
// }
|
||
// row++;
|
||
// }
|
||
|
||
// var ms = new MemoryStream();
|
||
|
||
// await package.SaveAsAsync(ms);
|
||
// var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||
|
||
// return new FileContentResult(ms.ToArray(), contentType);
|
||
|
||
// }
|
||
// }
|
||
// private List<TResult> HandlerResult<TResult>(object o)
|
||
// {
|
||
// var originalValue = (Task<List<TResult>>)o;
|
||
// if (!originalValue.IsCompleted)
|
||
// {
|
||
// return Task.Run(() => originalValue).GetAwaiter().GetResult();
|
||
// }
|
||
|
||
// return originalValue.Result;
|
||
// }
|
||
|
||
// public async Task DataImportAsync(IFormFile file)
|
||
// {
|
||
// //获取数据
|
||
// ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
||
// using (var package = new ExcelPackage(file.OpenReadStream()))
|
||
// {
|
||
// var worksheet = package.Workbook.Worksheets[0];
|
||
|
||
// //通过表名,获取实体表类型
|
||
// var type = GetEntityTypeByTableName(worksheet.Name);
|
||
// var tableType = typeof(List<>).MakeGenericType(type);
|
||
// var listGenericAddMethod = tableType.GetMethod("Add");
|
||
// var tableTypeInstance = Activator.CreateInstance(tableType);
|
||
|
||
// var headers = new List<string>();
|
||
// for (int col = 1; col <= worksheet.Dimension.Columns; col++)
|
||
// {
|
||
// headers.Add(worksheet.Cells[1, col].Value?.ToString() ?? string.Empty);
|
||
// }
|
||
|
||
// var properties = type.GetProperties();
|
||
// for (int row = 2; row <= worksheet.Dimension.Rows; row++)
|
||
// {
|
||
// var item = Activator.CreateInstance(type);
|
||
|
||
// for (int col = 1; col <= worksheet.Dimension.Columns; col++)
|
||
// {
|
||
// var header = headers[col - 1];
|
||
// var property = properties.FirstOrDefault(p => (p.GetCustomAttribute<SugarColumn>()?.ColumnDescription ?? p.Name).Equals(header, StringComparison.OrdinalIgnoreCase));
|
||
|
||
// var value = worksheet.Cells[row, col].Value.Adapt(typeof(object), property.PropertyType);
|
||
// if (property != null)
|
||
// {
|
||
// if (property.PropertyType.IsEnum)
|
||
// {
|
||
// try
|
||
// {
|
||
// var enumValue = Enum.Parse(property.PropertyType, value.ToString(), true);
|
||
// property.SetValue(item, enumValue);
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// Console.WriteLine($"无法将值 '{value}' 转换为枚举类型 '{property.PropertyType.Name}':{ex.Message}");
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// property.SetValue(item, value);
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
// listGenericAddMethod.Invoke(tableTypeInstance, new[] { item });
|
||
// }
|
||
|
||
// //foreach (var item in list)
|
||
// // await DbContext.Client.InsertableByObject(item).ExecuteCommandAsync();
|
||
|
||
// var insertMethod = typeof(IDbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance)
|
||
// .Where(x => x.Name == nameof(DbContext.InsertAsync))
|
||
// .Where(x =>
|
||
// {
|
||
// var t = x.GetParameters().First().ParameterType;
|
||
|
||
// var rr = x.GetParameters().First().ParameterType.IsAssignableTo(typeof(IList)) && !t.IsArray;
|
||
|
||
// return rr;
|
||
// })
|
||
// .First();
|
||
|
||
// var task = (Task<int>)insertMethod.MakeGenericMethod(type).Invoke(DbContext, new object[] { tableTypeInstance });
|
||
// await task.ConfigureAwait(false);
|
||
// }
|
||
// }
|
||
|
||
|
||
|
||
// public Type GetEntityTypeByTableName(string tableName)
|
||
// {
|
||
// var type = Assembly.Load("Dlhis.Entity")
|
||
// .GetTypes()
|
||
// .Where(x => x.IsClass && x.GetCustomAttribute<SugarTable>() != null)
|
||
// .FirstOrDefault(x => x.GetCustomAttribute<SugarTable>().TableName == tableName);
|
||
|
||
// return type;
|
||
// }
|
||
// }
|
||
//} |