VistarStarDataIntegration/admin.net.pro/Admin.NET/Vistar.Application/Service/ProductDesignLibrary/ProductDesignLibraryService.cs

1693 lines
70 KiB
C#
Raw Normal View History

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