using Infrastructure; using Infrastructure.Attribute; using MDM.Model; using MDM.Model.Material; using MDM.Model.Material.Dto; using MDM.Repository; using MDM.Service; using MDM.Services.IMaterialService; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; namespace MDM.Services.Material { /// /// 物料清单Service业务层处理 /// [AppService(ServiceType = typeof(IMaterialListService), ServiceLifetime = LifeTime.Transient)] public class MaterialListService : BaseService, IMaterialListService { /// /// 查询物料清单列表 /// /// /// public PagedInfo GetList(MaterialListQueryDto parm) { var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.FkMaterialTypeCode),it=>it.FkMaterialTypeCode.Contains(parm.FkMaterialTypeCode)) .AndIF(!string.IsNullOrEmpty(parm.Name),it=>it.Name.Contains(parm.Name)) .AndIF(!string.IsNullOrEmpty(parm.Code),it=>it.Code.Contains(parm.Code)) ; var response = Queryable() .Where(predicate.ToExpression()) .ToPage(parm); return response; } /// /// 获取详情 /// /// /// public MaterialList GetInfo(string Id) { var response = Queryable() .Where(x => x.Id == Id) .First(); return response; } /// /// 添加物料清单 /// /// /// public MaterialList AddMaterialList(MaterialList model) { return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改物料清单 /// /// /// public int UpdateMaterialList(MaterialList model) { //var response = Update(w => w.Id == model.Id, it => new MaterialList() //{ // FkMaterialTypeCode = model.FkMaterialTypeCode, // Name = model.Name, // Addcode = model.Addcode, // CustomerCode = model.CustomerCode, // Color = model.Color, // Specification = model.Specification, // Unit = model.Unit, // Description = model.Description, // SupplierCode = model.SupplierCode, // ExpirationUnit = model.ExpirationUnit, // ExpirationDate = model.ExpirationDate, // ShelfLifeWarningDays = model.ShelfLifeWarningDays, // IsShelfLife = model.IsShelfLife, // StartTime = model.StartTime, // StopTime = model.StopTime, // BarCode = model.BarCode, // IsBatch = model.IsBatch, // CreatedBy = model.CreatedBy, // CreatedTime = model.CreatedTime, // UpdatedBy = model.UpdatedBy, // UpdatedTime = model.UpdatedTime, //}); //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 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() { IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment)); // 使用Path.Combine构建正确的路径,避免硬编码的路径分隔符 string templatePath = Path.Combine(webHostEnvironment.ContentRootPath, "..\\MDM", "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 数组 } } } }