// 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 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;
///
/// 生产物料管理服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "ProductionMaterialsManagement", Order = 100)]
public class ProductionMaterialsManagementService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _obj112Rep;
private readonly SysConfigService _sysConfigService;
public SapService.SapService _sapService;
private readonly SqlSugarRepository _configurationsRep;
private readonly SqlSugarRepository _configurationDataRep;
public ProductionMaterialsManagementService(
SqlSugarRepository obj112Rep,
SysConfigService sysConfigService,
SapService.SapService sapService,
SqlSugarRepository configurationsRep,
SqlSugarRepository configurationDataRep
)
{
_obj112Rep = obj112Rep;
_sysConfigService = sysConfigService;
_sapService = sapService;
_configurationsRep = configurationsRep;
_configurationDataRep = configurationDataRep;
}
///
/// 分页查询生产物料管理
///
///
///
[ApiDescriptionSettings(Name = "page", Description = "分页查询生产物料管理", Order = 1000), HttpPost]
[DisplayName("分页查询生产物料管理")]
public async Task> 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()
.ToPagedListAsync(input.Page, input.PageSize);
return query;
}
///
/// 生产物料管理-同步到 SAP
///
///
[ApiDescriptionSettings(Name = "syncToSAP"), HttpPost]
[DisplayName("生产物料管理-同步到 SAP")]
public async Task 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();
var sapMaterialInput = new SapMaterialInput()
{
Reqkeyid = "",
Businessid = "",
Messageid = "",
Sndprn = "PLM",
Rcvprn = "SAP",
Requser = await _sysConfigService.GetConfigValueByCode(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;
}
///
/// 生产物料管理-批量同步到SAP
///
///
///
[ApiDescriptionSettings(Name = "BatchSyncToSAP"), HttpPost]
[DisplayName("生产物料管理-批量同步到SAP")]
public async Task> BatchSyncToSAP(List input)
{
const string lengthError = "物料描述长度大于40,请检查!";
const string synchronized = "该项目编号已同步或已停用!";
var SapOutputList = new List();
var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName);
for (int i = 0; i < input.Count; i++)
{
// 验证物料描述长度
if (input[i].fld004598.Length > 40)
{
await _obj112Rep.AsUpdateable()
.SetColumns(it => new Obj112
{
fld004606 = DateTime.Now,
fld004607 = "N",
fld004605 = lengthError,
fld004604 = "失败"
})
.Where(it => it.idRecord == input[i].idRecord)
.ExecuteCommandAsync().ConfigureAwait(false);
SapOutputList.Add(new SapOutput()
{
materialCode = input[i]._System_objNBS,
code = "失败",
msg = lengthError,
result = lengthError
});
continue;
}
if (input[i].fld004607 == "N" || input[i].fld004607 == "D" || input[i].fld004607 == null)
{
SapOutputList.Add(new SapOutput()
{
materialCode = input[i]._System_objNBS,
code = "失败",
msg = synchronized,
result = synchronized
});
continue;
}
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
var sapMaterialInput = new SapMaterialInput()
{
Reqkeyid = "",
Businessid = "",
Messageid = "",
Sndprn = "PLM",
Rcvprn = "SAP",
Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName),
Note1 = "",
Note2 = "",
Note3 = "",
Zwbid = millisecondTimestamp,
Matnr = input[i]._System_objNBS,
Mbrsh = input[i].fld004595,
Mtart = input[i].fld004596,
Maktx = input[i].fld004598,
Meins = input[i].fld004594,
Matkl = input[i].fld004599,
Bismt = input[i]._System_ObjDescription,
Groes = input[i].fld005323,
Normt = input[i].fld004592,
Ferth = input[i].fld004593,
Zeinr = input[i].fld004903,
Mstae = input[i].fld004696,
Raube = input[i].fld004904,
Mhdrz = input[i].fld004905.ToString(),
Mhdhb = input[i].fld004907.ToString(),
Werks = input[i].fld004597,
Beskz = input[i].fld004600,
Sobsl = input[i].fld004601,
Schgt = input[i].fld004602,
Rgekz = input[i].fld004603,
Zbom = input[i].fld004695
};
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
string msg = sapOutput.msg;
if (sapOutput.msg == "")
{
msg = "同步成功";
}
await _obj112Rep.AsUpdateable()
.SetColumns(it => new Obj112
{
fld004606 = DateTime.Now,
fld004604 = codeVal,
fld004607 = "N",
fld004605 = msg
})
.Where(it => it.idRecord == input[i].idRecord)
.ExecuteCommandAsync();
var output = new SapOutput()
{
parameter = sapOutput.parameter,
materialCode = input[0]._System_objNBS,
code = codeVal,
msg = msg,
result = sapOutput.result
};
SapOutputList.Add(output);
}
return SapOutputList;
}
///
/// 生产物料管理-定时同步到SAP
///
///
[ApiDescriptionSettings(Name = "TimingSyncToSAP"), HttpGet]
[DisplayName("生产物料管理-定时同步到SAP")]
[AllowAnonymous]
public async Task> TimingSyncToSAP()
{
var input = await _obj112Rep.AsQueryable()
.Where(x => (x.CheckedStatus == 7 || x.CheckedStatus == 8) && x.deleted == false && x.IsLatestVersion == true && (x.fld004607 == "A" || x.fld004607 == "M") && x.fld004973 == "发布")
.ToListAsync();
const string lengthError = "物料描述长度大于40,请检查!";
var SapOutputList = new List();
var Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName);
for (int i = 0; i < input.Count; i++)
{
// 验证物料描述长度
if (input[i].fld004598.Length > 40)
{
await _obj112Rep.AsUpdateable()
.SetColumns(it => new Obj112
{
fld004606 = DateTime.Now,
fld004607 = "N",
fld004605 = lengthError,
fld004604 = "失败"
})
.Where(it => it.idRecord == input[i].idRecord)
.ExecuteCommandAsync().ConfigureAwait(false);
SapOutputList.Add(new SapOutput()
{
materialCode = input[i]._System_objNBS,
code = "失败",
msg = lengthError,
result = lengthError
});
continue;
}
//获取时间戳精确到毫秒,sap要求每次调用生成不重复guid
string millisecondTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
var sapMaterialInput = new SapMaterialInput()
{
Reqkeyid = "",
Businessid = "",
Messageid = "",
Sndprn = "PLM",
Rcvprn = "SAP",
Requser = await _sysConfigService.GetConfigValueByCode(ConfigConst.SapUserName),
Note1 = "",
Note2 = "",
Note3 = "",
Zwbid = millisecondTimestamp,
Matnr = input[i]._System_objNBS,
Mbrsh = input[i].fld004595,
Mtart = input[i].fld004596,
Maktx = input[i].fld004598,
Meins = input[i].fld004594,
Matkl = input[i].fld004599,
Bismt = input[i]._System_ObjDescription,
Groes = input[i].fld005323,
Normt = input[i].fld004592,
Ferth = input[i].fld004593,
Zeinr = input[i].fld004903,
Mstae = input[i].fld004696,
Raube = input[i].fld004904,
Mhdrz = input[i].fld004905.ToString(),
Mhdhb = input[i].fld004907.ToString(),
Werks = input[i].fld004597,
Beskz = input[i].fld004600,
Sobsl = input[i].fld004601,
Schgt = input[i].fld004602,
Rgekz = input[i].fld004603,
Zbom = input[i].fld004695
};
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
string msg = sapOutput.msg;
if (sapOutput.msg == "")
{
msg = "同步成功";
}
await _obj112Rep.AsUpdateable()
.SetColumns(it => new Obj112
{
fld004606 = DateTime.Now,
fld004604 = codeVal,
fld004607 = "N",
fld004605 = msg
})
.Where(it => it.idRecord == input[i].idRecord)
.ExecuteCommandAsync();
var output = new SapOutput()
{
parameter = sapOutput.parameter,
materialCode = input[0]._System_objNBS,
code = codeVal,
msg = msg,
result = sapOutput.result
};
SapOutputList.Add(output);
}
return SapOutputList;
}
}