From 67cb906d84b5a176e0a52fb8ee105a8ca0401481 Mon Sep 17 00:00:00 2001 From: hehaibing-1996 Date: Mon, 10 Mar 2025 15:45:13 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=AE=9E=E6=97=B6=E5=AF=BC=E5=85=A5=E6=97=A7?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=BB=91=E5=AE=9A=E5=85=B3=E7=B3=BB=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2.物料基础数据导入优化 --- WCS.BLL/DbModels/STZL/MatBaseInfo.cs | 2 +- .../Services/Service/MatBaseInfoService.cs | 32 ++--- .../MatDetailCurrentInfoImportModel.cs | 10 ++ .../ApiModel/MatBaseInfo/MatBaseInfoModel.cs | 2 +- .../BatchBindMatDetailController.cs | 117 +++++++++++++++++- 5 files changed, 146 insertions(+), 17 deletions(-) diff --git a/WCS.BLL/DbModels/STZL/MatBaseInfo.cs b/WCS.BLL/DbModels/STZL/MatBaseInfo.cs index 017fa58..3aed9ae 100644 --- a/WCS.BLL/DbModels/STZL/MatBaseInfo.cs +++ b/WCS.BLL/DbModels/STZL/MatBaseInfo.cs @@ -78,7 +78,7 @@ namespace WCS.BLL.DbModels /// 物料默认数量 /// [SugarColumn(ColumnName = "mat_default_qty", IsNullable = true, ColumnDescription = "物料默认数量")] - public int? MatDefaultQty { get; set; } + public int? MatDefaultQty { get; set; } = 0; /// /// 是否启用 diff --git a/WCS.BLL/Services/Service/MatBaseInfoService.cs b/WCS.BLL/Services/Service/MatBaseInfoService.cs index ab5b066..adab4ae 100644 --- a/WCS.BLL/Services/Service/MatBaseInfoService.cs +++ b/WCS.BLL/Services/Service/MatBaseInfoService.cs @@ -133,27 +133,27 @@ namespace WCS.BLL.Services.Service #endregion #region 校验导入数据与数据库中数据是否有重复数据 - duplicates = await DbHelp.db.Queryable() + var duplicatesInDb = await DbHelp.db.Queryable() .Where(t => matCodes.Contains(t.MatCode)) - .Select(t => t.MatCode) + .ToListAsync(); - //有重复的情况 - if (duplicates.Count > 0) - { - return new ResponseCommon>() - { - Code = 201, - Message = "导入失败:以下物料编码已存在", - Data = duplicates - }; - } + ////有重复的情况 + //if (duplicates.Count > 0) + //{ + // return new ResponseCommon>() + // { + // Code = 201, + // Message = "导入失败:以下物料编码已存在", + // Data = duplicates + // }; + //} #endregion #region 导入数据 try { - await DbHelp.db.BeginTranAsync(); + var matBaseInfos = new List(); foreach (var mat in lists) { var matBaseInfo = new MatBaseInfo() @@ -166,8 +166,12 @@ namespace WCS.BLL.Services.Service ModifyUser = userName, IsEnable = mat.状态 == "启用" ? true : false, }; - await DbHelp.db.Insertable(matBaseInfo).ExecuteCommandAsync(); + matBaseInfos.Add(matBaseInfo); } + await DbHelp.db.BeginTranAsync(); + //删除数据库中重复数据 + DbHelp.db.Deleteable(duplicatesInDb).ExecuteCommand(); + await DbHelp.db.Insertable(matBaseInfos).ExecuteCommandAsync(); await DbHelp.db.CommitTranAsync(); return new ResponseCommon>() { diff --git a/WCS.Model/ApiModel/BatchBindMatDetail/MatDetailCurrentInfoImportModel.cs b/WCS.Model/ApiModel/BatchBindMatDetail/MatDetailCurrentInfoImportModel.cs index 738122d..edebb23 100644 --- a/WCS.Model/ApiModel/BatchBindMatDetail/MatDetailCurrentInfoImportModel.cs +++ b/WCS.Model/ApiModel/BatchBindMatDetail/MatDetailCurrentInfoImportModel.cs @@ -16,4 +16,14 @@ namespace WCS.Model.ApiModel.BatchBindMatDetail public int? 数量 { get; set; } public string 货架编码 { get; set; } } + + /// + /// 旧系统导入模板 + /// + public class OldSystemImportModel + { + public string 货架编号 { get; set; } + + public string 物料批次号 { get; set; } + } } diff --git a/WCS.Model/ApiModel/MatBaseInfo/MatBaseInfoModel.cs b/WCS.Model/ApiModel/MatBaseInfo/MatBaseInfoModel.cs index e8b92b0..22187bc 100644 --- a/WCS.Model/ApiModel/MatBaseInfo/MatBaseInfoModel.cs +++ b/WCS.Model/ApiModel/MatBaseInfo/MatBaseInfoModel.cs @@ -24,7 +24,7 @@ namespace WCS.BLL.DbModels /// /// 物料默认数量 /// - public int MatDefaultQty { get; set; } = 100; + public int? MatDefaultQty { get; set; } public string MatSupplier { get; set; } diff --git a/WCS.WebApi/Controllers/BatchBindMatDetailController.cs b/WCS.WebApi/Controllers/BatchBindMatDetailController.cs index d879172..779a5d4 100644 --- a/WCS.WebApi/Controllers/BatchBindMatDetailController.cs +++ b/WCS.WebApi/Controllers/BatchBindMatDetailController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; +using System.Linq.Expressions; using WCS.BLL.DbModels; using WCS.BLL.Services.IService; using WCS.BLL.Services.Service; @@ -164,7 +165,7 @@ namespace WCS.WebApi.Controllers stream.Position = 0; var list = MiniExcelLibs.MiniExcel.Query(stream, "物料绑定", ExcelType.XLSX).ToList(); //去除空白行 - //list.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料编码)); + //infoList.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料编码)); if (list == null || list.Count == 0) { return new ResponseCommon() @@ -177,5 +178,119 @@ namespace WCS.WebApi.Controllers } } + /// + /// 导入旧系统绑定关系 + /// + /// + /// + /// + /// + /// + [HttpPost("importOldSystem")] + public async Task importOldSystem([FromForm] IFormFile excelFile, [FromForm] string userName + , [FromForm] string deviceType) + { + try + { + //文件校验 + if (excelFile == null || excelFile.Length == 0) + { + return new ResponseCommon() + { + Code = 201, + Message = "导入失败:文件无有效内容!" + }; + } + //输入数据校验 + if (string.IsNullOrEmpty(userName)) + { + return new ResponseCommon() + { + Code = 201, + Message = "导入失败:参数[用户名]无效,请登录后重试!" + }; + } + if (string.IsNullOrEmpty(deviceType)) + { + return new ResponseCommon() + { + Code = 201, + Message = "导入失败:参数[设备类型]无效!" + }; + } + + + using (var stream = new MemoryStream()) + { + await excelFile.CopyToAsync(stream); + stream.Position = 0; + var infoList = MiniExcelLibs.MiniExcel.Query(stream, "货架管理", ExcelType.XLSX).ToList(); + //去除空白行 + infoList.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料批次号)); + if (infoList == null || infoList.Count == 0) + { + return new ResponseCommon() + { + Code = 201, + Message = "导入失败:上传的文件中不存在有效的数据!" + }; + } + var shelfCodes = infoList.Select(t => t.货架编号).Distinct().ToList(); + //获取系统中的货架 + var shelfs = DbHelp.db.Queryable() + .LeftJoin((si, mci) => mci.ShelfId == si.Id) + //.Where((si, mci) => mci.Id == 0)//本系统未采取绑定的才进行绑定 + .Where((si, mci) => shelfCodes.Contains(si.ShelfCode)) + .Select((si, mci) => si) + .Distinct() + .ToList(); + //var shelfCodesInDb = shelfs.Select(t => t.ShelfCode).Distinct().ToList(); + + var matCurrentInfos = new List(); + foreach (var info in infoList) + { + var shelf = shelfs.Where(t => t.ShelfCode == info.货架编号).FirstOrDefault(); + //此货架已在新系统绑定的情况 + if (shelf == null) + { + continue; + } + var matCurrentInfo = new MatDetailCurrentInfo() + { + ShelfId = shelf.Id, + ShelfCode = shelf.ShelfCode, + ShelfType = shelf.ShelfTypeName, + MatCode = info.物料批次号 + "(旧系统)", + MatName = info.物料批次号 + "(旧系统)", + MatBatch = "", + MatQty = 1, + ModifyUser = "system" + }; + matCurrentInfos.Add(matCurrentInfo); + } + + //删除上一次导入的信息 + DbHelp.db.Deleteable().Where(t => t.MatCode.Contains("(旧系统)")).ExecuteCommand(); + DbHelp.db.Insertable(matCurrentInfos).ExecuteCommand(); + + + return new ResponseCommon() + { + Code = 200, + Message = "Success" + }; + } + + } + catch (Exception ex) { + + + return new ResponseCommon() + { + Code = 201, + Message = "失败" + ex.Message + }; + } + } } }