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 { /// /// 工单导入服务 /// [AppService(ServiceType = typeof(IProWorkorderImportService), ServiceLifetime = LifeTime.Transient)] public class ProWorkorderImportService : BaseService, IProWorkorderImportService { /// /// 导入工单 必须整删除 整改 /// /// /// /// public int ImportData(IFormFile formFile, string username) { int result = 0; List workorderList = new List(); DateTime dateValue = DateTime.MinValue; // XXX 改为从物料清单获取信息 List mmMaterials = Context .Queryable() .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)) { continue; } //01 成品型号 ICell currentCell_02 = currentRow.GetCell(1); workorder.productionCode = currentCell_02?.ToString(); if (currentCell_02 == null || string.IsNullOrEmpty(workorder.productionCode)) { continue; } //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; //4 原材料名称 ICell currentCell_11 = currentRow.GetCell(4); workorder.MaterialName = currentCell_11?.ToString(); if (currentCell_11 == null || string.IsNullOrEmpty(workorder.MaterialName)) { continue; } //5 原材料编号 ICell currentCell_12 = currentRow.GetCell(5); workorder.MaterialCode = currentCell_12?.ToString(); if (currentCell_12 == null || string.IsNullOrEmpty(workorder.MaterialCode)) { continue; } //6 原材料材质 ICell currentCell_13 = currentRow.GetCell(6); workorder.MaterialtextureCode = currentCell_13?.ToString(); //7 炉号 ICell currentCell_14 = currentRow.GetCell(7); workorder.StoveCode = currentCell_14?.ToString(); if (currentCell_14 == null || string.IsNullOrEmpty(workorder.StoveCode)) { continue; } //8 图号 ICell currentCell_15 = currentRow.GetCell(8); workorder.DrawingCode = currentCell_15?.ToString(); if (currentCell_15 == null || string.IsNullOrEmpty(workorder.DrawingCode)) { continue; } //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().Where(it => it.WorkorderDate == dateValue).ExecuteCommand(); result = Context.Insertable(workorderList).ExecuteCommand(); }); return result; } /// /// 分批导入,追加导入 /// /// /// /// public int ImportDataAppend(IFormFile formFile, string username) { int result = 0; List workorderList = new List(); DateTime dateValue = DateTime.MinValue; // XXX 改为从物料清单获取信息 List mmMaterials = Context .Queryable() .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)) { continue; } //01 成品型号 ICell currentCell_02 = currentRow.GetCell(1); workorder.productionCode = currentCell_02?.ToString(); if (currentCell_02 == null || string.IsNullOrEmpty(workorder.productionCode)) { continue; } //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; //4 原材料名称 ICell currentCell_11 = currentRow.GetCell(4); workorder.MaterialName = currentCell_11?.ToString(); if (currentCell_11 == null || string.IsNullOrEmpty(workorder.MaterialName)) { continue; } //5 原材料编号 ICell currentCell_12 = currentRow.GetCell(5); workorder.MaterialCode = currentCell_12?.ToString(); if (currentCell_12 == null || string.IsNullOrEmpty(workorder.MaterialCode)) { continue; } //6 材质 ICell currentCell_13 = currentRow.GetCell(6); workorder.MaterialtextureCode = currentCell_13?.ToString(); if (currentCell_13 == null || string.IsNullOrEmpty(workorder.MaterialtextureCode)) { continue; } //7 炉号 ICell currentCell_14 = currentRow.GetCell(7); workorder.StoveCode = currentCell_14?.ToString(); if (currentCell_14 == null || string.IsNullOrEmpty(workorder.StoveCode)) { continue; } //8 图号 ICell currentCell_15 = currentRow.GetCell(8); workorder.DrawingCode = currentCell_15?.ToString(); if (currentCell_15 == null || string.IsNullOrEmpty(workorder.DrawingCode)) { continue; } //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; #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() .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) { return -1; } } UseTran2(() => { // Context.Deleteable().Where(it => it.WorkorderDate == dateValue).ExecuteCommand(); result = Context.Insertable(workorderList).ExecuteCommand(); }); return result; } } }