VistarStarDataIntegration/admin.net.pro/Admin.NET/Vistar.Application/Service/StockInquiry/StockInquiryService.cs

1312 lines
53 KiB
C#
Raw Normal View History

// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
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.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.StockInquiry.Dto;
using Vistar.Application.Util;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types;
namespace Vistar.Application.Service.StockInquiry;
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "StockInquiry", Order = 100)]
public class StockInquiryService : IDynamicApiController, ITransient
{
public Common.DataValidationService _dataValidationService;
public SqlSugarRepository<Obj137> _obj137Rep;
public SqlSugarRepository<Obj112> _obj112Rep;
public SqlSugarRepository<ConfigurationData> _configurationDataRep;
public SolidWorksManageService.SolidWorksManageService _solidWorksManageService;
private readonly SysConfigService _sysConfigService;
public SapService.SapService _sapService;
private readonly IServiceScopeFactory _scopeFactory;
public StockInquiryService
(
DataValidationService dataValidationService,
SqlSugarRepository<Obj137> obj137Rep,
SqlSugarRepository<Obj112> obj112Rep,
SqlSugarRepository<ConfigurationData> configurationDataRep,
SolidWorksManageService.SolidWorksManageService solidWorksManageService,
SysConfigService sysConfigService,
SapService.SapService sapService,
IServiceScopeFactory scopeFactory
)
{
_obj137Rep = obj137Rep;
_obj112Rep = obj112Rep;
_configurationDataRep = configurationDataRep;
_solidWorksManageService = solidWorksManageService;
_sysConfigService = sysConfigService;
_sapService = sapService;
_dataValidationService = dataValidationService;
_scopeFactory = scopeFactory;
}
/// <summary>
/// 产品管理库-库存查询
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetProductInventory", Description = "产品管理库-库存查询", Order = 1000), HttpGet]
[DisplayName("产品管理库-库存查询")]
[AllowAnonymous]
public async Task<SapEcnQueryOutput> GetProductInventory(string materialCode)
{
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var SapEcnQueryData = new SapEcnQueryInput
{
Reqkeyid = "",
Businessid = "",
Messageid = "",
Sndprn = "PLM",
Rcvprn = "SAP",
Requser = Requser,
Note1 = "",
Note2 = "",
Note3 = "",
Sign = "I",
Option = "EQ",
Low = materialCode,
High = ""
};
var apiOutput = await _sapService.SapEcnQueryApi(SapEcnQueryData);
if (apiOutput.Code == "S")
{
var data = await _obj137Rep.AsQueryable().Where(x => x._System_objNBS == materialCode && x.deleted == false && x.SWPDMConfigurationName == "默认").OrderByDescending(x => x.idRecord).FirstAsync();
if (data != null)
{
apiOutput.DateUpdated = DateTime.Now;
await _obj137Rep.AsUpdateable().SetColumns(it => new Obj137
{
fld006530 = apiOutput.Bdmng,
fld005623 = apiOutput.Labst,
fld006531 = apiOutput.ZwqslPo,
fld006532 = apiOutput.ZwqslPr,
fld005624 = apiOutput.DateUpdated
})
.Where(it => it.idRecord == data.idRecord)
.ExecuteCommandAsync();
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
{
fld006750 = apiOutput.Bdmng,
fld006751 = apiOutput.Labst,
fld006752 = apiOutput.ZwqslPo,
fld006753 = apiOutput.ZwqslPr,
fld006755 = apiOutput.DateUpdated
})
.Where(it => it.idRecord == RawMaterialData.idRecord)
.ExecuteCommandAsync();
return apiOutput;
}
}
return apiOutput;
}
/// <summary>
/// 产品管理库-Bom库存查询
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetBomProductInventory", Description = "产品管理库-Bom库存查询", Order = 1000), HttpGet]
[DisplayName("产品管理库-Bom库存查询")]
[AllowAnonymous]
public async Task<List<BomStockInquiryOutput>> GetBomProductInventory(string materialCode)
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var materialData = await db.CopyNew().Queryable<Obj137>()
.Where(x => x._System_objNBS == materialCode && x.deleted == false && x.SWPDMConfigurationName == "默认")
.OrderByDescending(x => x.idRecord)
.FirstAsync();
var bomList = await db.CopyNew().Queryable<ConfigurationData>()
.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<BomStockInquiryUpdateable>();
var UpdateableOutput = new List<BomStockInquiryOutput>();
int batchSize = 50; // 每批的大小
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 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);
_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); // 等待当前批次的所有任务完成
// 合并当前批次的结果
UpdateableOutput.AddRange(bomStockInquiryOutput);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
// 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();
}
/// <summary>
/// 获取原材料
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetRawMaterial", Description = "获取原材料", Order = 1000), HttpPost]
[DisplayName("获取原材料")]
[AllowAnonymous]
public async Task<List<RawMaterialOutput>> GetRawMaterial(RawMaterialInput input)
{
var data = await _obj112Rep.AsQueryable()
.Where(x => x.deleted == false)
.WhereIF(!string.IsNullOrWhiteSpace(input.materialCode), u => u._System_objNBS.Contains(input.materialCode.Trim()))
.Select<RawMaterialOutput>()
.ToListAsync();
return data;
}
/// <summary>
/// 原材料库存查询
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "RawMaterialStockInquiry", Description = "原材料库存查询", Order = 1000), HttpGet]
[DisplayName("原材料库存查询")]
[AllowAnonymous]
public async Task<List<BomStockInquiryOutput>> RawMaterialStockInquiry()
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var data = await db.CopyNew().Queryable<Obj112>()
.Where(x => x.deleted == false)
.Select<RawMaterialOutput>()
.ToListAsync();
var UpdateableList = new List<BomStockInquiryUpdateable>();
var UpdateableOutput = new List<BomStockInquiryOutput>();
int batchSize = 100; // 每批的大小
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 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未查询到库存信息"
};
}
}).ToList();
var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成
// 合并当前批次的结果
UpdateableOutput.AddRange(bomStockInquiryOutput);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
// 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;
db.CopyNew().Updateable<Obj112>().SetColumns(it => new Obj112
{
fld006750 = item.Bdmng,
fld006751 = item.Labst,
fld006752 = item.ZwqslPo,
fld006753 = item.ZwqslPr,
fld006755 = dateUpdated
})
.Where(it => it.idRecord == Convert.ToInt32(item.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段
.ExecuteCommand();
}
return UpdateableOutput.ToList();
}
/// <summary>
/// 产品管理库-价格查询
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetPriceInquiry", Description = "产品管理库-价格查询", Order = 1000), HttpGet]
[DisplayName("产品管理库-价格查询")]
[AllowAnonymous]
public async Task<SapPriceQueryOutput> GetPriceInquiry(string materialCode)
{
var Requser = await _sysConfigService.GetConfigValueByCode<string>(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;
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 && x.SWPDMConfigurationName == "默认").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;
}
/// <summary>
/// 产品管理库-Bom成本查询
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetBomPriceInquiry", Description = "产品管理库-Bom成本查询", Order = 1000), HttpGet]
[DisplayName("产品管理库-Bom成本查询")]
[AllowAnonymous]
public async Task<List<BomPriceInquiryOutput>> GetBomPriceInquiry(string materialCode)
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var materialData = await db.CopyNew().Queryable<Obj137>()
.Where(x => x._System_objNBS == materialCode && x.deleted == false && x.SWPDMConfigurationName == "默认")
.OrderByDescending(x => x.idRecord)
.FirstAsync();
var bomList = await db.CopyNew().Queryable<ConfigurationData>()
.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<BomPriceInquiryUpdateable>();
var UpdateableOutput = new List<BomPriceInquiryOutput>();
int batchSize = 50; // 每批的大小
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);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
return UpdateableOutput.ToList();
}
/// <summary>
/// 原材料成本查询
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "RawMaterialPriceInquiry", Description = "原材料成本查询", Order = 1000), HttpGet]
[DisplayName("原材料成本查询")]
[AllowAnonymous]
public async Task<List<BomPriceInquiryOutput>> RawMaterialPriceInquiry()
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var data = await db.CopyNew().Queryable<Obj112>()
.Where(x => x.deleted == false)
.Select<RawMaterialOutput>()
.ToListAsync();
var UpdateableList = new List<BomPriceInquiryUpdateable>();
var UpdateableOutput = new List<BomPriceInquiryOutput>();
int batchSize = 100; // 每批的大小
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<Obj112>().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<Obj112>().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);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
return UpdateableOutput.ToList();
}
/// <summary>
/// 获取PDM库物料
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetPdmMaterial", Description = "获取PDM库物料", Order = 1000), HttpPost]
[DisplayName("获取PDM库物料")]
[AllowAnonymous]
public async Task<List<RawMaterialOutput>> GetPdmMaterial(RawMaterialInput input)
{
var data = await _obj137Rep.AsQueryable().PartitionBy(it => it.RecordGuid)
.OrderBy(it => it.idRecord, OrderByType.Desc)
.Take(1)
.WhereIF(!string.IsNullOrWhiteSpace(input.materialCode), u => u._System_objNBS.Contains(input.materialCode.Trim()))
.Where(it => it.deleted == false && it.SWPDMConfigurationName == "默认" && it.IsLatestVersion == true && !SqlFunc.IsNullOrEmpty(it._System_objNBS) && !it._System_objNBS.Contains("默认"))
.Select<RawMaterialOutput>()
.ToListAsync();
return data;
}
/// <summary>
/// PDM库存查询
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "PdmMaterialStockInquiry", Description = "PDM库存查询", Order = 1000), HttpGet]
[DisplayName("PDM库存查询")]
[AllowAnonymous]
public async Task<List<BomStockInquiryOutput>> PdmMaterialStockInquiry()
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var data = await _obj137Rep.AsQueryable().PartitionBy(it => it.RecordGuid)
.OrderBy(it => it.idRecord, OrderByType.Desc)
.Take(1)
.Where(it => it.deleted == false && it.SWPDMConfigurationName == "默认" && it.IsLatestVersion == true && !SqlFunc.IsNullOrEmpty(it._System_objNBS) && !it._System_objNBS.Contains("默认"))
.Select<RawMaterialOutput>()
.ToListAsync();
var UpdateableList = new List<BomStockInquiryUpdateable>();
var UpdateableOutput = new List<BomStockInquiryOutput>();
int batchSize = 100; // 每批的大小
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 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);
db.CopyNew().Updateable<Obj137>().SetColumns(it => new Obj137
{
fld006530 = UpdateableData.Bdmng,
fld005623 = UpdateableData.Labst,
fld006531 = UpdateableData.ZwqslPo,
fld006532 = UpdateableData.ZwqslPr,
fld005624 = dateUpdated
})
.Where(it => it.idRecord == Convert.ToInt32(UpdateableData.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段
.ExecuteCommand();
return new BomStockInquiryOutput
{
materialCode = code,
materialName = name,
Labst = apiOutput.Labst,
Bdmng = apiOutput.Bdmng,
ZwqslPr = apiOutput.ZwqslPr,
ZwqslPo = apiOutput.ZwqslPo,
DateUpdated = dateUpdated,
State = "查询成功"
};
}
else
{
var UpdateableData = new BomStockInquiryUpdateable
{
Code = "",
RecordGuid = "",
Bdmng = "",
Labst = "",
ZwqslPo = "",
ZwqslPr = "",
DateUpdated = dateUpdated
};
UpdateableList.Add(UpdateableData);
db.CopyNew().Updateable<Obj137>().SetColumns(it => new Obj137
{
fld006530 = "",
fld005623 = "",
fld006531 = "",
fld006532 = "",
fld005624 = dateUpdated
})
.Where(it => it.idRecord == Convert.ToInt32(UpdateableData.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段
.ExecuteCommand();
return new BomStockInquiryOutput
{
materialCode = code,
materialName = name,
State = "SAP未查询到库存信息"
};
}
}).ToList();
var bomStockInquiryOutput = await Task.WhenAll(tasks); // 等待当前批次的所有任务完成
// 合并当前批次的结果
UpdateableOutput.AddRange(bomStockInquiryOutput);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
//foreach (var item in UpdateableList)
//{
// var dateUpdated = DateTime.Now;
// db.CopyNew().Updateable<Obj112>().SetColumns(it => new Obj112
// {
// fld006750 = item.Bdmng,
// fld006751 = item.Labst,
// fld006752 = item.ZwqslPo,
// fld006753 = item.ZwqslPr,
// fld006755 = dateUpdated
// })
// .Where(it => it.idRecord == Convert.ToInt32(item.RecordGuid))//在这个方法中使用RecordGuid字段作为id字段
// .ExecuteCommand();
//}
return UpdateableOutput.ToList();
}
/// <summary>
/// PDM成本查询
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "PDMMaterialPriceInquiry", Description = "PDM成本查询", Order = 1000), HttpGet]
[DisplayName("PDM成本查询")]
[AllowAnonymous]
public async Task<List<BomPriceInquiryOutput>> PdmMaterialPriceInquiry()
{
using var serviceScope = _scopeFactory.CreateScope();
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
var data = await _obj137Rep.AsQueryable().PartitionBy(it => it.RecordGuid)
.OrderBy(it => it.idRecord, OrderByType.Desc)
.Take(1)
.Where(it => it.deleted == false && it.SWPDMConfigurationName == "默认" && it.IsLatestVersion == true && !SqlFunc.IsNullOrEmpty(it._System_objNBS) && !it._System_objNBS.Contains("默认"))
.Select<RawMaterialOutput>()
.ToListAsync();
var UpdateableList = new List<BomPriceInquiryUpdateable>();
var UpdateableOutput = new List<BomPriceInquiryOutput>();
int batchSize = 100; // 每批的大小
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 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<Obj137>().SetColumns(it => new Obj137
{
fld005625 = priceValue,
fld005626 = 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<Obj137>().SetColumns(it => new Obj137
{
fld005625 = "",
fld005626 = 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);
// 如果需要暂停或者处理频率,可以在此处插入适当的延迟。
await Task.Delay(1000); // 如果需要等待1秒可以启用此行代码。
}
return UpdateableOutput.ToList();
}
/// <summary>
/// 获取物料版本
/// </summary>
/// <param name="materialCode"></param>
/// <returns></returns>
[AllowAnonymous]
public async Task<List<MaterialVersions>> GetPdmMaterialVersions(string materialCode)
{
var versionsLiat = await _obj137Rep.AsQueryable()
.Where(x => x._System_objNBS == materialCode && x.deleted == false && x._system_objConfigurationName == "默认")
.Select(x => new MaterialVersions { Version_index = x.VersionIndex, Version = x.revision })
.OrderByDescending(x => x.Version_index)
.ToListAsync();
return versionsLiat;
}
/// <summary>
/// 获取Bom
/// </summary>
/// <param name="materialCode"></param>
/// <param name="versions"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetPdmMaterialBom", Description = "获取Bom", Order = 1000), HttpGet]
[AllowAnonymous]
public async Task<List<PdmMaterialBom>> GetPdmMaterialBom(string materialCode, string versions)
{
int versionsIndex = 0;
// 匹配一个或多个数字
Match match = Regex.Match(versions, @"\d+");
if (match.Success)
{
// 提取匹配的数字
string matchedNumber = match.Value;
versionsIndex = int.Parse(matchedNumber);
}
var materialData = await _obj137Rep.AsQueryable()
.Where(x => x._System_objNBS == materialCode && x.deleted == false && x.SWPDMConfigurationName == "默认" && x.VersionIndex == versionsIndex)
.OrderByDescending(x => x.idRecord)
.FirstAsync();
if (materialData == null)
{
return null;
}
var bom137List = await _configurationDataRep.AsQueryable().InnerJoin<Obj137>((x, y) => x.ChildGuid == y.RecordGuid && x.childVersion == y.VersionIndex)
.Where(x => x.ParentGuid == materialData.RecordGuid && x.ParentVersion == materialData.VersionIndex && x.isDeleted == false && x.ConfigId == 8 && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false && (!(x.ParentSwFileType == 1 && x.isAddedFromSW == false)))
.Select((x, y) => new PdmMaterialBom
{
OrderIndex = (long)x.OrderIndex,
PartNumber = x.PartNumber,
Description = y._System_ObjDescription,
QtyManual = (float)x.QtyManual,
fld005747 = y._SWPDM_125_SW___,
fld005590 = y._SWPDM_65_SW___,
fld005587 = y._SWPDM_62_SW___,
fld006325 = y._SWPDM_61_SW_____,
fld005744 = y._SWPDM_126_SW___,
childVersion = y.VersionIndex.ToString()+" "+y.revision,
fld006928 = y.fld005623,
fld006929 = y.fld006530,
fld006930 = y.fld006532,
fld006931 = y.fld006531
})
.ToListAsync();
var bom112List = await _configurationDataRep.AsQueryable().InnerJoin<Obj112>((x, y) => x.ChildGuid == y.RecordGuid && x.childVersion == y.VersionIndex)
.Where(x => x.ParentGuid == materialData.RecordGuid && x.ParentVersion == materialData.VersionIndex && x.isDeleted == false && x.ConfigId == 8 && (x.fld005577 != "不包含" || x.fld005577 == null) && x.isSuppressed == false && x.inContext == false && (!(x.ParentSwFileType == 1 && x.isAddedFromSW == false)))
.Select((x, y) => new PdmMaterialBom
{
OrderIndex = (long)x.OrderIndex,
PartNumber = x.PartNumber,
Description = y._System_ObjDescription,
QtyManual = (float)x.QtyManual,
fld005747 = y.fld005323,
fld005590 = y.fld004592,
fld005587 = y.fld004593,
fld006325 = y.fld004903,
fld005744 = y.fld004594,
childVersion = y.VersionIndex.ToString() + " " + y.revision,
fld006928 = y.fld006751,
fld006929 = y.fld006750,
fld006930 = y.fld006753,
fld006931 = y.fld006752
})
.ToListAsync();
// 合并两个列表
var combinedList = bom137List.Concat(bom112List).OrderBy(item => item.OrderIndex).ToList();
return combinedList;
}
}