167 lines
7.2 KiB
C#
167 lines
7.2 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.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.ProductManagement;
|
|||
|
|
|||
|
[JobDetail("job_SyncProductManagementMaterialToSap", Description = "产品管理同步物料到SAP", GroupName = "default", Concurrent = false)]
|
|||
|
[PeriodMinutes(3, TriggerId = "trigger_SyncProductManagementMaterialToSapJob", Description = "产品管理同步物料到SAP", RunOnStart = false)]
|
|||
|
public class SyncProductManagementMaterialToSap : IJob
|
|||
|
{
|
|||
|
private readonly IServiceScopeFactory _scopeFactory;
|
|||
|
public SapService.SapService _sapService;
|
|||
|
private readonly SysConfigService _sysConfigService;
|
|||
|
public SyncProductManagementMaterialToSap(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<Obj110>()
|
|||
|
.Where(x => (x.CheckedStatus == 0 || x.CheckedStatus == 1) && x.fld005324 == "发布" && x.deleted == false && x.IsLatestVersion == true && (x.fld004312 == "A" || x.fld004312 == "M"))
|
|||
|
.ToList();
|
|||
|
var SapOutputList = new List<SapOutput>();
|
|||
|
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
|
|||
|
const string lengthError = "物料描述长度大于40,请检查!";
|
|||
|
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.fld004484.Length > 40)
|
|||
|
{
|
|||
|
db.CopyNew().Updateable<Obj110>()
|
|||
|
.SetColumns(it => new Obj110
|
|||
|
{
|
|||
|
fld004629 = DateTime.Now,
|
|||
|
fld004312 = "N",
|
|||
|
fld004313 = lengthError,
|
|||
|
fld004311 = "失败"
|
|||
|
})
|
|||
|
.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.fld004323,
|
|||
|
Mtart = item.fld004324,
|
|||
|
Maktx = item.fld004484,
|
|||
|
Meins = item.fld004861,
|
|||
|
Matkl = item.fld004485,
|
|||
|
Bismt = item._System_ObjDescription,
|
|||
|
Groes = item._System_objNBS,
|
|||
|
Normt = item.fld004887,
|
|||
|
Ferth = item.fld004882,
|
|||
|
Zeinr = item.fld004881,
|
|||
|
Mstae = item.fld004699,
|
|||
|
Raube = item.fld004877,
|
|||
|
Mhdrz = item.fld004876.ToString(),
|
|||
|
Mhdhb = item.fld004895.ToString(),
|
|||
|
Werks = item.fld004325,
|
|||
|
Beskz = item.fld004490,
|
|||
|
Sobsl = item.fld004491,
|
|||
|
Schgt = item.fld004873,
|
|||
|
Rgekz = item.fld004872,
|
|||
|
Zbom = item.fld004698
|
|||
|
};
|
|||
|
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
|
|||
|
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
|
|||
|
string msg = sapOutput.msg;
|
|||
|
|
|||
|
if (sapOutput.msg == "")
|
|||
|
{
|
|||
|
msg = "同步成功";
|
|||
|
}
|
|||
|
|
|||
|
db.CopyNew().Updateable<Obj110>()
|
|||
|
.SetColumns(it => new Obj110
|
|||
|
{
|
|||
|
fld004629 = DateTime.Now,
|
|||
|
fld004311 = codeVal,
|
|||
|
fld004312 = "N",
|
|||
|
fld004313 = 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();
|
|||
|
|
|||
|
}
|
|||
|
}
|