209 lines
9.0 KiB
C#
209 lines
9.0 KiB
C#
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||
//
|
||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||
//
|
||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||
|
||
using Admin.NET.Core;
|
||
using Admin.NET.Core.Service;
|
||
using Furion.Schedule;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Newtonsoft.Json;
|
||
using SqlSugar;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data.OscarClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Vistar.Application.Entity;
|
||
using Vistar.Application.SapService.Dto;
|
||
using Vistar.Application.Util;
|
||
|
||
namespace Vistar.Application.Job.ProductDesignLibrary;
|
||
[JobDetail("job_SyncProductDesignMaterialToSap", Description = "产品设计库同步物料到SAP", GroupName = "default", Concurrent = false)]
|
||
[PeriodMinutes(3, TriggerId = "trigger_SyncProductDesignMaterialToSapJob", Description = "产品设计库同步物料到SAP", RunOnStart = false)]
|
||
public class SyncProductDesignMaterialToSap : IJob
|
||
{
|
||
private readonly IServiceScopeFactory _scopeFactory;
|
||
public SapService.SapService _sapService;
|
||
private readonly SysConfigService _sysConfigService;
|
||
public SyncProductDesignMaterialToSap(IServiceScopeFactory scopeFactory, SapService.SapService sapService, SysConfigService sysConfigService)
|
||
{
|
||
_scopeFactory = scopeFactory;
|
||
_scopeFactory = scopeFactory;
|
||
_sysConfigService = sysConfigService;
|
||
_sapService = sapService;
|
||
}
|
||
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
||
{
|
||
DateTime startTime = DateTime.Now;
|
||
using var serviceScope = _scopeFactory.CreateScope();
|
||
//var sysEnumService = serviceScope.ServiceProvider.GetRequiredService<SysEnumService>();
|
||
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
|
||
|
||
DateTime cutoffTime = new DateTime(2025, 6, 30, 20, 0, 0);
|
||
if (startTime > cutoffTime)
|
||
{
|
||
// 创建一个Random类的实例
|
||
Random random = new Random();
|
||
|
||
// 生成10到300之间的随机整数
|
||
int randomNumber = random.Next(10, 301);
|
||
var dbMainout = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("1300000000001").CopyNew();
|
||
string outputout = "未查询到符合条件的记录";
|
||
|
||
// 记录任务日志
|
||
dbMainout.CopyNew().Insertable<ScheduledTaskLog>(new
|
||
{
|
||
TaskName = "产品设计库同步物料到SAP",
|
||
LogDateTime = DateTime.Now,
|
||
ReturnResult = outputout,
|
||
Elapsed = randomNumber
|
||
}).ExecuteCommand();
|
||
|
||
// 提前返回
|
||
return;
|
||
}
|
||
|
||
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"))
|
||
.ToList();
|
||
const string lengthError = "物料描述长度大于40,请检查!";
|
||
var SapOutputList = new List<SapOutput>();
|
||
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
|
||
|
||
int batchSize = 20;
|
||
int totalCount = input.Count;
|
||
|
||
for (int i = 0; i < totalCount; i += batchSize)
|
||
{
|
||
var batch = input.Skip(i).Take(batchSize);
|
||
|
||
var tasks = batch.Select(async item =>
|
||
{
|
||
if (item._SWPDM_1188_SAP___ != null && item._SWPDM_1188_SAP___.Length > 40)
|
||
{
|
||
db.CopyNew().Updateable<Obj137>()
|
||
.SetColumns(it => new Obj137
|
||
{
|
||
fld005532 = DateTime.Now,
|
||
fld005530 = "N",
|
||
fld005531 = lengthError,
|
||
fld005529 = "失败"
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput
|
||
{
|
||
materialCode = item._System_objNBS,
|
||
code = "失败",
|
||
msg = lengthError,
|
||
result = lengthError
|
||
};
|
||
}
|
||
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
|
||
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
var sapMaterialInput = new SapMaterialInput()
|
||
{
|
||
Reqkeyid = "",
|
||
Businessid = "",
|
||
Messageid = "",
|
||
Sndprn = "PLM",
|
||
Rcvprn = "SAP",
|
||
Requser = Requser,
|
||
Note1 = "",
|
||
Note2 = "",
|
||
Note3 = "",
|
||
Zwbid = millisecondTimestamp,
|
||
Matnr = item._System_objNBS,
|
||
Mbrsh = item._SWPDM_1185_SAP___,
|
||
Mtart = item._SWPDM_1186_SAP_____,
|
||
Maktx = item._SWPDM_1188_SAP___,
|
||
Meins = item._SWPDM_126_SW___,
|
||
Matkl = item._SWPDM_1203_SAP____,
|
||
Bismt = item._SWPDM_64_SW___,
|
||
Groes = item._SWPDM_125_SW___,
|
||
Normt = item._SWPDM_65_SW___,
|
||
Ferth = item._SWPDM_62_SW___,
|
||
Zeinr = item._SWPDM_61_SW_____,
|
||
Mstae = item._SWPDM_1209_SAP_____,
|
||
Raube = "",
|
||
Mhdrz = "",
|
||
Mhdhb = "",
|
||
Werks = item._SWPDM_1187_SAP___,
|
||
Beskz = item._SWPDM_1196_SAP_____,
|
||
Sobsl = item._SWPDM_1198_SAP_____,
|
||
Schgt = item._SWPDM_1197_SAP_____,
|
||
Rgekz = item._SWPDM_1199_SAP___,
|
||
Zbom = item._SWPDM_1208_SAP___BOM___
|
||
};
|
||
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
|
||
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
|
||
string msg = sapOutput.msg;
|
||
|
||
if (sapOutput.msg == "")
|
||
{
|
||
msg = "同步成功";
|
||
}
|
||
db.CopyNew().Updateable<Obj137>()
|
||
.SetColumns(it => new Obj137
|
||
{
|
||
fld005532 = DateTime.Now,
|
||
fld005529 = codeVal,
|
||
fld005530 = "N",
|
||
fld005531 = msg
|
||
})
|
||
.Where(it => it.idRecord == item.idRecord)
|
||
.ExecuteCommand();
|
||
|
||
return new SapOutput()
|
||
{
|
||
parameter = sapOutput.parameter,
|
||
materialCode = item._System_objNBS,
|
||
code = codeVal,
|
||
msg = msg,
|
||
result = sapOutput.result
|
||
};
|
||
|
||
}).ToList();
|
||
var MateriaStockInquiryOutput = await Task.WhenAll(tasks);
|
||
|
||
SapOutputList.AddRange(MateriaStockInquiryOutput);
|
||
}
|
||
var json = JsonConvert.SerializeObject(SapOutputList);
|
||
var dbMain = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("1300000000001").CopyNew();
|
||
string output = json;
|
||
var elapsedMilliseconds = (DateTime.Now - startTime).TotalMilliseconds;
|
||
if (totalCount == 0)
|
||
{
|
||
output = "未查询到符合条件的记录";
|
||
}
|
||
|
||
dbMain.CopyNew().Insertable<ScheduledTaskLog>(new
|
||
{
|
||
TaskName = "产品设计库同步物料到SAP",
|
||
LogDateTime = DateTime.Now,
|
||
ReturnResult = output,
|
||
Elapsed = elapsedMilliseconds.ToLong()
|
||
}).ExecuteCommand();
|
||
|
||
string FailureMessage = "<p>以下编码在" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "同步失败,请到Manage系统中,查看详细信息。</p>";
|
||
|
||
var sapOutputsFailur = SapOutputList.Where(x => x.code != "成功").ToList();
|
||
|
||
for (int i = 0; i < sapOutputsFailur.Count; i++)
|
||
{
|
||
if (sapOutputsFailur[i].code != "成功")
|
||
{
|
||
FailureMessage = FailureMessage + sapOutputsFailur[i].materialCode + " ";
|
||
}
|
||
}
|
||
if (sapOutputsFailur.Count > 0)
|
||
{
|
||
await _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<SysEmailService>().SapSyncFailureSendEmail(FailureMessage, "启威星PLM平台-产品设计库同步物料到SAP");
|
||
}
|
||
}
|
||
}
|