Files
kunshan-bzfm-mes-backend/DOAN.Service/MES/Product/ProWorkorderImportService.cs
git_rabbit 898ee3de2a feat(工单): 添加工单缺陷数量字段并优化导入逻辑
为工单模型添加缺陷数量字段,优化Excel导入逻辑
增加必填字段校验和异常处理,完善默认值设置
2026-02-09 16:23:13 +08:00

500 lines
23 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using DOAN.Model;
using DOAN.Model.BZFM;
using DOAN.Model.MES.base_;
using DOAN.Model.MES.product;
using DOAN.Service.MES.product.IService;
using Infrastructure;
using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SqlSugar;
namespace DOAN.Service.MES.product
{
/// <summary>
/// 工单导入服务
/// </summary>
[AppService(ServiceType = typeof(IProWorkorderImportService), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderImportService : BaseService<ProWorkorder>, IProWorkorderImportService
{
/// <summary>
/// 导入工单 必须整删除 整改
/// </summary>
/// <param name="formFile"></param>
/// <param name="username"></param>
/// <returns></returns>
public int ImportData(IFormFile formFile, string username)
{
int result = 0;
List<ProWorkorder> workorderList = new List<ProWorkorder>();
DateTime dateValue = DateTime.MinValue;
// XXX 改为从物料清单获取信息
List<MmMaterial> mmMaterials = Context
.Queryable<MmMaterial>()
.Where(it => it.Status == "启用")
.ToList();
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
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) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
//00 主体品名
ICell currentCell_01 = currentRow.GetCell(0);
workorder.productionName = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(workorder.productionName))
{
throw new Exception($"{row + 1}行【主体品名】不可为空");
}
//01 成品型号
ICell currentCell_02 = currentRow.GetCell(1);
workorder.productionCode = currentCell_02?.ToString();
if (currentCell_02 == null || string.IsNullOrEmpty(workorder.productionCode))
{
throw new Exception($"{row + 1}行【主体型号】不可为空");
}
//02 单位
ICell currentCell_04 = currentRow.GetCell(2);
workorder.Unit = currentCell_04?.ToString();
//3 计划数量
ICell currentCell_07 = currentRow.GetCell(3);
workorder.PlanNum = (int)currentCell_07?.NumericCellValue;
if (currentCell_07 == null || workorder.PlanNum < 0)
{
workorder.PlanNum = 0;
}
//4 原材料名称
ICell currentCell_11 = currentRow.GetCell(4);
workorder.MaterialName = currentCell_11?.ToString();
if (currentCell_11 == null || string.IsNullOrEmpty(workorder.MaterialName))
{
throw new Exception($"{row + 1}行【材料型号】不可为空");
}
//5 原材料编号
ICell currentCell_12 = currentRow.GetCell(5);
workorder.MaterialCode = currentCell_12?.ToString();
if (currentCell_12 == null || string.IsNullOrEmpty(workorder.MaterialCode))
{
throw new Exception($"{row + 1}行【材料编码】不可为空");
}
//6 原材料材质
ICell currentCell_13 = currentRow.GetCell(6);
workorder.MaterialtextureCode = currentCell_13?.ToString();
//7 炉号
ICell currentCell_14 = currentRow.GetCell(7);
workorder.StoveCode = currentCell_14?.ToString();
//8 图号
ICell currentCell_15 = currentRow.GetCell(8);
workorder.DrawingCode = currentCell_15?.ToString();
//9 版本
ICell currentCell_16 = currentRow.GetCell(9);
workorder.Version = currentCell_16?.ToString();
//10 指导日期
ICell cell17 = currentRow.GetCell(10);
// 将单元格的数字值转换为DateTime
workorder.InstructionDate = cell17.DateCellValue.Value;
// 11 车间code
ICell currentCell_18 = currentRow.GetCell(11);
if (currentCell_18 == null)
{
workorder.WorkshopCode = string.Empty;
}
else
{
if (currentCell_18.CellType == CellType.Numeric)
{
workorder.WorkshopCode = currentCell_18.NumericCellValue.ToString();
}
else
{
workorder.WorkshopCode = currentCell_18.StringCellValue;
}
}
//12 组号code
ICell currentCell_19 = currentRow.GetCell(12);
if (currentCell_19 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_19.CellType == CellType.Numeric)
{
workorder.GroupCode = currentCell_19.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_19.StringCellValue;
}
}
//13 班号code
ICell currentCell_20 = currentRow.GetCell(13);
if (currentCell_20 == null)
{
workorder.RouteCode = string.Empty;
}
else
{
if (currentCell_20.CellType == CellType.Numeric)
{
workorder.RouteCode = currentCell_20.NumericCellValue.ToString();
}
else
{
workorder.RouteCode = currentCell_20.StringCellValue;
}
}
//14 优先级
ICell currentCell_21 = currentRow.GetCell(14);
if (currentCell_21.StringCellValue == "优先")
{
workorder.Priority = 3;
}
else
{
if (currentCell_21.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (currentCell_21.StringCellValue == "正常" || string.IsNullOrEmpty(currentCell_11.StringCellValue))
{
workorder.Priority = 1;
}
}
//15 节拍
ICell currentCell_22 = currentRow.GetCell(15);
workorder.Beat = (int)currentCell_22?.NumericCellValue;
//16 进料单号(外购)
ICell currentCell_010 = currentRow.GetCell(16);
workorder.FeedOrder = currentCell_010?.StringCellValue;
//17 客户单号(出货)
ICell currentCell_011 = currentRow.GetCell(17);
workorder.CustomerOrder = currentCell_011?.StringCellValue;
//18 备注
ICell currentCell_012 = currentRow.GetCell(18);
workorder.Remark01 = currentCell_012?.StringCellValue;
workorder.Id = SnowFlakeSingle.Instance.NextId().ToString();
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
// 工单 2024-9-13-最终顺序号
int index = (row - 3);
//TODO nickCode改为从物料清单进行获取
string nickCode = mmMaterials
.Where(it => it.MaterialCode == workorder.productionCode)
.Select(it => it.Type)
.FirstOrDefault();
workorder.Workorder = dateValue.ToString("yyyyMMdd") + "_" + workorder.GroupCode + workorder.RouteCode + "_" + nickCode + "_" + index.ToString("000");
workorder.Sort = index * 10;
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
#endregion
}
catch (Exception ex)
{
return -1;
}
}
UseTran2(() =>
{
Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
result = Context.Insertable(workorderList).ExecuteCommand();
});
return result;
}
/// <summary>
/// 分批导入,追加导入
/// </summary>
/// <param name="formFile"></param>
/// <param name="username"></param>
/// <returns></returns>
public int ImportDataAppend(IFormFile formFile, string username)
{
int result = 0;
List<ProWorkorder> workorderList = new List<ProWorkorder>();
DateTime dateValue = DateTime.MinValue;
// XXX 改为从物料清单获取信息
List<MmMaterial> mmMaterials = Context
.Queryable<MmMaterial>()
.Where(it => it.Status == "启用")
.ToList();
using (var stream = formFile.OpenReadStream())
{
try
{
IWorkbook workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
// 处理第2行 获取日期
IRow secondRow = sheet.GetRow(1);
ICell cell = secondRow.GetCell(0);
// 将单元格的数字值转换为DateTime
dateValue = cell.DateCellValue.Value;
// 遍历每一行
for (int row = 4; row <= sheet.LastRowNum; row++)
{
IRow currentRow = sheet.GetRow(row);
if (currentRow != null) // 确保行不为空
{
ProWorkorder workorder = new ProWorkorder();
#region excel
//00 成品名称
ICell currentCell_01 = currentRow.GetCell(0);
workorder.productionName = currentCell_01?.ToString();
if (currentCell_01 == null || string.IsNullOrEmpty(workorder.productionName))
{
throw new Exception($"{row + 1}行【主体品名】不可为空");
}
//01 成品型号
ICell currentCell_02 = currentRow.GetCell(1);
workorder.productionCode = currentCell_02?.ToString();
if (currentCell_02 == null || string.IsNullOrEmpty(workorder.productionCode))
{
throw new Exception($"{row + 1}行【主体型号】不可为空");
}
//02 单位
ICell currentCell_04 = currentRow.GetCell(2);
workorder.Unit = currentCell_04?.ToString();
//3 计划数量
ICell currentCell_07 = currentRow.GetCell(3);
workorder.PlanNum = (int)currentCell_07?.NumericCellValue;
if (currentCell_07 == null || workorder.PlanNum < 0)
{
workorder.PlanNum = 0;
}
//4 原材料名称
ICell currentCell_11 = currentRow.GetCell(4);
workorder.MaterialName = currentCell_11?.ToString();
if (currentCell_11 == null || string.IsNullOrEmpty(workorder.MaterialName))
{
throw new Exception($"{row + 1}行【材料型号】不可为空");
}
//5 原材料编号
ICell currentCell_12 = currentRow.GetCell(5);
workorder.MaterialCode = currentCell_12?.ToString();
if (currentCell_12 == null || string.IsNullOrEmpty(workorder.MaterialCode))
{
throw new Exception($"{row + 1}行【材料编码】不可为空");
}
//6 材质
ICell currentCell_13 = currentRow.GetCell(6);
workorder.MaterialtextureCode = currentCell_13?.ToString();
//7 炉号
ICell currentCell_14 = currentRow.GetCell(7);
workorder.StoveCode = currentCell_14?.ToString();
//8 图号
ICell currentCell_15 = currentRow.GetCell(8);
workorder.DrawingCode = currentCell_15?.ToString();
//9 版本
ICell currentCell_16 = currentRow.GetCell(9);
workorder.Version = currentCell_16?.ToString();
//10 指导日期
ICell cell17 = currentRow.GetCell(10);
// 将单元格的数字值转换为DateTime
workorder.InstructionDate = cell17.DateCellValue.Value;
if(cell17 == null)
{
workorder.InstructionDate = dateValue;
}
// 11 车间code
ICell currentCell_18 = currentRow.GetCell(11);
if (currentCell_18 == null)
{
workorder.WorkshopCode = string.Empty;
}
else
{
if (currentCell_18.CellType == CellType.Numeric)
{
workorder.WorkshopCode = currentCell_18.NumericCellValue.ToString();
}
else
{
workorder.WorkshopCode = currentCell_18.StringCellValue;
}
}
//12 组号code
ICell currentCell_19 = currentRow.GetCell(12);
if (currentCell_19 == null)
{
workorder.GroupCode = string.Empty;
}
else
{
if (currentCell_19.CellType == CellType.Numeric)
{
workorder.GroupCode = currentCell_19.NumericCellValue.ToString();
}
else
{
workorder.GroupCode = currentCell_19.StringCellValue;
}
}
//13 班号code
ICell currentCell_20 = currentRow.GetCell(13);
if (currentCell_20 == null)
{
workorder.RouteCode = string.Empty;
}
else
{
if (currentCell_20.CellType == CellType.Numeric)
{
workorder.RouteCode = currentCell_20.NumericCellValue.ToString();
}
else
{
workorder.RouteCode = currentCell_20.StringCellValue;
}
}
//14 优先级
ICell currentCell_21 = currentRow.GetCell(14);
if (currentCell_21.StringCellValue == "优先")
{
workorder.Priority = 3;
}
else
{
if (currentCell_21.StringCellValue == "插单")
{
workorder.Priority = 2;
}
else if (currentCell_21.StringCellValue == "正常" || string.IsNullOrEmpty(currentCell_11.StringCellValue))
{
workorder.Priority = 1;
}
}
//15 节拍
ICell currentCell_22 = currentRow.GetCell(15);
workorder.Beat = (int)currentCell_22?.NumericCellValue;
//16 进料单号(外购)
ICell currentCell_010 = currentRow.GetCell(16);
workorder.FeedOrder = currentCell_010?.StringCellValue;
//17 客户单号(出货)
ICell currentCell_011 = currentRow.GetCell(17);
workorder.CustomerOrder = currentCell_011?.StringCellValue;
//18 备注
ICell currentCell_012 = currentRow.GetCell(18);
workorder.Remark01 = currentCell_012?.StringCellValue;
#endregion
workorder.Id = SnowFlakeSingle.Instance.NextId().ToString();
workorder.CreatedBy = username;
workorder.CreatedTime = DateTime.Now;
workorder.WorkorderDate = dateValue;
workorder.Status = 1;
// 获取当前日期工单最大顺序号和序号
DateTime currentDate = dateValue.Date;
var MaxWorkorder = Context
.Queryable<ProWorkorder>()
.Where(it => it.WorkorderDate == currentDate)
.OrderByDescending(it => it.Sort)
.Select(it => new { it.Workorder, it.Sort })
.First();
// 工单 2024-9-13-最终顺序号
int index = (row - 3);
//TODO nickCode改为从物料清单进行获取
string nickCode = mmMaterials
.Where(it => it.MaterialCode == workorder.productionCode)
.Select(it => it.Type)
.FirstOrDefault();
workorder.Workorder = dateValue.ToString("yyyyMMdd") + "_" + workorder.GroupCode + workorder.RouteCode + "_" + nickCode + "_" + index.ToString("000");
workorder.Sort = index * 10 + (MaxWorkorder?.Sort ?? 0);
CultureInfo culture = CultureInfo.CurrentCulture;
workorderList.Add(workorder);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
UseTran2(() =>
{
// Context.Deleteable<ProWorkorder>().Where(it => it.WorkorderDate == dateValue).ExecuteCommand();
result = Context.Insertable(workorderList).ExecuteCommand();
});
return result;
}
}
}