Files
kunshan-bzfm-mes-backend/DOAN.Service/MES/Material/MmMaterialService.cs
2026-01-08 11:45:06 +08:00

381 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DOAN.Common;
using DOAN.Model.BZFM;
using DOAN.Model.BZFM.Dto;
using DOAN.Model.MES.product;
using DOAN.Model.System;
using DOAN.Model.System.Dto;
using DOAN.Repository;
using DOAN.Service.BZFM.IBZFMService;
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Http;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace DOAN.Service.BZFM
{
/// <summary>
/// 物料表Service业务层处理
/// </summary>
[AppService(ServiceType = typeof(IMmMaterialService), ServiceLifetime = LifeTime.Transient)]
public class MmMaterialService : BaseService<MmMaterial>, IMmMaterialService
{
/// <summary>
/// 查询物料表列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public PagedInfo<MmMaterialDto> GetList(MmMaterialQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(it => it.Type)
.ToPage<MmMaterial, MmMaterialDto>(parm);
return response;
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public MmMaterial GetInfo(int Id)
{
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
/// <summary>
/// 添加物料表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public MmMaterial AddMmMaterial(MmMaterial model)
{
return Insertable(model).ExecuteReturnEntity();
}
/// <summary>
/// 修改物料表
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int UpdateMmMaterial(MmMaterial model)
{
return Update(model, true);
}
/// <summary>
/// 查询导出表达式
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
private static Expressionable<MmMaterial> QueryExp(MmMaterialQueryDto parm)
{
var predicate = Expressionable
.Create<MmMaterial>()
.AndIF(
!string.IsNullOrEmpty(parm.CategoryCode),
m => m.CategoryCode.Contains(parm.CategoryCode)
)
.AndIF(
!string.IsNullOrEmpty(parm.MaterialName),
m => m.MaterialName.Contains(parm.MaterialName)
)
.AndIF(
!string.IsNullOrEmpty(parm.MaterialCode),
m => m.MaterialCode.Contains(parm.MaterialCode)
)
.AndIF(
!string.IsNullOrEmpty(parm.Specification),
m => m.Specification.Contains(parm.Specification)
)
.AndIF(!string.IsNullOrEmpty(parm.Type), m => m.Type.Contains(parm.Type))
.AndIF(!string.IsNullOrEmpty(parm.Status), m => m.Status == parm.Status);
return predicate;
}
/// <summary>
/// 获取物料类别下拉框
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<MmMaterialCategoryOptionsDto> GetMmMaterialCategoryOptions(
MmMaterialCategoryDto parm
)
{
try
{
return Context
.Queryable<MmMaterialCategory>()
.WhereIF(
!string.IsNullOrEmpty(parm.CategoryCode),
it => it.CategoryCode.Contains(parm.CategoryCode)
)
.WhereIF(
!string.IsNullOrEmpty(parm.CategoryName),
it => it.CategoryName.Contains(parm.CategoryName)
)
.Select(it => new MmMaterialCategoryOptionsDto
{
Label = it.CategoryName,
Value = it.CategoryCode,
})
.ToList();
}
catch (Exception)
{
// TODO 处理错误日志
throw;
}
}
/// <summary>
/// 导入数据
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
public int Importmaterial(IFormFile formFile)
{
// TODO 1.构建空的待导入数据集合MmMaterialExcelDto多余部分删除
List<MmMaterialExcelDto> materialList = new();
// TODO 2.使用NPOI读取Excel内容填充到MmMaterialExcelDto集合中注意列的对应关系id不要忘且跳过标题行目前需要从第2行开始读取注意修改row
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
NPOI.SS.UserModel.ICell cell = secondRow.GetCell(0);
#region excel
// 遍历每一行
for (int row = 4; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
MmMaterial material = new MmMaterial();
//00 ID
NPOI.SS.UserModel.ICell currentCell_00 = currentRow.GetCell(0);
material.Id = (int)currentCell_00?.NumericCellValue;
//01 物料标号
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(1);
material.Type = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(material.Type))
{
continue;
}
//02物料编码
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(2);
material.MaterialCode = currentCell_02?.ToString();
if (
currentCell_02 == null
|| string.IsNullOrEmpty(material.MaterialCode)
)
{
continue;
}
//03物料名称
NPOI.SS.UserModel.ICell currentCell_03 = currentRow.GetCell(3);
material.MaterialName = currentCell_03?.ToString();
if (
currentCell_03 == null
|| string.IsNullOrEmpty(material.MaterialName)
)
{
continue;
}
//04规格
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(4);
material.Specification = currentCell_04?.ToString();
if (currentCell_04 == null || string.IsNullOrEmpty(material.Specification))
{
continue;
}
//05 类型编码
NPOI.SS.UserModel.ICell currentCell_05 = currentRow.GetCell(5);
material.CategoryCode = currentCell_05?.ToString();
if (
currentCell_05 == null
|| string.IsNullOrEmpty(material.CategoryCode)
)
{
continue;
}
//06 类型名称
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(6);
material.CategoryName = currentCell_06?.ToString();
if (
currentCell_06 == null
|| string.IsNullOrEmpty(material.CategoryName)
)
{
continue;
}
//07 单位
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(7);
material.Unit = currentCell_07?.ToString();
if (
currentCell_07 == null
|| string.IsNullOrEmpty(material.Unit)
)
{
continue;
}
//08 供应商编码
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(8);
material.SupplierCode = currentCell_08?.ToString();
if (currentCell_08 == null || string.IsNullOrEmpty(material.SupplierCode))
{
continue;
}
//09 供应商名称
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(9);
material.SupplierName = currentCell_09?.ToString();
if (
currentCell_09 == null
|| string.IsNullOrEmpty(material.SupplierName)
)
{
continue;
}
//
//10 状态
NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10);
material.Status = currentCell_10?.ToString();
if (currentCell_10 == null || string.IsNullOrEmpty(material.Status))
{
continue;
}
}
}
#endregion
}
catch (Exception ex)
{
return -1;
}
}
// TODO 3.调用SplitInsert方法实现导入操作,注意主键列的配置(建议优化为ID相同则修改不同则新增)
var x = Context
.Storageable(materialList)
.SplitInsert(it => !it.Any())
.WhereColumns(it => new { it.Id, it.MaterialCode }) //如果不是主键可以这样实现多字段it=>new{it.x1,it.x2}
.ToStorage();
var result = x.AsInsertable.ExecuteCommand(); //插入可插入部分;
var importResult = new ImportResultDto
{
Message = "导入完成",
Inserted = x.InsertList.Count,
Updated = x.UpdateList.Count,
ErrorCount = x.ErrorList.Count,
IgnoredCount = x.IgnoreList.Count,
Deleted = x.DeleteList.Count,
Total = x.TotalList.Count,
};
//输出统计
Console.WriteLine(importResult);
// 4.收集错误与忽略信息返回导入结果ImportResultDto 提示需要修改IServer相关返回格式
foreach (var item in x.ErrorList)
{
importResult.Errors.Add(
new ImportErrorDto
{
MaterialCode = item.Item.MaterialCode,
Message = item.StorageMessage,
}
);
}
foreach (var item in x.IgnoreList)
{
importResult.Ignored.Add(
new ImportErrorDto
{
MaterialCode = item.Item.MaterialCode,
Message = item.StorageMessage,
}
);
}
return importResult;
}
/// <summary>
/// 导出物料表列表
/// </summary>
/// <returns></returns>
public PagedInfo<MmMaterialExcelDto> SelectMaterialList(
MmMaterialQueryDto material,
PagerInfo pager
)
{
// Use the same predicate builder as GetList to support consistent filtering
var predicate = QueryExp(material);
var query = Queryable()
.Where(predicate.ToExpression())
.Select(it => new MmMaterialExcelDto
{
Id = it.Id,
Type = it.Type,
MaterialCode = it.MaterialCode,
MaterialName = it.MaterialName,
Specification = it.Specification,
CategoryCode = it.CategoryCode,
CategoryName = it.CategoryName,
Unit = it.Unit,
SupplierCode = it.SupplierCode,
SupplierName = it.SupplierName,
SafetyStock = it.SafetyStock,
Status = it.Status,
CreatedTime = it.CreatedTime,
UpdatedTime = it.UpdatedTime,
Description = it.Description
});
return query.ToPage(pager);
}
}
}