VistarStarDataIntegration/admin.net.pro/Admin.NET/Vistar.Application/Service/AdministrativeMaterialManagement/AdministrativeMaterialManagementService.cs

400 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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.AdministrativeMaterialManagement.Dto;
using Vistar.Application.Util;
namespace Vistar.Application.Service.AdministrativeMaterialManagement;
/// <summary>
/// 行政物料管理服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "AdministrativeMaterialManagement", Order = 100)]
public class AdministrativeMaterialManagementService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Obj133> _obj133Rep;
private readonly SysConfigService _sysConfigService;
public SapService.SapService _sapService;
private readonly SqlSugarRepository<Configurations> _configurationsRep;
private readonly SqlSugarRepository<ConfigurationData> _configurationDataRep;
public AdministrativeMaterialManagementService(
SqlSugarRepository<Obj133> obj133Rep,
SysConfigService sysConfigService,
SapService.SapService sapService,
SqlSugarRepository<Configurations> configurationsRep,
SqlSugarRepository<ConfigurationData> configurationDataRep
)
{
_obj133Rep = obj133Rep;
_sysConfigService = sysConfigService;
_sapService = sapService;
_configurationsRep = configurationsRep;
_configurationDataRep = configurationDataRep;
}
/// <summary>
/// 分页查询行政物料管理
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Name = "page", Description = "分页查询行政物料管理", Order = 1000), HttpPost]
[DisplayName("分页查询行政物料管理")]
public async Task<SqlSugarPagedList<AdministrativeMaterialManagementOutput>> Page(PageAdministrativeMaterialManagementInput input)
{
var query = await _obj133Rep.AsQueryable()
.Where(x => x.CheckedStatus == 7 && x.deleted == false && x.IsLatestVersion == true&&x.fld004974=="发布")
.WhereIF(!string.IsNullOrWhiteSpace(input._System_objNBS), u => u._System_objNBS.Contains(input._System_objNBS.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.fld004824), u => u.fld004824.Contains(input.fld004824.Trim()))
.WhereIF(input.fld004847Range != null && input.fld004847Range.Length == 2, u => u.fld004847 >= input.fld004847Range[0] && u.fld004847 <= input.fld004847Range[1])
.Select<AdministrativeMaterialManagementOutput>()
.ToPagedListAsync(input.Page, input.PageSize);
return query;
}
/// <summary>
/// 行政物料管理-同步到 SAP
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "syncToSAP"), HttpPost]
[DisplayName("行政物料管理-同步到 SAP")]
public async Task<SapOutput> SyncToSAP(AdministrativeMaterialManagementInput input)
{
const string lengthError = "项目描述长度大于40请检查";
// 验证物料描述长度
if (input.fld004824.Length > 40)
{
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004922 = "N",
fld004921 = lengthError,
fld004920 = "失败"
})
.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.fld004852,
Mtart = input.fld004851,
Maktx = input.fld004824,
Meins = input.fld004823,
Matkl = input.fld004850,
Bismt = input.fld004912,
Groes = input.fld004913,
Normt = input.fld004822,
Ferth = input.fld004914,
Zeinr = input.fld004915,
Mstae = input.fld004857,
Raube = input.fld004917,
Mhdrz = input.fld005285.ToString(),
Mhdhb = input.fld005287.ToString(),
Werks = input.fld004856,
Beskz = input.fld004855,
Sobsl = input.fld004854,
Schgt = input.fld004853,
Rgekz = input.fld004916,
Zbom = input.fld004858
};
var apiOutput = await _sapService.SapMaterialApi(sapMaterialInput);
string codeVal = apiOutput.code == "S" ? "成功" : "失败";
var msg = apiOutput.msg;
if (apiOutput.msg == "")
{
msg = "同步成功";
}
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004920 = codeVal,
fld004922 = "N",
fld004921 = 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<AdministrativeMaterialManagementInput> input)
{
const string lengthError = "物料描述长度大于40请检查";
const string synchronized = "该项目编号已同步!";
var SapOutputList = new List<SapOutput>();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
for (int i = 0; i < input.Count; i++)
{
// 验证物料描述长度
if (input[i].fld004824.Length > 40)
{
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004922 = "N",
fld004921 = lengthError,
fld004920 = "失败"
})
.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].fld004922 != "A"|| input[i].fld004922 != "M"|| input[i].fld004922==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<string>(ConfigConst.SapUserName),
Note1 = "",
Note2 = "",
Note3 = "",
Zwbid = millisecondTimestamp,
Matnr = input[i]._System_objNBS,
Mbrsh = input[i].fld004852,
Mtart = input[i].fld004851,
Maktx = input[i].fld004824,
Meins = input[i].fld004823,
Matkl = input[i].fld004850,
Bismt = input[i].fld004912,
Groes = input[i].fld004913,
Normt = input[i].fld004822,
Ferth = input[i].fld004914,
Zeinr = input[i].fld004915,
Mstae = input[i].fld004857,
Raube = input[i].fld004917,
Mhdrz = input[i].fld005285.ToString(),
Mhdhb = input[i].fld005287.ToString(),
Werks = input[i].fld004856,
Beskz = input[i].fld004855,
Sobsl = input[i].fld004854,
Schgt = input[i].fld004853,
Rgekz = input[i].fld004916,
Zbom = input[i].fld004858
};
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
string msg = sapOutput.msg;
if (sapOutput.msg == "")
{
msg = "同步成功";
}
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004920 = codeVal,
fld004922 = "N",
fld004921 = 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;
}
/// <summary>
/// 行政物料管理-定时同步到SAP
/// </summary>
/// <returns></returns>
[ApiDescriptionSettings(Name = "TimingSyncToSAP"), HttpGet]
[DisplayName("行政物料管理-定时同步到SAP")]
[AllowAnonymous]
public async Task<List<SapOutput>> TimingSyncToSAP()
{
var input = await _obj133Rep.AsQueryable()
.Where(x => x.CheckedStatus == 7 && x.deleted == false && x.IsLatestVersion == true && (x.fld004922 == "A" || x.fld004922 == "M") && x.fld004974 == "发布")
.ToListAsync();
const string lengthError = "物料描述长度大于40请检查";
var SapOutputList = new List<SapOutput>();
var Requser = await _sysConfigService.GetConfigValueByCode<string>(ConfigConst.SapUserName);
for (int i = 0; i < input.Count; i++)
{
// 验证物料描述长度
if (input[i].fld004824.Length > 40)
{
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004922 = "N",
fld004921 = lengthError,
fld004920 = "失败"
})
.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<string>(ConfigConst.SapUserName),
Note1 = "",
Note2 = "",
Note3 = "",
Zwbid = millisecondTimestamp,
Matnr = input[i]._System_objNBS,
Mbrsh = input[i].fld004852,
Mtart = input[i].fld004851,
Maktx = input[i].fld004824,
Meins = input[i].fld004823,
Matkl = input[i].fld004850,
Bismt = input[i].fld004912,
Groes = input[i].fld004913,
Normt = input[i].fld004822,
Ferth = input[i].fld004914,
Zeinr = input[i].fld004915,
Mstae = input[i].fld004857,
Raube = input[i].fld004917,
Mhdrz = input[i].fld005285.ToString(),
Mhdhb = input[i].fld005287.ToString(),
Werks = input[i].fld004856,
Beskz = input[i].fld004855,
Sobsl = input[i].fld004854,
Schgt = input[i].fld004853,
Rgekz = input[i].fld004916,
Zbom = input[i].fld004858
};
var sapOutput = await _sapService.SapMaterialApi(sapMaterialInput);
string codeVal = sapOutput.code == "S" ? "成功" : "失败";
string msg = sapOutput.msg;
if (sapOutput.msg == "")
{
msg = "同步成功";
}
await _obj133Rep.AsUpdateable()
.SetColumns(it => new Obj133
{
fld004923 = DateTime.Now,
fld004920 = codeVal,
fld004922 = "N",
fld004921 = 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;
}
}