2026-01-06 12:06:05 +08:00
|
|
|
|
using DOAN.Common;
|
2025-12-25 12:02:03 +08:00
|
|
|
|
using DOAN.Model.BZFM;
|
2026-01-06 12:06:05 +08:00
|
|
|
|
using DOAN.Model.BZFM.Dto;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
using DOAN.Model.MES.product;
|
2026-01-06 12:06:05 +08:00
|
|
|
|
using DOAN.Model.System;
|
2026-01-06 17:26:47 +08:00
|
|
|
|
using DOAN.Model.System.Dto;
|
2025-12-25 12:02:03 +08:00
|
|
|
|
using DOAN.Repository;
|
|
|
|
|
|
using DOAN.Service.BZFM.IBZFMService;
|
2026-01-06 12:06:05 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Infrastructure.Attribute;
|
|
|
|
|
|
using Infrastructure.Extensions;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
|
|
|
using NPOI.SS.UserModel;
|
|
|
|
|
|
using NPOI.XSSF.UserModel;
|
2025-12-25 12:02:03 +08:00
|
|
|
|
|
|
|
|
|
|
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())
|
2026-01-05 12:56:28 +08:00
|
|
|
|
.OrderBy(it => it.Type)
|
2025-12-25 12:02:03 +08:00
|
|
|
|
.ToPage<MmMaterial, MmMaterialDto>(parm);
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取详情
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="Id"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public MmMaterial GetInfo(int Id)
|
|
|
|
|
|
{
|
2026-01-07 15:39:28 +08:00
|
|
|
|
var response = Queryable().Where(x => x.Id == Id).First();
|
2025-12-25 12:02:03 +08:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2026-01-07 15:39:28 +08:00
|
|
|
|
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)
|
|
|
|
|
|
)
|
2026-01-05 12:56:28 +08:00
|
|
|
|
.AndIF(!string.IsNullOrEmpty(parm.Type), m => m.Type.Contains(parm.Type))
|
2025-12-25 17:38:24 +08:00
|
|
|
|
.AndIF(!string.IsNullOrEmpty(parm.Status), m => m.Status == parm.Status);
|
2025-12-25 12:02:03 +08:00
|
|
|
|
return predicate;
|
|
|
|
|
|
}
|
2025-12-26 14:53:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取物料类别下拉框
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parm"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2026-01-07 15:39:28 +08:00
|
|
|
|
public List<MmMaterialCategoryOptionsDto> GetMmMaterialCategoryOptions(
|
|
|
|
|
|
MmMaterialCategoryDto parm
|
|
|
|
|
|
)
|
2025-12-26 14:53:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2026-01-07 15:39:28 +08:00
|
|
|
|
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)
|
|
|
|
|
|
)
|
2025-12-26 14:53:50 +08:00
|
|
|
|
.Select(it => new MmMaterialCategoryOptionsDto
|
|
|
|
|
|
{
|
|
|
|
|
|
Label = it.CategoryName,
|
2026-01-07 15:39:28 +08:00
|
|
|
|
Value = it.CategoryCode,
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToList();
|
2025-12-26 14:53:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO 处理错误日志
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-06 12:06:05 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导入数据
|
|
|
|
|
|
/// </summary>
|
2026-01-08 11:11:37 +08:00
|
|
|
|
/// <param name="formFile"></param>
|
2026-01-06 12:06:05 +08:00
|
|
|
|
/// <returns></returns>
|
2026-01-08 14:23:13 +08:00
|
|
|
|
public ImportResultDto Importmaterial(IFormFile formFile, string username)
|
2026-01-06 12:06:05 +08:00
|
|
|
|
{
|
2026-01-08 16:05:04 +08:00
|
|
|
|
string message = "导入成功";
|
2026-01-08 11:11:13 +08:00
|
|
|
|
// TODO 1.构建空的待导入数据集合MmMaterialExcelDto,多余部分删除
|
2026-01-08 11:45:06 +08:00
|
|
|
|
List<MmMaterialExcelDto> materialList = new();
|
2026-01-08 11:11:13 +08:00
|
|
|
|
// TODO 2.使用NPOI读取Excel内容,填充到MmMaterialExcelDto集合中,注意列的对应关系,id不要忘,且跳过标题行,目前需要从第2行开始读取注意修改row
|
2026-01-08 10:50:03 +08:00
|
|
|
|
using (var stream = formFile.OpenReadStream())
|
2026-01-07 16:38:07 +08:00
|
|
|
|
{
|
2026-01-08 10:50:03 +08:00
|
|
|
|
try
|
2026-01-07 16:38:07 +08:00
|
|
|
|
{
|
2026-01-08 10:50:03 +08:00
|
|
|
|
IWorkbook workbook = new XSSFWorkbook(stream);
|
|
|
|
|
|
ISheet sheet = workbook.GetSheetAt(0);
|
|
|
|
|
|
|
|
|
|
|
|
#region 读取excel
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历每一行
|
2026-01-08 14:41:18 +08:00
|
|
|
|
for (int row = 1; row <= sheet.LastRowNum; row++)
|
2026-01-08 10:50:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
IRow currentRow = sheet.GetRow(row);
|
|
|
|
|
|
if (currentRow != null) // 确保行不为空
|
|
|
|
|
|
{
|
2026-01-08 14:10:31 +08:00
|
|
|
|
MmMaterialExcelDto material = new MmMaterialExcelDto();
|
2026-01-08 10:50:03 +08:00
|
|
|
|
|
2026-01-08 11:45:06 +08:00
|
|
|
|
//00 ID
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_00 = currentRow.GetCell(0);
|
|
|
|
|
|
material.Id = (int)currentCell_00?.NumericCellValue;
|
|
|
|
|
|
|
2026-01-08 10:50:03 +08:00
|
|
|
|
//01 物料标号
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_01 = currentRow.GetCell(1);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.Type = currentCell_01?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
|
|
|
|
|
|
//02物料编码
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_02 = currentRow.GetCell(2);
|
|
|
|
|
|
material.MaterialCode = currentCell_02?.ToString();
|
|
|
|
|
|
if (
|
|
|
|
|
|
currentCell_02 == null
|
|
|
|
|
|
|| string.IsNullOrEmpty(material.MaterialCode)
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
2026-01-08 16:05:04 +08:00
|
|
|
|
message = $"物料编码不可为空,第{row + 1}行";
|
|
|
|
|
|
break;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//03物料名称
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_03 = currentRow.GetCell(3);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.MaterialName = currentCell_03?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
|
|
|
|
|
|
//04规格
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_04 = currentRow.GetCell(4);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.Specification = currentCell_04?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
|
|
|
|
|
|
//05 类型编码
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_05 = currentRow.GetCell(5);
|
|
|
|
|
|
material.CategoryCode = currentCell_05?.ToString();
|
|
|
|
|
|
if (
|
|
|
|
|
|
currentCell_05 == null
|
|
|
|
|
|
|| string.IsNullOrEmpty(material.CategoryCode)
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
2026-01-08 16:05:04 +08:00
|
|
|
|
message = $"类型编码不可为空,第{row + 1}行";
|
|
|
|
|
|
break;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//06 类型名称
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_06 = currentRow.GetCell(6);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.CategoryName = currentCell_06?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
//07 单位
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_07 = currentRow.GetCell(7);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.Unit = currentCell_07?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
//08 供应商编码
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_08 = currentRow.GetCell(8);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.SupplierCode = currentCell_08?.ToString() ?? string.Empty;
|
2026-01-08 10:50:03 +08:00
|
|
|
|
//09 供应商名称
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_09 = currentRow.GetCell(9);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.SupplierName = currentCell_09?.ToString() ?? string.Empty;
|
|
|
|
|
|
//10 安全库存
|
2026-01-08 10:50:03 +08:00
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_10 = currentRow.GetCell(10);
|
2026-01-08 16:05:04 +08:00
|
|
|
|
material.SafetyStock = (int)currentCell_10?.NumericCellValue;
|
|
|
|
|
|
//11 状态
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_11 = currentRow.GetCell(11);
|
|
|
|
|
|
material.Status = currentCell_11?.ToString() ?? "启用";
|
|
|
|
|
|
//12 创建时间
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_12 = currentRow.GetCell(12);
|
|
|
|
|
|
material.CreatedTime = currentCell_12?.DateCellValue ?? DateTime.Now;
|
|
|
|
|
|
//13 更新时间
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_13 = currentRow.GetCell(13);
|
|
|
|
|
|
material.UpdatedTime = currentCell_13?.DateCellValue ?? DateTime.Now;
|
|
|
|
|
|
//14 描述
|
|
|
|
|
|
NPOI.SS.UserModel.ICell currentCell_14 = currentRow.GetCell(14);
|
|
|
|
|
|
material.Description = currentCell_14?.ToString() ?? "Excel导入";
|
2026-01-08 14:23:13 +08:00
|
|
|
|
materialList.Add(material);
|
2026-01-08 10:50:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2026-01-07 16:38:07 +08:00
|
|
|
|
}
|
2026-01-08 10:50:03 +08:00
|
|
|
|
catch (Exception ex)
|
2026-01-07 16:38:07 +08:00
|
|
|
|
{
|
2026-01-08 14:23:13 +08:00
|
|
|
|
return null;
|
2026-01-07 16:38:07 +08:00
|
|
|
|
}
|
2026-01-08 10:50:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2026-01-08 11:11:13 +08:00
|
|
|
|
// TODO 3.调用SplitInsert方法实现导入操作,注意主键列的配置(建议优化为,ID相同则修改,不同则新增)
|
|
|
|
|
|
|
2026-01-07 15:39:28 +08:00
|
|
|
|
var x = Context
|
2026-01-08 11:45:06 +08:00
|
|
|
|
.Storageable(materialList)
|
2026-01-08 16:05:04 +08:00
|
|
|
|
//.SplitInsert(it => !it.Any())
|
|
|
|
|
|
//.WhereColumns(it => it.Id) //如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
|
2026-01-06 12:06:05 +08:00
|
|
|
|
.ToStorage();
|
2026-01-08 16:05:04 +08:00
|
|
|
|
var result = x.AsInsertable.ExecuteCommand();
|
|
|
|
|
|
var result2 = x.AsUpdateable.IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); //插入可插入部分;
|
2026-01-06 12:06:05 +08:00
|
|
|
|
|
2026-01-06 17:26:47 +08:00
|
|
|
|
var importResult = new ImportResultDto
|
|
|
|
|
|
{
|
2026-01-08 16:05:04 +08:00
|
|
|
|
Message = message,
|
2026-01-06 17:26:47 +08:00
|
|
|
|
Inserted = x.InsertList.Count,
|
|
|
|
|
|
Updated = x.UpdateList.Count,
|
|
|
|
|
|
ErrorCount = x.ErrorList.Count,
|
|
|
|
|
|
IgnoredCount = x.IgnoreList.Count,
|
|
|
|
|
|
Deleted = x.DeleteList.Count,
|
2026-01-07 15:39:28 +08:00
|
|
|
|
Total = x.TotalList.Count,
|
2026-01-06 17:26:47 +08:00
|
|
|
|
};
|
2026-01-07 15:39:28 +08:00
|
|
|
|
//输出统计
|
2026-01-06 17:26:47 +08:00
|
|
|
|
Console.WriteLine(importResult);
|
2026-01-08 11:11:13 +08:00
|
|
|
|
// 4.收集错误与忽略信息,返回导入结果ImportResultDto 提示,需要修改IServer相关返回格式
|
2026-01-07 16:38:07 +08:00
|
|
|
|
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,
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-01-06 12:06:05 +08:00
|
|
|
|
|
2026-01-06 17:26:47 +08:00
|
|
|
|
return importResult;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-01-07 15:39:28 +08:00
|
|
|
|
/// 导出物料表列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public PagedInfo<MmMaterialExcelDto> SelectMaterialList(
|
|
|
|
|
|
MmMaterialQueryDto material,
|
|
|
|
|
|
PagerInfo pager
|
|
|
|
|
|
)
|
2026-01-06 17:26:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
// Use the same predicate builder as GetList to support consistent filtering
|
|
|
|
|
|
var predicate = QueryExp(material);
|
|
|
|
|
|
|
|
|
|
|
|
var query = Queryable()
|
2026-01-07 15:36:08 +08:00
|
|
|
|
.Where(predicate.ToExpression())
|
2026-01-07 15:37:19 +08:00
|
|
|
|
.Select(it => new MmMaterialExcelDto
|
|
|
|
|
|
{
|
2026-01-07 15:39:28 +08:00
|
|
|
|
Id = it.Id,
|
|
|
|
|
|
Type = it.Type,
|
|
|
|
|
|
MaterialCode = it.MaterialCode,
|
2026-01-07 15:44:02 +08:00
|
|
|
|
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
|
2026-01-07 15:39:28 +08:00
|
|
|
|
});
|
2026-01-06 17:26:47 +08:00
|
|
|
|
|
|
|
|
|
|
return query.ToPage(pager);
|
2026-01-06 12:06:05 +08:00
|
|
|
|
}
|
2025-12-25 12:02:03 +08:00
|
|
|
|
}
|
2026-01-07 15:39:28 +08:00
|
|
|
|
}
|