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; using System.Linq.Expressions; namespace MDM.Services.Material { /// /// Service业务层处理 /// [AppService(ServiceType = typeof(IMaterialBomService), ServiceLifetime = LifeTime.Transient)] public class MaterialBomService : BaseService, IMaterialBomService { /// /// 查询列表 /// /// /// public PagedInfo GetList(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 response = Queryable() .Where(predicate.ToExpression()) .OrderBy("created_time DESC") .ToPage(parm); 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), // 改动1:注释掉CreatedTime聚合(临时避开) // CreatedTime = SqlFunc.AggregateMax(it.CreatedTime), UpdatedBy = SqlFunc.AggregateMax(it.UpdatedBy), UpdatedTime = SqlFunc.AggregateMax(it.UpdatedTime) }) // 改动2:手动指定用UpdatedTime(数据库列updated_time)排序,避开CreatedTime .OrderBy("MAX(updated_time) DESC"); // 匹配你的PagedInfo类的分页逻辑 int pageIndex = 1; int pageSize = 10; int totalNum = query.Count(); var response = new PagedInfo() { PageIndex = pageIndex, PageSize = pageSize, TotalNum = totalNum, Result = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(), Extra = new Dictionary() }; return response; } /// /// 获取子件 /// /// /// public List GetSonInvList(MaterialBomQueryDto parm) { return Context.Queryable().Where(it => it.InvCode == parm.InvCode).ToList(); } /// /// 获取详情 /// /// /// public MaterialBom GetInfo(string Id) { var response = Queryable() .Where(x => x.Id == Id) .First(); return response; } /// /// 添加 /// /// /// public MaterialBom AddMaterialBom(MaterialBom model) { return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改 /// /// /// public int UpdateMaterialBom(MaterialBom model) { //var response = Update(w => w.Id == model.Id, it => new MaterialBom() //{ // InvCode = model.InvCode, // InvName = model.InvName, // SubInvCode = model.SubInvCode, // SubInvName = model.SubInvName, // Iusequantity = model.Iusequantity, // BOMVersion = model.BOMVersion, // 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("物料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(2) != null && currentRow.GetCell(2).CellType == CellType.String) item.SubInvCode = currentRow.GetCell(2).ToString(); //子件名称 if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String) item.SubInvName = currentRow.GetCell(3).ToString(); //使用数量 if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String) item.Iusequantity = currentRow.GetCell(4).ToString(); //BOM版本号 if (currentRow.GetCell(5) != null && currentRow.GetCell(5).CellType == CellType.String) item.BOMVersion = currentRow.GetCell(5).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() { 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() .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 数组 } } } }