diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs new file mode 100644 index 00000000..7dcdc78c --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsImportAndExportService.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JNPF.Common.Configuration; +using JNPF.Common.Extension; +using JNPF.Common.Helper; +using JNPF.Common.Models; +using JNPF.Common.Security; +using JNPF.Extras.CollectiveOAuth.Enums; +using JNPF.FriendlyException; +using Mapster; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using NPOI.HPSF; +using NPOI.HSSF.UserModel; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; +using SqlSugar; +using Tnb.BasicData.Entities; +using Tnb.WarehouseMgr.Entities; + +namespace Tnb.WarehouseMgr +{ + public class WmsImportAndExportService + { + private readonly ISqlSugarClient _db; + public WmsImportAndExportService(ISqlSugarRepository repository) + { + _db = repository.AsSugarClient(); + } + + + /// + /// 载具导入 + /// + /// + /// + public async Task CarryImport(IFormFile file) + { + List> dics = WmsImport(file); + List carrys = new List(); + var carryStds = await _db.Queryable().ToListAsync(); + List locCodes = new(); + WmsCarryH carryH = new WmsCarryH(); + if (carryStds != null) + { + string carryStdId = string.Empty; + foreach (var d in dics) + { + carryStdId = carryStds.Find(x => x.carrystd_code == d["carrystd_code"])!.id; + d.Remove("carrystd_code"); + d.Add("carrystd_id", carryStdId); + locCodes.Add(d["location_code"]); + carryH = d.Adapt(); + } + var locs = await _db.Queryable().Where(it=>locCodes.Contains(it.location_code)).ToListAsync(); + } + return Task.FromResult(0); + } + + /// + /// WMS导入 + /// + /// + private static List> WmsImport(IFormFile file) + { + int rowIndex = 1; + List> dics = new List>(); + 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); + Dictionary dic = new Dictionary(); + + 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); + } + dics.Add(dic); + } + } + } + } + } + } + catch (Exception) + { + + throw; + } + + return dics; + } + + /// + /// WMS导入模板 + /// + /// + /// + /// + private static dynamic WmsTemplate(List> dics, string sheetName) + { + XSSFWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + IRow nRow = sheet.CreateRow(0); + IRow dRow = sheet.CreateRow(1); + + for (int i = 0; i < dics?.Count; i++) + { + ICell cell1 = nRow.CreateCell(i); + cell1.SetCellValue(dics[i].Keys.ToString()); + ICell cell2 = dRow.CreateCell(i); + cell2.SetCellValue(dics[i].Values.ToString()); + } + + 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; + } + } +}