268 lines
9.7 KiB
C#
Raw Normal View History

2025-11-15 14:33:00 +08:00
2025-11-18 14:34:26 +08:00
using Infrastructure;
2025-11-15 14:33:00 +08:00
using Infrastructure.Attribute;
2025-11-16 15:16:51 +08:00
using MDM.Model;
using MDM.Model.Material;
using MDM.Model.Material.Dto;
using MDM.Repository;
using MDM.Service;
using MDM.Services.IMaterialService;
2025-11-18 14:34:26 +08:00
using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
2025-11-16 15:16:51 +08:00
using System.Linq.Expressions;
2025-11-15 14:33:00 +08:00
namespace MES_Model.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)
{
2025-11-18 14:34:26 +08:00
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))
;
;
2025-11-15 14:33:00 +08:00
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage<MaterialBom, MaterialBomDto>(parm);
return response;
}
2025-11-18 14:34:26 +08:00
/// <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()
{
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<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();
2025-11-15 14:33:00 +08:00
2025-11-18 14:34:26 +08:00
}
2025-11-15 14:33:00 +08:00
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public MaterialBom GetInfo(string Id)
{
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);
}
2025-11-18 14:34:26 +08:00
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);
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<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()
{
string templatePath = Path.Combine(
Directory.GetCurrentDirectory(),
"wwwroot",
"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 数组
}
}
2025-11-15 14:33:00 +08:00
}
}