227 lines
9.5 KiB
C#
227 lines
9.5 KiB
C#
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WMS.Web.Core.Dto.Erp;
|
|
using WMS.Web.Core.Dto.JuShuiTan;
|
|
using WMS.Web.Core.Help;
|
|
using WMS.Web.Core.Internal.Results;
|
|
using WMS.Web.Domain.IService.Public;
|
|
using WMS.Web.Domain.Options;
|
|
|
|
namespace WMS.Web.Domain.Services.Public
|
|
{
|
|
public class JuShuiTanService : IJuShuiTanService
|
|
{
|
|
private readonly IHttpClientService _httpClientService;
|
|
private readonly ILogger<JuShuiTanService> _logger;
|
|
private readonly IMemoryCache _memoryCache;
|
|
private int hours = 10;//过期时间 默认10小时
|
|
private readonly JuShuiTanOptions _option;
|
|
public JuShuiTanService(IHttpClientService httpClientService, ILogger<JuShuiTanService> logger,
|
|
IMemoryCache memoryCache, IOptions<JuShuiTanOptions> option)
|
|
{
|
|
this._memoryCache = memoryCache;
|
|
this._httpClientService = httpClientService;
|
|
this._logger = logger;
|
|
_option = option?.Value;
|
|
}
|
|
public async Task GetTest()
|
|
{
|
|
//await GetStock();
|
|
var list = new List<int>();
|
|
list.Add(10923342);
|
|
await GetInventory(list);
|
|
}
|
|
|
|
public async Task<Result<List<JuShuiTanStockResponse>>> GetStock()
|
|
{
|
|
var stock_list = _memoryCache.Get<List<JuShuiTanStockResponse>>("jushuitan_stock");
|
|
if (stock_list != null && stock_list.Count > 0)
|
|
return Result<List<JuShuiTanStockResponse>>.ReSuccess(stock_list);
|
|
|
|
List<JuShuiTanStockResponse> list = new List<JuShuiTanStockResponse>();
|
|
|
|
JuShuiTanStockRequst request = new JuShuiTanStockRequst();
|
|
var resContent = PostUrl("/open/wms/partner/query", JsonConvert.SerializeObject(request));
|
|
JObject resData = JObject.Parse(resContent);
|
|
|
|
if (Convert.ToInt32(resData["code"]) != 0)
|
|
_logger.LogInformation($"聚水潭获取仓库失败:{resData["msg"].ToString()}");
|
|
string json = resData["data"]["datas"].ToString();
|
|
var datas = JsonConvert.DeserializeObject<List<JuShuiTanStockResponse>>(json);
|
|
list.AddRange(datas);
|
|
|
|
int count = Convert.ToInt32(resData["data"]["page_count"]);
|
|
for (int i = 1; i < count; i++)
|
|
{
|
|
request.page_index++;
|
|
resContent = PostUrl("/open/wms/partner/query", JsonConvert.SerializeObject(request));
|
|
resData = JObject.Parse(resContent);
|
|
|
|
if (Convert.ToInt32(resData["code"]) != 0)
|
|
_logger.LogInformation($"聚水潭获取仓库失败:{resData["msg"].ToString()}");
|
|
json = resData["data"]["datas"].ToString();
|
|
datas = JsonConvert.DeserializeObject<List<JuShuiTanStockResponse>>(json);
|
|
list.AddRange(datas);
|
|
}
|
|
|
|
//设置缓存
|
|
_memoryCache.Set("jushuitan_stock", list, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
|
|
return Result<List<JuShuiTanStockResponse>>.ReSuccess(list);
|
|
}
|
|
|
|
|
|
public async Task<Result<List<JuShuiTanInventoryResponse>>> GetInventory(List<int> StockId)
|
|
{
|
|
List<JuShuiTanInventoryResponse> list = new List<JuShuiTanInventoryResponse>();
|
|
foreach (var id in StockId)
|
|
{
|
|
JuShuiTanInventoryRequst request = new JuShuiTanInventoryRequst();
|
|
request.wms_co_id = id;//10923342;
|
|
request.page_size = 100;
|
|
request.modified_begin = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss");
|
|
request.modified_end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
var resContent = PostUrl("/open/inventory/query", JsonConvert.SerializeObject(request));
|
|
JObject resData = JObject.Parse(resContent);
|
|
|
|
if (Convert.ToInt32(resData["code"]) != 0)
|
|
_logger.LogInformation($"聚水潭获取即时库存失败:{resData["msg"].ToString()}");
|
|
string json = resData["data"]["inventorys"].ToString();
|
|
var datas = JsonConvert.DeserializeObject<List<JuShuiTanInventoryResponse>>(json);
|
|
list.AddRange(datas);
|
|
|
|
int count = Convert.ToInt32(resData["data"]["page_count"]);
|
|
for (int i = 1; i < count; i++)
|
|
{
|
|
request.page_index++;
|
|
resContent = PostUrl("/open/inventory/query", JsonConvert.SerializeObject(request));
|
|
resData = JObject.Parse(resContent);
|
|
|
|
if (Convert.ToInt32(resData["code"]) != 0)
|
|
_logger.LogInformation($"聚水潭获取即时库存失败:{resData["msg"].ToString()}");
|
|
json = resData["data"]["inventorys"].ToString();
|
|
datas = JsonConvert.DeserializeObject<List<JuShuiTanInventoryResponse>>(json);
|
|
list.AddRange(datas);
|
|
}
|
|
}
|
|
//主仓实际库存-订单占有数+虚拟库存+采购在途(业务设置)+进货仓(业务设置)+销退仓库存(业务设置)
|
|
foreach (var item in list)
|
|
{
|
|
item.qty = item.qty == null ? 0 : item.qty;
|
|
item.ky_qty = (item.qty ?? 0) - (item.order_lock ?? 0) + (item.virtual_qty ?? 0)
|
|
+ (item.purchase_qty ?? 0) + (item.in_qty ?? 0) + (item.return_qty ?? 0);
|
|
}
|
|
var resList = list.Where(w => w.qty > 0 || w.ky_qty > 0).ToList();
|
|
return Result<List<JuShuiTanInventoryResponse>>.ReSuccess(resList);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 调用接口
|
|
/// </summary>
|
|
private string PostUrl(string urlStr, string biz)
|
|
{
|
|
Hashtable ht = new Hashtable();
|
|
System.DateTime starttime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
|
string time = Math.Floor((System.DateTime.Now - starttime).TotalSeconds).ToString();
|
|
string app_key = _option.AppKey;// "17231841286c45f99eb9acf445404349";
|
|
string app_secret = _option.AppSecret;// "5ce9641ae6db4abb8b96eea7df6a81e9";
|
|
string access_token = _option.AccessToken;// "4605358c37374780a8a4395e6cdb1c3f";
|
|
|
|
|
|
//string biz = "{\"modified_begin\":\"2022-06-10 00:00:00\",\"modified_end\":\"2022-06-12 00:00:00\",\"page_index\":1,\"page_size\":30}";
|
|
|
|
ht.Add("access_token", access_token);
|
|
ht.Add("app_key", app_key);
|
|
ht.Add("biz", biz);
|
|
ht.Add("charset", "utf-8");
|
|
ht.Add("timestamp", time);
|
|
ht.Add("version", "2");
|
|
string sign = getSign(app_secret, ht);
|
|
ht.Add("sign", sign);
|
|
string posturl = _option.Url + urlStr;
|
|
Encoding datacode = Encoding.UTF8;
|
|
string paramData = GetParamData(ht);
|
|
return PostWebRequest(posturl, paramData, datacode);
|
|
}
|
|
|
|
string getSign(string signKey, Hashtable param)
|
|
{
|
|
|
|
ArrayList keys = new ArrayList(param.Keys);
|
|
keys.Sort(); //按字母顺序进行排序
|
|
string resultStr = "";
|
|
foreach (string key in keys)
|
|
{
|
|
if (key != null && key != "" && key != "sign")
|
|
{
|
|
resultStr = resultStr + key + param[key];
|
|
}
|
|
}
|
|
resultStr = signKey + resultStr;
|
|
MD5 md5 = MD5.Create();
|
|
byte[] rstRes = md5.ComputeHash(Encoding.UTF8.GetBytes(resultStr));
|
|
string hex = BitConverter.ToString(rstRes, 0).Replace("-", string.Empty).ToLower();
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
|
|
private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
|
|
{
|
|
string ret = string.Empty;
|
|
try
|
|
{
|
|
|
|
byte[] byteArray = dataEncode.GetBytes(paramData);
|
|
//转化
|
|
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
|
|
webReq.Method = "POST";
|
|
webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
webReq.ContentLength = byteArray.Length;
|
|
|
|
Stream newStream = webReq.GetRequestStream();
|
|
newStream.Write(byteArray, 0, byteArray.Length);
|
|
//写入参数
|
|
newStream.Close();
|
|
|
|
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
|
|
StreamReader sr = new StreamReader(response.GetResponseStream(), dataEncode);
|
|
ret = sr.ReadToEnd();
|
|
sr.Close();
|
|
response.Close();
|
|
newStream.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
private string GetParamData(Hashtable ht)
|
|
{
|
|
string str = "";
|
|
foreach (DictionaryEntry items in ht)
|
|
{
|
|
|
|
str += items.Key.ToString() + "=" + System.Web.HttpUtility.UrlEncode(items.Value.ToString(), System.Text.Encoding.UTF8) + "&";
|
|
}
|
|
|
|
return str.Substring(0, str.Length - 1);
|
|
}
|
|
|
|
}
|
|
}
|