2026-01-10 13:47:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Service业务层处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[AppService(ServiceType = typeof(IMaterialBomService), ServiceLifetime = LifeTime.Transient)]
|
|
|
|
|
|
public class MaterialBomService : BaseService<MaterialBom>, IMaterialBomService
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public PagedInfo<MaterialBomDto> GetList(MaterialBomQueryDto parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
var predicate = Expressionable.Create<MaterialBom>()
|
|
|
|
|
|
.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())
|
|
|
|
|
|
.ToPage<MaterialBom, MaterialBomDto>(parm);
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取母件BOM清单
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public PagedInfo<MaterialBomDto> GetMonterInvList(MaterialBomQueryDto parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
var predicate = Expressionable.Create<MaterialBom>()
|
|
|
|
|
|
.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<MaterialBom>()
|
|
|
|
|
|
.Where(predicate.ToExpression())
|
|
|
|
|
|
.GroupBy(it => it.InvCode)
|
|
|
|
|
|
.Select(it => new MaterialBomDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2026-01-13 18:49:46 +08:00
|
|
|
|
//Id = SqlFunc.AggregateMax(it.Id),
|
2026-01-10 13:47:54 +08:00
|
|
|
|
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<MaterialBomDto, MaterialBomDto>(parm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取子件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<MaterialBom> GetSonInvList(MaterialBomQueryDto parm)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Queryable<MaterialBom>().Where(it => it.InvCode == parm.InvCode).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取详情
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="Id"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2026-01-13 18:49:46 +08:00
|
|
|
|
public MaterialBom GetInfo(int Id)
|
2026-01-10 13:47:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
var response = Queryable()
|
|
|
|
|
|
.Where(x => x.Id == Id)
|
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public MaterialBom AddMaterialBom(MaterialBom model)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Context.Insertable(model).ExecuteReturnEntity();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 修改
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
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<MaterialBom> materialBoms = new List<MaterialBom>();
|
|
|
|
|
|
// 遍历每一行
|
|
|
|
|
|
for (int row = 1; row <= sheet.LastRowNum; row++)
|
|
|
|
|
|
{
|
|
|
|
|
|
IRow currentRow = sheet.GetRow(row);
|
2026-01-15 14:51:20 +08:00
|
|
|
|
//目前没加物料是否正确校验
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-10 13:47:54 +08:00
|
|
|
|
if (currentRow != null && currentRow.GetCell(0) != null) // 确保行不为空
|
|
|
|
|
|
{
|
|
|
|
|
|
MaterialBom item = new MaterialBom();
|
|
|
|
|
|
//母件编码
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
//子件编码
|
2026-01-15 14:51:20 +08:00
|
|
|
|
if (currentRow.GetCell(2) != null && currentRow.GetCell(2).CellType == CellType.String)
|
|
|
|
|
|
item.SubInvCode = currentRow.GetCell(2).ToString();
|
2026-01-10 13:47:54 +08:00
|
|
|
|
//子件名称
|
2026-01-15 14:51:20 +08:00
|
|
|
|
if (currentRow.GetCell(3) != null && currentRow.GetCell(3).CellType == CellType.String)
|
|
|
|
|
|
item.SubInvName = currentRow.GetCell(3).ToString();
|
2026-01-10 13:47:54 +08:00
|
|
|
|
//使用数量
|
2026-01-15 14:51:20 +08:00
|
|
|
|
if (currentRow.GetCell(4) != null && currentRow.GetCell(4).CellType == CellType.String)
|
|
|
|
|
|
item.Iusequantity = currentRow.GetCell(4).ToString();
|
2026-01-10 13:47:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//BOM版本号
|
2026-01-15 14:51:20 +08:00
|
|
|
|
if (currentRow.GetCell(5) != null && currentRow.GetCell(5).CellType == CellType.String)
|
|
|
|
|
|
item.BOMVersion = currentRow.GetCell(5).ToString();
|
2026-01-10 13:47:54 +08:00
|
|
|
|
|
|
|
|
|
|
materialBoms.Add(item);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (materialBoms.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
UseTran2(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// Context.Deleteable<ProPlanAchievementrateVersion2>().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<MaterialType> dataList = Context.Queryable<MaterialType>()
|
|
|
|
|
|
.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 数组
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|