diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Common/DataValidationService.cs b/admin.net.pro/Admin.NET/Vistar.Application/Common/DataValidationService.cs index 6052ab9..d19c4f0 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Common/DataValidationService.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/Common/DataValidationService.cs @@ -7,6 +7,7 @@ using Admin.NET.Core; using Admin.NET.Core.Service; using DocumentFormat.OpenXml.Bibliography; +using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Drawing.Spreadsheet; using Furion.DependencyInjection; using Furion.DynamicApiController; @@ -27,6 +28,7 @@ using Vistar.Application.Entity; using Vistar.Application.SapService.Dto; using Vistar.Application.Util; using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinAccountGetAccountBasicInfoResponse.Types; +using static SKIT.FlurlHttpClient.Wechat.Api.Models.ScanProductAddV2Request.Types.Product.Types; namespace Vistar.Application.Common; public class DataValidationService : IDynamicApiController, ITransient @@ -80,10 +82,10 @@ public class DataValidationService : IDynamicApiController, ITransient string verification = "不存在"; - if (objId==110) + if (objId == 110) { var data = db.CopyNew().Queryable().Where(x => x._System_objNBS == materialCode && x.deleted == false && x.RecordGuid == recordGuid).Count(); - if (data>0) + if (data > 0) { verification = "存在"; } @@ -194,7 +196,7 @@ public class DataValidationService : IDynamicApiController, ITransient { case 110: data = await db.CopyNew().Queryable() - .Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x=>x.idRecord) + .Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord) .FirstAsync(); break; case 109: @@ -395,32 +397,35 @@ public class DataValidationService : IDynamicApiController, ITransient /// public async Task ModifyInventoryInformation(string recordGuid, string code, long objectid, string Bdmng, string Labst, string ZwqslPo, string ZwqslPr, DateTime dateUpdated) { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); if (code == "S") { if (objectid == 137) { - var data = _obj137Rep.AsQueryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).First(); + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); if (data != null) { - await _obj137Rep.AsUpdateable().SetColumns(it => new Obj137 - { - fld006530 = Bdmng, - fld005623 = Labst, - fld006531 = ZwqslPo, - fld006532 = ZwqslPr, - fld005624 = dateUpdated - }) + db.CopyNew().Updateable() + .SetColumns(it => new Obj137 + { + fld006530 = Bdmng, + fld005623 = Labst, + fld006531 = ZwqslPo, + fld006532 = ZwqslPr, + fld005624 = dateUpdated + }) .Where(it => it.idRecord == data.idRecord) - .ExecuteCommandAsync(); + .ExecuteCommand(); } } if (objectid == 112) { - var data = _obj112Rep.AsQueryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).First(); + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).FirstAsync(); if (data != null) { - await _obj112Rep.AsUpdateable().SetColumns(it => new Obj112 + db.CopyNew().Updateable().SetColumns(it => new Obj112 { fld006750 = Bdmng, fld006751 = Labst, @@ -429,7 +434,7 @@ public class DataValidationService : IDynamicApiController, ITransient fld006755 = dateUpdated }) .Where(it => it.idRecord == data.idRecord) - .ExecuteCommandAsync(); + .ExecuteCommand(); } } } @@ -437,10 +442,10 @@ public class DataValidationService : IDynamicApiController, ITransient { if (objectid == 137) { - var data = await _obj137Rep.AsQueryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); if (data != null) { - await _obj137Rep.AsUpdateable().SetColumns(it => new Obj137 + db.CopyNew().Updateable().SetColumns(it => new Obj137 { fld006530 = "", fld005623 = "", @@ -449,31 +454,114 @@ public class DataValidationService : IDynamicApiController, ITransient fld005624 = dateUpdated }) .Where(it => it.idRecord == data.idRecord) - .ExecuteCommandAsync(); + .ExecuteCommand(); } } if (objectid == 112) { - var data = await _obj112Rep.AsQueryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).FirstAsync(); + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).FirstAsync(); if (data != null) { - await _obj112Rep.AsUpdateable().SetColumns(it => new Obj112 - { - fld006750 = "", - fld006751 = "", - fld006752 = "", - fld006753 = "", - fld006755 = dateUpdated - }) + db.CopyNew().Updateable() + .SetColumns(it => new Obj112 + { + fld006750 = "", + fld006751 = "", + fld006752 = "", + fld006753 = "", + fld006755 = dateUpdated + }) .Where(it => it.idRecord == data.idRecord) - .ExecuteCommandAsync(); + .ExecuteCommand(); } } } return "修改成功"; + } + /// + /// 修改价格信息 + /// + /// + /// + /// + /// + /// + /// + public async Task ModifyPriceInformation(string recordGuid, string code, long objectid, string Verpr, DateTime dateUpdated) + { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); + if (code == "S") + { + if (objectid == 137) + { + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); + if (data != null) + { + db.CopyNew().Updateable() + .SetColumns(it => new Obj137 + { + fld005625 = Verpr, + fld005626 = dateUpdated + }) + .Where(it => it.idRecord == data.idRecord) + .ExecuteCommand(); + } + } + + if (objectid == 112) + { + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).FirstAsync(); + if (data != null) + { + db.CopyNew().Updateable().SetColumns(it => new Obj112 + { + fld006754 = Verpr, + fld006756 = dateUpdated + }) + .Where(it => it.idRecord == data.idRecord) + .ExecuteCommand(); + } + } + } + else + { + if (objectid == 137) + { + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); + if (data != null) + { + db.CopyNew().Updateable().SetColumns(it => new Obj137 + { + fld005625 = "", + fld005626 = dateUpdated + }) + .Where(it => it.idRecord == data.idRecord) + .ExecuteCommand(); + } + } + + if (objectid == 112) + { + var data = await db.CopyNew().Queryable().Where(x => x.RecordGuid == recordGuid && x.deleted == false).FirstAsync(); + if (data != null) + { + db.CopyNew().Updateable() + .SetColumns(it => new Obj112 + { + fld006754 = "", + fld006756 = dateUpdated + }) + .Where(it => it.idRecord == data.idRecord) + .ExecuteCommand(); + } + } + } + + return "修改成功"; } /// @@ -511,6 +599,42 @@ public class DataValidationService : IDynamicApiController, ITransient return status; } + public async Task ProcessNumber(decimal number) + { + if (number < 1) + { + string numStr = number.ToString(); + int decimalIndex = numStr.IndexOf('.'); + string decimalPart = numStr.Substring(decimalIndex + 1); + // 去除小数部分末尾的 0 + decimalPart = decimalPart.TrimEnd('0'); + if (decimalPart.Length == 1) + { + return number; + } + int firstNonZeroIndex = 0; + while (firstNonZeroIndex < decimalPart.Length && decimalPart[firstNonZeroIndex] == '0') + { + firstNonZeroIndex++; + } + if (firstNonZeroIndex == decimalPart.Length - 1) + { + // 若只有一位非零数字,找到合适的进位位置 + string newDecimalPart = new string('0', firstNonZeroIndex - 1) + "1"; + return decimal.Parse("0." + newDecimalPart); + } + return Math.Round(number, decimalPart.Length - firstNonZeroIndex - 1); + } + else + { + if (number < 10000) + { + int power = (int)Math.Floor(Math.Log10((double)number)); + return (decimal)(Math.Floor((double)number / Math.Pow(10, power)) * Math.Pow(10, power)); + } + return (decimal)(Math.Floor((double)number / 1000) * 1000); + } + } ///// ///// 验证是否存在bom和工艺路线 diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj112.cs b/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj112.cs index 552e90a..7f40b7a 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj112.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj112.cs @@ -553,4 +553,9 @@ public class Obj112 /// public DateTime fld006755 { get; set; } + /// + /// 成本更新日期 + /// + public DateTime fld006756 { get; set; } + } diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj137.cs b/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj137.cs index de5e85a..f600d5a 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj137.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/Entity/Obj137.cs @@ -1407,4 +1407,9 @@ public class Obj137 /// 库存更新日期 /// public DateTime fld005624 { get; set; } + + /// + /// 成本更新日期 + /// + public DateTime fld005626 { get; set; } } diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/Dto/BomPriceInquiryOutput.cs b/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/Dto/BomPriceInquiryOutput.cs new file mode 100644 index 0000000..e000836 --- /dev/null +++ b/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/Dto/BomPriceInquiryOutput.cs @@ -0,0 +1,79 @@ +// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 +// +// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 +// +// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Vistar.Application.Service.StockInquiry.Dto; +public class BomPriceInquiryOutput +{ + /// + /// 物料编码 + /// + public string materialCode { get; set; } + + /// + /// 物料描述 + /// + public string materialName { get; set; } + + /// + /// sap入参 + /// + public string Parameter { get; set; } + + /// + /// 消息类型 + /// + public string Code { get; set; } + + /// + /// 物料编码 + /// + public string Matnr { get; set; } + + /// + /// 工厂 + /// + public string Bwkey { get; set; } + + /// + /// 价格 + /// + public string Verpr { get; set; } + + /// + /// sap出参 + /// + public string Result { get; set; } + + /// + /// 更新日期 + /// + public DateTime DateUpdated { get; set; } + + /// + /// 查询状态 + /// + public string State { get; set; } +} + +public class BomPriceInquiryUpdateable +{ + public long Objectid { get; set; } + public string RecordGuid { get; set; } + public string Code { get; set; } + + /// + /// 价格 + /// + public string Verpr { get; set; } + + public DateTime DateUpdated { get; set; } +} diff --git a/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/StockInquiryService.cs b/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/StockInquiryService.cs index 700b275..7b04dca 100644 --- a/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/StockInquiryService.cs +++ b/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/StockInquiryService.cs @@ -6,15 +6,18 @@ using Admin.NET.Core; using Admin.NET.Core.Service; +using DocumentFormat.OpenXml.VariantTypes; using Furion.DependencyInjection; using Furion.DynamicApiController; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Data.OscarClient; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -37,6 +40,7 @@ public class StockInquiryService : IDynamicApiController, ITransient public SolidWorksManageService.SolidWorksManageService _solidWorksManageService; private readonly SysConfigService _sysConfigService; public SapService.SapService _sapService; + private readonly IServiceScopeFactory _scopeFactory; public StockInquiryService ( DataValidationService dataValidationService, @@ -45,7 +49,8 @@ public class StockInquiryService : IDynamicApiController, ITransient SqlSugarRepository configurationDataRep, SolidWorksManageService.SolidWorksManageService solidWorksManageService, SysConfigService sysConfigService, - SapService.SapService sapService + SapService.SapService sapService, + IServiceScopeFactory scopeFactory ) { _obj137Rep = obj137Rep; @@ -55,6 +60,7 @@ public class StockInquiryService : IDynamicApiController, ITransient _sysConfigService = sysConfigService; _sapService = sapService; _dataValidationService = dataValidationService; + _scopeFactory = scopeFactory; } /// @@ -137,14 +143,17 @@ public class StockInquiryService : IDynamicApiController, ITransient [AllowAnonymous] public async Task> GetBomProductInventory(string materialCode) { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); + var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); - var materialData = await _obj137Rep.AsQueryable() + var materialData = await db.CopyNew().Queryable() .Where(x => x._System_objNBS == materialCode && x.deleted == false) .OrderByDescending(x => x.idRecord) .FirstAsync(); - var bomList = await _configurationDataRep.AsQueryable() + var bomList = await db.CopyNew().Queryable() .Where(x => x.ParentGuid == materialData.RecordGuid && x.isDeleted == false && x.ParentVersion == materialData.VersionIndex && (x.fld005577 != "不包含" || x.fld005577 == null) @@ -152,95 +161,191 @@ public class StockInquiryService : IDynamicApiController, ITransient .ToListAsync(); var UpdateableList = new List(); + var UpdateableOutput = new List(); - var tasks = bomList.Select(async item => + int batchSize = 20; // 每批的大小 + int totalCount = bomList.ToList().Count; + + for (int i = 0; i < totalCount; i += batchSize) { - var code = item.PartNumber; - var name = item.Description; - var objectid = item.ChildObjID; - var childGuid = item.ChildGuid; + var batch = bomList.Skip(i).Take(batchSize); // 分批获取数据 - var SapEcnQueryData = new SapEcnQueryInput + // 处理当前批次的数据 + var tasks = batch.Select(async item => { - Reqkeyid = "", - Businessid = "", - Messageid = "", - Sndprn = "PLM", - Rcvprn = "SAP", - Requser = Requser, - Note1 = "", - Note2 = "", - Note3 = "", - Sign = "I", - Option = "EQ", - Low = code, - High = "" - }; + var code = item.PartNumber; + var name = item.Description; + var objectid = item.ChildObjID; + var childGuid = item.ChildGuid; - var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); - var dateUpdated = DateTime.Now; - if (apiOutput.Code == "S") - { - var UpdateableData = new BomStockInquiryUpdateable + var SapEcnQueryData = new SapEcnQueryInput { - Objectid = objectid, - Code = apiOutput.Code, - RecordGuid = childGuid, - Bdmng = apiOutput.Bdmng, - Labst = apiOutput.Labst, - ZwqslPo = apiOutput.ZwqslPo, - ZwqslPr = apiOutput.ZwqslPr, - DateUpdated = dateUpdated + Reqkeyid = "", + Businessid = "", + Messageid = "", + Sndprn = "PLM", + Rcvprn = "SAP", + Requser = Requser, + Note1 = "", + Note2 = "", + Note3 = "", + Sign = "I", + Option = "EQ", + Low = code, + High = "" }; - UpdateableList.Add(UpdateableData); - } - else - { - var UpdateableData = new BomStockInquiryUpdateable - { - Code = "", - RecordGuid = "", - Bdmng = "", - Labst = "", - ZwqslPo = "", - ZwqslPr = "", - DateUpdated = dateUpdated - }; - UpdateableList.Add(UpdateableData); - } - if (apiOutput.Code == "S") - { - return new BomStockInquiryOutput - { - materialCode = code, - materialName = name, - Labst = apiOutput.Labst, - Bdmng = apiOutput.Bdmng, - ZwqslPr = apiOutput.ZwqslPr, - ZwqslPo = apiOutput.ZwqslPo, - DateUpdated = dateUpdated, - State = "查询成功" - }; - } - else - { - return new BomStockInquiryOutput - { - materialCode = code, - materialName = name, - State = "SAP未查询库存信息" - }; - } - }); - var bomStockInquiryOutput = await Task.WhenAll(tasks); + var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); + var dateUpdated = DateTime.Now; + if (apiOutput.Code == "S") + { + var UpdateableData = new BomStockInquiryUpdateable + { + Objectid = objectid, + Code = apiOutput.Code, + RecordGuid = childGuid, + Bdmng = apiOutput.Bdmng, + Labst = apiOutput.Labst, + ZwqslPo = apiOutput.ZwqslPo, + ZwqslPr = apiOutput.ZwqslPr, + DateUpdated = dateUpdated + }; + //UpdateableList.Add(UpdateableData); + _dataValidationService.ModifyInventoryInformation(UpdateableData.RecordGuid, UpdateableData.Code, UpdateableData.Objectid, UpdateableData.Bdmng, UpdateableData.Labst, UpdateableData.ZwqslPo, UpdateableData.ZwqslPr, UpdateableData.DateUpdated); + } + else + { + var UpdateableData = new BomStockInquiryUpdateable + { + Code = "", + RecordGuid = "", + Bdmng = "", + Labst = "", + ZwqslPo = "", + ZwqslPr = "", + DateUpdated = dateUpdated + }; + //UpdateableList.Add(UpdateableData); + _dataValidationService.ModifyInventoryInformation(UpdateableData.RecordGuid, UpdateableData.Code, UpdateableData.Objectid, UpdateableData.Bdmng, UpdateableData.Labst, UpdateableData.ZwqslPo, UpdateableData.ZwqslPr, UpdateableData.DateUpdated); + } + if (apiOutput.Code == "S") + { + return new BomStockInquiryOutput + { + materialCode = code, + materialName = name, + Labst = apiOutput.Labst, + Bdmng = apiOutput.Bdmng, + ZwqslPr = apiOutput.ZwqslPr, + ZwqslPo = apiOutput.ZwqslPo, + DateUpdated = dateUpdated, + State = "查询成功" + }; + } + else + { + return new BomStockInquiryOutput + { + materialCode = code, + materialName = name, + State = "SAP未查询到库存信息" + }; + } + }).ToList(); + var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成 - foreach (var item in UpdateableList) - { - await _dataValidationService.ModifyInventoryInformation(item.RecordGuid, item.Code, item.Objectid, item.Bdmng, item.Labst, item.ZwqslPo, item.ZwqslPr, item.DateUpdated); + // 合并当前批次的结果 + UpdateableOutput.AddRange(bomStockInquiryOutput); } - return bomStockInquiryOutput.ToList(); + // var tasks = bomList.Select(async item => + //{ + // var code = item.PartNumber; + // var name = item.Description; + // var objectid = item.ChildObjID; + // var childGuid = item.ChildGuid; + + // var SapEcnQueryData = new SapEcnQueryInput + // { + // Reqkeyid = "", + // Businessid = "", + // Messageid = "", + // Sndprn = "PLM", + // Rcvprn = "SAP", + // Requser = Requser, + // Note1 = "", + // Note2 = "", + // Note3 = "", + // Sign = "I", + // Option = "EQ", + // Low = code, + // High = "" + // }; + + // var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); + // var dateUpdated = DateTime.Now; + // if (apiOutput.Code == "S") + // { + // var UpdateableData = new BomStockInquiryUpdateable + // { + // Objectid = objectid, + // Code = apiOutput.Code, + // RecordGuid = childGuid, + // Bdmng = apiOutput.Bdmng, + // Labst = apiOutput.Labst, + // ZwqslPo = apiOutput.ZwqslPo, + // ZwqslPr = apiOutput.ZwqslPr, + // DateUpdated = dateUpdated + // }; + // UpdateableList.Add(UpdateableData); + // } + // else + // { + // var UpdateableData = new BomStockInquiryUpdateable + // { + // Code = "", + // RecordGuid = "", + // Bdmng = "", + // Labst = "", + // ZwqslPo = "", + // ZwqslPr = "", + // DateUpdated = dateUpdated + // }; + // UpdateableList.Add(UpdateableData); + // } + // if (apiOutput.Code == "S") + // { + // return new BomStockInquiryOutput + // { + // materialCode = code, + // materialName = name, + // Labst = apiOutput.Labst, + // Bdmng = apiOutput.Bdmng, + // ZwqslPr = apiOutput.ZwqslPr, + // ZwqslPo = apiOutput.ZwqslPo, + // DateUpdated = dateUpdated, + // State = "查询成功" + // }; + // } + // else + // { + // return new BomStockInquiryOutput + // { + // materialCode = code, + // materialName = name, + // State = "SAP未查询到库存信息" + // }; + // } + //}); + + //var bomStockInquiryOutput = await Task.WhenAll(tasks); + + //foreach (var item in UpdateableList) + //{ + // await _dataValidationService.ModifyInventoryInformation(item.RecordGuid, item.Code, item.Objectid, item.Bdmng, item.Labst, item.ZwqslPo, item.ZwqslPr, item.DateUpdated); + //} + + return UpdateableOutput.ToList(); } /// @@ -270,97 +375,194 @@ public class StockInquiryService : IDynamicApiController, ITransient [AllowAnonymous] public async Task> RawMaterialStockInquiry() { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); + var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); - var data = await _obj112Rep.AsQueryable() + var data = await db.CopyNew().Queryable() .Where(x => x.deleted == false) .Select() .ToListAsync(); var UpdateableList = new List(); + var UpdateableOutput = new List(); - var tasks = data.Select(async item => + int batchSize = 20; // 每批的大小 + int totalCount = data.ToList().Count; + + for (int i = 0; i < totalCount; i += batchSize) { - var code = item._System_objNBS; - var name = item._System_objDescription; - var objectid = 112; - var id = item.idRecord; - var SapEcnQueryData = new SapEcnQueryInput - { - Reqkeyid = "", - Businessid = "", - Messageid = "", - Sndprn = "PLM", - Rcvprn = "SAP", - Requser = Requser, - Note1 = "", - Note2 = "", - Note3 = "", - Sign = "I", - Option = "EQ", - Low = code, - High = "" - }; + var batch = data.Skip(i).Take(batchSize); // 分批获取数据 - var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); - var dateUpdated = DateTime.Now; - if (apiOutput.Code == "S") + // 处理当前批次的数据 + var tasks = batch.Select(async item => { - var UpdateableData = new BomStockInquiryUpdateable + var code = item._System_objNBS; + var name = item._System_objDescription; + var objectid = 112; + var id = item.idRecord; + var SapEcnQueryData = new SapEcnQueryInput { - Objectid = objectid, - Code = apiOutput.Code, - RecordGuid = id.ToString(),//在这个方法中使用RecordGuid字段作为id字段 - Bdmng = apiOutput.Bdmng, - Labst = apiOutput.Labst, - ZwqslPo = apiOutput.ZwqslPo, - ZwqslPr = apiOutput.ZwqslPr, - DateUpdated = dateUpdated + Reqkeyid = "", + Businessid = "", + Messageid = "", + Sndprn = "PLM", + Rcvprn = "SAP", + Requser = Requser, + Note1 = "", + Note2 = "", + Note3 = "", + Sign = "I", + Option = "EQ", + Low = code, + High = "" }; - UpdateableList.Add(UpdateableData); - } - else - { - var UpdateableData = new BomStockInquiryUpdateable - { - Code = "", - RecordGuid = "", - Bdmng = "", - Labst = "", - ZwqslPo = "", - ZwqslPr = "", - DateUpdated = dateUpdated - }; - UpdateableList.Add(UpdateableData); - } - if (apiOutput.Code == "S") - { - return new BomStockInquiryOutput - { - materialCode = code, - materialName = name, - Labst = apiOutput.Labst, - Bdmng = apiOutput.Bdmng, - ZwqslPr = apiOutput.ZwqslPr, - ZwqslPo = apiOutput.ZwqslPo, - DateUpdated = dateUpdated, - State = "查询成功" - }; - } - else - { - return new BomStockInquiryOutput - { - materialCode = code, - materialName = name, - State = "SAP未查询库存信息" - }; - } - }); - var bomStockInquiryOutput = await Task.WhenAll(tasks); + var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); + var dateUpdated = DateTime.Now; + if (apiOutput.Code == "S") + { + var UpdateableData = new BomStockInquiryUpdateable + { + Objectid = objectid, + Code = apiOutput.Code, + RecordGuid = id.ToString(),//在这个方法中使用RecordGuid字段作为id字段 + Bdmng = apiOutput.Bdmng, + Labst = apiOutput.Labst, + ZwqslPo = apiOutput.ZwqslPo, + ZwqslPr = apiOutput.ZwqslPr, + DateUpdated = dateUpdated + }; + UpdateableList.Add(UpdateableData); + } + else + { + var UpdateableData = new BomStockInquiryUpdateable + { + Code = "", + RecordGuid = "", + Bdmng = "", + Labst = "", + ZwqslPo = "", + ZwqslPr = "", + DateUpdated = dateUpdated + }; + UpdateableList.Add(UpdateableData); + } + if (apiOutput.Code == "S") + { + return new BomStockInquiryOutput + { + materialCode = code, + materialName = name, + Labst = apiOutput.Labst, + Bdmng = apiOutput.Bdmng, + ZwqslPr = apiOutput.ZwqslPr, + ZwqslPo = apiOutput.ZwqslPo, + DateUpdated = dateUpdated, + State = "查询成功" + }; + } + else + { + return new BomStockInquiryOutput + { + materialCode = code, + materialName = name, + State = "SAP未查询到库存信息" + }; + } + }).ToList(); + + var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成 + + // 合并当前批次的结果 + UpdateableOutput.AddRange(bomStockInquiryOutput); + } + + // var tasks = data.Select(async item => + //{ + // var code = item._System_objNBS; + // var name = item._System_objDescription; + // var objectid = 112; + // var id = item.idRecord; + // var SapEcnQueryData = new SapEcnQueryInput + // { + // Reqkeyid = "", + // Businessid = "", + // Messageid = "", + // Sndprn = "PLM", + // Rcvprn = "SAP", + // Requser = Requser, + // Note1 = "", + // Note2 = "", + // Note3 = "", + // Sign = "I", + // Option = "EQ", + // Low = code, + // High = "" + // }; + + // var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData); + // var dateUpdated = DateTime.Now; + // if (apiOutput.Code == "S") + // { + // var UpdateableData = new BomStockInquiryUpdateable + // { + // Objectid = objectid, + // Code = apiOutput.Code, + // RecordGuid = id.ToString(),//在这个方法中使用RecordGuid字段作为id字段 + // Bdmng = apiOutput.Bdmng, + // Labst = apiOutput.Labst, + // ZwqslPo = apiOutput.ZwqslPo, + // ZwqslPr = apiOutput.ZwqslPr, + // DateUpdated = dateUpdated + // }; + // UpdateableList.Add(UpdateableData); + // } + // else + // { + // var UpdateableData = new BomStockInquiryUpdateable + // { + // Code = "", + // RecordGuid = "", + // Bdmng = "", + // Labst = "", + // ZwqslPo = "", + // ZwqslPr = "", + // DateUpdated = dateUpdated + // }; + // UpdateableList.Add(UpdateableData); + // } + // if (apiOutput.Code == "S") + // { + // return new BomStockInquiryOutput + // { + // materialCode = code, + // materialName = name, + // Labst = apiOutput.Labst, + // Bdmng = apiOutput.Bdmng, + // ZwqslPr = apiOutput.ZwqslPr, + // ZwqslPo = apiOutput.ZwqslPo, + // DateUpdated = dateUpdated, + // State = "查询成功" + // }; + // } + // else + // { + // return new BomStockInquiryOutput + // { + // materialCode = code, + // materialName = name, + // State = "SAP未查询到库存信息" + // }; + // } + //}); + + //var bomStockInquiryOutput = await Task.WhenAll(tasks); foreach (var item in UpdateableList) { var dateUpdated = DateTime.Now; - await _obj112Rep.AsUpdateable().SetColumns(it => new Obj112 + db.CopyNew().Updateable().SetColumns(it => new Obj112 { fld006750 = item.Bdmng, fld006751 = item.Labst, @@ -368,11 +570,349 @@ public class StockInquiryService : IDynamicApiController, ITransient fld006753 = item.ZwqslPr, fld006755 = dateUpdated }) - .Where(it => it.idRecord == Convert.ToInt32(item.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段 - .ExecuteCommandAsync(); + .Where(it => it.idRecord == Convert.ToInt32(item.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段 + .ExecuteCommand(); } - return bomStockInquiryOutput.ToList(); + return UpdateableOutput.ToList(); + } + + /// + /// 产品管理库-价格查询 + /// + /// + /// + [ApiDescriptionSettings(Name = "GetPriceInquiry", Description = "产品管理库-价格查询", Order = 1000), HttpGet] + [DisplayName("产品管理库-价格查询")] + [AllowAnonymous] + public async Task GetPriceInquiry(string materialCode) + { + var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + + var SapPriceQueryData = new SapPriceQueryInput + { + Reqkeyid = "", + Businessid = "", + Messageid = "", + Sndprn = "PLM", + Rcvprn = "SAP", + Requser = Requser, + Note1 = "", + Note2 = "", + Note3 = "", + Sign = "I", + Option = "EQ", + Low = materialCode, + High = "" + }; + + var apiOutput = await _sapService.SapPriceQueryApi(SapPriceQueryData); + + if (apiOutput.Code == "S") + { + string priceValue = apiOutput.Verpr; + var Verpr2 = await _dataValidationService.ProcessNumber(0); + var Verpr3 = await _dataValidationService.ProcessNumber(apiOutput.Verpr.ToDecimal()); + var priceValue3 = Verpr3 == 0 ? "9999.99" : Verpr3.ToString(); + if (apiOutput.Verpr != "0.0") + { + var Verpr = await _dataValidationService.ProcessNumber(apiOutput.Verpr.ToDecimal()); + priceValue = Verpr == 0 ? "9999.99" : Verpr.ToString(); + } + else { + priceValue = "9999.99"; + } + + var data = await _obj137Rep.AsQueryable().Where(x => x._System_objNBS == materialCode && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); + if (data != null) + { + apiOutput.DateUpdated = DateTime.Now; + await _obj137Rep.AsUpdateable().SetColumns(it => new Obj137 + { + fld005625 = priceValue, + fld005626 = apiOutput.DateUpdated + }) + .Where(it => it.idRecord == data.idRecord) + .ExecuteCommandAsync(); + apiOutput.Verpr = priceValue; + return apiOutput; + } + + var RawMaterialData = await _obj112Rep.AsQueryable().Where(x => x._System_objNBS == materialCode && x.deleted == false).FirstAsync(); + + if (RawMaterialData != null) + { + apiOutput.DateUpdated = DateTime.Now; + await _obj112Rep.AsUpdateable().SetColumns(it => new Obj112 + { + fld006754 = priceValue, + fld006756 = apiOutput.DateUpdated + }) + .Where(it => it.idRecord == RawMaterialData.idRecord) + .ExecuteCommandAsync(); + apiOutput.Verpr = priceValue; + return apiOutput; + } + } + + return apiOutput; + } + + /// + /// 产品管理库-Bom成本查询 + /// + /// + /// + [ApiDescriptionSettings(Name = "GetBomPriceInquiry", Description = "产品管理库-Bom成本查询", Order = 1000), HttpGet] + [DisplayName("产品管理库-Bom成本查询")] + [AllowAnonymous] + public async Task> GetBomPriceInquiry(string materialCode) + { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); + + var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + + var materialData = await db.CopyNew().Queryable() + .Where(x => x._System_objNBS == materialCode && x.deleted == false) + .OrderByDescending(x => x.idRecord) + .FirstAsync(); + + var bomList = await db.CopyNew().Queryable() + .Where(x => x.ParentGuid == materialData.RecordGuid && x.isDeleted == false + && x.ParentVersion == materialData.VersionIndex + && (x.fld005577 != "不包含" || x.fld005577 == null) + && x.isSuppressed == false && x.inContext == false) + .ToListAsync(); + + var UpdateableList = new List(); + var UpdateableOutput = new List(); + + int batchSize = 20; // 每批的大小 + int totalCount = bomList.ToList().Count; + + for (int i = 0; i < totalCount; i += batchSize) + { + var batch = bomList.Skip(i).Take(batchSize); // 分批获取数据 + + // 处理当前批次的数据 + var tasks = batch.Select(async item => + { + var code = item.PartNumber; + var name = item.Description; + var objectid = item.ChildObjID; + var childGuid = item.ChildGuid; + + var SapPriceQueryData = new SapPriceQueryInput + { + Reqkeyid = "", + Businessid = "", + Messageid = "", + Sndprn = "PLM", + Rcvprn = "SAP", + Requser = Requser, + Note1 = "", + Note2 = "", + Note3 = "", + Sign = "I", + Option = "EQ", + Low = code, + High = "" + }; + + var apiOutput = await _sapService.SapPriceQueryApi(SapPriceQueryData); + var dateUpdated = DateTime.Now; + if (apiOutput.Code == "S") + { + string priceValue = apiOutput.Verpr; + + if (apiOutput.Verpr != "0.0") + { + var Verpr = await _dataValidationService.ProcessNumber(apiOutput.Verpr.ToDecimal()); + priceValue = Verpr == 0 ? "9999.99" : Verpr.ToString(); + } + else + { + priceValue = "9999.99"; + } + + var UpdateableData = new BomPriceInquiryUpdateable + { + Objectid = objectid, + Code = apiOutput.Code, + RecordGuid = childGuid, + Verpr = priceValue, + DateUpdated = dateUpdated + }; + //UpdateableList.Add(UpdateableData); + _dataValidationService.ModifyPriceInformation(UpdateableData.RecordGuid, UpdateableData.Code, UpdateableData.Objectid, UpdateableData.Verpr, UpdateableData.DateUpdated); + + return new BomPriceInquiryOutput + { + materialCode = code, + materialName = name, + Verpr = priceValue, + DateUpdated = dateUpdated, + State = "查询成功" + }; + } + else + { + var UpdateableData = new BomPriceInquiryUpdateable + { + Code = "", + RecordGuid = "", + Verpr = "", + DateUpdated = dateUpdated + }; + //UpdateableList.Add(UpdateableData); + _dataValidationService.ModifyPriceInformation(UpdateableData.RecordGuid, UpdateableData.Code, UpdateableData.Objectid, UpdateableData.Verpr, UpdateableData.DateUpdated); + + return new BomPriceInquiryOutput + { + materialCode = code, + materialName = name, + State = "查询失败" + }; + } + //if (apiOutput.Code == "S") + //{ + // return new BomPriceInquiryOutput + // { + // materialCode = code, + // materialName = name, + // Verpr = apiOutput.Verpr, + // DateUpdated = dateUpdated, + // State = "查询成功" + // }; + //} + //else + //{ + // return new BomPriceInquiryOutput + // { + // materialCode = code, + // materialName = name, + // State = "SAP未查询到价格信息" + // }; + //} + }).ToList(); + var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成 + + // 合并当前批次的结果 + UpdateableOutput.AddRange(bomStockInquiryOutput); + } + return UpdateableOutput.ToList(); + } + + /// + /// 原材料成本查询 + /// + /// + [ApiDescriptionSettings(Name = "RawMaterialPriceInquiry", Description = "原材料成本查询", Order = 1000), HttpGet] + [DisplayName("原材料成本查询")] + [AllowAnonymous] + public async Task> RawMaterialPriceInquiry() + { + using var serviceScope = _scopeFactory.CreateScope(); + var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); + + var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); + var data = await db.CopyNew().Queryable() + .Where(x => x.deleted == false) + .Select() + .ToListAsync(); + var UpdateableList = new List(); + var UpdateableOutput = new List(); + + int batchSize = 20; // 每批的大小 + int totalCount = data.ToList().Count; + + for (int i = 0; i < totalCount; i += batchSize) + { + var batch = data.Skip(i).Take(batchSize); // 分批获取数据 + + // 处理当前批次的数据 + var tasks = batch.Select(async item => + { + var code = item._System_objNBS; + var name = item._System_objDescription; + var objectid = 112; + var id = item.idRecord; + var SapPriceQueryData = new SapPriceQueryInput + { + Reqkeyid = "", + Businessid = "", + Messageid = "", + Sndprn = "PLM", + Rcvprn = "SAP", + Requser = Requser, + Note1 = "", + Note2 = "", + Note3 = "", + Sign = "I", + Option = "EQ", + Low = code, + High = "" + }; + + var apiOutput = await _sapService.SapPriceQueryApi(SapPriceQueryData); + var dateUpdated = DateTime.Now; + if (apiOutput.Code == "S") + { + string priceValue = apiOutput.Verpr; + + if (apiOutput.Verpr != "0.0") + { + var Verpr = await _dataValidationService.ProcessNumber(apiOutput.Verpr.ToDecimal()); + priceValue = Verpr == 0 ? "9999.99" : Verpr.ToString(); + } + else + { + priceValue = "9999.99"; + } + + + db.CopyNew().Updateable().SetColumns(it => new Obj112 + { + fld006754 = priceValue, + fld006755 = dateUpdated + }) + .Where(it => it.idRecord == id)//在这个方法中使用RecordGuid字段作为id字段 + .ExecuteCommand(); + + return new BomPriceInquiryOutput + { + materialCode = code, + materialName = name, + Verpr = priceValue, + DateUpdated = dateUpdated, + State = "查询成功" + }; + } + else + { + db.CopyNew().Updateable().SetColumns(it => new Obj112 + { + fld006754 = "", + fld006755 = dateUpdated + }).Where(it => it.idRecord == id) + .ExecuteCommand(); + + return new BomPriceInquiryOutput + { + materialCode = code, + materialName = name, + State = "查询失败" + }; + } + }).ToList(); + + var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成 + + // 合并当前批次的结果 + UpdateableOutput.AddRange(bomStockInquiryOutput); + } + return UpdateableOutput.ToList(); } }