172 lines
7.3 KiB
C#
172 lines
7.3 KiB
C#
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|||
|
//
|
|||
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|||
|
//
|
|||
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|||
|
|
|||
|
using Admin.NET.Core;
|
|||
|
using Admin.NET.Core.Service;
|
|||
|
using DocumentFormat.OpenXml.Drawing;
|
|||
|
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.ProductionMaterials;
|
|||
|
|
|||
|
[JobDetail("job_SyncProductionMaterialsToSap", Description = "生产物料同步到SAP", GroupName = "default", Concurrent = false)]
|
|||
|
[PeriodMinutes(5, TriggerId = "trigger_SyncProductionMaterialsToSapJob", Description = "生产物料同步到SAP", RunOnStart = false)]
|
|||
|
public class SyncProductionMaterialsToSap : IJob
|
|||
|
{
|
|||
|
private readonly IServiceScopeFactory _scopeFactory;
|
|||
|
public SapService.SapService _sapService;
|
|||
|
private readonly SysConfigService _sysConfigService;
|
|||
|
public SyncProductionMaterialsToSap(IServiceScopeFactory scopeFactory, SapService.SapService sapService, SysConfigService sysConfigService)
|
|||
|
{
|
|||
|
_scopeFactory = scopeFactory;
|
|||
|
_sysConfigService = sysConfigService;
|
|||
|
_sapService=sapService;
|
|||
|
}
|
|||
|
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
|
|||
|
{
|
|||
|
DateTime startTime = DateTime.Now;
|
|||
|
|
|||
|
using var serviceScope = _scopeFactory.CreateScope();
|
|||
|
var db = serviceScope.ServiceProvider.GetRequiredService<ISqlSugarClient>().AsTenant().GetConnectionScope("启威星 1.94.4.74").CopyNew();
|
|||
|
|
|||
|
var input = db.CopyNew().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);
|
|||
|
|
|||
|
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.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();
|
|||
|
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
|
|||
|
};
|
|||
|
}).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();
|
|||
|
|
|||
|
}
|
|||
|
}
|