309 lines
7.7 KiB
C#
309 lines
7.7 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using Microsoft.Extensions.Options;
|
||
using MySqlX.XDevAPI.Common;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WMS.Web.Core.Dto.Erp;
|
||
using WMS.Web.Core.Dto.JuShuiTan;
|
||
using WMS.Web.Core.Internal.Results;
|
||
using WMS.Web.Domain.IService.Public;
|
||
using WMS.Web.Domain.Options;
|
||
using WMS.Web.Domain.Values;
|
||
|
||
namespace WMS.Web.Domain.Services.Public
|
||
{
|
||
public class ErpInventoryService : IErpInventoryService
|
||
{
|
||
private ErpOptions _erpOptions;
|
||
private readonly ILogger<ErpInventoryService> _logger;
|
||
public ErpInventoryService(IOptions<ErpOptions> erpOptions, ILogger<ErpInventoryService> logger)
|
||
{
|
||
_erpOptions = erpOptions?.Value;
|
||
_logger = logger;
|
||
}
|
||
|
||
private bool Login(HttpClientEx httpClient)
|
||
{
|
||
string baseUrl = _erpOptions.EndpointAddress.Replace("/ERPGW.asmx", ""); //"http://192.168.1.1/k3cloud";//服务器地址
|
||
|
||
httpClient.Url = string.Format("{0}/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc", baseUrl);
|
||
List<object> Parameters = new List<object>();
|
||
Parameters.Add(_erpOptions.ZhangTaoId);//帐套Id
|
||
Parameters.Add(_erpOptions.UserName);//用户名
|
||
Parameters.Add(_erpOptions.Password);//密码
|
||
Parameters.Add(2052);//多语言:中文
|
||
|
||
httpClient.Content = JsonConvert.SerializeObject(Parameters);
|
||
string response = httpClient.AsyncRequest();
|
||
var iResult = JObject.Parse(response)["LoginResultType"].Value<int>();
|
||
return iResult == 1 ? true : false;
|
||
}
|
||
public async Task<Result<List<ErpInventoryDto>>> GetInventory(List<string> stockCodes)
|
||
{
|
||
List<ErpInventoryDto> list = new List<ErpInventoryDto>();
|
||
HttpClientEx httpClient = new HttpClientEx();
|
||
var isSuccess = Login(httpClient);
|
||
string baseUrl = _erpOptions.EndpointAddress.Replace("/ERPGW.asmx", "");
|
||
httpClient.Url = string.Format("{0}/Kingdee.K3.SCM.WebApi.ServicesStub.InventoryQueryService.GetInventoryData.common.kdsvc", baseUrl);
|
||
|
||
foreach (var code in stockCodes)
|
||
{
|
||
List<object> Parameters = new List<object>();
|
||
var model = new InventoryParamModel();
|
||
model.fstocknumbers = code;
|
||
//model.fmaterialnumbers = "G01-11-572474";
|
||
model.isshowauxprop = true;
|
||
model.isshowstockloc = true;
|
||
model.pageindex = 1;
|
||
model.pagerows = 10000;
|
||
Parameters.Add(model);
|
||
|
||
httpClient.Content = JsonConvert.SerializeObject(Parameters);
|
||
_logger.LogInformation($"1.开始进行连接 {code}");
|
||
string content = httpClient.AsyncRequest();
|
||
//_logger.LogInformation($"金蝶返回数据{content}");
|
||
JObject resData = JObject.Parse(content);
|
||
|
||
if (resData["success"].ToString().ToLower() != "true")
|
||
return Result<List<ErpInventoryDto>>.ReFailure(ResultCodes.Erp_Inventory_Error);
|
||
|
||
string json = resData["data"].ToString();
|
||
var datas = JsonConvert.DeserializeObject<List<ErpInventoryDto>>(json);
|
||
list.AddRange(datas);
|
||
|
||
int num = Convert.ToInt32(resData["rowcount"]) / 10000;
|
||
for (int i = 1; i <= num; i++)
|
||
{
|
||
model.pageindex++;
|
||
|
||
httpClient.Content = JsonConvert.SerializeObject(Parameters);
|
||
|
||
content = httpClient.AsyncRequest();
|
||
//_logger.LogInformation($"金蝶返回数据{content}");
|
||
resData = JObject.Parse(content);
|
||
|
||
if (resData["success"].ToString().ToLower() != "true")
|
||
return Result<List<ErpInventoryDto>>.ReFailure(ResultCodes.Erp_Inventory_Error);
|
||
|
||
json = resData["data"].ToString();
|
||
datas = JsonConvert.DeserializeObject<List<ErpInventoryDto>>(json);
|
||
list.AddRange(datas);
|
||
}
|
||
}
|
||
|
||
var blist = list.Where(w => w.Qty > 0 || w.BeforeQty > 0).ToList();
|
||
|
||
return Result<List<ErpInventoryDto>>.ReSuccess(blist);
|
||
}
|
||
|
||
}
|
||
class HttpClientEx
|
||
|
||
{
|
||
|
||
/// <summary>
|
||
|
||
/// Seivice URL
|
||
|
||
/// </summary>
|
||
|
||
public string Url { get; set; }
|
||
|
||
/// <summary>
|
||
|
||
/// 内容
|
||
|
||
/// </summary>
|
||
|
||
public string Content { get; set; }
|
||
|
||
/// <summary>
|
||
|
||
/// Cookie,保证登录后,所有访问持有一个Cookie;
|
||
|
||
/// </summary>
|
||
|
||
static CookieContainer Cookie = new CookieContainer();
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// HTTP访问
|
||
|
||
/// </summary>
|
||
|
||
public string AsyncRequest()
|
||
|
||
{
|
||
|
||
HttpWebRequest httpRequest = HttpWebRequest.Create(Url) as HttpWebRequest;
|
||
|
||
httpRequest.Method = "POST";
|
||
|
||
httpRequest.ContentType = "application/json";
|
||
|
||
httpRequest.CookieContainer = Cookie;
|
||
|
||
httpRequest.Timeout = 1000 * 60 * 10;//10min
|
||
|
||
|
||
|
||
using (Stream reqStream = httpRequest.GetRequestStream())
|
||
|
||
{
|
||
|
||
JObject jObj = new JObject();
|
||
|
||
jObj.Add("format", 1);
|
||
|
||
jObj.Add("useragent", "ApiClient");
|
||
|
||
jObj.Add("rid", Guid.NewGuid().ToString().GetHashCode().ToString());
|
||
|
||
jObj.Add("parameters", Content);
|
||
|
||
jObj.Add("timestamp", DateTime.Now);
|
||
|
||
jObj.Add("v", "1.0");
|
||
|
||
string sContent = jObj.ToString();
|
||
|
||
var bytes = UnicodeEncoding.UTF8.GetBytes(sContent);
|
||
|
||
reqStream.Write(bytes, 0, bytes.Length);
|
||
|
||
reqStream.Flush();
|
||
|
||
}
|
||
|
||
using (var repStream = httpRequest.GetResponse().GetResponseStream())
|
||
|
||
{
|
||
|
||
using (var reader = new StreamReader(repStream))
|
||
|
||
{
|
||
|
||
return ValidateResult(reader.ReadToEnd());
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
private static string ValidateResult(string responseText)
|
||
|
||
{
|
||
|
||
if (responseText.StartsWith("response_error:"))
|
||
|
||
{
|
||
|
||
return responseText.TrimStart("response_error:".ToCharArray());
|
||
|
||
}
|
||
|
||
return responseText;
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
class InventoryParamModel
|
||
|
||
{
|
||
|
||
/// <summary>
|
||
|
||
/// 库存组织编码,多个使用英文逗号【,】分隔
|
||
|
||
/// </summary>
|
||
|
||
public string fstockorgnumbers { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 物料编码,多个使用英文逗号【,】分隔
|
||
|
||
/// </summary>
|
||
|
||
public string fmaterialnumbers { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 仓库编码,多个使用英文逗号【,】分隔
|
||
|
||
/// </summary>
|
||
|
||
public string fstocknumbers { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 批号编码,多个使用英文逗号【,】分隔
|
||
|
||
/// </summary>
|
||
|
||
public string flotnumbers { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 是否查询仓位
|
||
|
||
/// </summary>
|
||
|
||
public bool isshowstockloc { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 是否查询辅助属性
|
||
|
||
/// </summary>
|
||
|
||
public bool isshowauxprop { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 当前页码
|
||
|
||
/// </summary>
|
||
|
||
public int pageindex { get; set; }
|
||
|
||
|
||
|
||
/// <summary>
|
||
|
||
/// 每页显示行数
|
||
|
||
/// </summary>
|
||
|
||
public int pagerows { get; set; }
|
||
|
||
}
|
||
|
||
}
|