382 lines
14 KiB
C#
382 lines
14 KiB
C#
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="material"></param>
|
||
/// <returns></returns>
|
||
public ImportResultDto Importmaterial(IFormFile formFile)
|
||
{
|
||
int result = 0;
|
||
List<ProWorkorder> materialList = new();
|
||
DateTime dateValue = DateTime.MinValue;
|
||
// 获取 产品代号
|
||
List<MmMaterialExcelDto> ProductCodeList = Context
|
||
.Queryable<MmMaterialExcelDto>()
|
||
.Where(it => it.Type == "type")
|
||
.ToList();
|
||
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);
|
||
|
||
// 将单元格的数字值转换为DateTime
|
||
dateValue = cell.DateCellValue.Value;
|
||
#region 读取excel
|
||
|
||
// 遍历每一行
|
||
for (int row = 4; row <= sheet.LastRowNum; row++)
|
||
{
|
||
IRow currentRow = sheet.GetRow(row);
|
||
if (currentRow != null) // 确保行不为空
|
||
{
|
||
MmMaterial material = new MmMaterial();
|
||
|
||
//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;
|
||
}
|
||
|
||
|
||
}
|
||
var x = Context
|
||
.Storageable(material)
|
||
.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);
|
||
|
||
// 收集错误与忽略信息
|
||
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);
|
||
}
|
||
}
|
||
}
|