优化金蝶获取数据
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using AutoMapper;
|
||||
using ERP;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
@@ -9,6 +10,7 @@ using System.ServiceModel;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto.Erp;
|
||||
using WMS.Web.Core.Dto.Erp.Org;
|
||||
using WMS.Web.Core.Dto.Erp.Purchase;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
@@ -26,35 +28,50 @@ namespace WMS.Web.Domain.Services.Public
|
||||
private IMapper _mapper;
|
||||
private ErpOptions _erpOptions;
|
||||
private ILogger<ErpService> _logger;
|
||||
private ERPGWSoapClient _client;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
public ErpService(
|
||||
IMapper mapper,
|
||||
IOptions<ErpOptions> erpOptions,
|
||||
ILogger<ErpService> logger)
|
||||
ILogger<ErpService> logger,
|
||||
IMemoryCache memoryCache)
|
||||
{
|
||||
this._erpOptions = erpOptions?.Value;
|
||||
this._mapper = mapper;
|
||||
this._logger = logger;
|
||||
this._memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化ERP:登录到ERP
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<Result<AccessResult>> Init()
|
||||
public async Task<Result<string>> Init()
|
||||
{
|
||||
BasicHttpBinding binding = new BasicHttpBinding();
|
||||
EndpointAddress address = new EndpointAddress(this._erpOptions.EndpointAddress);
|
||||
ERPGWSoapClient client = new ERPGWSoapClient(binding, address);
|
||||
this._client = new ERPGWSoapClient(binding, address);
|
||||
try
|
||||
{
|
||||
var result = await client.ValidateSystemAsync(this._erpOptions.UserName, this._erpOptions.Password, this._erpOptions.ErpId, "");
|
||||
if (result == null)
|
||||
return Result<AccessResult>.ReFailure(ResultCodes.Erp_Login_Error);
|
||||
return Result<AccessResult>.ReSuccess(result);
|
||||
var appId = "wms";
|
||||
var cache_key = "erp_accesstoken_"+ appId;
|
||||
var cache_value = _memoryCache.Get<string>(cache_key);
|
||||
if (!string.IsNullOrEmpty(cache_value))
|
||||
return Result<string>.ReSuccess(cache_value);
|
||||
else
|
||||
{
|
||||
var result = await this._client.ValidateSystemAsync(this._erpOptions.UserName, this._erpOptions.Password, this._erpOptions.ErpId, appId);
|
||||
if (result == null)
|
||||
return Result<string>.ReFailure(ResultCodes.Erp_Login_Error);
|
||||
|
||||
_memoryCache.Set(cache_key, true, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(30)));
|
||||
return Result<string>.ReSuccess(result.AccessToken);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<AccessResult>.ReFailure(ResultCodes.Erp_Login_Error);
|
||||
return Result<string>.ReFailure(ResultCodes.Erp_Login_Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,17 +83,17 @@ namespace WMS.Web.Domain.Services.Public
|
||||
{
|
||||
try
|
||||
{
|
||||
BasicHttpBinding binding = new BasicHttpBinding();
|
||||
EndpointAddress address = new EndpointAddress(this._erpOptions.EndpointAddress);
|
||||
ERPGWSoapClient client = new ERPGWSoapClient(binding, address);
|
||||
var token_result = await client.ValidateSystemAsync(this._erpOptions.UserName, this._erpOptions.Password, this._erpOptions.ErpId, "");
|
||||
var query = new ErpBillQueryDto(token_result.AccessToken);
|
||||
var token_result = await this.Init();
|
||||
if(!token_result.IsSuccess)
|
||||
return ResultList<ErpPurchaseInStockResultDto>.ReFailure(token_result);
|
||||
|
||||
var query = new ErpBillQueryDto(token_result.Data);
|
||||
var param = new ErpBillQueryParamDto(FormIdParam.STK_InStock.ToString());
|
||||
param.FieldKeys = this._erpOptions.PurchaseInstockFieldKeys;
|
||||
query.Data = JsonConvert.SerializeObject(param);
|
||||
var json = JsonConvert.SerializeObject(query);
|
||||
|
||||
var result_json = await client.ExecuteBillQueryAsync(json);
|
||||
var result_json = await _client.ExecuteBillQueryAsync(json);
|
||||
var result = JsonConvert.DeserializeObject<List<List<string>>>(result_json);
|
||||
|
||||
var list = new List<ErpPurchaseInStockResultDto>();
|
||||
@@ -97,10 +114,48 @@ namespace WMS.Web.Domain.Services.Public
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
return ResultList<ErpPurchaseInStockResultDto>.ReFailure("错误",10001);
|
||||
return ResultList<ErpPurchaseInStockResultDto>.ReFailure("错误", 10001);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// erp:单据查询-组织数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<ResultList<ErpOrgDto>> BillQueryForOrg()
|
||||
{
|
||||
try
|
||||
{
|
||||
var token_result = await this.Init();
|
||||
if (!token_result.IsSuccess)
|
||||
return ResultList<ErpOrgDto>.ReFailure(token_result);
|
||||
var query = new ErpBillQueryDto(token_result.Data);
|
||||
var param = new ErpBillQueryParamDto(FormIdParam.ORG_Organizations.ToString());
|
||||
param.FieldKeys = "FOrgID,FName";
|
||||
param.FilterString = "FOrgID=100008";
|
||||
query.Data = JsonConvert.SerializeObject(param);
|
||||
var json = JsonConvert.SerializeObject(query);
|
||||
|
||||
var result_json = await _client.ExecuteBillQueryAsync(json);
|
||||
var result = JsonConvert.DeserializeObject<List<List<string>>>(result_json);
|
||||
|
||||
var list = new List<ErpOrgDto>();
|
||||
foreach (var item in result)
|
||||
{
|
||||
var lis = new ErpOrgDto();
|
||||
lis.Id = int.Parse(item[0]);
|
||||
lis.Name = item[1];
|
||||
|
||||
list.Add(lis);
|
||||
|
||||
}
|
||||
return ResultList<ErpOrgDto>.ReSuccess(list);
|
||||
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return ResultList<ErpOrgDto>.ReFailure("错误", 10002);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user