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
{
///
/// 物料表Service业务层处理
///
[AppService(ServiceType = typeof(IMmMaterialService), ServiceLifetime = LifeTime.Transient)]
public class MmMaterialService : BaseService, IMmMaterialService
{
///
/// 查询物料表列表
///
///
///
public PagedInfo GetList(MmMaterialQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(it => it.Type)
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public MmMaterial GetInfo(int Id)
{
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
///
/// 添加物料表
///
///
///
public MmMaterial AddMmMaterial(MmMaterial model)
{
return Insertable(model).ExecuteReturnEntity();
}
///
/// 修改物料表
///
///
///
public int UpdateMmMaterial(MmMaterial model)
{
return Update(model, true);
}
///
/// 查询导出表达式
///
///
///
private static Expressionable QueryExp(MmMaterialQueryDto parm)
{
var predicate = Expressionable
.Create()
.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;
}
///
/// 获取物料类别下拉框
///
///
///
///
public List GetMmMaterialCategoryOptions(
MmMaterialCategoryDto parm
)
{
try
{
return Context
.Queryable()
.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;
}
}
///
/// 导入数据
///
///
///
public int Importmaterial(IFormFile formFile)
{
// TODO 1.构建空的待导入数据集合MmMaterialExcelDto,多余部分删除
List 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;
}
///
/// 导出物料表列表
///
///
public PagedInfo 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);
}
}
}