265 lines
9.8 KiB
C#
265 lines
9.8 KiB
C#
|
||
|
||
|
||
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())
|
||
.OrderBy("created_time DESC")
|
||
.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()
|
||
{
|
||
|
||
//Id = SqlFunc.AggregateMax(it.Id),
|
||
InvCode = 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)
|
||
|
||
|
||
}).OrderBy(it => SqlFunc.AggregateMax(it.CreatedTime), OrderByType.Desc);
|
||
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>
|
||
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);
|
||
}
|
||
|
||
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(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<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 数组
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
}
|
||
} |