425 lines
17 KiB
C#
425 lines
17 KiB
C#
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||
//
|
||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||
//
|
||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||
|
||
using Admin.NET.Core;
|
||
using Admin.NET.Core.Service;
|
||
using Furion.DependencyInjection;
|
||
using Furion.DynamicApiController;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Vistar.Application.Const;
|
||
using Vistar.Application.Entity;
|
||
using Vistar.Application.SapService.Dto;
|
||
using Vistar.Application.Service.ProductionMaterialsManagement.Dto;
|
||
using Vistar.Application.Util;
|
||
|
||
namespace Vistar.Application.Service.ProductionMaterialsManagement;
|
||
/// <summary>
|
||
/// 生产物料管理服务
|
||
/// </summary>
|
||
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "ProductionMaterialsManagement", Order = 100)]
|
||
public class ProductionMaterialsManagementService : IDynamicApiController, ITransient
|
||
{
|
||
private readonly SqlSugarRepository<Obj112> _obj112Rep;
|
||
private readonly SysConfigService _sysConfigService;
|
||
public SapService.SapService _sapService;
|
||
private readonly SqlSugarRepository<Configurations> _configurationsRep;
|
||
private readonly SqlSugarRepository<ConfigurationData> _configurationDataRep;
|
||
private readonly IServiceScopeFactory _scopeFactory;
|
||
|
||
public ProductionMaterialsManagementService(
|
||
SqlSugarRepository<Obj112> obj112Rep,
|
||
SysConfigService sysConfigService,
|
||
SapService.SapService sapService,
|
||
SqlSugarRepository<Configurations> configurationsRep,
|
||
SqlSugarRepository<ConfigurationData> configurationDataRep,
|
||
IServiceScopeFactory scopeFactory
|
||
)
|
||
{
|
||
_obj112Rep = obj112Rep;
|
||
_sysConfigService = sysConfigService;
|
||
_sapService = sapService;
|
||
_configurationsRep = configurationsRep;
|
||
_configurationDataRep = configurationDataRep;
|
||
_scopeFactory = scopeFactory;
|
||
}
|
||
/// <summary>
|
||
/// 分页查询生产物料管理
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "page", Description = "分页查询生产物料管理", Order = 1000), HttpPost]
|
||
[DisplayName("分页查询生产物料管理")]
|
||
public async Task<SqlSugarPagedList<ProductionMaterialsManagementOutput>> Page(PageProductionMaterialsManagementInput input)
|
||
{
|
||
var query = await _obj112Rep.AsQueryable()
|
||
.Where(x => (x.CheckedStatus == 7 || x.CheckedStatus == 8) && x.deleted == false && x.IsLatestVersion == true && x.fld004973 == "发布")
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input._System_objNBS), u => u._System_objNBS.Contains(input._System_objNBS.Trim()))
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.fld004598), u => u.fld004598.Contains(input.fld004598.Trim()))
|
||
.WhereIF(input.fld004609Range != null && input.fld004609Range.Length == 2, u => u.fld004609 >= input.fld004609Range[0] && u.fld004609 <= input.fld004609Range[1])
|
||
.Select<ProductionMaterialsManagementOutput>()
|
||
.ToPagedListAsync(input.Page, input.PageSize);
|
||
return query;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生产物料管理-同步到 SAP
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "syncToSAP"), HttpPost]
|
||
[DisplayName("生产物料管理-同步到 SAP")]
|
||
public async Task<SapOutput> SyncToSAP(ProductionMaterialsManagementInput input)
|
||
{
|
||
const string lengthError = "项目描述长度大于40,请检查!";
|
||
|
||
// 验证物料描述长度
|
||
if (input.fld004598.Length > 40)
|
||
{
|
||
await _obj112Rep.AsUpdateable()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004607 = "N",
|
||
fld004605 = lengthError,
|
||
fld004604 = "失败"
|
||
})
|
||
.Where(it => it.idRecord == input.idRecord)
|
||
.ExecuteCommandAsync().ConfigureAwait(false);
|
||
|
||
return new SapOutput
|
||
{
|
||
code = "失败",
|
||
msg = lengthError,
|
||
result = lengthError
|
||
};
|
||
}
|
||
|
||
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
|
||
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
Random random = new Random();
|
||
// 生成1000到9999之间的随机数
|
||
string randomNumber = random.Next(1000, 10000).ToString();
|
||
millisecondTimestamp = millisecondTimestamp + randomNumber;
|
||
|
||
var sapMaterialInput = new SapMaterialInput()
|
||
{
|
||
Reqkeyid = "",
|
||
Businessid = "",
|
||
Messageid = "",
|
||
Sndprn = "PLM",
|
||
Rcvprn = "SAP",
|
||
Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName),
|
||
Note1 = "",
|
||
Note2 = "",
|
||
Note3 = "",
|
||
Zwbid = millisecondTimestamp,
|
||
Matnr = input._System_objNBS,
|
||
Mbrsh = input.fld004595,
|
||
Mtart = input.fld004596,
|
||
Maktx = input.fld004598,
|
||
Meins = input.fld004594,
|
||
Matkl = input.fld004599,
|
||
Bismt = input._System_ObjDescription,
|
||
Groes = input.fld005323,
|
||
Normt = input.fld004592,
|
||
Ferth = input.fld004593,
|
||
Zeinr = input.fld004903,
|
||
Mstae = input.fld004696,
|
||
Raube = input.fld004904,
|
||
Mhdrz = input.fld004905.ToString(),
|
||
Mhdhb = input.fld004907.ToString(),
|
||
Werks = input.fld004597,
|
||
Beskz = input.fld004600,
|
||
Sobsl = input.fld004601,
|
||
Schgt = input.fld004602,
|
||
Rgekz = input.fld004603,
|
||
Zbom = input.fld004695
|
||
};
|
||
|
||
var apiOutput = await _sapService.SapMaterialApi(sapMaterialInput);
|
||
string codeVal = apiOutput.code == "S" ? "成功" : "失败";
|
||
var msg = apiOutput.msg;
|
||
if (apiOutput.msg == "")
|
||
{
|
||
msg = "同步成功";
|
||
}
|
||
|
||
await _obj112Rep.AsUpdateable()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004604 = codeVal,
|
||
fld004607 = "N",
|
||
fld004605 = 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>
|
||
/// 生产物料管理-批量同步到SAP
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "BatchSyncToSAP"), HttpPost]
|
||
[DisplayName("生产物料管理-批量同步到SAP")]
|
||
public async Task<List<SapOutput>> BatchSyncToSAP(List<ProductionMaterialsManagementInput> input)
|
||
{
|
||
using var serviceScope = _scopeFactory.CreateScope();
|
||
var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
|
||
|
||
const string lengthError = "物料描述长度大于40,请检查!";
|
||
const string synchronized = "该项目编号已同步或已停用!";
|
||
var SapOutputList = new List<SapOutput>();
|
||
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
|
||
|
||
var tasks = input.Select(async item =>
|
||
{
|
||
if (item.fld004598.Length > 40)
|
||
{
|
||
db.CopyNew().Updateable<Obj112>()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004607 = "N",
|
||
fld004605 = lengthError,
|
||
fld004604 = "失败"
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput
|
||
{
|
||
materialCode = item._System_objNBS,
|
||
code = "失败",
|
||
msg = lengthError,
|
||
result = lengthError
|
||
};
|
||
}
|
||
|
||
if (item.fld004607 == "N" || item.fld004607 == "D" || item.fld004607 == null)
|
||
{
|
||
return new SapOutput
|
||
{
|
||
materialCode = item._System_objNBS,
|
||
code = "失败",
|
||
msg = synchronized,
|
||
result = synchronized
|
||
};
|
||
}
|
||
|
||
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
|
||
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
Random random = new Random();
|
||
// 生成1000到9999之间的随机数
|
||
string randomNumber = random.Next(1000, 10000).ToString();
|
||
millisecondTimestamp = millisecondTimestamp + randomNumber;
|
||
|
||
var sapMaterialInput = new SapMaterialInput()
|
||
{
|
||
Reqkeyid = "",
|
||
Businessid = "",
|
||
Messageid = "",
|
||
Sndprn = "PLM",
|
||
Rcvprn = "SAP",
|
||
Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName),
|
||
Note1 = "",
|
||
Note2 = "",
|
||
Note3 = "",
|
||
Zwbid = millisecondTimestamp,
|
||
Matnr = item._System_objNBS,
|
||
Mbrsh = item.fld004595,
|
||
Mtart = item.fld004596,
|
||
Maktx = item.fld004598,
|
||
Meins = item.fld004594,
|
||
Matkl = item.fld004599,
|
||
Bismt = item._System_ObjDescription,
|
||
Groes = item.fld005323,
|
||
Normt = item.fld004592,
|
||
Ferth = item.fld004593,
|
||
Zeinr = item.fld004903,
|
||
Mstae = item.fld004696,
|
||
Raube = item.fld004904,
|
||
Mhdrz = item.fld004905.ToString(),
|
||
Mhdhb = item.fld004907.ToString(),
|
||
Werks = item.fld004597,
|
||
Beskz = item.fld004600,
|
||
Sobsl = item.fld004601,
|
||
Schgt = item.fld004602,
|
||
Rgekz = item.fld004603,
|
||
Zbom = item.fld004695
|
||
};
|
||
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
|
||
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
|
||
string msg = sapOutput.msg;
|
||
|
||
if (sapOutput.msg == "")
|
||
{
|
||
msg = "同步成功";
|
||
}
|
||
db.CopyNew().Updateable<Obj112>()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004604 = codeVal,
|
||
fld004607 = "N",
|
||
fld004605 = msg
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput()
|
||
{
|
||
parameter = sapOutput.parameter,
|
||
materialCode = item._System_objNBS,
|
||
code = codeVal,
|
||
msg = msg,
|
||
result = sapOutput.result
|
||
};
|
||
|
||
});
|
||
var materialOutput = await Task.WhenAll(tasks);
|
||
return materialOutput.ToList();
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生产物料管理-定时同步到SAP
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "TimingSyncToSAP"), HttpGet]
|
||
[DisplayName("生产物料管理-定时同步到SAP")]
|
||
[AllowAnonymous]
|
||
public async Task<List<SapOutput>> TimingSyncToSAP()
|
||
{
|
||
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.Queryable<Obj112>()
|
||
.Where(x => (x.CheckedStatus == 7 || x.CheckedStatus == 8) && x.deleted == false && x.IsLatestVersion == true && (x.fld004607 == "A" || x.fld004607 == "M") && x.fld004973 == "发布")
|
||
.ToList();
|
||
const string lengthError = "物料描述长度大于40,请检查!";
|
||
var SapOutputList = new List<SapOutput>();
|
||
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
|
||
|
||
var tasks = input.Select(async item =>
|
||
{
|
||
if (item.fld004598.Length > 40)
|
||
{
|
||
db.CopyNew().Updateable<Obj112>()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004607 = "N",
|
||
fld004605 = lengthError,
|
||
fld004604 = "失败"
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput
|
||
{
|
||
materialCode = item._System_objNBS,
|
||
code = "失败",
|
||
msg = lengthError,
|
||
result = lengthError
|
||
};
|
||
}
|
||
|
||
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
|
||
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
Random random = new Random();
|
||
// 生成1000到9999之间的随机数
|
||
string randomNumber = random.Next(1000, 10000).ToString();
|
||
millisecondTimestamp = millisecondTimestamp + randomNumber;
|
||
|
||
var sapMaterialInput = new SapMaterialInput()
|
||
{
|
||
Reqkeyid = "",
|
||
Businessid = "",
|
||
Messageid = "",
|
||
Sndprn = "PLM",
|
||
Rcvprn = "SAP",
|
||
Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName),
|
||
Note1 = "",
|
||
Note2 = "",
|
||
Note3 = "",
|
||
Zwbid = millisecondTimestamp,
|
||
Matnr = item._System_objNBS,
|
||
Mbrsh = item.fld004595,
|
||
Mtart = item.fld004596,
|
||
Maktx = item.fld004598,
|
||
Meins = item.fld004594,
|
||
Matkl = item.fld004599,
|
||
Bismt = item._System_ObjDescription,
|
||
Groes = item.fld005323,
|
||
Normt = item.fld004592,
|
||
Ferth = item.fld004593,
|
||
Zeinr = item.fld004903,
|
||
Mstae = item.fld004696,
|
||
Raube = item.fld004904,
|
||
Mhdrz = item.fld004905.ToString(),
|
||
Mhdhb = item.fld004907.ToString(),
|
||
Werks = item.fld004597,
|
||
Beskz = item.fld004600,
|
||
Sobsl = item.fld004601,
|
||
Schgt = item.fld004602,
|
||
Rgekz = item.fld004603,
|
||
Zbom = item.fld004695
|
||
};
|
||
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
|
||
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
|
||
string msg = sapOutput.msg;
|
||
|
||
if (sapOutput.msg == "")
|
||
{
|
||
msg = "同步成功";
|
||
}
|
||
db.CopyNew().Updateable<Obj112>()
|
||
.SetColumns(it => new Obj112
|
||
{
|
||
fld004606 = DateTime.Now,
|
||
fld004604 = codeVal,
|
||
fld004607 = "N",
|
||
fld004605 = msg
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput()
|
||
{
|
||
parameter = sapOutput.parameter,
|
||
materialCode = item._System_objNBS,
|
||
code = codeVal,
|
||
msg = msg,
|
||
result = sapOutput.result
|
||
};
|
||
|
||
});
|
||
var materialOutput = await Task.WhenAll(tasks);
|
||
return materialOutput.ToList();
|
||
}
|
||
|
||
}
|