diff --git a/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MaterialMODEL.xlsx b/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MaterialMODEL.xlsx new file mode 100644 index 0000000..df09c79 Binary files /dev/null and b/DOAN.Admin.WebApi/wwwroot/ImportTemplate/MaterialMODEL.xlsx differ diff --git a/DOAN.Service/DOAN.Service.csproj b/DOAN.Service/DOAN.Service.csproj index 4384de7..3fa5064 100644 --- a/DOAN.Service/DOAN.Service.csproj +++ b/DOAN.Service/DOAN.Service.csproj @@ -17,7 +17,6 @@ - diff --git a/DOAN.ServiceCore/DOAN.ServiceCore.csproj b/DOAN.ServiceCore/DOAN.ServiceCore.csproj index 17ea851..d7bee41 100644 --- a/DOAN.ServiceCore/DOAN.ServiceCore.csproj +++ b/DOAN.ServiceCore/DOAN.ServiceCore.csproj @@ -16,6 +16,7 @@ + diff --git a/Infrastructure/Controllers/BaseController.cs b/Infrastructure/Controllers/BaseController.cs index 9a88f4e..a51b348 100644 --- a/Infrastructure/Controllers/BaseController.cs +++ b/Infrastructure/Controllers/BaseController.cs @@ -210,10 +210,14 @@ namespace Infrastructure.Controllers protected (string, string) DownloadImportTemplate(string fileName) { IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment)); + // string path= "./Assets/ImportTemplate"; string sFileName = $"{fileName}.xlsx"; string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "ImportTemplate", sFileName); return (sFileName, fullPath); } + + + } } diff --git a/MES_Model/Assets/ImportTemplate/MaterialMODEL.xlsx b/MES_Model/Assets/ImportTemplate/MaterialMODEL.xlsx new file mode 100644 index 0000000..df09c79 Binary files /dev/null and b/MES_Model/Assets/ImportTemplate/MaterialMODEL.xlsx differ diff --git a/MES_Model/Controllers/Material/MaterialBomController.cs b/MES_Model/Controllers/Material/MaterialBomController.cs index 8333df2..1816448 100644 --- a/MES_Model/Controllers/Material/MaterialBomController.cs +++ b/MES_Model/Controllers/Material/MaterialBomController.cs @@ -13,6 +13,7 @@ using MDM.Services.IMaterialService; using MDM.Model.Material.Dto; using MDM.Model.Material; using MDM.Repository; +using DOAN.Model.MES.base_.Dto; //创建时间:2025-11-15 namespace MDM.Controllers.Material @@ -112,6 +113,22 @@ namespace MDM.Controllers.Material } + //TODO 获取母件 + [HttpPost("get_monter_inv")] + public IActionResult GetMonterInvList([FromBody] MaterialBomQueryDto parm) + { + var response = _MaterialBomService.GetMonterInvList(parm); + return SUCCESS(response); + } + + //TODO 获取子件 + [HttpPost("get_son_inv")] + public IActionResult GetSonInvList([FromBody] MaterialBomQueryDto parm) + { + + var response = _MaterialBomService.GetSonInvList(parm); + return SUCCESS(response); + } } diff --git a/MES_Model/Controllers/Material/MaterialListController.cs b/MES_Model/Controllers/Material/MaterialListController.cs index 391ec1d..bd8f898 100644 --- a/MES_Model/Controllers/Material/MaterialListController.cs +++ b/MES_Model/Controllers/Material/MaterialListController.cs @@ -12,6 +12,9 @@ using Infrastructure.Model; using MDM.Services.IMaterialService; using MDM.Model.Material.Dto; using MDM.Model.Material; +using MDM.Services.Material; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; //创建时间:2025-11-15 namespace MDM.Controllers.Material @@ -111,6 +114,54 @@ namespace MDM.Controllers.Material } + //TODO 导出模板 + [HttpGet("importTemplate")] + [Log(Title = "物料类型模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)] + [AllowAnonymous] + public IActionResult ImportTemplateExcel() + { + string path = "./Assets/ImportTemplate"; + string sFileName = $"MaterialMODEL.xlsx"; + string fullPath = Path.Combine(path, sFileName); + (string, string) result = (sFileName, fullPath); + return ExportExcel(result.Item2, result.Item1); + } + + + //TODO 导入excel + [HttpPost("importData")] + [Log(Title = "物料类型导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)] + [AllowAnonymous] + public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile) + { + + if (formFile == null) + { + return SUCCESS(null); + } + int response = _MaterialListService.ImportData(formFile, HttpContext.GetName()); + + return SUCCESS(response); + } + + //TODO 导出excel + [HttpGet("exportData")] + [Log(Title = "物料类型导出", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)] + [AllowAnonymous] + public IActionResult ExportData() + { + + var excelBytes = _MaterialListService.ExportData(); + string fileName = $"PlanAchievementRate_{DateTime.Now.Date:yyyyMMdd}.xlsx"; + return File( + excelBytes, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + fileName + ); + + } + + } diff --git a/MES_Model/Controllers/Material/MaterialTypeController.cs b/MES_Model/Controllers/Material/MaterialTypeController.cs index 6dff0e1..5e5bfc5 100644 --- a/MES_Model/Controllers/Material/MaterialTypeController.cs +++ b/MES_Model/Controllers/Material/MaterialTypeController.cs @@ -12,6 +12,11 @@ using Infrastructure.Model; using MDM.Services.IMaterialService; using MDM.Model.Material.Dto; using MDM.Model.Material; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Hosting; +using NPOI.HPSF; +using DOAN.Model.Mobile.Dto; //创建时间:2025-11-15 namespace MDM.Controllers.Material @@ -111,6 +116,54 @@ namespace MDM.Controllers.Material } + //TODO 导出模板 + [HttpGet("importTemplate")] + [Log(Title = "物料类型模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)] + [AllowAnonymous] + public IActionResult ImportTemplateExcel() + { + string path= "./Assets/ImportTemplate"; + string sFileName = $"MaterialMODEL.xlsx"; + string fullPath = Path.Combine(path, sFileName); + (string, string) result = (sFileName,fullPath); + return ExportExcel(result.Item2, result.Item1); + } + + + //TODO 导入excel + [HttpPost("importData")] + [Log(Title = "物料类型导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)] + [AllowAnonymous] + public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile) + { + + if (formFile == null) + { + return SUCCESS(null); + } + int response = _MaterialTypeService.ImportData(formFile, HttpContext.GetName()); + + return SUCCESS(response); + } + + //TODO 导出excel + [HttpGet("exportData")] + [Log(Title = "物料类型导出", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)] + [AllowAnonymous] + public IActionResult ExportData() + { + + var excelBytes = _MaterialTypeService.ExportData(); + string fileName = $"PlanAchievementRate_{DateTime.Now.Date:yyyyMMdd}.xlsx"; + return File( + excelBytes, + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + fileName + ); + + } + + } diff --git a/MES_Model/MDM.csproj b/MES_Model/MDM.csproj index 63df30d..eef89ab 100644 --- a/MES_Model/MDM.csproj +++ b/MES_Model/MDM.csproj @@ -10,6 +10,14 @@ + + + true + PreserveNewest + Always + + + diff --git a/MES_Model/Models/Material/Dto/MaterialBomDto.cs b/MES_Model/Models/Material/Dto/MaterialBomDto.cs index 355b7f3..4f45dbb 100644 --- a/MES_Model/Models/Material/Dto/MaterialBomDto.cs +++ b/MES_Model/Models/Material/Dto/MaterialBomDto.cs @@ -2,11 +2,14 @@ using System.ComponentModel.DataAnnotations; namespace MDM.Model.Material.Dto { + /// /// 查询对象 /// public class MaterialBomQueryDto : PagerInfo { + public string InvCode { get; set; } + public string SubInvCode { get; set; } } /// diff --git a/MES_Model/Services/Material/IService/IMaterialBomService.cs b/MES_Model/Services/Material/IService/IMaterialBomService.cs index bd0f7fc..4b6f095 100644 --- a/MES_Model/Services/Material/IService/IMaterialBomService.cs +++ b/MES_Model/Services/Material/IService/IMaterialBomService.cs @@ -3,6 +3,7 @@ using MDM.Model; using MDM.Model.Material; using MDM.Model.Material.Dto; using MDM.Service; +using Microsoft.AspNetCore.Http; namespace MDM.Services.IMaterialService { @@ -13,11 +14,22 @@ namespace MDM.Services.IMaterialService { PagedInfo GetList(MaterialBomQueryDto parm); + PagedInfo GetMonterInvList(MaterialBomQueryDto parm); + + List GetSonInvList(MaterialBomQueryDto parm); + MaterialBom GetInfo(string Id); MaterialBom AddMaterialBom(MaterialBom parm); int UpdateMaterialBom(MaterialBom parm); + int ImportData(IFormFile formFile, string name); + + + byte[] ExportData(); + + + } } diff --git a/MES_Model/Services/Material/IService/IMaterialListService.cs b/MES_Model/Services/Material/IService/IMaterialListService.cs index a3cc1b9..09b1df4 100644 --- a/MES_Model/Services/Material/IService/IMaterialListService.cs +++ b/MES_Model/Services/Material/IService/IMaterialListService.cs @@ -2,6 +2,7 @@ using MDM.Model; using MDM.Model.Material; using MDM.Model.Material.Dto; using MDM.Service; +using Microsoft.AspNetCore.Http; namespace MDM.Services.IMaterialService { @@ -17,6 +18,10 @@ namespace MDM.Services.IMaterialService MaterialList AddMaterialList(MaterialList parm); int UpdateMaterialList(MaterialList parm); + int ImportData(IFormFile formFile, string name); + + + byte[] ExportData(); } } diff --git a/MES_Model/Services/Material/IService/IMaterialTypeService.cs b/MES_Model/Services/Material/IService/IMaterialTypeService.cs index 36ba00e..b6e21b9 100644 --- a/MES_Model/Services/Material/IService/IMaterialTypeService.cs +++ b/MES_Model/Services/Material/IService/IMaterialTypeService.cs @@ -2,6 +2,7 @@ using MDM.Model; using MDM.Model.Material; using MDM.Model.Material.Dto; using MDM.Service; +using Microsoft.AspNetCore.Http; namespace MDM.Services.IMaterialService { @@ -18,5 +19,10 @@ namespace MDM.Services.IMaterialService int UpdateMaterialType(MaterialType parm); + int ImportData(IFormFile formFile, string name); + + + byte[] ExportData(); + } } diff --git a/MES_Model/Services/Material/MaterialBomService.cs b/MES_Model/Services/Material/MaterialBomService.cs index 3a2d944..fc2d2f1 100644 --- a/MES_Model/Services/Material/MaterialBomService.cs +++ b/MES_Model/Services/Material/MaterialBomService.cs @@ -1,5 +1,7 @@ + +using Infrastructure; using Infrastructure.Attribute; using MDM.Model; using MDM.Model.Material; @@ -7,6 +9,9 @@ using MDM.Model.Material.Dto; using MDM.Repository; using MDM.Service; using MDM.Services.IMaterialService; +using Microsoft.AspNetCore.Http; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using System.Linq.Expressions; @@ -25,7 +30,13 @@ namespace MES_Model.Services.Material /// public PagedInfo GetList(MaterialBomQueryDto parm) { - var predicate = Expressionable.Create(); + var predicate = Expressionable.Create() + .AndIF(!string.IsNullOrEmpty(parm.InvCode), it => it.InvCode.Contains(parm.InvCode)) + .AndIF(!string.IsNullOrEmpty(parm.SubInvCode), it => it.SubInvCode.Contains(parm.SubInvCode)) + ; + + + ; var response = Queryable() .Where(predicate.ToExpression()) @@ -33,7 +44,55 @@ namespace MES_Model.Services.Material return response; } + /// + /// 获取母件BOM清单 + /// + /// + /// + public PagedInfo GetMonterInvList(MaterialBomQueryDto parm) + { + var predicate = Expressionable.Create() + .AndIF(!string.IsNullOrEmpty(parm.InvCode), it => it.InvCode.Contains(parm.InvCode)) + .AndIF(!string.IsNullOrEmpty(parm.SubInvCode), it => it.SubInvCode.Contains(parm.SubInvCode)) + ; + var query = Context.Queryable() + .Where(predicate.ToExpression()) + .GroupBy(it => it.InvCode) + .Select(it => new MaterialBomDto() + { + + Id = SqlFunc.AggregateMax(it.Id), + InvCode = SqlFunc.AggregateMax(it.InvCode), + InvName = SqlFunc.AggregateMax(it.InvName), + SubInvName = SqlFunc.AggregateMax(it.SubInvName), + Iusequantity = SqlFunc.AggregateMax(it.Iusequantity), + BOMVersion = SqlFunc.AggregateMax(it.BOMVersion), + CreatedBy = SqlFunc.AggregateMax(it.CreatedBy), + CreatedTime = SqlFunc.AggregateMax(it.CreatedTime), + UpdatedBy = SqlFunc.AggregateMax(it.UpdatedBy), + UpdatedTime = SqlFunc.AggregateMax(it.UpdatedTime) + + + }); + var response = query.ToPage(parm); + + + return response; + + + } + + /// + /// 获取子件 + /// + /// + /// + public List GetSonInvList(MaterialBomQueryDto parm) + { + return Context.Queryable().Where(it => it.InvCode == parm.InvCode).ToList(); + + } /// /// 获取详情 @@ -83,6 +142,127 @@ namespace MES_Model.Services.Material return Update(model, true); } - + public int ImportData(IFormFile formFile, string name) + { + using (var stream = formFile.OpenReadStream()) + { + try + { + IWorkbook workbook = new XSSFWorkbook(stream); + ISheet sheet = workbook.GetSheet("物料BOM清单"); + List materialBoms = new List(); + // 遍历每一行 + for (int row = 1; row <= sheet.LastRowNum; row++) + { + IRow currentRow = sheet.GetRow(row); + if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空 + { + MaterialBom item = new MaterialBom(); + item.Id = XueHua; + //母件编码 + if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String) + item.InvCode = currentRow.GetCell(0).ToString(); + //母件名称 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.InvName = currentRow.GetCell(1).ToString(); + //母件名称 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.InvName = currentRow.GetCell(1).ToString(); + + //子件编码 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.SubInvCode = currentRow.GetCell(1).ToString(); + //子件名称 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.SubInvName = currentRow.GetCell(1).ToString(); + //使用数量 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.Iusequantity = currentRow.GetCell(1).ToString(); + + + //BOM版本号 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.BOMVersion = currentRow.GetCell(1).ToString(); + + materialBoms.Add(item); + + } + } + + if (materialBoms.Count == 0) + { + return 0; + } + UseTran2(() => + { + // Context.Deleteable().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand(); + Context.Insertable(materialBoms).ExecuteCommand(); + }); + return materialBoms.Count; + } + catch (Exception ex) + { + throw new CustomException("excel文件异常,请详细核对"); + + + } + + + + } + + } + + + public byte[] ExportData() + { + string templatePath = Path.Combine( + Directory.GetCurrentDirectory(), + "wwwroot", + "ImportTemplate", + "MaterialMODEL.xlsx" + ); + + if (!global::System.IO.File.Exists(templatePath)) + { + throw new CustomException("Excel 模板文件不存在"); + } + + List dataList = Context.Queryable() + .Where(it => it.Status == 1) + .ToList(); + + using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = WorkbookFactory.Create(fileStream); + ISheet sheet = workbook.GetSheet("物料类型"); + + int startRowIndex = 1; + for (int i = 0; i < dataList.Count; i++) + { + var item = dataList[i]; + IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i); + + // 为每个需要数字的单元格设置值,并应用 numberCellStyle + row.CreateCell(0).SetCellValue(item.Name); + + // 以下为字符串类型字段,不需要数字格式 + row.CreateCell(1).SetCellValue(item.Code); + row.CreateCell(2).SetCellValue(item.ParentCode); + row.CreateCell(3).SetCellValue((double)item.Status); + } + + // 改为写入 MemoryStream 然后转成 byte[] + var memoryStream = new MemoryStream(); + workbook.Write(memoryStream); + // memoryStream.Position = 0; + return memoryStream.ToArray(); // ✅ 直接返回 byte 数组 + } + + + + } + + } } \ No newline at end of file diff --git a/MES_Model/Services/Material/MaterialListService.cs b/MES_Model/Services/Material/MaterialListService.cs index 8365993..b9f1355 100644 --- a/MES_Model/Services/Material/MaterialListService.cs +++ b/MES_Model/Services/Material/MaterialListService.cs @@ -1,3 +1,4 @@ +using Infrastructure; using Infrastructure.Attribute; using MDM.Model; using MDM.Model.Material; @@ -5,6 +6,9 @@ using MDM.Model.Material.Dto; using MDM.Repository; using MDM.Service; using MDM.Services.IMaterialService; +using Microsoft.AspNetCore.Http; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; @@ -92,5 +96,191 @@ namespace MDM.Services.Material return Update(model, true); } + public int ImportData(IFormFile formFile, string name) + { + using (var stream = formFile.OpenReadStream()) + { + try + { + IWorkbook workbook = new XSSFWorkbook(stream); + ISheet sheet = workbook.GetSheet("物料列表"); + List materialLists = new List(); + // 遍历每一行 + for (int row = 1; row <= sheet.LastRowNum; row++) + { + IRow currentRow = sheet.GetRow(row); + if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空 + { + MaterialList item = new MaterialList(); + item.Id = XueHua; + //物料类型code + if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String) + item.FkMaterialTypeCode = currentRow.GetCell(0).ToString(); + //物料名称 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.Name = currentRow.GetCell(1).ToString(); + + //物料编码 + if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.String) + item.Code = currentRow.GetCell(2).ToString(); + + //物料代码 + if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String) + item.Addcode = currentRow.GetCell(3).ToString(); + + + //客户编码 + if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String) + item.CustomerCode = currentRow.GetCell(4).ToString(); + + //颜色 + if (currentRow.GetCell(5) != null && currentRow.GetCell(5).CellType == CellType.String) + item.Color = currentRow.GetCell(5).ToString(); + + //规格型号 + if (currentRow.GetCell(6) != null && currentRow.GetCell(6).CellType == CellType.String) + item.Specification = currentRow.GetCell(6).ToString(); + + + //单位 + if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String) + item.Unit = currentRow.GetCell(7).ToString(); + + //描述 + if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String) + item.Description = currentRow.GetCell(7).ToString(); + + //供应商代码 + if (currentRow.GetCell(7) != null && currentRow.GetCell(7).CellType == CellType.String) + item.SupplierCode = currentRow.GetCell(7).ToString(); + + //保质期单位 + if (currentRow.GetCell(8) != null && currentRow.GetCell(8).CellType == CellType.String) + item.ExpirationUnit = currentRow.GetCell(8).ToString(); + + //保质期单位 + if (currentRow.GetCell(8) != null && currentRow.GetCell(8).CellType == CellType.String) + item.ExpirationUnit = currentRow.GetCell(8).ToString(); + + //保质期(天) + if (currentRow.GetCell(9) != null && currentRow.GetCell(9).CellType == CellType.Numeric) + item.ExpirationDate =Convert.ToInt16(currentRow.GetCell(9).ToString()); + + + //保质期预警天数 + if (currentRow.GetCell(10) != null && currentRow.GetCell(10).CellType == CellType.Numeric) + item.ShelfLifeWarningDays = Convert.ToInt16(currentRow.GetCell(10).ToString()); + + //是否有效期管理 + if (currentRow.GetCell(11) != null && currentRow.GetCell(11).CellType == CellType.Numeric) + item.IsShelfLife = Convert.ToInt16(currentRow.GetCell(11).ToString()); + + + //启用日期 + //if (currentRow.GetCell(12) != null && currentRow.GetCell(12).CellType == CellType.Formula) + // item.StartTime = Convert.ToInt16(currentRow.GetCell(12).ToString()); + + + + //对应条码 + if (currentRow.GetCell(12) != null && currentRow.GetCell(12).CellType == CellType.String) + item.BarCode = currentRow.GetCell(12).ToString(); + + + + + + + + materialLists.Add(item); + + } + } + + if (materialLists.Count == 0) + { + return 0; + } + UseTran2(() => + { + // Context.Deleteable().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand(); + Context.Insertable(materialLists).ExecuteCommand(); + }); + return materialLists.Count; + + } + catch (Exception ex) + { + throw new CustomException("excel文件异常,请详细核对"); + + + } + + + + } + + } + + + public byte[] ExportData() + { + string templatePath = Path.Combine( + Directory.GetCurrentDirectory(), + "Assets", + "ImportTemplate", + "MaterialMODEL.xlsx" + ); + + if (!global::System.IO.File.Exists(templatePath)) + { + throw new CustomException("Excel 模板文件不存在"); + } + + List dataList = Context.Queryable() + .ToList(); + + using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = WorkbookFactory.Create(fileStream); + ISheet sheet = workbook.GetSheet("物料台账"); + + int startRowIndex = 1; + for (int i = 0; i < dataList.Count; i++) + { + var item = dataList[i]; + IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i); + + + row.CreateCell(0).SetCellValue(item.FkMaterialTypeCode); + row.CreateCell(1).SetCellValue(item.Name); + row.CreateCell(2).SetCellValue(item.Code); + row.CreateCell(3).SetCellValue(item.CustomerCode); + row.CreateCell(4).SetCellValue(item.Color); + row.CreateCell(5).SetCellValue(item.Specification); + row.CreateCell(6).SetCellValue(item.Unit); + row.CreateCell(7).SetCellValue(item.Description); + row.CreateCell(8).SetCellValue(item.SupplierCode); + row.CreateCell(9).SetCellValue(item.ExpirationUnit); + row.CreateCell(10).SetCellValue((double)item.ExpirationDate); + row.CreateCell(11).SetCellValue((double)item.ShelfLifeWarningDays); + row.CreateCell(12).SetCellValue((double)item.IsShelfLife); + row.CreateCell(13).SetCellValue(item.BarCode); + + + } + + // 改为写入 MemoryStream 然后转成 byte[] + var memoryStream = new MemoryStream(); + workbook.Write(memoryStream); + // memoryStream.Position = 0; + return memoryStream.ToArray(); // ✅ 直接返回 byte 数组 + } + + + + } + + } } \ No newline at end of file diff --git a/MES_Model/Services/Material/MaterialTypeService.cs b/MES_Model/Services/Material/MaterialTypeService.cs index cfc7c3a..91152a0 100644 --- a/MES_Model/Services/Material/MaterialTypeService.cs +++ b/MES_Model/Services/Material/MaterialTypeService.cs @@ -7,6 +7,13 @@ using MDM.Model.Material.Dto; using MDM.Service; using MDM.Model; using MDM.Repository; +using DOAN.Model.MES.product; +using Infrastructure; +using Microsoft.AspNetCore.Http; +using Microsoft.VisualBasic; +using System.Collections.Generic; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; namespace MDM.Services.Material { /// @@ -86,6 +93,131 @@ namespace MDM.Services.Material //return response; return Update(model, true); } + public int ImportData(IFormFile formFile, string name) + { + using (var stream = formFile.OpenReadStream()) + { + try + { + IWorkbook workbook = new XSSFWorkbook(stream); + ISheet sheet = workbook.GetSheet("物料类型"); + List materialTypes = new List(); + // 遍历每一行 + for (int row = 1; row <= sheet.LastRowNum; row++) + { + IRow currentRow = sheet.GetRow(row); + if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空 + { + MaterialType item = new MaterialType(); + + //物料类型名称 + if (currentRow.GetCell(0) != null && currentRow.GetCell(0).CellType == CellType.String) + item.Name = currentRow.GetCell(0).ToString(); + //物料类别编码 + if (currentRow.GetCell(1) != null && currentRow.GetCell(1).CellType == CellType.String) + item.Code = currentRow.GetCell(1).ToString(); + + //物料类别父级编码 + if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.String) + item.ParentCode = currentRow.GetCell(2).ToString(); + //状态 Status + if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String) + { + string statusstring = currentRow.GetCell(3).ToString(); + if(statusstring == "启用") + { + item.Status = 1; + } + else if (statusstring == "停用") + { + item.Status = 0; + } + + } + //备注 + if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String) + item.Remark = currentRow.GetCell(4).ToString(); + + + materialTypes.Add(item); + + } + } + + if (materialTypes.Count == 0) + { + return 0; + } + UseTran2(() => + { + // Context.Deleteable().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand(); + Context.Insertable(materialTypes).ExecuteCommand(); + }); + return materialTypes.Count; + + } + catch (Exception ex) + { + throw new CustomException("excel文件异常,请详细核对"); + + + } + + + + } + + } + + + public byte[] ExportData() + { + string templatePath = Path.Combine( + Directory.GetCurrentDirectory(), + "wwwroot", + "ImportTemplate", + "MaterialMODEL.xlsx" + ); + + if (!global::System.IO.File.Exists(templatePath)) + { + throw new CustomException("Excel 模板文件不存在"); + } + + List dataList = Context.Queryable() + .Where(it=>it.Status==1) + .ToList(); + + using (var fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = WorkbookFactory.Create(fileStream); + ISheet sheet = workbook.GetSheet("物料类型"); + + int startRowIndex = 1; + for (int i = 0; i < dataList.Count; i++) + { + var item = dataList[i]; + IRow row = sheet.GetRow(startRowIndex + i) ?? sheet.CreateRow(startRowIndex + i); + + // 为每个需要数字的单元格设置值,并应用 numberCellStyle + row.CreateCell(0).SetCellValue(item.Name); + + // 以下为字符串类型字段,不需要数字格式 + row.CreateCell(1).SetCellValue(item.Code); + row.CreateCell(2).SetCellValue(item.ParentCode); + row.CreateCell(3).SetCellValue((double)item.Status); + } + + // 改为写入 MemoryStream 然后转成 byte[] + var memoryStream = new MemoryStream(); + workbook.Write(memoryStream); + // memoryStream.Position = 0; + return memoryStream.ToArray(); // ✅ 直接返回 byte 数组 + } + + + + } } } \ No newline at end of file