VistarStarDataIntegration/admin.net.pro/Admin.NET/Vistar.Application/Service/MaterialManagement/ProductManagementService.cs

315 lines
12 KiB
C#
Raw Normal View History

2024-09-23 11:34:19 +08:00
using Admin.NET.Core;
using COSXML.Model.Tag;
using DocumentFormat.OpenXml.Drawing.Diagrams;
using Elastic.Clients.Elasticsearch;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NewLife.Remoting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Vistar.Application.Const;
using Vistar.Application.Entity;
using Vistar.Application.Service.MaterialManagement.Dto;
using System.Net.Security;
using Org.BouncyCastle.Ocsp;
using System.Net.Http;
using NewLife.Configuration;
using Furion;
using Admin.NET.Core.Service;
using Vistar.Application.Util;
using Microsoft.Extensions.Options;
using Vistar.Application.SapService.Dto;
using DocumentFormat.OpenXml.ExtendedProperties;
using Vistar.Application.SapService;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ComponentTCBBatchCreateContainerServiceVersionRequest.Types;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using static COSXML.Model.Object.SelectObjectResult;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
2024-09-23 11:34:19 +08:00
namespace Vistar.Application.Service.MaterialManagement;
/// <summary>
/// 产品管理服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "ProductManagement", Order = 100)]
public class ProductManagementService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Obj110> _obj110Rep;
private readonly SqlSugarRepository<Configurations> _configurationsRep;
private readonly SysConfigService _sysConfigService;
private readonly SqlSugarRepository<ConfigurationData> _configurationDataRep;
public SapService.SapService _sapBomService;
public SqlSugarRepository<Obj118> _obj118Rep;
2024-09-23 11:34:19 +08:00
public ProductManagementService(
SqlSugarRepository<Obj110> obj110Rep,
SqlSugarRepository<Configurations> configurationsRep,
SysConfigService sysConfigService,
SqlSugarRepository<ConfigurationData> configurationDataRep,
SapService.SapService sapBomService,
SqlSugarRepository<Obj118> obj118Rep
)
2024-09-23 11:34:19 +08:00
{
_obj110Rep = obj110Rep;
_configurationsRep = configurationsRep;
_sysConfigService = sysConfigService;
_configurationDataRep = configurationDataRep;
_sapBomService = sapBomService;
_obj118Rep = obj118Rep;
2024-09-23 11:34:19 +08:00
}
/// <summary>
/// 分页查询产品管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "page", Description = "分页查询", Order = 1000), HttpPost]
[DisplayName("分页查询产品管理")]
//[Authorize(AuthenticationSchemes = SignatureAuthenticationDefaults.AuthenticationScheme)]
public async Task<SqlSugarPagedList<ProductManagementOutput>> Page(PageProductManagementInput input)
{
var query = await _obj110Rep.AsQueryable()
2024-09-23 11:34:19 +08:00
.Where(x => x.CheckedStatus == false && x.deleted == false && x.IsLatestVersion == true)
.WhereIF(!string.IsNullOrWhiteSpace(input._System_objNBS), u => u._System_objNBS.Contains(input._System_objNBS.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.fld004484), u => u.fld004484.Contains(input.fld004484.Trim()))
.WhereIF(input.fld004494Range != null && input.fld004494Range.Length == 2, u => u.fld004494 >= input.fld004494Range[0] && u.fld004494 <= input.fld004494Range[1])
.Select<ProductManagementOutput>()
.ToPagedListAsync(input.Page, input.PageSize);
return query;
2024-09-23 11:34:19 +08:00
}
///// <summary>
///// 获取BOM类型
///// </summary>
///// <returns></returns>
//[HttpGet]
//public async Task<List<ProductManagementBomTypeOutput>> BomType()
//{
// var BomType = await _configurationsRep.AsQueryable().Where(x => x.ObjectId == 110).ToListAsync();
// var BomTypeData = new List<ProductManagementBomTypeOutput>();
// for (int i = 0; i < BomType.Count; i++)
// {
// var data = new ProductManagementBomTypeOutput()
// {
// Id = BomType[i].ConfigID,
// Name = BomType[i].ConfigLabel
// };
// BomTypeData.Add(data);
// }
// return BomTypeData;
//}
2024-09-23 11:34:19 +08:00
/// <summary>
/// 产品管理-同步到 SAP
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "syncToSAP"), HttpPost]
[DisplayName("产品管理-同步到 SAP")]
public async Task<SapOutput> SyncToSAP(ProductManagementBaseInput input)
2024-09-23 11:34:19 +08:00
{
const string lengthError = "物料编码长度大于40请检查";
// 验证物料描述长度
if (input.fld004484.Length > 40)
{
await _obj110Rep.AsUpdateable()
.SetColumns(it => new Obj110
{
fld004629 = DateTime.Now,
fld004312 = "N",
fld004313 = lengthError
})
.Where(it => it.idRecord == input.idRecord)
.ExecuteCommandAsync().ConfigureAwait(false);
return new SapOutput
2024-09-23 11:34:19 +08:00
{
code = "失败",
msg = lengthError,
result = lengthError
};
}
//获取时间戳精确到毫秒sap要求每次调用生成不重复guid
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
2024-09-23 11:34:19 +08:00
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.fld004323,
Mtart = input.fld004324,
Maktx = input.fld004484,
Meins = input.fld004861,
Matkl = input.fld004485,
Bismt = input.fld004880,
Groes = input.fld004879,
Normt = input.fld004887,
Ferth = input.fld004882,
Zeinr = input.fld004881,
Mstae = input.fld004699,
Raube = input.fld004877,
Mhdrz = input.fld004876,
Mhdhb = input.fld004895,
Werks = input.fld004325,
Beskz = input.fld004490,
Sobsl = input.fld004491,
Schgt = input.fld004873,
Rgekz = input.fld004872,
Zbom = input.fld004698
};
2024-09-23 11:34:19 +08:00
var apiOutput = await _sapBomService.SapMaterialApi(sapMaterialInput);
string codeVal = apiOutput.code == "S" ? "成功" : "失败";
var msg = apiOutput.msg;
if (apiOutput.msg=="")
{
msg = "同步成功";
}
2024-09-23 11:34:19 +08:00
await _obj110Rep.AsUpdateable()
.SetColumns(it => new Obj110
{
fld004629 = DateTime.Now,
fld004311 = codeVal,
fld004312 = "N",
fld004313 = msg
})
.Where(it => it.idRecord == input.idRecord)
.ExecuteCommandAsync();
var output = new SapOutput()
{
code = codeVal,
msg = msg,
result = apiOutput.result
};
return output;
}
/// <summary>
/// 获取BOM
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "GetBOM", Description = "分页查询", Order = 1000), HttpPost]
[DisplayName("分页查询产品管理")]
public async Task<SqlSugarPagedList<ProductManagementBomOutput>> GetBOM(BomPageProductManagementInput input)
{
var configid = await _configurationsRep.AsQueryable().Where(x => x.ObjectId == 110 && x.ConfigLabel == "mBOM").FirstAsync();
var parent = await _obj110Rep.AsQueryable().Where(x => x.RecordGuid == input.ParentGuid && x.deleted == false).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)
.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<ProductManagementBomOutput>().ToPagedListAsync(input.Page, input.PageSize);
return BomData;
}
/// <summary>
/// Bom同步到SAP
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<SapOutput> SyncToSAPBom(List<BomPageProductManagementInput> input)
{
var ParentData = await _obj110Rep.AsQueryable().Where(x => x.RecordGuid == input[0].ParentGuid && x.deleted == false && x.VersionIndex == input[0].ParentVersion).FirstAsync();
var EcnData = await _obj118Rep.AsQueryable().Where(x => x.RecordGuid == ParentData.fld004944_Rec && x.deleted == false).OrderByDescending(x => x.VersionIndex).FirstAsync();
var ecn = "";
if (EcnData != null)
{
ecn = EcnData._System_objNBS;
}
2024-09-23 11:34:19 +08:00
var itemDataList = new List<ItemData>();
2024-09-23 11:34:19 +08:00
for (int i = 0; i < input.Count; i++)
2024-09-23 11:34:19 +08:00
{
int sun = i + 1;
var itemData = new ItemData()
2024-09-23 11:34:19 +08:00
{
POSNR = sun.ToString(),//组件序号
IDNRK = input[i].PartNumber,//组件物料号
MENGE = input[i].Quantity.ToString(),//组件数量
SORTF = input[i].fld004936,
ITISOB = input[i].fld004937,
ALPGR = input[i].fld004938,
ALPRF = input[i].fld004939,
ZDELETE = input[i].fld004940,
POSTP = input[i].fld004492,
ZYFMK = input[i].fld004941
2024-09-23 11:34:19 +08:00
};
itemDataList.Add(itemData);
2024-09-23 11:34:19 +08:00
}
//获取时间戳精确到毫秒sap要求每次调用生成不重复guid
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
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.fld004325,//工厂
Bmeng = ParentData.fld004942.ToString(),
Aennr = ecn,
Datuv = ParentData.fld004945.ToString(),
ItemList = itemDataList
};
var apiOutput = await _sapBomService.SapBomApi(isreq);
string codeVal = apiOutput.code == "S" ? "成功" : "失败";
await _obj110Rep.AsUpdateable()
.SetColumns(it => new Obj110
{
fld004631 = DateTime.Now,
fld004315 = codeVal,
fld004316 = "N",
fld004317 = apiOutput.msg
})
.Where(it => it.idRecord == ParentData.idRecord)
.ExecuteCommandAsync();
var msg = apiOutput.msg;
if (apiOutput.msg == "")
{
msg = "同步成功";
}
var output = new SapOutput()
{
code = codeVal,
msg = msg,
result = apiOutput.result
};
return output;
2024-09-23 11:34:19 +08:00
}
}