From 96a99ed2dcf9c0ba8cf201499cbe8182998b958c Mon Sep 17 00:00:00 2001 From: FanLian Date: Mon, 7 Aug 2023 17:48:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Tnb.WarehouseMgr/BaseWareHouseService.cs | 49 +++++- .../WmsImportAndExportService.cs | 156 +++--------------- 2 files changed, 68 insertions(+), 137 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs index bc5544b2..a412c42c 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs @@ -16,6 +16,7 @@ using JNPF.Common.Extension; using JNPF.DataEncryption; using JNPF.DependencyInjection; using JNPF.DynamicApiController; +using JNPF.Extras.CollectiveOAuth.Enums; using JNPF.FriendlyException; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; @@ -92,15 +93,16 @@ namespace Tnb.WarehouseMgr } return Task.FromResult(isMatch); } + /// /// 解析Excel到内存 /// /// /// - protected Task> ImportExcelToMemory(IFormFile file) + protected Task>> ImportExcelToMemory(IFormFile file) { int rowIndex = 1; - Dictionary dic = new(); + List> dics = new(); IWorkbook? workbook = null; try { @@ -127,6 +129,7 @@ namespace Tnb.WarehouseMgr var nameRow = sheet?.GetRow(0); if (nameRow != null) { + Dictionary dic = new Dictionary(); List columns = new List(); columns = nameRow.Cells.Select(x => x.StringCellValue).ToList(); if (columns?.Count > 0) @@ -141,19 +144,57 @@ namespace Tnb.WarehouseMgr ICell cell = row.GetCell(columns.IndexOf(col)); dic.Add(col, cell.StringCellValue); } + dics.Add(dic); } } } } + } } catch (Exception) { + throw; } - return Task.FromResult(dic); - } + return Task.FromResult(dics); + } + + /// + /// WMS导入模板 + /// + /// + /// + /// + protected dynamic WmsTemplate(Dictionary dic, string sheetName) + { + XSSFWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + IRow nRow = sheet.CreateRow(0); + IRow dRow = sheet.CreateRow(1); + dRow.Hidden = true; + dic.Remove("id"); + dic.Remove("create_id"); + dic.Remove("create_time"); + //sheet.SetColumnHidden(1, true); + for (int i = 0; i < dic?.Count; i++) + { + ICell cell1 = nRow.CreateCell(i); + cell1.SetCellValue(dic.Values.GetIndex(i)); + ICell cell2 = dRow.CreateCell(i); + cell2.SetCellValue(dic.Keys.GetIndex(i)); + } + + MemoryStream ms = new MemoryStream(); + + workbook.Write(ms); + MemoryStream ms2 = new MemoryStream(ms.ToArray()); + ms2.Position = 0; + FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "template.xlsx" }; + return fileStreamResult; + } + /// /// 数据导入 /// diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs index 1dbe871d..b8e639e0 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs @@ -14,6 +14,7 @@ using JNPF.Common.Models; using JNPF.Common.Security; using JNPF.Extras.CollectiveOAuth.Enums; using JNPF.FriendlyException; +using JNPF.Logging; using Mapster; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -24,6 +25,7 @@ using NPOI.XSSF.UserModel; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities; +using Tnb.WarehouseMgr.Entities.Enums; namespace Tnb.WarehouseMgr { @@ -49,10 +51,10 @@ namespace Tnb.WarehouseMgr public async Task CarryImportTemplate() { int row = 0; - Dictionary dic = new Dictionary(); - dic.Add("carry_code","载具编号"); - dic.Add("carry_name","载具名称" ); - dic.Add("carrystd_id","载具规格" ); + Dictionary dic = new Dictionary(); + dic.Add("carry_code", "载具编号"); + dic.Add("carry_name", "载具名称"); + dic.Add("carrystd_id", "载具规格"); //headers = _db.DbMaintenance.GetColumnInfosByTableName("wms_carry_h").FindAll(x=>!x.IsNullable).ToDictionary(x => x.DbColumnName ,x => x.ColumnDescription); FileStreamResult fileStreamResult = WmsTemplate(dic, "载具台账"); @@ -71,8 +73,8 @@ namespace Tnb.WarehouseMgr try { //例1 获取所有表 - - Dictionary dic = WmsImport(file); + + List> dics = await ImportExcelToMemory(file); List carrys = new List(); var carryStdDic = await _db.Queryable().ToDictionaryAsync(x => x.carrystd_code, x => x.id); List locCodes = new(); @@ -80,16 +82,16 @@ namespace Tnb.WarehouseMgr if (carryStdDic?.Count > 0) { string carryStdId = string.Empty; - foreach (var d in dic) + foreach (var d in dics) { - var stdCodeKey = d.Key; + var stdCodeKey = d["carrystd_code"].ToString(); carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : ""; d.Add("carrystd_id", carryStdId); - locCodes.Add(d); + locCodes.Add(d["location_code"]?.ToString() ?? string.Empty); carryH = d.Adapt(); carrys.Add(carryH); } - if (locCodes != null) + if (!locCodes.IsNullOrEmpty()) { var locs = await _db.Queryable().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id); foreach (var loc in locCodes) @@ -102,137 +104,25 @@ namespace Tnb.WarehouseMgr carrys.ForEach(x => { x.id = SnowflakeIdHelper.NextId(); + x.is_lock = 0; + x.carry_status = ((int)EnumCarryStatus.空闲).ToString(); + x.out_status = ((int)EnumOutStatus.正常).ToString(); + x.is_check = 0; x.create_id = _userManager.UserId; x.create_time = DateTime.Now; }); } - row = await _db.Storageable(carrys).ExecuteCommandAsync(); + await _db.Ado.BeginTranAsync(); + row = await _db.Insertable(carrys).ExecuteCommandAsync(); + await _db.Ado.CommitTranAsync(); } - catch (Exception) + catch (Exception ex) { - return false; - throw; + await _db.Ado.RollbackTranAsync(); + Log.Error("导入失败",ex) + throw Oops.Bah("导入失败"); } 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 Dictionary WmsImport(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) - { - - throw; - } - - return dic; - } - - /// - /// WMS导入模板 - /// - /// - /// - /// - private static dynamic WmsTemplate(Dictionary dic, string sheetName) - { - XSSFWorkbook workbook = new XSSFWorkbook(); - ISheet sheet = workbook.CreateSheet(sheetName); - IRow nRow = sheet.CreateRow(0); - IRow dRow = sheet.CreateRow(1); - dRow.Hidden = true; - dic.Remove("id"); - dic.Remove("create_id"); - dic.Remove("create_time"); - //sheet.SetColumnHidden(1, true); - for (int i = 0; i < dic?.Count; i++) - { - ICell cell1 = nRow.CreateCell(i); - cell1.SetCellValue(dic.Values.GetIndex(i)); - ICell cell2 = dRow.CreateCell(i); - cell2.SetCellValue(dic.Keys.GetIndex(i)); - } - - MemoryStream ms = new MemoryStream(); - - workbook.Write(ms); - MemoryStream ms2 = new MemoryStream(ms.ToArray()); - ms2.Position = 0; - FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "template.xlsx" }; - return fileStreamResult; - } } } From 1ebf011961fe946b0395312e943394b304019b3a Mon Sep 17 00:00:00 2001 From: FanLian Date: Mon, 7 Aug 2023 17:50:55 +0800 Subject: [PATCH 2/4] 1 --- WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs index b8e639e0..a2370c70 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs @@ -119,7 +119,7 @@ namespace Tnb.WarehouseMgr catch (Exception ex) { await _db.Ado.RollbackTranAsync(); - Log.Error("导入失败",ex) + Log.Error("导入失败", ex); throw Oops.Bah("导入失败"); } return row > 0; From 744d053801fa037627b5b9238adc067dfe221465 Mon Sep 17 00:00:00 2001 From: FanLian Date: Mon, 7 Aug 2023 17:53:34 +0800 Subject: [PATCH 3/4] 1 --- .../Tnb.WarehouseMgr/WmsCarryLedgerService.cs | 66 ++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs index 2e35df68..c1d3ede4 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs @@ -19,6 +19,12 @@ using JNPF.VisualDev.Entitys; using Aspose.Cells.Drawing; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; +using JNPF.Logging; +using JNPF.FriendlyException; +using JNPF.Common.Security; +using Tnb.BasicData.Entities; +using Mapster; +using Aop.Api.Domain; namespace Tnb.WarehouseMgr { @@ -32,19 +38,73 @@ namespace Tnb.WarehouseMgr private readonly ISqlSugarClient _db; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; - public WmsCarryLedgerService(ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService) + private readonly IUserManager _userManager; + public WmsCarryLedgerService(ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService,IUserManager userManager) { _db = repository.AsSugarClient(); _runService = runService; _visualDevService = visualDevService; + _userManager = userManager; OverideFuncs.ImportAsync = DataImport; } private async Task DataImport(IFormFile file) { + int row = 0; + try + { + //例1 获取所有表 - return Task.FromResult(null); + List> dics = await ImportExcelToMemory(file); + List carrys = new List(); + var carryStdDic = await _db.Queryable().ToDictionaryAsync(x => x.carrystd_code, x => x.id); + List locCodes = new(); + WmsCarryH carryH = new WmsCarryH(); + if (carryStdDic?.Count > 0) + { + string carryStdId = string.Empty; + foreach (var d in dics) + { + var stdCodeKey = d["carrystd_code"].ToString(); + carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : ""; + d.Add("carrystd_id", carryStdId); + locCodes.Add(d["location_code"]?.ToString() ?? string.Empty); + carryH = d.Adapt(); + carrys.Add(carryH); + } + if (!locCodes.IsNullOrEmpty()) + { + var locs = await _db.Queryable().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id); + foreach (var loc in locCodes) + { + var c = carrys.Find(x => x.location_code == loc); + if (c != null) + c.location_id = locs[loc].ToString(); + } + } + carrys.ForEach(x => + { + x.id = SnowflakeIdHelper.NextId(); + x.is_lock = 0; + x.carry_status = ((int)EnumCarryStatus.空闲).ToString(); + x.out_status = ((int)EnumOutStatus.正常).ToString(); + x.is_check = 0; + x.create_id = _userManager.UserId; + x.create_time = DateTime.Now; + }); + } + await _db.Ado.BeginTranAsync(); + row = await _db.Insertable(carrys).ExecuteCommandAsync(); + await _db.Ado.CommitTranAsync(); + } + catch (Exception ex) + { + await _db.Ado.RollbackTranAsync(); + Log.Error("导入失败", ex); + throw Oops.Bah("导入失败"); + } + return row > 0; } - } } + From fabf6a2f4e491ed95c96eec716073973bc7ad033 Mon Sep 17 00:00:00 2001 From: FanLian Date: Tue, 8 Aug 2023 09:15:17 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BD=BD=E5=85=B7?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Consts/ModuleConsts.cs | 4 ++ .../Tnb.WarehouseMgr/BaseWareHouseService.cs | 6 +- .../Tnb.WarehouseMgr/WmsCarryLedgerService.cs | 17 ++++- .../WmsImportAndExportService.cs | 63 ------------------- 4 files changed, 23 insertions(+), 67 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Consts/ModuleConsts.cs b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Consts/ModuleConsts.cs index 604345c6..84d82bf2 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Consts/ModuleConsts.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Consts/ModuleConsts.cs @@ -164,5 +164,9 @@ public class ModuleConsts /// 模块标识-载具台账 /// public const string MODULE_WMSCARRY_ID = "26038721525029"; + /// + /// 模块标识-仓库定义 + /// + public const string MODULE_WMSWAREHOUSEDEFINITION_ID = "25905106252053"; } \ No newline at end of file diff --git a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs index e85b3053..9961055d 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/BaseWareHouseService.cs @@ -124,7 +124,7 @@ namespace Tnb.WarehouseMgr ICellStyle style = workbook.CreateCellStyle(); IFont font = workbook.CreateFont(); - font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色 + font.Color = IndexedColors.Black.Index; // 将字体颜色设置为红色 style.SetFont(font); var nameRow = sheet?.GetRow(0); if (nameRow != null) @@ -132,6 +132,10 @@ namespace Tnb.WarehouseMgr Dictionary dic = new Dictionary(); List columns = new List(); columns = nameRow.Cells.Select(x => x.StringCellValue).ToList(); + columns.ForEach(x => { + var strings =x.Split(new char[2] { '(', ')' }); + x = strings[1]; + }); if (columns?.Count > 0) { for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs index c1d3ede4..d6967c4b 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsCarryLedgerService.cs @@ -48,6 +48,11 @@ namespace Tnb.WarehouseMgr OverideFuncs.ImportAsync = DataImport; } + /// + /// 载具导入 + /// + /// + /// private async Task DataImport(IFormFile file) { int row = 0; @@ -65,9 +70,10 @@ namespace Tnb.WarehouseMgr string carryStdId = string.Empty; foreach (var d in dics) { - var stdCodeKey = d["carrystd_code"].ToString(); + var stdCodeKey = d["carrystd_id"].ToString(); carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : ""; - d.Add("carrystd_id", carryStdId); + d.Add("carrystd_code", stdCodeKey??""); + d["carrystd_id"] = carryStdId; locCodes.Add(d["location_code"]?.ToString() ?? string.Empty); carryH = d.Adapt(); carrys.Add(carryH); @@ -80,17 +86,22 @@ namespace Tnb.WarehouseMgr var c = carrys.Find(x => x.location_code == loc); if (c != null) c.location_id = locs[loc].ToString(); + } } carrys.ForEach(x => { x.id = SnowflakeIdHelper.NextId(); + x.org_id = _userManager.User.OrganizeId; + x.status = 1; x.is_lock = 0; x.carry_status = ((int)EnumCarryStatus.空闲).ToString(); x.out_status = ((int)EnumOutStatus.正常).ToString(); - x.is_check = 0; + x.is_check = 1; x.create_id = _userManager.UserId; x.create_time = DateTime.Now; + x.modify_id = null; + x.modify_time = null; }); } await _db.Ado.BeginTranAsync(); diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs index a2370c70..79d92592 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs @@ -61,68 +61,5 @@ namespace Tnb.WarehouseMgr return fileStreamResult; } - /// - /// 载具导入 - /// - /// - /// - [HttpPost] - public async Task CarryImport(IFormFile file) - { - int row = 0; - try - { - //例1 获取所有表 - - List> dics = await ImportExcelToMemory(file); - List carrys = new List(); - var carryStdDic = await _db.Queryable().ToDictionaryAsync(x => x.carrystd_code, x => x.id); - List locCodes = new(); - WmsCarryH carryH = new WmsCarryH(); - if (carryStdDic?.Count > 0) - { - string carryStdId = string.Empty; - foreach (var d in dics) - { - var stdCodeKey = d["carrystd_code"].ToString(); - carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : ""; - d.Add("carrystd_id", carryStdId); - locCodes.Add(d["location_code"]?.ToString() ?? string.Empty); - carryH = d.Adapt(); - carrys.Add(carryH); - } - if (!locCodes.IsNullOrEmpty()) - { - var locs = await _db.Queryable().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id); - foreach (var loc in locCodes) - { - var c = carrys.Find(x => x.location_code == loc); - if (c != null) - c.location_id = locs[loc].ToString(); - } - } - carrys.ForEach(x => - { - x.id = SnowflakeIdHelper.NextId(); - x.is_lock = 0; - x.carry_status = ((int)EnumCarryStatus.空闲).ToString(); - x.out_status = ((int)EnumOutStatus.正常).ToString(); - x.is_check = 0; - x.create_id = _userManager.UserId; - x.create_time = DateTime.Now; - }); - } - await _db.Ado.BeginTranAsync(); - row = await _db.Insertable(carrys).ExecuteCommandAsync(); - await _db.Ado.CommitTranAsync(); - } - catch (Exception ex) - { - await _db.Ado.RollbackTranAsync(); - Log.Error("导入失败", ex); - throw Oops.Bah("导入失败"); - } - return row > 0; - } } }