// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! using Admin.NET.Core; using Admin.NET.Core.Service; using DocumentFormat.OpenXml.Wordprocessing; using Furion.DependencyInjection; using Furion.DynamicApiController; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Qiniu.CDN; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Vistar.Application.Common; using Vistar.Application.Const; using Vistar.Application.Entity; using Vistar.Application.SapService.Dto; using Vistar.Application.Service.ProductDesignLibrary.Dto; using Vistar.Application.SolidWorksManageService.Dto; using Vistar.Application.Util; using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinAccountGetAccountBasicInfoResponse.Types; namespace Vistar.Application.Service.ProductDesignLibrary; /// /// 产品设计库服务 /// [ApiDescriptionSettings(ApplicationConst.GroupName, Name = "ProductDesignLibrary", Order = 100)] public class ProductDesignLibraryService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _obj137Rep; private readonly SysConfigService _sysConfigService; public SapService.SapService _sapService; private readonly SqlSugarRepository _configurationsRep; private readonly SqlSugarRepository _configurationDataRep; public SqlSugarRepository _obj118Rep; public Common.DataValidationService _dataValidationService; public SqlSugarRepository _obj122Rep; public SqlSugarRepository _obj112Rep; public SqlSugarRepository _obj121Rep; public SolidWorksManageService.SolidWorksManageService _solidWorksManageService; private readonly IServiceScopeFactory _scopeFactory; public ProductDesignLibraryService( SqlSugarRepository obj137Rep, SysConfigService sysConfigService, SapService.SapService sapService, SqlSugarRepository configurationsRep, SqlSugarRepository configurationDataRep, SqlSugarRepository obj118Rep, DataValidationService dataValidationService, SqlSugarRepository obj122Rep, SqlSugarRepository obj112Rep, SqlSugarRepository obj121Rep, SolidWorksManageService.SolidWorksManageService solidWorksManageService, IServiceScopeFactory scopeFactory ) { _obj137Rep = obj137Rep; _sysConfigService = sysConfigService; _sapService = sapService; _configurationsRep = configurationsRep; _configurationDataRep = configurationDataRep; _obj118Rep = obj118Rep; _dataValidationService = dataValidationService; _obj122Rep = obj122Rep; _obj112Rep = obj112Rep; _obj121Rep = obj121Rep; _solidWorksManageService = solidWorksManageService; _scopeFactory = scopeFactory; } /// /// 分页查询产品设计库 /// /// /// [ApiDescriptionSettings(Name = "page", Description = "分页查询", Order = 1000), HttpPost] [DisplayName("分页查询产品设计库")] public async Task> Page(PageProductDesignLibraryInput input) { var query = await _obj137Rep.AsQueryable() .Where(x => x.CheckedStatus == 0 && x.deleted == false && x.IsLatestVersion == true && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW")) .WhereIF(!string.IsNullOrWhiteSpace(input._System_objNBS), u => u._System_objNBS.Contains(input._System_objNBS.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input._SWPDM_1188_SAP___), u => u._SWPDM_1188_SAP___.Contains(input._SWPDM_1188_SAP___.Trim())) .Select() .OrderByDescending(it => it.idRecord) .ToPagedListAsync(input.Page, input.PageSize); return query; } /// /// 产品设计库-同步到 SAP /// /// [ApiDescriptionSettings(Name = "syncToSAP"), HttpPost] [DisplayName("产品设计库-同步到 SAP")] public async Task SyncToSAP(ProductDesignLibraryInput input) { const string lengthError = "物料描述长度大于40,请检查!"; // 验证物料描述长度 if (input._SWPDM_1188_SAP___.Length > 40) { await _obj137Rep.AsUpdateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005530 = "N", fld005531 = lengthError, fld005529 = "失败" }) .Where(it => it.idRecord == input.idRecord) .ExecuteCommandAsync().ConfigureAwait(false); return new SapOutput { code = "失败", msg = lengthError, result = lengthError }; } //获取时间戳精确到毫秒,sap要求每次调用生成不重复guid string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); Random random = new Random(); // 生成1000到9999之间的随机数 string randomNumber = random.Next(1000, 10000).ToString(); millisecondTimestamp = millisecondTimestamp + randomNumber; var sapMaterialInput = new SapMaterialInput() { Reqkeyid = "", Businessid = "", Messageid = "", Sndprn = "PLM", Rcvprn = "SAP", Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName), Note1 = "", Note2 = "", Note3 = "", Zwbid = millisecondTimestamp, Matnr = input._System_objNBS, Mbrsh = input._SWPDM_1185_SAP___, Mtart = input._SWPDM_1186_SAP_____, Maktx = input._SWPDM_1188_SAP___, Meins = input._SWPDM_126_SW___, Matkl = input._SWPDM_1203_SAP____, Bismt = input._SWPDM_64_SW___, Groes = input._SWPDM_125_SW___, Normt = input._SWPDM_65_SW___, Ferth = input._SWPDM_62_SW___, Zeinr = input._SWPDM_61_SW_____, Mstae = input._SWPDM_1209_SAP_____, Raube = "", Mhdrz = "", Mhdhb = "", Werks = input._SWPDM_1187_SAP___, Beskz = input._SWPDM_1196_SAP_____, Sobsl = input._SWPDM_1198_SAP_____, Schgt = input._SWPDM_1197_SAP_____, Rgekz = input._SWPDM_1199_SAP___, Zbom = input._SWPDM_1208_SAP___BOM___ }; var apiOutput = await _sapService.SapMaterialApi(sapMaterialInput); string codeVal = apiOutput.code == "S" ? "成功" : "失败"; var msg = apiOutput.msg; if (apiOutput.msg == "") { msg = "同步成功"; } await _obj137Rep.AsUpdateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005529 = codeVal, fld005530 = "N", fld005531 = msg }) .Where(it => it.idRecord == input.idRecord) .ExecuteCommandAsync(); var output = new SapOutput() { parameter = apiOutput.parameter, code = codeVal, msg = msg, result = apiOutput.result }; return output; } /// /// 获取BOM /// /// /// [ApiDescriptionSettings(Name = "GetBOM", Description = "获取BOM", Order = 1000), HttpPost] [DisplayName("获取BOM")] public async Task> GetBOM(BomPageProductDesignLibraryInput input) { var configid = await _configurationsRep.AsQueryable().Where(x => x.ObjectId == 137 && x.ConfigSystemName == "SolidWorks").FirstAsync(); var parent = await _obj137Rep.AsQueryable() .Where(x => x.RecordGuid == input.ParentGuid && x.deleted == false && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW")) .MaxAsync(x => x.VersionIndex); var BomData = await _configurationDataRep.AsQueryable() .Where(x => x.ConfigId == configid.ConfigID && x.ParentGuid == input.ParentGuid && x.isDeleted == false && x.ParentVersion == parent && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false && (!(x.ParentSwFileType == 1 && x.isAddedFromSW == false))) .Select().ToListAsync(); foreach (var item in BomData) { if (item.ChildObjID == 137) { if (item.PartNumber != null && Regex.IsMatch(item.PartNumber, "默认")) { var data137 = await _obj137Rep.AsQueryable().Where(x => x._System_objNBS == item.ChildGuid && x.deleted == false && x._system_objConfigurationName == "默认").OrderByDescending(x => x.idRecord).FirstAsync(); if (data137 != null) { item.PartNumber = data137._System_objNBS; item.Description = data137._System_ObjDescription; } } } } var data = BomData.ToPagedList(input.Page, input.PageSize); //.WhereIF(!string.IsNullOrWhiteSpace(input.Description), u => u.Description.Contains(input.Description.Trim())) //.WhereIF(!string.IsNullOrWhiteSpace(input.PartNumber), u => u.PartNumber.Contains(input.PartNumber.Trim())) //.Select().ToPagedListAsync(input.Page, input.PageSize); return data; } /// /// 产品设计库-Bom同步到SAP /// /// /// [ApiDescriptionSettings(Name = "SyncToSAPBom"), HttpPost] [DisplayName("产品设计库-Bom同步到SAP")] public async Task SyncToSAPBom(List input) { var ParentData = await _obj137Rep.AsQueryable().Where(x => x.RecordGuid == input[0].ParentGuid && x.deleted == false && x.VersionIndex == input[0].ParentVersion && (x._SWPDM_1202_SW_____ != "不展开" || x._SWPDM_1202_SW_____ == null) && x.fld005530 == "N" && x.fld005529 == "成功" && (x.fld005534 == "A" || x.fld005534 == "M") && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW")).FirstAsync(); //var EcnData = await _obj118Rep.AsQueryable().Where(x => x.RecordGuid == ParentData.fld005679_Rec && x.deleted == false).OrderByDescending(x => x.VersionIndex).FirstAsync(); //var ecn = ""; //if (EcnData != null) //{ // ecn = EcnData._System_objNBS; //} //var itemDataList = new List(); var BomDataList = new List(); foreach (var item in input) { var bomData = new BomData { PartNumber = item.PartNumber, ParentGuid = item.ParentGuid, ChildGuid = item.ChildGuid, ChildObjID = item.ChildObjID, ConfigId = item.ConfigId, childVersion = item.childVersion, ParentVersion = ParentData.VersionIndex, QtyManual = item.QtyManual, SORTF = item.fld005541, ITISOB = item.fld005542, ALPGR = item.fld005543, ALPRF = item.fld005544, ZDELETE = item.fld004940, POSTP = item.fld005546, ZYFMK = item.fld005547 }; BomDataList.Add(bomData); } var syncBomData = new SyncBomData { Id = ParentData.idRecord, MaterialCode = ParentData._System_objNBS, RecordGuid = ParentData.RecordGuid, SyncVersion = ParentData.fld005686, Bmeng = ParentData.fld005678, Werks = ParentData._SWPDM_1187_SAP___, Datuv = ParentData.fld005680, VersionIndex = ParentData.VersionIndex, //Ecn = group.FirstOrDefault()?.ecn, ecnGuid = ParentData.fld005679_Rec, Bom = BomDataList }; var output = await SyncBomToSap(syncBomData); return output; } /// /// 产品设计库-批量同步到SAP /// /// /// [ApiDescriptionSettings(Name = "BatchSyncToSAP"), HttpPost] [DisplayName("产品设计库-批量同步到SAP")] public async Task> BatchSyncToSAP(List input) { using var serviceScope = _scopeFactory.CreateScope(); var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); const string lengthError = "物料描述长度大于40,请检查!"; const string synchronized = "该物料编码已同步或已停用!"; var SapOutputList = new List(); var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); var tasks = input.Select(async item => { if (item._SWPDM_1188_SAP___.Length > 40) { db.CopyNew().Updateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005530 = "N", fld005531 = lengthError, fld005529 = "失败" }) .Where(it => it.idRecord == item.idRecord) .ExecuteCommand(); return new SapOutput { materialCode = item._System_objNBS, code = "失败", msg = lengthError, result = lengthError }; } if (item.fld005530 == "N" || item.fld005530 == "D" || item.fld005530 == null) { return new SapOutput { materialCode = item._System_objNBS, code = "失败", msg = synchronized, result = synchronized }; } //获取时间戳精确到毫秒,sap要求每次调用生成不重复guid string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); Random random = new Random(); // 生成1000到9999之间的随机数 string randomNumber = random.Next(1000, 10000).ToString(); millisecondTimestamp = millisecondTimestamp + randomNumber; var sapMaterialInput = new SapMaterialInput() { Reqkeyid = "", Businessid = "", Messageid = "", Sndprn = "PLM", Rcvprn = "SAP", Requser = Requser, Note1 = "", Note2 = "", Note3 = "", Zwbid = millisecondTimestamp, Matnr = item._System_objNBS, Mbrsh = item._SWPDM_1185_SAP___, Mtart = item._SWPDM_1186_SAP_____, Maktx = item._SWPDM_1188_SAP___, Meins = item._SWPDM_126_SW___, Matkl = item._SWPDM_1203_SAP____, Bismt = item._SWPDM_64_SW___, Groes = item._SWPDM_125_SW___, Normt = item._SWPDM_65_SW___, Ferth = item._SWPDM_62_SW___, Zeinr = item._SWPDM_61_SW_____, Mstae = item._SWPDM_1209_SAP_____, Raube = "", Mhdrz = "", Mhdhb = "", Werks = item._SWPDM_1187_SAP___, Beskz = item._SWPDM_1196_SAP_____, Sobsl = item._SWPDM_1198_SAP_____, Schgt = item._SWPDM_1197_SAP_____, Rgekz = item._SWPDM_1199_SAP___, Zbom = item._SWPDM_1208_SAP___BOM___ }; var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput); string codeVal = sapOutput.code == "S" ? "成功" : "失败"; string msg = sapOutput.msg; if (sapOutput.msg == "") { msg = "同步成功"; } db.CopyNew().Updateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005529 = codeVal, fld005530 = "N", fld005531 = msg }) .Where(it => it.idRecord == item.idRecord) .ExecuteCommand(); return new SapOutput() { parameter = sapOutput.parameter, materialCode = item._System_objNBS, code = codeVal, msg = msg, result = sapOutput.result }; }); var materialOutput = await Task.WhenAll(tasks); return materialOutput.ToList(); } /// /// 产品设计库-定时同步到SAP /// /// [ApiDescriptionSettings(Name = "TimingSyncToSAP"), HttpGet] [DisplayName("产品设计库-定时同步到SAP")] [AllowAnonymous] public async Task> TimingSyncToSAP() { using var serviceScope = _scopeFactory.CreateScope(); var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); var input = db.CopyNew().Queryable() .Where(x => x.CheckedStatus == 0 && x.deleted == false && x.IsLatestVersion == true && (x.fld005530 == "A" || x.fld005530 == "M") && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW")) .ToList(); const string lengthError = "物料描述长度大于40,请检查!"; var SapOutputList = new List(); var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); var tasks = input.Select(async item => { if (item._SWPDM_1188_SAP___.Length > 40) { db.CopyNew().Updateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005530 = "N", fld005531 = lengthError, fld005529 = "失败" }) .Where(it => it.idRecord == item.idRecord) .ExecuteCommand(); return new SapOutput { materialCode = item._System_objNBS, code = "失败", msg = lengthError, result = lengthError }; } //获取时间戳精确到毫秒,sap要求每次调用生成不重复guid string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); Random random = new Random(); // 生成1000到9999之间的随机数 string randomNumber = random.Next(1000, 10000).ToString(); millisecondTimestamp = millisecondTimestamp + randomNumber; var sapMaterialInput = new SapMaterialInput() { Reqkeyid = "", Businessid = "", Messageid = "", Sndprn = "PLM", Rcvprn = "SAP", Requser = Requser, Note1 = "", Note2 = "", Note3 = "", Zwbid = millisecondTimestamp, Matnr = item._System_objNBS, Mbrsh = item._SWPDM_1185_SAP___, Mtart = item._SWPDM_1186_SAP_____, Maktx = item._SWPDM_1188_SAP___, Meins = item._SWPDM_126_SW___, Matkl = item._SWPDM_1203_SAP____, Bismt = item._SWPDM_64_SW___, Groes = item._SWPDM_125_SW___, Normt = item._SWPDM_65_SW___, Ferth = item._SWPDM_62_SW___, Zeinr = item._SWPDM_61_SW_____, Mstae = item._SWPDM_1209_SAP_____, Raube = "", Mhdrz = "", Mhdhb = "", Werks = item._SWPDM_1187_SAP___, Beskz = item._SWPDM_1196_SAP_____, Sobsl = item._SWPDM_1198_SAP_____, Schgt = item._SWPDM_1197_SAP_____, Rgekz = item._SWPDM_1199_SAP___, Zbom = item._SWPDM_1208_SAP___BOM___ }; var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput); string codeVal = sapOutput.code == "S" ? "成功" : "失败"; string msg = sapOutput.msg; if (sapOutput.msg == "") { msg = "同步成功"; } db.CopyNew().Updateable() .SetColumns(it => new Obj137 { fld005532 = DateTime.Now, fld005529 = codeVal, fld005530 = "N", fld005531 = msg }) .Where(it => it.idRecord == item.idRecord) .ExecuteCommand(); return new SapOutput() { parameter = sapOutput.parameter, materialCode = item._System_objNBS, code = codeVal, msg = msg, result = sapOutput.result }; }); var materialOutput = await Task.WhenAll(tasks); return materialOutput.ToList(); } /// /// 产品设计库-定时同步 BOM 到 SAP /// /// [ApiDescriptionSettings(Name = "TimingSyncToSAPBom"), HttpGet] [DisplayName("产品设计库-定时同步 Bom 到 SAP")] [AllowAnonymous] public async Task> TimingSyncToSAPBom() { //.Where(x=> SqlFunc.Subqueryable().Where(y => y.RecordGuid == x.RecordGuid).Max(x => x.VersionIndex) == x.VersionIndex) using var serviceScope = _scopeFactory.CreateScope(); var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); var syncData = db.Queryable() .InnerJoin((x, y) => x.RecordGuid == y.ParentGuid && x.VersionIndex == y.ParentVersion) //.InnerJoin((x, y, z) => x.fld005679_Rec == z.RecordGuid) .Where((x, y) => x.CheckedStatus == 0 && x.deleted == false && x.IsLatestVersion == true && (x._SWPDM_1202_SW_____ != "不展开" || x._SWPDM_1202_SW_____ == null) && x.fld005530 == "N" && x.fld005529 == "成功" && (x.fld005534 == "A" || x.fld005534 == "M") && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW") && y.isDeleted == false && y.ConfigId == 8 && (y.fld005577 != "不包含" || y.fld005577 == null) && y.isSuppressed == false && y.inContext == false && (!(y.ParentSwFileType == 1 && y.isAddedFromSW == false))) .Select((x, y) => new { x.idRecord, x.VersionIndex, x._System_objNBS, x.RecordGuid, x.fld005679_Rec, x.fld005686, x.fld005678, x._SWPDM_1187_SAP___, x.fld005680, y.ParentGuid, y.ParentVersion, y.ChildGuid, y.ChildObjID, y.childVersion, y.ConfigId, y.QtyManual, y.PartNumber, y.fld005541, y.fld005542, y.fld005543, y.fld005544, y.fld004940, y.fld005546, y.fld005547, //ecn = z._System_objNBS }) .ToList(); var syncList = syncData.GroupBy(record => record.RecordGuid) .Select(group => new SyncBomData { Id = group.FirstOrDefault()?.idRecord, MaterialCode = group.FirstOrDefault()?._System_objNBS, RecordGuid = group.FirstOrDefault()?.RecordGuid, SyncVersion = group.FirstOrDefault()?.fld005686, Bmeng = group.FirstOrDefault()?.fld005678, Werks = group.FirstOrDefault()?._SWPDM_1187_SAP___, Datuv = group.FirstOrDefault()?.fld005680, VersionIndex = group.FirstOrDefault()?.VersionIndex, //Ecn = group.FirstOrDefault()?.ecn, ecnGuid = group.FirstOrDefault()?.fld005679_Rec, Bom = group.Select(g => new BomData { PartNumber = g.PartNumber, ParentGuid = g.ParentGuid, ChildGuid = g.ChildGuid, ChildObjID = g.ChildObjID, ConfigId = g.ConfigId, childVersion = g.childVersion, ParentVersion = g.VersionIndex, QtyManual = g.QtyManual, SORTF = g.fld005541, ITISOB = g.fld005542, ALPGR = g.fld005543, ALPRF = g.fld005544, ZDELETE = g.fld004940, POSTP = g.fld005546, ZYFMK = g.fld005547 }).ToList() }); List sapOutputs = new List(); var tasks = syncList.Select(async item => { var output = await SyncBomToSap(item); return output; }); var bomStockInquiryOutput = await Task.WhenAll(tasks); return bomStockInquiryOutput.ToList(); } /// /// 同步Bom到Sap /// /// /// public async Task SyncBomToSap(SyncBomData data) { using var serviceScope = _scopeFactory.CreateScope(); //var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74"); var ecn = db.CopyNew().Queryable().Where(x => x.RecordGuid == data.ecnGuid).OrderByDescending(x => x.idRecord).First(); if (ecn != null) { data.Ecn = ecn._System_objNBS; } var itemDataList = new List(); foreach (var item in data.Bom) { string verification = "不存在"; if (item.ChildObjID == 137) { var data137 = await db.CopyNew().Queryable().Where(x => x.deleted == false && x.deleted == false && x.RecordGuid == item.ChildGuid && x._system_objConfigurationName == "默认").OrderByDescending(x => x.idRecord).FirstAsync(); if (data137 == null) { verification = "不存在"; } else { item.PartNumber = data137._System_objNBS; verification = "存在"; } } else { verification = await _dataValidationService.VerificationMaterial(item.ChildObjID, item.PartNumber, item.ChildGuid); } if (verification == "不存在") { continue; //if (item.ChildObjID == 137) //{ // string partNumber = item.PartNumber.Replace("默认 ", "");//过滤BOM表PartNumber字段存在“默认 ” // if (partNumber == item.PartNumber)//判断过滤后,物料编码是否和原料相同,相同的话跳出循环 // { // continue; // } // var data137 = db.CopyNew().Queryable().Where(x => x._System_objNBS == item.ChildGuid && x.deleted == false && x._system_objConfigurationName == "默认").OrderByDescending(x => x.idRecord).First(); // if (data137 != null) // { // item.PartNumber = data137._System_objNBS; // } // else // { // continue; // } //} //else //{ // continue; //} } var itemData = new ItemData { POSNR = (data.Bom.IndexOf(item) + 1).ToString(), IDNRK = item.PartNumber, MENGE = Math.Round((decimal)item.QtyManual, 3).ToString(), SORTF = item.SORTF, ITISOB = item.ITISOB, ALPGR = item.ALPGR, ALPRF = item.ALPRF, ZDELETE = item.ZDELETE, POSTP = item.POSTP, ZYFMK = item.ZYFMK, ChildGuid = item.ChildGuid }; itemDataList.Add(itemData); } if (!string.IsNullOrEmpty(data.SyncVersion)) { //var BomData2 = await _configurationDataRep.AsQueryable().Where(x => x.ConfigId == 8 && x.ParentGuid == data.RecordGuid && x.isDeleted == false && x.ParentVersion == Convert.ToInt32(data.fld005686) && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false).ToListAsync(); var BomData = db.CopyNew().Queryable().Where(x => x.ConfigId == 8 && x.ParentGuid == data.RecordGuid && x.isDeleted == false && x.ParentVersion == Convert.ToInt32(data.SyncVersion) && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false && (!(x.ParentSwFileType == 1 && x.isAddedFromSW == false))).ToList(); foreach (var item in BomData) { var itemList = itemDataList.Where(x => x.ChildGuid == item.ChildGuid).ToList(); if (itemList.Count == 0) { if (item.ChildObjID == 137) { var data137 = db.CopyNew().Queryable().Where(x => x.RecordGuid == item.ChildGuid && x.deleted == false).OrderByDescending(x => x.idRecord).First(); if (data137 != null) { item.PartNumber = data137._System_objNBS; } else { continue; } } else { var Verification = await _dataValidationService.VerificationMaterial(item.ChildObjID, item.PartNumber, item.ChildGuid); if (Verification == "不存在") { continue; } } //int sun = i + 1; var itemData = new ItemData() { POSNR = "",//组件序号 sun.ToString() IDNRK = item.PartNumber,//组件物料号 MENGE = Math.Round((decimal)item.QtyManual, 3).ToString(),//组件数量 SORTF = item.fld005541, ITISOB = item.fld005542, ALPGR = item.fld005543, ALPRF = item.fld005544, ZDELETE = "X", POSTP = item.fld005546, ZYFMK = item.fld005547 }; itemDataList.Add(itemData); } } } string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); Random random = new Random(); // 生成1000到9999之间的随机数 string randomNumber = random.Next(1000, 10000).ToString(); millisecondTimestamp = millisecondTimestamp + randomNumber; var Bmeng = data.Bmeng ?? 1; var sapUserName = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName); var isreq = new IS_REQ { ReqKeyId = "", BusinessId = "", MessageId = "", SndPrn = "PLM", RcvPrn = "SAP", ReqUser = sapUserName, Note1 = "", Note2 = "", Note3 = "", Zwbid = millisecondTimestamp, Matnr = data.MaterialCode, Werks = data.Werks, Bmeng = Bmeng.ToString(), Aennr = data.Ecn, Datuv = data.Datuv.ToString(), ItemList = itemDataList }; var apiOutput = await _sapService.SapBomApi(isreq); string codeVal = apiOutput.code == "S" ? "成功" : "失败"; if (codeVal == "成功") { db.CopyNew().Updateable().Where(it => it.idRecord == data.Id) .SetColumns(it => new Obj137 { fld005539 = DateTime.Now, fld005533 = codeVal, fld005534 = "N", fld005535 = apiOutput.msg, fld005686 = data.VersionIndex.ToString() }).ExecuteCommand(); } else { db.CopyNew().Updateable().Where(it => it.idRecord == data.Id) .SetColumns(it => new Obj137 { fld005539 = DateTime.Now, fld005533 = codeVal, fld005534 = "N", fld005535 = apiOutput.msg }) .ExecuteCommand(); } var msg = apiOutput.msg; if (string.IsNullOrEmpty(msg)) { msg = "同步成功"; } var output = new SapOutput { parameter = apiOutput.parameter, materialCode = data.MaterialCode, code = codeVal, msg = msg, result = apiOutput.result }; return output; } /// /// 产品设计库-获取工艺路线 /// /// /// [ApiDescriptionSettings(Name = "GetProcessRoute", Description = "获取工艺路线", Order = 1000), HttpPost] [DisplayName("获取工艺路线")] public async Task> GetProcessRoute(BomPageProductDesignLibraryInput input) { var parent = await _obj137Rep.AsQueryable() .Where(x => x.RecordGuid == input.ParentGuid && x.CheckedStatus == 0 && x.deleted == false && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW") && (x._SWPDM_1202_SW_____ != "不展开" || x._SWPDM_1202_SW_____ == null) && x.fld005530 == "N" && x.fld005529 == "成功" && (x.fld005537 == "A" || x.fld005537 == "M") ) .MaxAsync(x => x.VersionIndex); var BomData = await _configurationDataRep.AsQueryable() .Where(x => x.ConfigId == 9 && x.ParentGuid == input.ParentGuid && x.isDeleted == false && x.ParentVersion == parent) .WhereIF(!string.IsNullOrWhiteSpace(input.Description), u => u.Description.Contains(input.Description.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.PartNumber), u => u.PartNumber.Contains(input.PartNumber.Trim())) .Select().ToPagedListAsync(input.Page, input.PageSize); return BomData; } /// /// 产品设计库-工艺路线同步到SAP /// /// /// [ApiDescriptionSettings(Name = "SyncToSAPProcessRoute"), HttpPost] [DisplayName("产品设计库-工艺路线同步到SAP")] public async Task SyncToSAPProcessRoute(List input) { var ParentData = await _obj137Rep.AsQueryable() .Where(x => x.RecordGuid == input[0].ParentGuid && x.deleted == false && x.VersionIndex == input[0].ParentVersion && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW") && x.CheckedStatus == 0 && (x._SWPDM_1202_SW_____ != "不展开" || x._SWPDM_1202_SW_____ == null) && x.fld005530 == "N" && x.fld005529 == "成功" && (x.fld005537 == "A" || x.fld005537 == "M") ) .FirstAsync(); var processRouteList = new List(); foreach (var item in input) { var processRoute = new ProcessRouteList { ParentGuid = item.ParentGuid, ChildGuid = item.ChildGuid, ChildObjID = item.ChildObjID, PartNumber = item.PartNumber, VORNR = item.Marker, VGW01 = item.fld005571, VGE01 = item.fld005572, STEUS = item.fld005573, BMSCH = item.fld005575 }; processRouteList.Add(processRoute); } var syncProcessRouteData = new SyncProcessRouteData { idRecord = ParentData.idRecord, _System_objNBS = ParentData._System_objNBS, VersionIndex = ParentData.VersionIndex, Werks = ParentData._SWPDM_1187_SAP___, Datuv = ParentData.fld005680, ProcessRouteList = processRouteList }; var output = await SyncProcessRouteToSap(syncProcessRouteData); return output; } /// /// 产品设计库-定时同步工艺路线到SAP /// /// [ApiDescriptionSettings(Name = "TimingSyncToSAPProcessRoute"), HttpGet] [DisplayName("产品设计库-定时同步工艺路线到SAP")] [AllowAnonymous] public async Task> TimingSyncToSAPProcessRoute() { using var serviceScope = _scopeFactory.CreateScope(); var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); var syncPbom = db.Queryable() .InnerJoin((x, y) => x.RecordGuid == y.ParentGuid && x.VersionIndex == y.ParentVersion) .Where((x, y) => x.CheckedStatus == 0 && x.deleted == false && x.IsLatestVersion == true && x._system_objConfigurationName == "默认" && !x.SWPDMFileName.Contains("SLDDRW") && (x._SWPDM_1202_SW_____ != "不展开" || x._SWPDM_1202_SW_____ == null) && x.fld005530 == "N" && x.fld005529 == "成功" && (x.fld005537 == "A" || x.fld005537 == "M") && y.ConfigId == 9 && y.isDeleted == false) .Select((x, y) => new { x.idRecord, x.VersionIndex, x.RecordGuid, x._System_objNBS, x._SWPDM_1187_SAP___, x.fld005680, y.ParentGuid, y.ChildGuid, y.ChildObjID, y.PartNumber, y.Marker, y.fld005571, y.fld005572, y.fld005573, y.fld005575, }).ToList(); var syncList = syncPbom.GroupBy(record => record.RecordGuid) .Select(group => new SyncProcessRouteData { idRecord = group.FirstOrDefault()?.idRecord, _System_objNBS = group.FirstOrDefault()?._System_objNBS, VersionIndex = group.FirstOrDefault()?.VersionIndex, Datuv = group.FirstOrDefault()?.fld005680, ProcessRouteList = group.Select(g => new ProcessRouteList { ParentGuid = g.ParentGuid, ChildGuid = g.ChildGuid, ChildObjID = g.ChildObjID, PartNumber = g.PartNumber, VORNR = g.Marker, VGW01 = g.fld005571, VGE01 = g.fld005572, STEUS = g.fld005573, BMSCH = g.fld005575 }).ToList() }); var tasks = syncList.Select(async item => { var output = await SyncProcessRouteToSap(item); return output; }); var bomStockInquiryOutput = await Task.WhenAll(tasks); return bomStockInquiryOutput.ToList(); } /// /// 同步工艺路线到Sap /// /// /// public async Task SyncProcessRouteToSap(SyncProcessRouteData data) { using var serviceScope = _scopeFactory.CreateScope(); //var sysEnumService = serviceScope.ServiceProvider.GetRequiredService(); var db = serviceScope.ServiceProvider.GetRequiredService().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew(); var itemDataList = new List(); foreach (var item in data.ProcessRouteList) { var verification = await _dataValidationService.VerificationMaterial(item.ChildObjID, item.PartNumber, item.ChildGuid); if (verification == "不存在") { continue; } //获取工序描述 var procedureDescription = db.CopyNew().Queryable().Where(x => x.RecordGuid == item.ChildGuid).First(); ; //获取工作中心 var workCenter = db.CopyNew().Queryable().Where(x => x.RecordGuid == procedureDescription.fld005059_Rec).First(); var itemData = new SapProcessRouteItemData() { VORNR = item.VORNR, LTXA1 = procedureDescription._System_ObjDescription, ARBPL = workCenter._System_objNBS, VGW01 = item.VGW01, VGE01 = item.VGE01, STEUS = item.STEUS, BMSCH = item.BMSCH }; itemDataList.Add(itemData); } //获取时间戳精确到毫秒,sap要求每次调用生成不重复guid string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); Random random = new Random(); // 生成1000到9999之间的随机数 string randomNumber = random.Next(1000, 10000).ToString(); millisecondTimestamp = millisecondTimestamp + randomNumber; var Datuv = data.Datuv.ToString(); if (!string.IsNullOrEmpty(Datuv)) { Datuv = data.Datuv.ToDateTime().ToString("yyyy-MM-dd"); } var isreq = new SapProcessRouteIS_REQ() { ReqKeyId = "", BusinessId = "", MessageId = "", SndPrn = "PLM", RcvPrn = "SAP", ReqUser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName), Note1 = "", Note2 = "", Note3 = "", Zwbid = millisecondTimestamp, Matnr = data._System_objNBS, Werks = data.Werks, Datuv = Datuv, Plnal = "", ItemList = itemDataList }; var apiOutput = await _sapService.SapProcessRouteApi(isreq); string codeVal = apiOutput.code == "S" ? "成功" : "失败"; db.CopyNew().Updateable() .SetColumns(it => new Obj137 { fld005540 = DateTime.Now, fld005536 = codeVal, fld005537 = "N", fld005538 = apiOutput.msg }) .Where(it => it.idRecord == data.idRecord) .ExecuteCommand(); var msg = apiOutput.msg; if (apiOutput.msg == "") { msg = "同步成功"; } var output = new SapOutput() { materialCode= data._System_objNBS, parameter = apiOutput.parameter, code = codeVal, msg = msg, result = apiOutput.result }; return output; } /// /// 创建排版软件 /// /// /// [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + "," + SignatureAuthenticationDefaults.AuthenticationScheme)] [ApiDescriptionSettings(Name = "CreateLayout"), HttpPost] [DisplayName("创建排版软件")] public async Task CreateLayout(CreateLayoutInput input) { string output = ""; if (string.IsNullOrEmpty(input.ParentCode)) { return "请输入父项编码"; } var parentData = await _obj137Rep.AsQueryable().Where(x => x._System_objNBS == input.ParentCode && x.deleted == false && x._system_objConfigurationName == "默认").OrderByDescending(x => x.idRecord).FirstAsync(); if (parentData == null) { return "父项编码不存在,请检查"; } if (!parentData.SWPDMStatusName.Contains("设计") && !parentData.SWPDMStatusName.Contains("工艺")) { return "不在设计或工艺阶段不允许添加"; } var bom = await _configurationDataRep.AsQueryable().Where(x => x.ParentGuid == parentData.RecordGuid && x.isDeleted == false && x.ParentVersion == parentData.VersionIndex && x.ConfigId == 8 && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false).ToListAsync(); if (bom.Count != 0) { return "编码已存在原材料不允许修改"; } foreach (var item in input.Item) { var outputData = output + "子项编码" + item.ChildCode; if (string.IsNullOrEmpty(item.ChildCode)) { output = outputData + "未输入必填项"; continue; } if (item.Quantity == 0) { output = outputData + "请输入数量"; continue; } var childData = await _obj112Rep.AsQueryable().Where(x => x._System_objNBS == item.ChildCode && x.deleted == false).OrderByDescending(x => x.idRecord).ToListAsync(); if (childData.Count == 0) { output = outputData + "不存在,请检查;"; continue; } double num = item.Quantity.ToDouble(); // 将数值保留三位小数,再判断是否等于原数 bool isMoreThanThreeDecimalPlaces = Math.Round(num, 3) != num; if (isMoreThanThreeDecimalPlaces) { output = outputData + "数量格式不正确,请检查,只允许小数点后三位;"; continue; } } if (output == "") { int lineNumber = 0; foreach (var item in input.Item) { lineNumber++; string lineNumberStr = (lineNumber * 10).ToString("0000"); var childData = await _obj112Rep.AsQueryable().Where(x => x._System_objNBS == item.ChildCode && x.deleted == false).OrderByDescending(x => x.idRecord).FirstAsync(); var configurationData = await _configurationDataRep.AsQueryable().Where(x => x.RecordImage != null && x.ChildObjID == 112).FirstAsync(); var bomData = new ConfigurationData { ParentGuid = parentData.RecordGuid, ChildGuid = childData.RecordGuid, ChildObjID = 112, ConfigId = 8, childVersion = childData.VersionIndex, ParentVersion = (int)parentData.VersionIndex, isDeleted = false, ChildConfigID = 0, CreatedUserID = 2, ModifiedUserID = 2, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now, Quantity = item.Quantity.ToDouble(), Description = parentData._System_ObjDescription, EffectiveFrom = DateTime.Now, EffectiveTo = DateTime.Now.AddYears(100), Revision = "", Marker = lineNumberStr, Type = "正常", OrderIndex = lineNumber, PartNumber = childData._System_objNBS, RecordImage = configurationData.RecordImage, Folder = configurationData.Folder, ParentObjectID = 137, ChildSwFileType = 0, ParentSwFileType = 1, isSuppressed = false, isAddedFromSW = true, QtyManual = item.Quantity.ToDouble(), SwConfiguration = "", inContext = false, PrimaryFile = "", bKeepQuantityInSync = true, BOMGuid = await _dataValidationService.GetGuid(), IsPDMReference = false }; var addChildBomBom = await _configurationDataRep.AsInsertable(bomData).InsertColumns(x => new { x.ParentGuid, x.ChildGuid, x.ChildObjID, x.ConfigId, x.childVersion, x.ParentVersion, x.isDeleted, x.ChildConfigID, x.CreatedUserID, x.ModifiedUserID, x.CreatedDate, x.ModifiedDate, x.Quantity, x.Description, x.EffectiveFrom, x.EffectiveTo, x.Revision, x.Marker, x.Type, x.OrderIndex, x.PartNumber, x.RecordImage, x.Folder, x.ParentObjectID, x.ChildSwFileType, x.ParentSwFileType, x.isSuppressed, x.isAddedFromSW, x.QtyManual, x.SwConfiguration, x.inContext, x.PrimaryFile, x.bKeepQuantityInSync, x.BOMGuid, x.IsPDMReference }).ExecuteReturnEntityAsync(); } return "成功"; } else { return output; } } }