diff --git a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Attributes/RelationalFieldAttribute.cs b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Attributes/RelationalFieldAttribute.cs new file mode 100644 index 00000000..4434e42b --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Attributes/RelationalFieldAttribute.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tnb.WarehouseMgr.Entities.Attributes +{ + + [AttributeUsage(AttributeTargets.Property)] + public class RelationalFieldAttribute : Attribute + { + /// + /// 关联表名称 + /// + public string Table { get; set; } + /// + /// 字段 + /// + public string Field { get; set; } + } +} diff --git a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs index f51c99cb..bc5544b2 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Security.Claims; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Aspose.Cells.Drawing; @@ -15,10 +16,15 @@ using JNPF.Common.Extension; using JNPF.DataEncryption; using JNPF.DependencyInjection; using JNPF.DynamicApiController; +using JNPF.FriendlyException; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; +using NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities; @@ -86,6 +92,77 @@ namespace Tnb.WarehouseMgr } return Task.FromResult(isMatch); } + /// + /// 解析Excel到内存 + /// + /// + /// + protected Task> ImportExcelToMemory(IFormFile file) + { + int rowIndex = 1; + Dictionary dic = new(); + IWorkbook? workbook = null; + try + { + using (Stream stream = file.OpenReadStream()) + { + // 2007版本 + if (file.Name.IndexOf(".xlsx") > 0) + workbook = new XSSFWorkbook(stream); + else if (file.Name.IndexOf(".xls") > 0) + workbook = new HSSFWorkbook(stream); + + ISheet? sheet = workbook?.GetSheetAt(0); + + if (workbook == null || sheet == null) + throw Oops.Bah("无导入数据"); + + if (sheet?.LastRowNum <= 1) + throw Oops.Bah("无导入数据"); + + ICellStyle style = workbook.CreateCellStyle(); + IFont font = workbook.CreateFont(); + font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色 + style.SetFont(font); + var nameRow = sheet?.GetRow(0); + if (nameRow != null) + { + List columns = new List(); + columns = nameRow.Cells.Select(x => x.StringCellValue).ToList(); + if (columns?.Count > 0) + { + for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++) + { + var row = sheet?.GetRow(rowIndex); + if (row != null) + { + foreach (var col in columns) + { + ICell cell = row.GetCell(columns.IndexOf(col)); + dic.Add(col, cell.StringCellValue); + } + } + } + } + } + } + } + catch (Exception) + { + + } + return Task.FromResult(dic); + } + + /// + /// 数据导入 + /// + /// + /// + protected virtual Task DataImport(IFormFile file) + { + return Task.CompletedTask; + } [NonAction] protected async Task DoUpdate(WareHouseUpInput input) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs index a80f04a3..e7f98a4b 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using DingTalk.Api.Request; using JNPF.Common.Configuration; +using JNPF.Common.Contracts; using JNPF.Common.Core.Manager; using JNPF.Common.Extension; using JNPF.Common.Helper; @@ -68,7 +70,9 @@ namespace Tnb.WarehouseMgr int row = 0; try { - List> dics = WmsImportExcelToMemory(file); + //例1 获取所有表 + + List> dics = WmsImport(file); List carrys = new List(); var carryStdDic = await _db.Queryable().ToDictionaryAsync(x => x.carrystd_code, x => x.id); List locCodes = new(); @@ -112,14 +116,35 @@ namespace Tnb.WarehouseMgr return row > 0; } + public async Task DataImport(IFormFile file,Func> getColumns) where T : BaseEntity, new() + { + var nonBeNullColumnList = new List(); + var entityInfo = _db.EntityMaintenance.GetEntityInfo(); + foreach (var column in entityInfo.Columns) + { + if (!column.IsNullable) + { + nonBeNullColumnList.Add(column.DbColumnName); + } + } + var importData = WmsImport(file); //ImportExcelToMemory where T:BaseEntity,new() + var fillColumns = nonBeNullColumnList.Except(importData.Keys).ToList(); + if (fillColumns?.Count > 0) + { + + } + + } + + /// /// WMS导入 /// /// - private static List> WmsImportExcelToMemory(IFormFile file) + private static Dictionary WmsImport(IFormFile file) { int rowIndex = 1; - List> dics = new List>(); + Dictionary dic = new(); IWorkbook? workbook = null; try { @@ -144,8 +169,6 @@ namespace Tnb.WarehouseMgr font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色 style.SetFont(font); var nameRow = sheet?.GetRow(0); - Dictionary dic = new Dictionary(); - if (nameRow != null) { List columns = new List(); @@ -162,11 +185,11 @@ namespace Tnb.WarehouseMgr ICell cell = row.GetCell(columns.IndexOf(col)); dic.Add(col, cell.StringCellValue); } - dics.Add(dic); } } } } + } } catch (Exception) @@ -175,7 +198,7 @@ namespace Tnb.WarehouseMgr throw; } - return dics; + return dic; } ///