zhuangpei-mesbackend/MDM/Services/Material/MaterialListService.cs

285 lines
12 KiB
C#
Raw Normal View History

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-19 11:49:05 +08:00
using Microsoft.AspNetCore.Hosting;
2025-11-18 14:34:26 +08:00
using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
2025-11-15 14:33:00 +08:00
2025-11-16 15:16:51 +08:00
namespace MDM.Services.Material
2025-11-15 14:33:00 +08:00
{
/// <summary>
/// 物料清单Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IMaterialListService), ServiceLifetime = LifeTime.Transient)]
public class MaterialListService : BaseService<MaterialList>, IMaterialListService
{
/// <summary>
/// 查询物料清单列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<MaterialListDto> GetList(MaterialListQueryDto parm)
{
var predicate = Expressionable.Create<MaterialList>();
var response = Queryable()
.Where(predicate.ToExpression())
.ToPage<MaterialList, MaterialListDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public MaterialList GetInfo(string Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
/// <summary>
/// 添加物料清单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public MaterialList AddMaterialList(MaterialList model)
{
return Context.Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改物料清单
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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);
}
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);
2025-11-19 11:49:05 +08:00
ISheet sheet = workbook.GetSheet("物料类型");
2025-11-18 14:34:26 +08:00
List<MaterialList> materialLists = new List<MaterialList>();
// 遍历每一行
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<ProPlanAchievementrateVersion2>().Where(it => it.RecordDate == RecordDate.Date).ExecuteCommand();
Context.Insertable(materialLists).ExecuteCommand();
});
return materialLists.Count;
}
catch (Exception ex)
{
throw new CustomException("excel文件异常请详细核对");
}
}
}
public byte[] ExportData()
{
2025-11-19 11:49:05 +08:00
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
// 使用Path.Combine构建正确的路径避免硬编码的路径分隔符
string templatePath = Path.Combine(webHostEnvironment.ContentRootPath, "..\\MDM", "Assets", "ImportTemplate", "MaterialMODEL.xlsx");
2025-11-18 14:34:26 +08:00
if (!global::System.IO.File.Exists(templatePath))
{
throw new CustomException("Excel 模板文件不存在");
}
List<MaterialList> dataList = Context.Queryable<MaterialList>()
.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 数组
}
}
2025-11-15 14:33:00 +08:00
}
}